Commit c300a7a8 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Do not allow leaving __world__ group

parent e80f6941
...@@ -18,6 +18,8 @@ function allowLeavingGroups(settings: SidebarSettings): boolean { ...@@ -18,6 +18,8 @@ function allowLeavingGroups(settings: SidebarSettings): boolean {
return !!config.allowLeavingGroups; return !!config.allowLeavingGroups;
} }
export const PUBLIC_GROUP_ID = '__world__';
/** /**
* Combine groups from multiple api calls together to form a unique list of groups. * Combine groups from multiple api calls together to form a unique list of groups.
* Add an isMember property to each group indicating whether the logged in user is a member. * Add an isMember property to each group indicating whether the logged in user is a member.
...@@ -34,7 +36,7 @@ export function combineGroups( ...@@ -34,7 +36,7 @@ export function combineGroups(
uri: string | null, uri: string | null,
settings: SidebarSettings, settings: SidebarSettings,
) { ) {
const worldGroup = featuredGroups.find(g => g.id === '__world__'); const worldGroup = featuredGroups.find(g => g.id === PUBLIC_GROUP_ID);
if (worldGroup) { if (worldGroup) {
userGroups.unshift(worldGroup); userGroups.unshift(worldGroup);
} }
...@@ -50,7 +52,11 @@ export function combineGroups( ...@@ -50,7 +52,11 @@ export function combineGroups(
// Set flag indicating whether user can leave group. // Set flag indicating whether user can leave group.
for (const group of groups) { for (const group of groups) {
group.canLeave = allowLeavingGroups(settings) && group.isMember; group.canLeave =
allowLeavingGroups(settings) &&
group.isMember &&
// People should not be able to leave the "Public" group.
group.id !== PUBLIC_GROUP_ID;
} }
// Add isScopedToUri property indicating whether a group is within scope // Add isScopedToUri property indicating whether a group is within scope
......
...@@ -97,6 +97,40 @@ describe('sidebar/helpers/groups', () => { ...@@ -97,6 +97,40 @@ describe('sidebar/helpers/groups', () => {
}, },
); );
it('sets `canLeave` to false for the `__world__` group', () => {
const userGroups = [
{ id: '__world__', name: 'Public', type: 'open' },
{ id: 'groupa', name: 'GroupA', type: 'private' },
{ id: 'groupc', name: 'GroupC', type: 'open' },
{ id: 'groupd', name: 'GroupD', type: 'restricted' },
];
const groups = combineGroups(userGroups, [], 'https://foo.com/bar');
const expected = [
{
id: '__world__',
canLeave: false,
},
{
id: 'groupa',
canLeave: true,
},
{
id: 'groupc',
canLeave: true,
},
{
id: 'groupd',
canLeave: true,
},
];
for (const { id, canLeave } of expected) {
const group = groups.find(g => g.id === id);
assert.strictEqual(group.canLeave, canLeave);
}
});
it('combines groups from both lists uniquely', () => { it('combines groups from both lists uniquely', () => {
const userGroups = [ const userGroups = [
{ id: 'groupa', name: 'GroupA' }, { id: 'groupa', name: 'GroupA' },
......
...@@ -7,7 +7,7 @@ import type { SidebarSettings } from '../../types/config'; ...@@ -7,7 +7,7 @@ import type { SidebarSettings } from '../../types/config';
import type { Service } from '../../types/config'; import type { Service } from '../../types/config';
import { serviceConfig } from '../config/service-config'; import { serviceConfig } from '../config/service-config';
import { isReply } from '../helpers/annotation-metadata'; import { isReply } from '../helpers/annotation-metadata';
import { combineGroups } from '../helpers/groups'; import { combineGroups, PUBLIC_GROUP_ID } from '../helpers/groups';
import type { SidebarStore } from '../store'; import type { SidebarStore } from '../store';
import { awaitStateChange } from '../store/util'; import { awaitStateChange } from '../store/util';
import { watch } from '../util/watch'; import { watch } from '../util/watch';
...@@ -121,7 +121,7 @@ export class GroupsService { ...@@ -121,7 +121,7 @@ export class GroupsService {
// If the main document URL has no groups associated with it, always show // If the main document URL has no groups associated with it, always show
// the "Public" group. // the "Public" group.
const pageHasAssociatedGroups = groups.some( const pageHasAssociatedGroups = groups.some(
g => g.id !== '__world__' && g.isScopedToUri, g => g.id !== PUBLIC_GROUP_ID && g.isScopedToUri,
); );
if (!pageHasAssociatedGroups) { if (!pageHasAssociatedGroups) {
return groups; return groups;
...@@ -130,14 +130,14 @@ export class GroupsService { ...@@ -130,14 +130,14 @@ export class GroupsService {
// If directLinkedGroup or directLinkedAnnotationGroupId is the "Public" group, // If directLinkedGroup or directLinkedAnnotationGroupId is the "Public" group,
// always return groups. // always return groups.
if ( if (
directLinkedGroupId === '__world__' || directLinkedGroupId === PUBLIC_GROUP_ID ||
directLinkedAnnotationGroupId === '__world__' directLinkedAnnotationGroupId === PUBLIC_GROUP_ID
) { ) {
return groups; return groups;
} }
// Return non-world groups. // Return non-world groups.
return groups.filter(g => g.id !== '__world__'); return groups.filter(g => g.id !== PUBLIC_GROUP_ID);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment