Unverified Commit 2a95f092 authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #799 from hypothesis/add-service-groups-config-option

Add service.groups setting to filter groups
parents 6783b96d ac3e3e4d
...@@ -186,6 +186,15 @@ loads. ...@@ -186,6 +186,15 @@ loads.
group. The image should be suitable for display at 16x16px and the group. The image should be suitable for display at 16x16px and the
recommended format is SVG. recommended format is SVG.
.. option:: groups
``String[]|null``. An array of group IDs. If provided, the list of groups
fetched from the API will be filtered against this list so that the user
can only select from these groups.
This can be useful in contexts where it is important that annotations
are made in a particular group.
.. option:: onLoginRequest .. option:: onLoginRequest
``function``. A JavaScript function that the Hypothesis client will ``function``. A JavaScript function that the Hypothesis client will
......
...@@ -48,6 +48,13 @@ function groups($rootScope, store, api, isSidebar, localStorage, serviceUrl, ses ...@@ -48,6 +48,13 @@ function groups($rootScope, store, api, isSidebar, localStorage, serviceUrl, ses
* @return {Promise<Group[]>} * @return {Promise<Group[]>}
*/ */
function filterGroups(groups, isLoggedIn, directLinkedAnnotationId) { function filterGroups(groups, isLoggedIn, directLinkedAnnotationId) {
// If service groups are specified only return those.
// If a service group doesn't exist in the list of groups don't return it.
if (svc && svc.groups) {
const focusedGroups = groups.filter(g => svc.groups.includes(g.id));
return Promise.resolve(focusedGroups);
}
// Logged-in users always see the "Public" group. // Logged-in users always see the "Public" group.
if (isLoggedIn) { if (isLoggedIn) {
return Promise.resolve(groups); return Promise.resolve(groups);
......
...@@ -253,6 +253,46 @@ describe('groups', function() { ...@@ -253,6 +253,46 @@ describe('groups', function() {
}); });
}); });
}); });
[{
description: 'shows service groups',
services: [{ groups: ['abc123']}],
expected: ['abc123'],
},{
description: 'only shows service groups that exist',
services: [{ groups: ['abc123', 'no_exist']}],
expected: ['abc123'],
},{
description: 'shows no groups if no service groups exist',
services: [{ groups: ['no_exist']}],
expected: [],
},{
description: 'shows all groups if service is null',
services: null,
expected: ['__world__', 'abc123'],
},{
description: 'shows all groups if service groups does not exist',
services: [{}],
expected: ['__world__', 'abc123'],
}].forEach(({ description, services, expected }) => {
it(description, () => {
fakeSettings.services = services;
const svc = service();
// Create groups response from server.
const groups = [{ name: 'Public', id: '__world__' }, { name: 'ABC', id: 'abc123'}];
fakeApi.groups.list.returns(Promise.resolve({
token: '1234',
data: groups,
}));
return svc.load().then(groups => {
let displayedGroups = groups.map(g => g.id);
assert.deepEqual(displayedGroups, expected);
});
});
});
}); });
describe('#get', function() { describe('#get', function() {
......
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