Skip to content

Commit

Permalink
Do not allow leaving __world__ group
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Nov 22, 2024
1 parent e80f694 commit c300a7a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/sidebar/helpers/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function allowLeavingGroups(settings: SidebarSettings): boolean {
return !!config.allowLeavingGroups;
}

export const PUBLIC_GROUP_ID = '__world__';

/**
* 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.
Expand All @@ -34,7 +36,7 @@ export function combineGroups(
uri: string | null,
settings: SidebarSettings,
) {
const worldGroup = featuredGroups.find(g => g.id === '__world__');
const worldGroup = featuredGroups.find(g => g.id === PUBLIC_GROUP_ID);
if (worldGroup) {
userGroups.unshift(worldGroup);
}
Expand All @@ -50,7 +52,11 @@ export function combineGroups(

// Set flag indicating whether user can leave group.
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
Expand Down
34 changes: 34 additions & 0 deletions src/sidebar/helpers/test/groups-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
const userGroups = [
{ id: 'groupa', name: 'GroupA' },
Expand Down
10 changes: 5 additions & 5 deletions src/sidebar/services/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { SidebarSettings } from '../../types/config';
import type { Service } from '../../types/config';
import { serviceConfig } from '../config/service-config';
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 { awaitStateChange } from '../store/util';
import { watch } from '../util/watch';
Expand Down Expand Up @@ -121,7 +121,7 @@ export class GroupsService {
// If the main document URL has no groups associated with it, always show
// the "Public" group.
const pageHasAssociatedGroups = groups.some(
g => g.id !== '__world__' && g.isScopedToUri,
g => g.id !== PUBLIC_GROUP_ID && g.isScopedToUri,
);
if (!pageHasAssociatedGroups) {
return groups;
Expand All @@ -130,14 +130,14 @@ export class GroupsService {
// If directLinkedGroup or directLinkedAnnotationGroupId is the "Public" group,
// always return groups.
if (
directLinkedGroupId === '__world__' ||
directLinkedAnnotationGroupId === '__world__'
directLinkedGroupId === PUBLIC_GROUP_ID ||
directLinkedAnnotationGroupId === PUBLIC_GROUP_ID
) {
return groups;
}

// Return non-world groups.
return groups.filter(g => g.id !== '__world__');
return groups.filter(g => g.id !== PUBLIC_GROUP_ID);
}

/**
Expand Down

0 comments on commit c300a7a

Please sign in to comment.