@openpeepshq/common 0.5.0 → 0.5.11

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.
@@ -284,6 +284,14 @@ export declare const canCreatePost: (authData: AuthorizationData, type: PostType
284
284
  export declare const canCreatePostTypeInAnyGroup: (authData: AuthorizationData, type: PostType) => boolean;
285
285
  export declare const canCreatePostTypeWithVisibility: (authData: AuthorizationData, type: PostType, visibility: VisibilityType) => boolean;
286
286
  export declare const canCreatePostType: (authData: AuthorizationData, type: PostType) => boolean;
287
+ /**
288
+ * Scope-free variant of {@link canCreatePostType}: may this profile create a
289
+ * post of `type` in any of `visibilities`, judging capabilities only. Token
290
+ * scopes load after the first paint in the clients, so gating UI on them would
291
+ * flash a denial for allowed users; the create endpoints still enforce
292
+ * scopes.
293
+ */
294
+ export declare const canCreatePostTypeIgnoringScopes: (authData: AuthorizationData, type: PostType, visibilities?: readonly VisibilityType[]) => boolean;
287
295
  export type PostActionAvailability = {
288
296
  canReply: boolean;
289
297
  canRepost: boolean;
@@ -2,7 +2,7 @@ import { POLL_OPTION_MAX_LENGTH, visibilityTypeValues, } from '../types/index.js
2
2
  import { canModerateJam } from './jamHelpers.js';
3
3
  import { defaultRsvpRecurrenceId, effectiveEventTimes, isRecurringEvent, listRsvpOccurrences, normalizeRecurrenceId, sameRecurrenceId, } from './eventRecurrence.js';
4
4
  import { formatEventWhen } from './eventTime.js';
5
- import { checkGroupCapabilities, checkPostCapabilities, checkRoleCapabilities, } from './capabilitiesHelpers.js';
5
+ import { checkCapabilities, checkGroupCapabilities, checkPostCapabilities, checkRoleCapabilities, getGroupCapabilities, } from './capabilitiesHelpers.js';
6
6
  import { scopeMatches } from './scopeHelpers.js';
7
7
  import { countBy, dateSorter, groupBy, transformValues } from './utils.js';
8
8
  import { truncateText } from './text.js';
@@ -460,6 +460,18 @@ export const canCreatePostTypeWithVisibility = (authData, type, visibility) => v
460
460
  requiredScope: { scopeLevel: 'write', resource: { type: 'posts' } },
461
461
  });
462
462
  export const canCreatePostType = (authData, type) => visibilityTypeValues.some((visibility) => canCreatePostTypeWithVisibility(authData, type, visibility));
