@openpeepshq/common 0.1.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/lib/adminSections.d.ts +53 -0
- package/dist/lib/adminSections.js +62 -0
- package/dist/lib/attachmentHelpers.d.ts +1 -0
- package/dist/lib/attachmentHelpers.js +13 -0
- package/dist/lib/capabilitiesHelpers.d.ts +118 -0
- package/dist/lib/capabilitiesHelpers.js +238 -0
- package/dist/lib/colors.d.ts +5 -0
- package/dist/lib/colors.js +17 -0
- package/dist/lib/csvHelpers.d.ts +6 -0
- package/dist/lib/csvHelpers.js +17 -0
- package/dist/lib/eventIcs.d.ts +4 -0
- package/dist/lib/eventIcs.js +83 -0
- package/dist/lib/groupHelpers.d.ts +134 -0
- package/dist/lib/groupHelpers.js +216 -0
- package/dist/lib/index.d.ts +17 -0
- package/dist/lib/index.js +17 -0
- package/dist/lib/jamHelpers.d.ts +5 -0
- package/dist/lib/jamHelpers.js +37 -0
- package/dist/lib/markdownHelpers.d.ts +1 -0
- package/dist/lib/markdownHelpers.js +24 -0
- package/dist/lib/mediaDimensions.d.ts +19 -0
- package/dist/lib/mediaDimensions.js +19 -0
- package/dist/lib/postHelpers.d.ts +146 -0
- package/dist/lib/postHelpers.js +144 -0
- package/dist/lib/profileHelpers.d.ts +30 -0
- package/dist/lib/profileHelpers.js +75 -0
- package/dist/lib/pushHelpers.d.ts +4 -0
- package/dist/lib/pushHelpers.js +6 -0
- package/dist/lib/scopeHelpers.d.ts +25 -0
- package/dist/lib/scopeHelpers.js +85 -0
- package/dist/lib/text.d.ts +3 -0
- package/dist/lib/text.js +18 -0
- package/dist/lib/tokenHelpers.d.ts +21 -0
- package/dist/lib/tokenHelpers.js +81 -0
- package/dist/lib/utils.d.ts +43 -0
- package/dist/lib/utils.js +200 -0
- package/dist/types/admin-db.d.ts +62 -0
- package/dist/types/admin-db.js +41 -0
- package/dist/types/admin-email.d.ts +39 -0
- package/dist/types/admin-email.js +41 -0
- package/dist/types/admin-jobs.d.ts +15 -0
- package/dist/types/admin-jobs.js +14 -0
- package/dist/types/admin-performance.d.ts +23 -0
- package/dist/types/admin-performance.js +14 -0
- package/dist/types/analytics.d.ts +583 -0
- package/dist/types/analytics.js +213 -0
- package/dist/types/api.d.ts +20076 -0
- package/dist/types/api.js +492 -0
- package/dist/types/apple-app-site-association.d.ts +50 -0
- package/dist/types/apple-app-site-association.js +38 -0
- package/dist/types/auth.d.ts +367 -0
- package/dist/types/auth.js +17 -0
- package/dist/types/capabilities.d.ts +24 -0
- package/dist/types/capabilities.js +144 -0
- package/dist/types/config.d.ts +858 -0
- package/dist/types/config.js +352 -0
- package/dist/types/db.d.ts +8780 -0
- package/dist/types/db.js +65 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/index.js +18 -0
- package/dist/types/internal.d.ts +44337 -0
- package/dist/types/internal.js +154 -0
- package/dist/types/models.d.ts +2619 -0
- package/dist/types/models.js +599 -0
- package/dist/types/notifications.d.ts +148 -0
- package/dist/types/notifications.js +40 -0
- package/dist/types/plugins.d.ts +66 -0
- package/dist/types/plugins.js +29 -0
- package/dist/types/stats.d.ts +10216 -0
- package/dist/types/stats.js +109 -0
- package/dist/types/stripe.d.ts +42 -0
- package/dist/types/stripe.js +35 -0
- package/dist/types/utils.d.ts +84 -0
- package/dist/types/utils.js +39 -0
- package/package.json +54 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 All Peep Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Role } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Admin sidebar sections and the role capabilities required to see each one.
|
|
4
|
+
* All listed capabilities must be present (AND). Matches `ensureRoleCapabilities`
|
|
5
|
+
* on the corresponding admin API routes.
|
|
6
|
+
*/
|
|
7
|
+
export declare const adminSections: readonly [{
|
|
8
|
+
readonly key: "members";
|
|
9
|
+
readonly path: "/admin/members";
|
|
10
|
+
readonly capabilities: readonly ["core-accounts-read", "core-profiles-read"];
|
|
11
|
+
}, {
|
|
12
|
+
readonly key: "groups";
|
|
13
|
+
readonly path: "/admin/groups";
|
|
14
|
+
readonly capabilities: readonly ["core-groups-read"];
|
|
15
|
+
}, {
|
|
16
|
+
readonly key: "invites";
|
|
17
|
+
readonly path: "/admin/invites";
|
|
18
|
+
readonly capabilities: readonly ["core-inviteLinks-read"];
|
|
19
|
+
}, {
|
|
20
|
+
readonly key: "moderation";
|
|
21
|
+
readonly path: "/admin/moderation";
|
|
22
|
+
readonly capabilities: readonly ["core-reports-read"];
|
|
23
|
+
}, {
|
|
24
|
+
readonly key: "backups";
|
|
25
|
+
readonly path: "/admin/backups";
|
|
26
|
+
readonly capabilities: readonly ["core-backups-read"];
|
|
27
|
+
}, {
|
|
28
|
+
readonly key: "analytics";
|
|
29
|
+
readonly path: "/admin/analytics";
|
|
30
|
+
readonly capabilities: readonly ["core-analytics-read"];
|
|
31
|
+
}, {
|
|
32
|
+
readonly key: "apiKeys";
|
|
33
|
+
readonly path: "/admin/api-keys";
|
|
34
|
+
readonly capabilities: readonly ["core-serviceTokens-read"];
|
|
35
|
+
}, {
|
|
36
|
+
readonly key: "configuration";
|
|
37
|
+
readonly path: "/admin/configuration";
|
|
38
|
+
readonly capabilities: readonly ["core-config-read"];
|
|
39
|
+
}, {
|
|
40
|
+
readonly key: "diagnostics";
|
|
41
|
+
readonly path: "/admin/diagnostics";
|
|
42
|
+
readonly capabilities: readonly ["core-config-read"];
|
|
43
|
+
}, {
|
|
44
|
+
readonly key: "database";
|
|
45
|
+
readonly path: "/admin/db";
|
|
46
|
+
readonly capabilities: readonly ["core-db-access"];
|
|
47
|
+
}];
|
|
48
|
+
export type AdminSectionKey = (typeof adminSections)[number]['key'];
|
|
49
|
+
export type AdminSection = (typeof adminSections)[number];
|
|
50
|
+
export declare const canAccessAdminSection: (roles: Role[] | undefined, section: AdminSection) => boolean;
|
|
51
|
+
export declare const getVisibleAdminSections: (roles: Role[] | undefined) => AdminSection[];
|
|
52
|
+
/** True when the profile may see at least one admin menu entry. */
|
|
53
|
+
export declare const canAccessAdminMenu: (roles: Role[] | undefined) => boolean;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { checkRoleCapabilities } from './capabilitiesHelpers.js';
|
|
2
|
+
/**
|
|
3
|
+
* Admin sidebar sections and the role capabilities required to see each one.
|
|
4
|
+
* All listed capabilities must be present (AND). Matches `ensureRoleCapabilities`
|
|
5
|
+
* on the corresponding admin API routes.
|
|
6
|
+
*/
|
|
7
|
+
export const adminSections = [
|
|
8
|
+
{
|
|
9
|
+
key: 'members',
|
|
10
|
+
path: '/admin/members',
|
|
11
|
+
capabilities: ['core-accounts-read', 'core-profiles-read'],
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
key: 'groups',
|
|
15
|
+
path: '/admin/groups',
|
|
16
|
+
capabilities: ['core-groups-read'],
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
key: 'invites',
|
|
20
|
+
path: '/admin/invites',
|
|
21
|
+
capabilities: ['core-inviteLinks-read'],
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
key: 'moderation',
|
|
25
|
+
path: '/admin/moderation',
|
|
26
|
+
capabilities: ['core-reports-read'],
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
key: 'backups',
|
|
30
|
+
path: '/admin/backups',
|
|
31
|
+
capabilities: ['core-backups-read'],
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
key: 'analytics',
|
|
35
|
+
path: '/admin/analytics',
|
|
36
|
+
capabilities: ['core-analytics-read'],
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
key: 'apiKeys',
|
|
40
|
+
path: '/admin/api-keys',
|
|
41
|
+
capabilities: ['core-serviceTokens-read'],
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
key: 'configuration',
|
|
45
|
+
path: '/admin/configuration',
|
|
46
|
+
capabilities: ['core-config-read'],
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
key: 'diagnostics',
|
|
50
|
+
path: '/admin/diagnostics',
|
|
51
|
+
capabilities: ['core-config-read'],
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
key: 'database',
|
|
55
|
+
path: '/admin/db',
|
|
56
|
+
capabilities: ['core-db-access'],
|
|
57
|
+
},
|
|
58
|
+
];
|
|
59
|
+
export const canAccessAdminSection = (roles, section) => checkRoleCapabilities(roles !== null && roles !== void 0 ? roles : [], [...section.capabilities]).success;
|
|
60
|
+
export const getVisibleAdminSections = (roles) => adminSections.filter((section) => canAccessAdminSection(roles, section));
|
|
61
|
+
/** True when the profile may see at least one admin menu entry. */
|
|
62
|
+
export const canAccessAdminMenu = (roles) => getVisibleAdminSections(roles).length > 0;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getAttachmentType: (file: File) => string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const getAttachmentType = (file) => {
|
|
2
|
+
if (file.name.endsWith('.mkv')) {
|
|
3
|
+
return 'video';
|
|
4
|
+
}
|
|
5
|
+
if (file.name.endsWith('.pdf') || file.name.endsWith('.doc') || file.name.endsWith('.docx') || file.name.endsWith('.odt') || file.name.endsWith('.txt')) {
|
|
6
|
+
return 'document';
|
|
7
|
+
}
|
|
8
|
+
const mimeTypeFirstPart = file.type.split('/')[0];
|
|
9
|
+
if (mimeTypeFirstPart === 'application' || mimeTypeFirstPart === 'text') {
|
|
10
|
+
return 'document';
|
|
11
|
+
}
|
|
12
|
+
return mimeTypeFirstPart;
|
|
13
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { AccessTokenRelationship, AccountWithMeta, AuthorizationData, Capabilities, CapabilitiesConfig, GroupRelationship, PostRelationship, ProfileRelationship, ProfileWithMeta, PublicPost, PublicPostInput, PublicProfile, PublicReport, ReportRelationship, Role, AccessTokenWithMeta, GroupWithMeta } from '../types';
|
|
2
|
+
export declare const checkCapabilities: (neededCapabilities: string[], presentCapabilities?: {
|
|
3
|
+
add?: string[];
|
|
4
|
+
remove?: string[];
|
|
5
|
+
}) => {
|
|
6
|
+
success: boolean;
|
|
7
|
+
missingCapabilities: string[];
|
|
8
|
+
};
|
|
9
|
+
export declare const mergeCapabilities: (capabilities?: Partial<Capabilities>[]) => Capabilities;
|
|
10
|
+
export declare const checkRoleCapabilities: (roles: Role[] | undefined, neededCapabilities: string[]) => {
|
|
11
|
+
success: boolean;
|
|
12
|
+
missingCapabilities: string[];
|
|
13
|
+
};
|
|
14
|
+
/** Any of these matching `roles` shows the Administration row (submenu is separate UX). */
|
|
15
|
+
export declare const ADMIN_SIDEBAR_MENU_CAPABILITIES: readonly ["admin", "core-roles-read", "core-logs-read", "core-accounts-list", "core-backups-read", "core-analytics-read", "core-inviteLinks-read", "core-maintenance-restart", "core-config-read", "core-customization-read"];
|
|
16
|
+
export declare const hasAdminSidebarAccess: (roles?: Role[]) => boolean;
|
|
17
|
+
export declare const isLocal: (profile: ProfileWithMeta) => boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Returns `['local', 'none']` when the caller is "trusted local" — either a local
|
|
20
|
+
* profile or an authenticated service — and `['none']` otherwise.
|
|
21
|
+
*/
|
|
22
|
+
export declare const localOrNone: (authData: AuthorizationData) => ["local", "none"] | ["none"];
|
|
23
|
+
export declare const getGroupRelationships: (authData: AuthorizationData, group: GroupWithMeta) => [GroupRelationship, ...GroupRelationship[]];
|
|
24
|
+
export declare const getProfileRelationships: (authData: AuthorizationData, otherProfile: PublicProfile) => ProfileRelationship[];
|
|
25
|
+
export declare const getPostRelationships: (authData: AuthorizationData, post: PublicPostInput) => PostRelationship[];
|
|
26
|
+
export declare const getReportRelationships: (authData: AuthorizationData, report: PublicReport) => ReportRelationship[];
|
|
27
|
+
export declare const getAccessTokenRelationships: (authData: AuthorizationData, accessToken: AccessTokenWithMeta) => AccessTokenRelationship[];
|
|
28
|
+
export declare const getGroupCapabilitiesByRoles: (roles: string[] | undefined | null, group?: GroupWithMeta | null) => {
|
|
29
|
+
add?: string[] | undefined;
|
|
30
|
+
remove?: string[] | undefined;
|
|
31
|
+
} | undefined;
|
|
32
|
+
export declare const getGroupCapabilities: (authData: AuthorizationData, group?: GroupWithMeta | null) => Capabilities;
|
|
33
|
+
export declare const getProfileCapabilities: (authData: AuthorizationData, targetProfile: PublicProfile, config: CapabilitiesConfig) => Capabilities;
|
|
34
|
+
export declare const getPostCapabilities: (authData: AuthorizationData, post: PublicPost, config: CapabilitiesConfig) => Capabilities;
|
|
35
|
+
export declare const checkGroupCapabilities: (authData: AuthorizationData, neededCapabilities: string[], group: GroupWithMeta) => {
|
|
36
|
+
success: boolean;
|
|
37
|
+
missingCapabilities: string[];
|
|
38
|
+
missingScope: {
|
|
39
|
+
resource: {
|
|
40
|
+
type: "self" | "db" | "jams" | "profiles" | "*" | "jam" | "notifications" | "posts" | "groups" | "reports" | "webhooks" | "analytics" | "render";
|
|
41
|
+
id?: string | undefined;
|
|
42
|
+
};
|
|
43
|
+
scopeLevel?: "admin" | "write" | "read" | undefined;
|
|
44
|
+
} | undefined;
|
|
45
|
+
};
|
|
46
|
+
export declare const checkPostCapabilities: (authData: AuthorizationData, neededCapabilities: string[], post: PublicPost, config: CapabilitiesConfig) => {
|
|
47
|
+
success: boolean;
|
|
48
|
+
missingCapabilities: string[];
|
|
49
|
+
missingScope: {
|
|
50
|
+
resource: {
|
|
51
|
+
type: "self" | "db" | "jams" | "profiles" | "*" | "jam" | "notifications" | "posts" | "groups" | "reports" | "webhooks" | "analytics" | "render";
|
|
52
|
+
id?: string | undefined;
|
|
53
|
+
};
|
|
54
|
+
scopeLevel?: "admin" | "write" | "read" | undefined;
|
|
55
|
+
} | undefined;
|
|
56
|
+
};
|
|
57
|
+
export declare const checkProfileCapabilities: (authData: AuthorizationData, neededCapabilities: string[], targetProfile: PublicProfile, config: CapabilitiesConfig) => {
|
|
58
|
+
success: boolean;
|
|
59
|
+
missingCapabilities: string[];
|
|
60
|
+
missingScope: {
|
|
61
|
+
resource: {
|
|
62
|
+
type: "self" | "db" | "jams" | "profiles" | "*" | "jam" | "notifications" | "posts" | "groups" | "reports" | "webhooks" | "analytics" | "render";
|
|
63
|
+
id?: string | undefined;
|
|
64
|
+
};
|
|
65
|
+
scopeLevel?: "admin" | "write" | "read" | undefined;
|
|
66
|
+
} | undefined;
|
|
67
|
+
};
|
|
68
|
+
export declare const checkReportCapabilities: (authData: AuthorizationData, neededCapabilities: string[], report: PublicReport, config: CapabilitiesConfig) => {
|
|
69
|
+
success: boolean;
|
|
70
|
+
missingCapabilities: string[];
|
|
71
|
+
missingScope: {
|
|
72
|
+
resource: {
|
|
73
|
+
type: "self" | "db" | "jams" | "profiles" | "*" | "jam" | "notifications" | "posts" | "groups" | "reports" | "webhooks" | "analytics" | "render";
|
|
74
|
+
id?: string | undefined;
|
|
75
|
+
};
|
|
76
|
+
scopeLevel?: "admin" | "write" | "read" | undefined;
|
|
77
|
+
} | undefined;
|
|
78
|
+
};
|
|
79
|
+
export declare const checkAccessTokenCapabilities: (authData: AuthorizationData, neededCapabilities: string[], accessToken: AccessTokenWithMeta, config: CapabilitiesConfig) => {
|
|
80
|
+
success: boolean;
|
|
81
|
+
missingCapabilities: string[];
|
|
82
|
+
missingScope: {
|
|
83
|
+
resource: {
|
|
84
|
+
type: "self" | "db" | "jams" | "profiles" | "*" | "jam" | "notifications" | "posts" | "groups" | "reports" | "webhooks" | "analytics" | "render";
|
|
85
|
+
id?: string | undefined;
|
|
86
|
+
};
|
|
87
|
+
scopeLevel?: "admin" | "write" | "read" | undefined;
|
|
88
|
+
} | undefined;
|
|
89
|
+
};
|
|
90
|
+
export declare const checkAccountCapabilities: (authData: AuthorizationData, neededCapabilities: string[], targetAccount: AccountWithMeta) => {
|
|
91
|
+
success: boolean;
|
|
92
|
+
missingCapabilities: string[];
|
|
93
|
+
missingScope: {
|
|
94
|
+
resource: {
|
|
95
|
+
type: "self" | "db" | "jams" | "profiles" | "*" | "jam" | "notifications" | "posts" | "groups" | "reports" | "webhooks" | "analytics" | "render";
|
|
96
|
+
id?: string | undefined;
|
|
97
|
+
};
|
|
98
|
+
scopeLevel?: "admin" | "write" | "read" | undefined;
|
|
99
|
+
} | undefined;
|
|
100
|
+
};
|
|
101
|
+
/** Capability required to create accounts when public sign-ups are closed. */
|
|
102
|
+
export declare const ACCOUNT_CREATE_CAPABILITY = "core-accounts-create";
|
|
103
|
+
/**
|
|
104
|
+
* Whether the caller may register a new account/profile while sign-ups are closed.
|
|
105
|
+
* Profile tokens need the role capability; service tokens are treated as having it
|
|
106
|
+
* and are gated by write scope on profiles (same as other create operations).
|
|
107
|
+
*/
|
|
108
|
+
export declare const checkAccountCreateAuthorization: (authData: AuthorizationData) => {
|
|
109
|
+
success: boolean;
|
|
110
|
+
missingCapabilities: string[];
|
|
111
|
+
missingScope: {
|
|
112
|
+
resource: {
|
|
113
|
+
type: "self" | "db" | "jams" | "profiles" | "*" | "jam" | "notifications" | "posts" | "groups" | "reports" | "webhooks" | "analytics" | "render";
|
|
114
|
+
id?: string | undefined;
|
|
115
|
+
};
|
|
116
|
+
scopeLevel?: "admin" | "write" | "read" | undefined;
|
|
117
|
+
} | undefined;
|
|
118
|
+
};
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { calculateRequiredScope, scopeMatches, withPublicPostReadScopes, withPublicGroupReadScopes, } from './scopeHelpers.js';
|
|
2
|
+
export const checkCapabilities = (neededCapabilities, presentCapabilities = {
|
|
3
|
+
add: [],
|
|
4
|
+
remove: [],
|
|
5
|
+
}) => {
|
|
6
|
+
const containsCapability = (c, clist) => !!(clist === null || clist === void 0 ? void 0 : clist.find((pc) => pc === c || (pc.includes('*') && c.startsWith(pc.split('*')[0]))));
|
|
7
|
+
const missingCapabilities = neededCapabilities.filter((c) => {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
return containsCapability(c, (_a = presentCapabilities.remove) !== null && _a !== void 0 ? _a : []) ||
|
|
10
|
+
!containsCapability(c, (_b = presentCapabilities.add) !== null && _b !== void 0 ? _b : []);
|
|
11
|
+
});
|
|
12
|
+
return {
|
|
13
|
+
success: missingCapabilities.length === 0,
|
|
14
|
+
missingCapabilities,
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export const mergeCapabilities = (capabilities) => {
|
|
18
|
+
var _a, _b;
|
|
19
|
+
return {
|
|
20
|
+
add: [...new Set((_a = capabilities === null || capabilities === void 0 ? void 0 : capabilities.map((c) => (c === null || c === void 0 ? void 0 : c.add) || []).flat()) !== null && _a !== void 0 ? _a : [])],
|
|
21
|
+
remove: [
|
|
22
|
+
...new Set((_b = capabilities === null || capabilities === void 0 ? void 0 : capabilities.map((c) => (c === null || c === void 0 ? void 0 : c.remove) || []).flat()) !== null && _b !== void 0 ? _b : []),
|
|
23
|
+
],
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export const checkRoleCapabilities = (roles = [], neededCapabilities) => checkCapabilities(neededCapabilities, mergeCapabilities(roles.map((r) => r.capabilities)));
|
|
27
|
+
/** Any of these matching `roles` shows the Administration row (submenu is separate UX). */
|
|
28
|
+
export const ADMIN_SIDEBAR_MENU_CAPABILITIES = [
|
|
29
|
+
'admin',
|
|
30
|
+
'core-roles-read',
|
|
31
|
+
'core-logs-read',
|
|
32
|
+
'core-accounts-list',
|
|
33
|
+
'core-backups-read',
|
|
34
|
+
'core-analytics-read',
|
|
35
|
+
'core-inviteLinks-read',
|
|
36
|
+
'core-maintenance-restart',
|
|
37
|
+
'core-config-read',
|
|
38
|
+
'core-customization-read',
|
|
39
|
+
];
|
|
40
|
+
export const hasAdminSidebarAccess = (roles = []) => ADMIN_SIDEBAR_MENU_CAPABILITIES.some((cap) => checkRoleCapabilities(roles, [cap]).success);
|
|
41
|
+
export const isLocal = (profile) => checkRoleCapabilities(profile.roles, ['core-local']).success;
|
|
42
|
+
/**
|
|
43
|
+
* Returns `['local', 'none']` when the caller is "trusted local" — either a local
|
|
44
|
+
* profile or an authenticated service — and `['none']` otherwise.
|
|
45
|
+
*/
|
|
46
|
+
export const localOrNone = (authData) => {
|
|
47
|
+
if (authData.service) {
|
|
48
|
+
return ['local', 'none'];
|
|
49
|
+
}
|
|
50
|
+
if (authData.profile && isLocal(authData.profile)) {
|
|
51
|
+
return ['local', 'none'];
|
|
52
|
+
}
|
|
53
|
+
return ['none'];
|
|
54
|
+
};
|
|
55
|
+
export const getGroupRelationships = (authData, group) => {
|
|
56
|
+
var _a, _b;
|
|
57
|
+
const { profile } = authData;
|
|
58
|
+
if (profile) {
|
|
59
|
+
return [
|
|
60
|
+
'local',
|
|
61
|
+
'none',
|
|
62
|
+
...((_b = (_a = profile.memberships.find((m) => m.group.id === group.id)) === null || _a === void 0 ? void 0 : _a.roles) !== null && _b !== void 0 ? _b : []),
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
if (authData.service) {
|
|
66
|
+
return ['local', 'none'];
|
|
67
|
+
}
|
|
68
|
+
return ['none'];
|
|
69
|
+
};
|
|
70
|
+
export const getProfileRelationships = (authData, otherProfile) => {
|
|
71
|
+
const { profile } = authData;
|
|
72
|
+
if (profile && profile.id === (otherProfile === null || otherProfile === void 0 ? void 0 : otherProfile.id)) {
|
|
73
|
+
return ['self', ...localOrNone(authData)];
|
|
74
|
+
}
|
|
75
|
+
const relationships = localOrNone(authData);
|
|
76
|
+
if (profile === null || profile === void 0 ? void 0 : profile.followers.map((f) => f.id).includes(otherProfile === null || otherProfile === void 0 ? void 0 : otherProfile.id)) {
|
|
77
|
+
relationships.push('followed-by');
|
|
78
|
+
}
|
|
79
|
+
if (profile === null || profile === void 0 ? void 0 : profile.following.map((f) => f.id).includes(otherProfile === null || otherProfile === void 0 ? void 0 : otherProfile.id)) {
|
|
80
|
+
relationships.push('following');
|
|
81
|
+
}
|
|
82
|
+
return relationships;
|
|
83
|
+
};
|
|
84
|
+
export const getPostRelationships = (authData, post) => {
|
|
85
|
+
var _a, _b, _c;
|
|
86
|
+
const { profile } = authData;
|
|
87
|
+
const relationships = [];
|
|
88
|
+
if (['public', 'local'].includes(post.visibility)) {
|
|
89
|
+
relationships.push(...localOrNone(authData));
|
|
90
|
+
}
|
|
91
|
+
if (profile && ((_a = post.audience) === null || _a === void 0 ? void 0 : _a.some((a) => a.id === profile.id))) {
|
|
92
|
+
relationships.push('audience');
|
|
93
|
+
}
|
|
94
|
+
if (profile && ((_b = post.mentions) === null || _b === void 0 ? void 0 : _b.some((m) => m.profile.id === profile.id))) {
|
|
95
|
+
relationships.push('mentioned');
|
|
96
|
+
}
|
|
97
|
+
if (profile &&
|
|
98
|
+
((_c = post.entries) === null || _c === void 0 ? void 0 : _c.some((e) => e.type === 'rsvp' &&
|
|
99
|
+
e.profile.id === profile.id &&
|
|
100
|
+
['yes', 'tentative'].includes(e.data.response)))) {
|
|
101
|
+
relationships.push('attendee');
|
|
102
|
+
}
|
|
103
|
+
return relationships;
|
|
104
|
+
};
|
|
105
|
+
export const getReportRelationships = (authData, report) => {
|
|
106
|
+
const { profile } = authData;
|
|
107
|
+
if (profile && report.reporterProfile.id === profile.id) {
|
|
108
|
+
return [...localOrNone(authData), 'reporter'];
|
|
109
|
+
}
|
|
110
|
+
if (profile && report.reportedProfile.id === profile.id) {
|
|
111
|
+
return [...localOrNone(authData), 'reported'];
|
|
112
|
+
}
|
|
113
|
+
return localOrNone(authData);
|
|
114
|
+
};
|
|
115
|
+
export const getAccessTokenRelationships = (authData, accessToken) => {
|
|
116
|
+
var _a;
|
|
117
|
+
const { profile } = authData;
|
|
118
|
+
const relationships = [...localOrNone(authData)];
|
|
119
|
+
const ownerProfileId = (_a = accessToken.ownedBy) === null || _a === void 0 ? void 0 : _a.id;
|
|
120
|
+
if (ownerProfileId && (profile === null || profile === void 0 ? void 0 : profile.id) === ownerProfileId) {
|
|
121
|
+
relationships.push('owner');
|
|
122
|
+
}
|
|
123
|
+
return relationships;
|
|
124
|
+
};
|
|
125
|
+
const getConfigCapabilities = (object, relationships, config) => mergeCapabilities(relationships.map((r) => {
|
|
126
|
+
var _a;
|
|
127
|
+
const group = config[object];
|
|
128
|
+
return (_a = group[r]) !== null && _a !== void 0 ? _a : { add: [], remove: [] };
|
|
129
|
+
}));
|
|
130
|
+
const getGroupRelationshipCapabilities = (authData, group) => mergeCapabilities(group
|
|
131
|
+
? getGroupRelationships(authData, group).map((r) => { var _a, _b; return (_b = (_a = group.capabilities) === null || _a === void 0 ? void 0 : _a[r]) !== null && _b !== void 0 ? _b : { add: [], remove: [] }; })
|
|
132
|
+
: []);
|
|
133
|
+
export const getGroupCapabilitiesByRoles = (roles, group) => mergeCapabilities((roles !== null && roles !== void 0 ? roles : []).map((r) => { var _a, _b; return (_b = (_a = group === null || group === void 0 ? void 0 : group.capabilities) === null || _a === void 0 ? void 0 : _a[r]) !== null && _b !== void 0 ? _b : { add: [], remove: [] }; }));
|
|
134
|
+
// Per-group powers (add member, post in group, moderate, …) come from the
|
|
135
|
+
// caller's relationship to the group (edge roles plus none/local), never from
|
|
136
|
+
// their instance role. Instance roles only gate group *creation*
|
|
137
|
+
// (`core-groups-create`), checked separately at the creation entry points.
|
|
138
|
+
export const getGroupCapabilities = (authData, group) => getGroupRelationshipCapabilities(authData, group);
|
|
139
|
+
const getRoleCapabilities = (profile) => { var _a; return mergeCapabilities((_a = profile === null || profile === void 0 ? void 0 : profile.roles.map((r) => r.capabilities)) !== null && _a !== void 0 ? _a : []); };
|
|
140
|
+
export const getProfileCapabilities = (authData, targetProfile, config) => getConfigCapabilities('profile', getProfileRelationships(authData, targetProfile), config);
|
|
141
|
+
const getPostVisibilityCapabilities = (post, profile) => {
|
|
142
|
+
switch (post.visibility) {
|
|
143
|
+
case 'public':
|
|
144
|
+
return { add: ['core-posts-read'], remove: [] };
|
|
145
|
+
case 'direct':
|
|
146
|
+
return {};
|
|
147
|
+
// Group posts derive their capabilities from the group relationship
|
|
148
|
+
// (`getGroupCapabilities`) and the author's `self` relationship only —
|
|
149
|
+
// never from instance roles. This keeps per-group powers consistent with
|
|
150
|
+
// every other group check (see getGroupCapabilities); instance roles only
|
|
151
|
+
// gate group creation.
|
|
152
|
+
case 'group':
|
|
153
|
+
return {};
|
|
154
|
+
default:
|
|
155
|
+
return getRoleCapabilities(profile);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const getScopedPostReadCapabilities = (authData, neededCapabilities, post) => {
|
|
159
|
+
if (authData.profile ||
|
|
160
|
+
post.visibility !== 'local' ||
|
|
161
|
+
neededCapabilities.length !== 1 ||
|
|
162
|
+
neededCapabilities[0] !== 'core-posts-read' ||
|
|
163
|
+
!scopeMatches({
|
|
164
|
+
scopes: authData.scopes,
|
|
165
|
+
requiredScope: {
|
|
166
|
+
scopeLevel: 'admin',
|
|
167
|
+
resource: { type: 'posts', id: post.id },
|
|
168
|
+
},
|
|
169
|
+
})) {
|
|
170
|
+
return { add: [], remove: [] };
|
|
171
|
+
}
|
|
172
|
+
return { add: ['core-posts-read'], remove: [] };
|
|
173
|
+
};
|
|
174
|
+
export const getPostCapabilities = (authData, post, config) => mergeCapabilities([
|
|
175
|
+
getPostVisibilityCapabilities(post, authData.profile),
|
|
176
|
+
getConfigCapabilities('post', getPostRelationships(authData, post), config),
|
|
177
|
+
getConfigCapabilities('profile', getProfileRelationships(authData, post.profile), config),
|
|
178
|
+
getGroupCapabilities(authData, post.group),
|
|
179
|
+
]);
|
|
180
|
+
const getReportCapabilities = (authData, report, config) => mergeCapabilities([
|
|
181
|
+
getConfigCapabilities('report', getReportRelationships(authData, report), config),
|
|
182
|
+
getRoleCapabilities(authData.profile),
|
|
183
|
+
]);
|
|
184
|
+
const getAccessTokenCapabilities = (authData, accessToken, config) => mergeCapabilities([
|
|
185
|
+
getConfigCapabilities('accessToken', getAccessTokenRelationships(authData, accessToken), config),
|
|
186
|
+
getRoleCapabilities(authData.profile),
|
|
187
|
+
]);
|
|
188
|
+
const getAccountCapabilities = (authData, targetAccount) => {
|
|
189
|
+
var _a;
|
|
190
|
+
return mergeCapabilities([
|
|
191
|
+
((_a = authData.account) === null || _a === void 0 ? void 0 : _a.id) === targetAccount.id
|
|
192
|
+
? { add: ['core-accounts-update'], remove: [] }
|
|
193
|
+
: { add: [], remove: [] },
|
|
194
|
+
getRoleCapabilities(authData.profile),
|
|
195
|
+
]);
|
|
196
|
+
};
|
|
197
|
+
const checkCapabilitiesWithCalculatedScope = (capabilities, authData, neededCapabilities, resource) => {
|
|
198
|
+
const requiredScope = calculateRequiredScope({
|
|
199
|
+
resource,
|
|
200
|
+
requiredCapabilities: neededCapabilities,
|
|
201
|
+
});
|
|
202
|
+
return checkCapabilitiesWithExplicitScope(capabilities, authData, neededCapabilities, requiredScope);
|
|
203
|
+
};
|
|
204
|
+
const checkCapabilitiesWithExplicitScope = (capabilities, authData, neededCapabilities, requiredScope) => {
|
|
205
|
+
const capabilitiesResult = checkCapabilities(neededCapabilities, capabilities);
|
|
206
|
+
const scopeResult = scopeMatches({ scopes: authData.scopes, requiredScope });
|
|
207
|
+
return {
|
|
208
|
+
success: capabilitiesResult.success && scopeResult,
|
|
209
|
+
missingCapabilities: capabilitiesResult.missingCapabilities,
|
|
210
|
+
missingScope: scopeResult ? undefined : requiredScope,
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
export const checkGroupCapabilities = (authData, neededCapabilities, group) => checkCapabilitiesWithCalculatedScope(getGroupCapabilities(authData, group), withPublicGroupReadScopes(authData, group, neededCapabilities), neededCapabilities, { type: 'groups', id: group.id });
|
|
214
|
+
export const checkPostCapabilities = (authData, neededCapabilities, post, config) => {
|
|
215
|
+
const capabilities = mergeCapabilities([
|
|
216
|
+
getPostCapabilities(authData, post, config),
|
|
217
|
+
getScopedPostReadCapabilities(authData, neededCapabilities, post),
|
|
218
|
+
]);
|
|
219
|
+
return checkCapabilitiesWithCalculatedScope(capabilities, withPublicPostReadScopes(authData, post, neededCapabilities), neededCapabilities, { type: 'posts', id: post.id });
|
|
220
|
+
};
|
|
221
|
+
export const checkProfileCapabilities = (authData, neededCapabilities, targetProfile, config) => checkCapabilitiesWithCalculatedScope(getProfileCapabilities(authData, targetProfile, config), authData, neededCapabilities, { type: 'profiles', id: targetProfile.id });
|
|
222
|
+
export const checkReportCapabilities = (authData, neededCapabilities, report, config) => checkCapabilitiesWithCalculatedScope(getReportCapabilities(authData, report, config), authData, neededCapabilities, { type: 'reports', id: report.id });
|
|
223
|
+
export const checkAccessTokenCapabilities = (authData, neededCapabilities, accessToken, config) => checkCapabilitiesWithExplicitScope(getAccessTokenCapabilities(authData, accessToken, config), authData, neededCapabilities, { scopeLevel: 'admin', resource: { type: 'self' } });
|
|
224
|
+
export const checkAccountCapabilities = (authData, neededCapabilities, targetAccount) => checkCapabilitiesWithExplicitScope(getAccountCapabilities(authData, targetAccount), authData, neededCapabilities, { scopeLevel: 'admin', resource: { type: 'self' } });
|
|
225
|
+
/** Capability required to create accounts when public sign-ups are closed. */
|
|
226
|
+
export const ACCOUNT_CREATE_CAPABILITY = 'core-accounts-create';
|
|
227
|
+
/**
|
|
228
|
+
* Whether the caller may register a new account/profile while sign-ups are closed.
|
|
229
|
+
* Profile tokens need the role capability; service tokens are treated as having it
|
|
230
|
+
* and are gated by write scope on profiles (same as other create operations).
|
|
231
|
+
*/
|
|
232
|
+
export const checkAccountCreateAuthorization = (authData) => {
|
|
233
|
+
const neededCapabilities = [ACCOUNT_CREATE_CAPABILITY];
|
|
234
|
+
const capabilities = authData.service
|
|
235
|
+
? { add: neededCapabilities, remove: [] }
|
|
236
|
+
: getRoleCapabilities(authData.profile);
|
|
237
|
+
return checkCapabilitiesWithCalculatedScope(capabilities, authData, neededCapabilities, { type: 'profiles', id: '*' });
|
|
238
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const hexToRgb = (hex) => {
|
|
2
|
+
// Remove the hash at the start if it's there
|
|
3
|
+
hex = hex.replace(/^#/, '');
|
|
4
|
+
// Expand 3-character hex to 6-character
|
|
5
|
+
if (hex.length === 3) {
|
|
6
|
+
hex = hex
|
|
7
|
+
.split('')
|
|
8
|
+
.map((char) => char + char)
|
|
9
|
+
.join('');
|
|
10
|
+
}
|
|
11
|
+
// Parse the r, g, b values
|
|
12
|
+
const bigint = parseInt(hex, 16);
|
|
13
|
+
const r = (bigint >> 16) & 255;
|
|
14
|
+
const g = (bigint >> 8) & 255;
|
|
15
|
+
const b = bigint & 255;
|
|
16
|
+
return { r, g, b };
|
|
17
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const escapeCsvCell: (value: string | number | boolean | null | undefined) => string;
|
|
2
|
+
export declare const toCsvRow: (values: Array<string | number | null | undefined | boolean>) => string;
|
|
3
|
+
export declare const formatMemberExportCustomFields: (fields: ReadonlyArray<{
|
|
4
|
+
name: string;
|
|
5
|
+
value: string;
|
|
6
|
+
}> | undefined) => string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const escapeCsvCell = (value) => {
|
|
2
|
+
if (value == null) {
|
|
3
|
+
return '';
|
|
4
|
+
}
|
|
5
|
+
const str = String(value);
|
|
6
|
+
if (/[",\n\r]/.test(str)) {
|
|
7
|
+
return `"${str.replace(/"/g, '""')}"`;
|
|
8
|
+
}
|
|
9
|
+
return str;
|
|
10
|
+
};
|
|
11
|
+
export const toCsvRow = (values) => values.map(escapeCsvCell).join(',');
|
|
12
|
+
export const formatMemberExportCustomFields = (fields) => {
|
|
13
|
+
if (!(fields === null || fields === void 0 ? void 0 : fields.length)) {
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
return JSON.stringify(fields.map(({ name, value }) => ({ name, value })));
|
|
17
|
+
};
|