@openpeepshq/common 0.5.0 → 0.5.12

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;
@@ -314,6 +322,18 @@ export declare const participantIds: (post: Pick<PublicPost, "audience" | "profi
314
322
  export declare const isOneToOneDm: (post: Pick<PublicPost, "visibility" | "audience" | "profile"> & {
315
323
  creatorId?: string;
316
324
  }) => boolean;
325
+ /**
326
+ * A direct-message leave is the reply that leaveConversation posts: it
327
+ * replies to the thread's newest post and leaves the leaver out of the
328
+ * audience, so the thread stops reaching them. Replies created through
329
+ * the conversations endpoint always keep their author in the audience
330
+ * (the endpoint rejects posters who are no longer in the newest post's
331
+ * audience), so a reply whose audience lacks its author is only ever a
332
+ * leave — while a root post whose author is missing from the audience is
333
+ * just a message. Deliberately marker-free: leave posts must stay plain
334
+ * notes so they remain valid ActivityPub direct messages.
335
+ */
336
+ export declare const isConversationLeavePost: (post: Pick<PublicPost, "inReplyToId" | "audience" | "profile">) => boolean;
317
337
  export declare const isOneToOneWithBlocked: (post: Pick<PublicPost, "visibility" | "audience" | "profile"> & {
318
338
  creatorId?: string;
319
339
  }, viewer: {
@@ -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
@@ -509,6 +521,23 @@ export const participantIds = (post) => {
509
521
  };
510
522
  /** Direct message between exactly two participants. */
511
523
  export const isOneToOneDm = (post) => post.visibility === 'direct' && participantIds(post).size === 2;
524
+ /**
525
+ * A direct-message leave is the reply that leaveConversation posts: it
526
+ * replies to the thread's newest post and leaves the leaver out of the
527
+ * audience, so the thread stops reaching them. Replies created through
528
+ * the conversations endpoint always keep their author in the audience
529
+ * (the endpoint rejects posters who are no longer in the newest post's
530
+ * audience), so a reply whose audience lacks its author is only ever a
531
+ * leave — while a root post whose author is missing from the audience is
532
+ * just a message. Deliberately marker-free: leave posts must stay plain
533
+ * notes so they remain valid ActivityPub direct messages.
534
+ */
535
+ export const isConversationLeavePost = (post) => {
536
+ const authorId = post.profile?.id;
537
+ if (!post.inReplyToId || !authorId)
538
+ return false;
539
+ return !post.audience?.some((p) => p.id === authorId);
540
+ };
512
541
  export const isOneToOneWithBlocked = (post, viewer, blockedIds) => {
513
542
  if (!viewer || post.visibility !== 'direct')
514
543
  return false;
@@ -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.12",
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;
@@ -827,6 +855,25 @@ export const isOneToOneDm = (
827
855
  },
828
856
  ): boolean => post.visibility === 'direct' && participantIds(post).size === 2;
829
857
 
858
+ /**
859
+ * A direct-message leave is the reply that leaveConversation posts: it
860
+ * replies to the thread's newest post and leaves the leaver out of the
861
+ * audience, so the thread stops reaching them. Replies created through
862
+ * the conversations endpoint always keep their author in the audience
863
+ * (the endpoint rejects posters who are no longer in the newest post's
864
+ * audience), so a reply whose audience lacks its author is only ever a
865
+ * leave — while a root post whose author is missing from the audience is
866
+ * just a message. Deliberately marker-free: leave posts must stay plain
867
+ * notes so they remain valid ActivityPub direct messages.
868
+ */
869
+ export const isConversationLeavePost = (
870
+ post: Pick<PublicPost, 'inReplyToId' | 'audience' | 'profile'>,
871
+ ): boolean => {
872
+ const authorId = post.profile?.id;
873
+ if (!post.inReplyToId || !authorId) return false;
874
+ return !post.audience?.some((p) => p.id === authorId);
875
+ };
876
+
830
877
  export const isOneToOneWithBlocked = (
831
878
  post: Pick<PublicPost, 'visibility' | 'audience' | 'profile'> & {
832
879
  creatorId?: string;
@@ -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 &&