463
+ /**
464
+ * Scope-free variant of {@link canCreatePostType}: may this profile create a
465
+ * post of `type` in any of `visibilities`, judging capabilities only. Token
466
+ * scopes load after the first paint in the clients, so gating UI on them would
467
+ * flash a denial for allowed users; the create endpoints still enforce
468
+ * scopes.
469
+ */
470
+ export const canCreatePostTypeIgnoringScopes = (authData, type, visibilities = visibilityTypeValues) => visibilities.some((visibility) => visibility === 'group'
471
+ ? !!authData.profile?.memberships?.some((m) => checkCapabilities([`core-posts-create-${type}`], getGroupCapabilities(authData, m.group)).success)
472
+ : checkRoleCapabilities(authData.profile?.roles, [
473
+ `core-posts-create-${type}-${visibility}`,
474
+ ]).success);
463
475
  /**
464
476
  * Whether Reply / Repost / Like should look live. Non-members of a group
465
477
  * post stay fully disabled (existing feed rule). Each action also follows
@@ -97,13 +97,17 @@ export const matchesQuery = (profile, query, queryOptions = { handle: true, disp
97
97
  containsIgnoreCase(profile.displayName, query))));
98
98
  export const inviteLinkMatchesQuery = (inviteLink, query, queryOptions = { handle: true, displayName: true, slug: true, group: true }) => {
99
99
  const profile = inviteLink.profile;
100
+ const redemptionMatches = inviteLink.redemptions.some((r) => matchesQuery(r, query, queryOptions));
100
101
  if (query.startsWith('@')) {
101
- return !!(profile?.handle && containsIgnoreCase(profile.handle, query.substring(1)));
102
+ return !!((profile?.handle &&
103
+ containsIgnoreCase(profile.handle, query.substring(1))) ||
104
+ redemptionMatches);
102
105
  }
103
106
  const groupMatches = queryOptions.group &&
104
107
  inviteLink.groups.some((group) => containsIgnoreCase(group.handle, query) ||
105
108
  (group.displayName && containsIgnoreCase(group.displayName, query)));
106
109
  return !!(groupMatches ||
110
+ redemptionMatches ||
107
111
  (profile &&
108
112
  ((queryOptions.handle &&
109
113
  profile.handle &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/common",
3
- "version": "0.5.0",
3
+ "version": "0.5.11",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -29,9 +29,11 @@ import {
29
29
  } from './eventRecurrence';
30
30
  import { formatEventWhen } from './eventTime';
31
31
  import {
32
+ checkCapabilities,
32
33
  checkGroupCapabilities,
33
34
  checkPostCapabilities,
34
35
  checkRoleCapabilities,
36
+ getGroupCapabilities,
35
37
  } from './capabilitiesHelpers';
36
38
  import { scopeMatches } from './scopeHelpers';
37
39
  import { countBy, dateSorter, groupBy, transformValues } from './utils';
@@ -733,6 +735,32 @@ export const canCreatePostType = (
733
735
  canCreatePostTypeWithVisibility(authData, type, visibility),
734
736
  );
735
737
 
738
+ /**
739
+ * Scope-free variant of {@link canCreatePostType}: may this profile create a
740
+ * post of `type` in any of `visibilities`, judging capabilities only. Token
741
+ * scopes load after the first paint in the clients, so gating UI on them would
742
+ * flash a denial for allowed users; the create endpoints still enforce
743
+ * scopes.
744
+ */
745
+ export const canCreatePostTypeIgnoringScopes = (
746
+ authData: AuthorizationData,
747
+ type: PostType,
748
+ visibilities: readonly VisibilityType[] = visibilityTypeValues,
749
+ ) =>
750
+ visibilities.some((visibility) =>
751
+ visibility === 'group'
752
+ ? !!authData.profile?.memberships?.some(
753
+ (m) =>
754
+ checkCapabilities(
755
+ [`core-posts-create-${type}`],
756
+ getGroupCapabilities(authData, m.group as GroupWithMeta),
757
+ ).success,
758
+ )
759
+ : checkRoleCapabilities(authData.profile?.roles, [
760
+ `core-posts-create-${type}-${visibility}`,
761
+ ]).success,
762
+ );
763
+
736
764
  export type PostActionAvailability = {
737
765
  canReply: boolean;
738
766
  canRepost: boolean;
@@ -154,9 +154,14 @@ export const inviteLinkMatchesQuery = (
154
154
  queryOptions = { handle: true, displayName: true, slug: true, group: true },
155
155
  ) => {
156
156
  const profile = inviteLink.profile as PublicProfile;
157
+ const redemptionMatches = inviteLink.redemptions.some((r) =>
158
+ matchesQuery(r, query, queryOptions),
159
+ );
157
160
  if (query.startsWith('@')) {
158
161
  return !!(
159
- profile?.handle && containsIgnoreCase(profile.handle, query.substring(1))
162
+ (profile?.handle &&
163
+ containsIgnoreCase(profile.handle, query.substring(1))) ||
164
+ redemptionMatches
160
165
  );
161
166
  }
162
167
  const groupMatches =
@@ -168,6 +173,7 @@ export const inviteLinkMatchesQuery = (
168
173
  );
169
174
  return !!(
170
175
  groupMatches ||
176
+ redemptionMatches ||
171
177
  (profile &&
172
178
  ((queryOptions.handle &&
173
179
  profile.handle &&