Commit 8d2cc1fc authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate groups to TypeScript

parent a19e2e24
/**
* @typedef {import('../../types/config').SidebarSettings} SidebarSettings
* @typedef {import('../../types/api').Group} Group
* @typedef {import('../../types/api').GroupIdentifier} GroupIdentifier
*/
import escapeStringRegexp from 'escape-string-regexp'; import escapeStringRegexp from 'escape-string-regexp';
import type { Group, GroupIdentifier } from '../../types/api';
import type { SidebarSettings } from '../../types/config';
import { serviceConfig } from '../config/service-config'; import { serviceConfig } from '../config/service-config';
/** /**
...@@ -12,11 +9,8 @@ import { serviceConfig } from '../config/service-config'; ...@@ -12,11 +9,8 @@ import { serviceConfig } from '../config/service-config';
* are a member? Users may leave private groups unless * are a member? Users may leave private groups unless
* explicitly disallowed in the service configuration of the * explicitly disallowed in the service configuration of the
* `settings` object. * `settings` object.
*
* @param {SidebarSettings} settings
* @return {boolean}
*/ */
function allowLeavingGroups(settings) { function allowLeavingGroups(settings: SidebarSettings): boolean {
const config = serviceConfig(settings); const config = serviceConfig(settings);
if (!config) { if (!config) {
return true; return true;
...@@ -30,12 +24,16 @@ function allowLeavingGroups(settings) { ...@@ -30,12 +24,16 @@ function allowLeavingGroups(settings) {
* Add an isScopedToUri property to each group indicating whether the uri matches the group's * Add an isScopedToUri property to each group indicating whether the uri matches the group's
* uri patterns. If no uri patterns are specified, defaults to True. * uri patterns. If no uri patterns are specified, defaults to True.
* *
* @param {Group[]} userGroups - groups the user is a member of * @param userGroups - groups the user is a member of
* @param {Group[]} featuredGroups - all other groups, may include some duplicates from the userGroups * @param featuredGroups - all other groups, may include some duplicates from the userGroups
* @param {string|null} uri - uri of the current page * @param uri - uri of the current page
* @param {SidebarSettings} settings
*/ */
export function combineGroups(userGroups, featuredGroups, uri, settings) { export function combineGroups(
userGroups: Group[],
featuredGroups: Group[],
uri: string | null,
settings: SidebarSettings
) {
const worldGroup = featuredGroups.find(g => g.id === '__world__'); const worldGroup = featuredGroups.find(g => g.id === '__world__');
if (worldGroup) { if (worldGroup) {
userGroups.unshift(worldGroup); userGroups.unshift(worldGroup);
...@@ -66,11 +64,7 @@ export function combineGroups(userGroups, featuredGroups, uri, settings) { ...@@ -66,11 +64,7 @@ export function combineGroups(userGroups, featuredGroups, uri, settings) {
return groups; return groups;
} }
/** function isScopedToUri(group: Group, uri: string | null): boolean {
* @param {Group} group
* @param {string|null} uri
*/
function isScopedToUri(group, uri) {
/* If a scope check cannot be performed, meaning: /* If a scope check cannot be performed, meaning:
* - the group doesn't have a scopes attribute * - the group doesn't have a scopes attribute
* - the group has no scopes.uri_patterns present * - the group has no scopes.uri_patterns present
...@@ -83,30 +77,23 @@ function isScopedToUri(group, uri) { ...@@ -83,30 +77,23 @@ function isScopedToUri(group, uri) {
return true; return true;
} }
/** function uriMatchesScopes(uri: string, scopes: string[]): boolean {
* @param {string} uri return scopes.some(uriRegex =>
* @param {string[]} scopes uri.match(
*/ // Convert *'s to .*'s for regex matching and escape all other special characters.
function uriMatchesScopes(uri, scopes) { uriRegex.split('*').map(escapeStringRegexp).join('.*')
return ( )
scopes.find(uriRegex =>
uri.match(
// Convert *'s to .*'s for regex matching and escape all other special characters.
uriRegex.split('*').map(escapeStringRegexp).join('.*')
)
) !== undefined
); );
} }
/** /**
* Find groups in `groups` by GroupIdentifier, which may be either an `id` or * Find groups in `groups` by GroupIdentifier, which may be either an `id` or
* `groupid`. * `groupid`.
*
* @param {GroupIdentifier[]} groupIds
* @param {Group[]} groups
* @return {Group[]}
*/ */
function findGroupsByAnyIds(groupIds, groups) { function findGroupsByAnyIds(
groupIds: GroupIdentifier[],
groups: Group[]
): Group[] {
return groups.filter( return groups.filter(
g => groupIds.includes(g.id) || (g.groupid && groupIds.includes(g.groupid)) g => groupIds.includes(g.id) || (g.groupid && groupIds.includes(g.groupid))
); );
...@@ -117,11 +104,10 @@ function findGroupsByAnyIds(groupIds, groups) { ...@@ -117,11 +104,10 @@ function findGroupsByAnyIds(groupIds, groups) {
* (pubid) or a `groupid` into a list of `id`s by locating associated groups * (pubid) or a `groupid` into a list of `id`s by locating associated groups
* in the set of all `groups`. Only return entries for groups that can be * in the set of all `groups`. Only return entries for groups that can be
* found in `groups`. * found in `groups`.
*
* @param {GroupIdentifier[]} groupIds
* @param {Group[]} groups
* @return {Group["id"][]}
*/ */
export function normalizeGroupIds(groupIds, groups) { export function normalizeGroupIds(
groupIds: GroupIdentifier[],
groups: Group[]
): Group['id'][] {
return findGroupsByAnyIds(groupIds, groups).map(g => g.id); return findGroupsByAnyIds(groupIds, groups).map(g => g.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