@openpeepshq/common 0.3.2 → 0.5.0

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.
Files changed (72) hide show
  1. package/dist/lib/adminSections.d.ts +4 -0
  2. package/dist/lib/adminSections.js +5 -0
  3. package/dist/lib/attachmentHelpers.js +5 -1
  4. package/dist/lib/capabilitiesHelpers.d.ts +9 -0
  5. package/dist/lib/capabilitiesHelpers.js +9 -0
  6. package/dist/lib/csvHelpers.d.ts +2 -0
  7. package/dist/lib/csvHelpers.js +13 -0
  8. package/dist/lib/eventRecurrence.d.ts +11 -1
  9. package/dist/lib/eventRecurrence.js +23 -13
  10. package/dist/lib/eventWaitlist.d.ts +58 -0
  11. package/dist/lib/eventWaitlist.js +217 -0
  12. package/dist/lib/groupHelpers.d.ts +5 -0
  13. package/dist/lib/groupHelpers.js +16 -0
  14. package/dist/lib/index.d.ts +1 -0
  15. package/dist/lib/index.js +1 -0
  16. package/dist/lib/markdownHelpers.d.ts +12 -3
  17. package/dist/lib/markdownHelpers.js +115 -6
  18. package/dist/lib/postHelpers.d.ts +27 -5
  19. package/dist/lib/postHelpers.js +73 -11
  20. package/dist/lib/profileHelpers.d.ts +12 -2
  21. package/dist/lib/profileHelpers.js +19 -2
  22. package/dist/lib/resources.d.ts +29 -1
  23. package/dist/lib/resources.js +74 -2
  24. package/dist/lib/utils.js +11 -11
  25. package/dist/types/admin-livekit.d.ts +23 -0
  26. package/dist/types/admin-livekit.js +18 -0
  27. package/dist/types/admin-media.d.ts +41 -0
  28. package/dist/types/admin-media.js +20 -0
  29. package/dist/types/api.d.ts +19945 -3526
  30. package/dist/types/api.js +16 -10
  31. package/dist/types/capabilities.d.ts +2 -2
  32. package/dist/types/capabilities.js +3 -0
  33. package/dist/types/config.d.ts +15 -0
  34. package/dist/types/config.js +14 -2
  35. package/dist/types/db.d.ts +65 -22
  36. package/dist/types/forbiddenHandles.d.ts +28 -0
  37. package/dist/types/forbiddenHandles.js +75 -0
  38. package/dist/types/index.d.ts +2 -0
  39. package/dist/types/index.js +2 -0
  40. package/dist/types/internal.d.ts +163 -44
  41. package/dist/types/models.d.ts +46 -14
  42. package/dist/types/models.js +38 -28
  43. package/dist/types/notifications.d.ts +2 -0
  44. package/dist/types/notifications.js +4 -2
  45. package/dist/types/roleDefaults.js +109 -95
  46. package/dist/types/stats.d.ts +348 -108
  47. package/package.json +7 -6
  48. package/src/lib/adminSections.ts +5 -0
  49. package/src/lib/attachmentHelpers.ts +18 -12
  50. package/src/lib/capabilitiesHelpers.ts +15 -0
  51. package/src/lib/csvHelpers.ts +19 -3
  52. package/src/lib/eventRecurrence.ts +33 -19
  53. package/src/lib/eventWaitlist.ts +325 -0
  54. package/src/lib/groupHelpers.ts +25 -0
  55. package/src/lib/index.ts +1 -0
  56. package/src/lib/markdownHelpers.ts +121 -7
  57. package/src/lib/postHelpers.ts +102 -13
  58. package/src/lib/profileHelpers.ts +31 -2
  59. package/src/lib/resources.ts +105 -2
  60. package/src/lib/utils.ts +40 -31
  61. package/src/types/admin-livekit.ts +24 -0
  62. package/src/types/admin-media.ts +29 -0
  63. package/src/types/api.ts +20 -10
  64. package/src/types/auth.ts +2 -2
  65. package/src/types/capabilities.ts +3 -0
  66. package/src/types/config.ts +19 -10
  67. package/src/types/forbiddenHandles.ts +103 -0
  68. package/src/types/index.ts +2 -0
  69. package/src/types/models.ts +70 -33
  70. package/src/types/notifications.ts +7 -7
  71. package/src/types/roleDefaults.ts +115 -96
  72. package/src/types/stripe.ts +3 -1
@@ -24,6 +24,10 @@ export declare const adminSections: readonly [{
24
24
  readonly key: "backups";
25
25
  readonly path: "/admin/backups";
26
26
  readonly capabilities: readonly ["core-backups-read"];
27
+ }, {
28
+ readonly key: "media";
29
+ readonly path: "/admin/media";
30
+ readonly capabilities: readonly ["core-media-read"];
27
31
  }, {
28
32
  readonly key: "analytics";
29
33
  readonly path: "/admin/analytics";
@@ -30,6 +30,11 @@ export const adminSections = [
30
30
  path: '/admin/backups',
31
31
  capabilities: ['core-backups-read'],
32
32
  },
33
+ {
34
+ key: 'media',
35
+ path: '/admin/media',
36
+ capabilities: ['core-media-read'],
37
+ },
33
38
  {
34
39
  key: 'analytics',
35
40
  path: '/admin/analytics',
@@ -2,7 +2,11 @@ export const getAttachmentType = (file) => {
2
2
  if (file.name.endsWith('.mkv')) {
3
3
  return 'video';
4
4
  }
5
- if (file.name.endsWith('.pdf') || file.name.endsWith('.doc') || file.name.endsWith('.docx') || file.name.endsWith('.odt') || file.name.endsWith('.txt')) {
5
+ if (file.name.endsWith('.pdf') ||
6
+ file.name.endsWith('.doc') ||
7
+ file.name.endsWith('.docx') ||
8
+ file.name.endsWith('.odt') ||
9
+ file.name.endsWith('.txt')) {
6
10
  return 'document';
7
11
  }
8
12
  const mimeTypeFirstPart = file.type.split('/')[0];
@@ -13,6 +13,15 @@ export declare const checkRoleCapabilities: (roles: Role[] | undefined, neededCa
13
13
  success: boolean;
14
14
  missingCapabilities: string[];
15
15
  };
16
+ /** Instance roles gate group creation; pending and limited members lack it. */
17
+ export declare const canCreateGroup: (roles?: Role[]) => boolean;
18
+ /** Same gate as `POST /admin/posts/:postId/announce` (`core-posts-announce`). */
19
+ export declare const canAnnouncePosts: (roles?: Role[]) => boolean;
20
+ /**
21
+ * Announcements notify the whole community, so composers offer them only for
22
+ * community-wide visibility (public or local).
23
+ */
24
+ export declare const isCommunityWideVisibility: (visibility?: string) => visibility is "local" | "public";
16
25
  /** Any of these matching `roles` shows the Administration row (submenu is separate UX). */
17
26
  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"];
18
27
  export declare const hasAdminSidebarAccess: (roles?: Role[]) => boolean;
@@ -29,6 +29,15 @@ export const capabilitySetsEqual = (a, b) => {
29
29
  return [...sa].every((value) => sb.has(value));
30
30
  };
31
31
  export const checkRoleCapabilities = (roles = [], neededCapabilities) => checkCapabilities(neededCapabilities, mergeCapabilities(roles.map((r) => r.capabilities)));
32
+ /** Instance roles gate group creation; pending and limited members lack it. */
33
+ export const canCreateGroup = (roles = []) => checkRoleCapabilities(roles, ['core-groups-create']).success;
34
+ /** Same gate as `POST /admin/posts/:postId/announce` (`core-posts-announce`). */
35
+ export const canAnnouncePosts = (roles = []) => checkRoleCapabilities(roles, ['core-posts-announce']).success;
36
+ /**
37
+ * Announcements notify the whole community, so composers offer them only for
38
+ * community-wide visibility (public or local).
39
+ */
40
+ export const isCommunityWideVisibility = (visibility) => visibility === 'public' || visibility === 'local';
32
41
  /** Any of these matching `roles` shows the Administration row (submenu is separate UX). */
33
42
  export const ADMIN_SIDEBAR_MENU_CAPABILITIES = [
34
43
  'admin',
@@ -1,5 +1,7 @@
1
1
  export declare const escapeCsvCell: (value: string | number | boolean | null | undefined) => string;
2
2
  export declare const toCsvRow: (values: Array<string | number | null | undefined | boolean>) => string;
3
+ /** Filesystem-safe `<event name>-<export time>-rsvps.csv`. */
4
+ export declare const rsvpExportFilename: (eventName: string | undefined, exportedAt: Date) => string;
3
5
  export declare const formatMemberExportCustomFields: (fields: ReadonlyArray<{
4
6
  name: string;
5
7
  value: string;
@@ -9,6 +9,19 @@ export const escapeCsvCell = (value) => {
9
9
  return str;
10
10
  };
11
11
  export const toCsvRow = (values) => values.map(escapeCsvCell).join(',');
12
+ /** Filesystem-safe `<event name>-<export time>-rsvps.csv`. */
13
+ export const rsvpExportFilename = (eventName, exportedAt) => {
14
+ const name = (eventName ?? '')
15
+ .normalize('NFKC')
16
+ .replace(/[^\p{L}\p{N}]+/gu, '-')
17
+ .replace(/^-+|-+$/g, '')
18
+ .slice(0, 80) || 'event';
19
+ const stamp = exportedAt
20
+ .toISOString()
21
+ .replace(/\.\d{3}Z$/, 'Z')
22
+ .replace(/:/g, '-');
23
+ return `${name}-${stamp}-rsvps.csv`;
24
+ };
12
25
  export const formatMemberExportCustomFields = (fields) => {
13
26
  if (!fields?.length) {
14
27
  return '';
@@ -21,8 +21,9 @@ export declare const expandEventOccurrences: (event: Event, window: {
21
21
  to: Date;
22
22
  }) => ExpandedOccurrence[];
23
23
  export declare const occurrencesForIndex: (event: Event, now?: Date) => ExpandedOccurrence[];
24
- export declare const listRsvpOccurrences: (event: Event, now?: Date) => ExpandedOccurrence[];
25
24
  export declare const occurrenceHasEnded: (start: string, end?: string, now?: Date) => boolean;
25
+ /** Occurrences that have not ended, including one that is already underway. */
26
+ export declare const listRsvpOccurrences: (event: Event, now?: Date) => ExpandedOccurrence[];
26
27
  export type EventRsvpOpenOptions = {
27
28
  recurrenceId?: string;
28
29
  lockToOccurrence?: boolean;
@@ -40,6 +41,15 @@ export type RsvpTimeTarget = {
40
41
  */
41
42
  export declare const rsvpTargetsPastEvent: (event: Event, target?: RsvpTimeTarget, now?: Date) => boolean;
42
43
  export declare const defaultRsvpRecurrenceId: (event: Event, queryId?: string, now?: Date) => string | undefined;
44
+ /**
45
+ * Occurrence a jam-start notification should show. The id the jam was opened
46
+ * with wins; otherwise the occurrence still open at `at`. A series must not
47
+ * fall back to its first date.
48
+ */
49
+ export declare const occurrenceForJamStart: (event: Event, options?: {
50
+ recurrenceId?: string;
51
+ at?: Date;
52
+ }) => ExpandedOccurrence | undefined;
43
53
  export declare const previewUpcomingOccurrences: (event: Event, count?: number, now?: Date) => ExpandedOccurrence[];
44
54
  export declare const effectiveEventTimes: (event: Event, recurrenceId?: string) => {
45
55
  start: string;
@@ -9,15 +9,6 @@ const WEEKDAY_ORDER = [
9
9
  'SU',
10
10
  ];
11
11
  export const OCCURRENCE_HORIZON_MS = 365 * 24 * 60 * 60 * 1000;
12
- const WEEKDAY_INDEX = {
13
- SU: 0,
14
- MO: 1,
15
- TU: 2,
16
- WE: 3,
17
- TH: 4,
18
- FR: 5,
19
- SA: 6,
20
- };
21
12
  const INDEX_WEEKDAY = [
22
13
  'SU',
23
14
  'MO',
@@ -174,10 +165,6 @@ export const occurrencesForIndex = (event, now = new Date()) => {
174
165
  to: new Date(now.getTime() + OCCURRENCE_HORIZON_MS),
175
166
  });
176
167
  };
177
- export const listRsvpOccurrences = (event, now = new Date()) => expandEventOccurrences(event, {
178
- from: now,
179
- to: new Date(now.getTime() + OCCURRENCE_HORIZON_MS),
180
- }).filter((occurrence) => !occurrence.cancelled);
181
168
  /** Matches the agenda: a missing end is one hour after start, not open forever. */
182
169
  const IMPLIED_EVENT_DURATION_MS = 60 * 60 * 1000;
183
170
  export const occurrenceHasEnded = (start, end, now = new Date()) => {
@@ -190,6 +177,12 @@ export const occurrenceHasEnded = (start, end, now = new Date()) => {
190
177
  : startMs + IMPLIED_EVENT_DURATION_MS;
191
178
  return endMs < now.getTime();
192
179
  };
180
+ /** Occurrences that have not ended, including one that is already underway. */
181
+ export const listRsvpOccurrences = (event, now = new Date()) => expandEventOccurrences(event, {
182
+ from: new Date(now.getTime() - OCCURRENCE_HORIZON_MS),
183
+ to: new Date(now.getTime() + OCCURRENCE_HORIZON_MS),
184
+ }).filter((occurrence) => !occurrence.cancelled &&
185
+ !occurrenceHasEnded(occurrence.start, occurrence.end, now));
193
186
  const findExpandedOccurrence = (event, recurrenceId) => {
194
187
  const anchor = new Date(recurrenceId).getTime();
195
188
  if (!Number.isFinite(anchor))
@@ -246,6 +239,23 @@ export const defaultRsvpRecurrenceId = (event, queryId, now = new Date()) => {
246
239
  return normalizeRecurrenceId(queryId);
247
240
  return listRsvpOccurrences(event, now)[0]?.recurrenceId;
248
241
  };
242
+ /**
243
+ * Occurrence a jam-start notification should show. The id the jam was opened
244
+ * with wins; otherwise the occurrence still open at `at`. A series must not
245
+ * fall back to its first date.
246
+ */
247
+ export const occurrenceForJamStart = (event, options) => {
248
+ if (!event.recurrence)
249
+ return undefined;
250
+ const at = options?.at ?? new Date();
251
+ const requested = options?.recurrenceId?.trim();
252
+ if (requested) {
253
+ const match = findExpandedOccurrence(event, requested);
254
+ if (match && !match.cancelled)
255
+ return match;
256
+ }
257
+ return listRsvpOccurrences(event, at)[0];
258
+ };
249
259
  export const previewUpcomingOccurrences = (event, count = 3, now = new Date()) => listRsvpOccurrences(event, now).slice(0, count);
250
260
  export const effectiveEventTimes = (event, recurrenceId) => {
251
261
  if (!recurrenceId) {
@@ -0,0 +1,58 @@
1
+ import type { Event, PublicPost, PublicRsvp, RsvpResponse } from '../types';
2
+ export type WaitlistMember = {
3
+ profileId: string;
4
+ profileType: PublicRsvp['profile']['type'];
5
+ joinedAt: string;
6
+ offerExpiresAt?: string;
7
+ };
8
+ export type WaitlistEntryWrite = {
9
+ profileId: string;
10
+ recurrenceId?: string;
11
+ waitlistJoinedAt: string;
12
+ offerExpiresAt?: string;
13
+ };
14
+ export type WaitlistSyncPlan = {
15
+ /** Expired offers move to the back of the line. */
16
+ requeues: WaitlistEntryWrite[];
17
+ /** Offers withdrawn because the seat was taken. Position is kept. */
18
+ withdraws: WaitlistEntryWrite[];
19
+ offers: WaitlistEntryWrite[];
20
+ clearOffers: {
21
+ profileId: string;
22
+ recurrenceId?: string;
23
+ }[];
24
+ };
25
+ export type CapacityRsvpDecision = {
26
+ ok: true;
27
+ occurrenceIds?: string[];
28
+ } | {
29
+ ok: false;
30
+ errorKey: string;
31
+ };
32
+ /**
33
+ * Waitlist state is a local RSVP entry, not an ActivityPub activity. Remote
34
+ * actors are not offered seats: the claim link and response window use local
35
+ * in-app, email, and push delivery.
36
+ */
37
+ export declare const orderedWaitlist: (post: PublicPost, recurrenceId?: string) => WaitlistMember[];
38
+ export declare const waitlistOfferWindowMs: (event: Event) => number;
39
+ export declare const offerStillOpen: (rsvp: Pick<PublicRsvp, "offerExpiresAt"> | undefined, now?: number) => boolean;
40
+ /** Seats a member who is not already attending can still take. */
41
+ export declare const canClaimYesRsvp: (post: PublicPost, profileId: string, recurrenceId?: string, now?: number) => boolean;
42
+ export declare const canJoinWaitlist: (post: PublicPost, profileId: string, recurrenceId?: string, now?: number) => boolean;
43
+ export declare const seriesHasClaimableOccurrence: (post: PublicPost, profileId: string, now?: Date) => boolean;
44
+ export declare const seriesClaimBlocked: (post: PublicPost, profileId: string, now?: Date) => boolean;
45
+ /** Public seat count. Seats held for the waitlist are not available. */
46
+ export declare const availableSpots: (post: PublicPost, recurrenceId?: string) => number | undefined;
47
+ export declare const waitlistClaimPath: (postId: string, recurrenceId?: string) => string;
48
+ export type WaitlistOfferNotice = {
49
+ offerExpiresAt: string;
50
+ recurrenceId?: string;
51
+ };
52
+ export declare const isWaitlistOfferNotice: (data: unknown) => data is WaitlistOfferNotice;
53
+ /**
54
+ * Ordered offer: the earliest local member without an open offer gets the
55
+ * seat. A missed window moves that member behind everyone still waiting.
56
+ */
57
+ export declare const planWaitlistSync: (post: PublicPost, recurrenceId: string | undefined, now?: number) => WaitlistSyncPlan;
58
+ export declare const capacityRsvpDecision: (post: PublicPost, profileId: string, response: RsvpResponse, occurrenceIds: string[] | undefined, now?: number) => CapacityRsvpDecision;
@@ -0,0 +1,217 @@
1
+ import { listRsvpOccurrences } from './eventRecurrence.js';
2
+ import { calculateEffectiveRsvps, countYesRsvps, getEffectiveRsvp, normalizeWaitlistOfferMinutes, DEFAULT_WAITLIST_OFFER_MINUTES, } from './postHelpers.js';
3
+ const offerOpen = (offerExpiresAt, now) => !!offerExpiresAt && Date.parse(offerExpiresAt) > now;
4
+ const byJoinedAt = (a, b) => Date.parse(a.joinedAt) - Date.parse(b.joinedAt) ||
5
+ a.profileId.localeCompare(b.profileId);
6
+ const eventOf = (post) => post.data?.type === 'event' ? post.data : undefined;
7
+ /**
8
+ * Waitlist state is a local RSVP entry, not an ActivityPub activity. Remote
9
+ * actors are not offered seats: the claim link and response window use local
10
+ * in-app, email, and push delivery.
11
+ */
12
+ export const orderedWaitlist = (post, recurrenceId) => calculateEffectiveRsvps(post, recurrenceId)
13
+ .filter((rsvp) => rsvp.response === 'waitlist')
14
+ .map((rsvp) => ({
15
+ profileId: rsvp.profile.id,
16
+ profileType: rsvp.profile.type,
17
+ joinedAt: rsvp.waitlistJoinedAt ?? rsvp.createdAt,
18
+ offerExpiresAt: rsvp.offerExpiresAt,
19
+ }))
20
+ .sort(byJoinedAt);
21
+ export const waitlistOfferWindowMs = (event) => (normalizeWaitlistOfferMinutes(event.waitlistOfferMinutes) ??
22
+ DEFAULT_WAITLIST_OFFER_MINUTES) * 60_000;
23
+ export const offerStillOpen = (rsvp, now = Date.now()) => offerOpen(rsvp?.offerExpiresAt, now);
24
+ /** Seats a member who is not already attending can still take. */
25
+ export const canClaimYesRsvp = (post, profileId, recurrenceId, now = Date.now()) => {
26
+ const event = eventOf(post);
27
+ if (!event?.maxAttendees)
28
+ return true;
29
+ const current = getEffectiveRsvp(post, profileId, recurrenceId);
30
+ if (current?.response === 'yes')
31
+ return true;
32
+ if (countYesRsvps(post, recurrenceId) >= event.maxAttendees)
33
+ return false;
34
+ const waiting = orderedWaitlist(post, recurrenceId);
35
+ const mine = waiting.find((member) => member.profileId === profileId);
36
+ if (mine && offerOpen(mine.offerExpiresAt, now))
37
+ return true;
38
+ return !waiting.some((member) => member.profileType !== 'federated');
39
+ };
40
+ export const canJoinWaitlist = (post, profileId, recurrenceId, now = Date.now()) => {
41
+ const event = eventOf(post);
42
+ if (!event?.maxAttendees)
43
+ return false;
44
+ const current = getEffectiveRsvp(post, profileId, recurrenceId);
45
+ if (current?.response === 'yes' ||
46
+ current?.response === 'removed' ||
47
+ current?.response === 'waitlist' ||
48
+ current?.response === 'tentative') {
49
+ return false;
50
+ }
51
+ return !canClaimYesRsvp(post, profileId, recurrenceId, now);
52
+ };
53
+ export const seriesHasClaimableOccurrence = (post, profileId, now = new Date()) => {
54
+ const event = eventOf(post);
55
+ if (!event)
56
+ return false;
57
+ if (!event.recurrence) {
58
+ return canClaimYesRsvp(post, profileId, undefined, now.getTime());
59
+ }
60
+ const occurrences = listRsvpOccurrences(event, now);
61
+ if (!occurrences.length) {
62
+ return canClaimYesRsvp(post, profileId, undefined, now.getTime());
63
+ }
64
+ return occurrences.some((occurrence) => canClaimYesRsvp(post, profileId, occurrence.recurrenceId, now.getTime()));
65
+ };
66
+ export const seriesClaimBlocked = (post, profileId, now = new Date()) => {
67
+ const event = eventOf(post);
68
+ if (!event?.maxAttendees)
69
+ return false;
70
+ if (!event.recurrence) {
71
+ return !canClaimYesRsvp(post, profileId, undefined, now.getTime());
72
+ }
73
+ const occurrences = listRsvpOccurrences(event, now);
74
+ if (!occurrences.length) {
75
+ return !canClaimYesRsvp(post, profileId, undefined, now.getTime());
76
+ }
77
+ return occurrences.some((occurrence) => !canClaimYesRsvp(post, profileId, occurrence.recurrenceId, now.getTime()));
78
+ };
79
+ /** Public seat count. Seats held for the waitlist are not available. */
80
+ export const availableSpots = (post, recurrenceId) => {
81
+ const event = eventOf(post);
82
+ if (!event?.maxAttendees)
83
+ return undefined;
84
+ if (event.recurrence && recurrenceId === undefined)
85
+ return undefined;
86
+ const open = event.maxAttendees - countYesRsvps(post, recurrenceId);
87
+ if (open <= 0)
88
+ return 0;
89
+ const localWaiting = orderedWaitlist(post, recurrenceId).some((member) => member.profileType !== 'federated');
90
+ if (localWaiting)
91
+ return 0;
92
+ return open;
93
+ };
94
+ export const waitlistClaimPath = (postId, recurrenceId) => {
95
+ const query = new URLSearchParams({ claim: '1' });
96
+ if (recurrenceId)
97
+ query.set('recurrenceId', recurrenceId);
98
+ return `/posts/${postId}?${query.toString()}`;
99
+ };
100
+ export const isWaitlistOfferNotice = (data) => {
101
+ if (!data || typeof data !== 'object')
102
+ return false;
103
+ const notice = data;
104
+ return typeof notice.offerExpiresAt === 'string';
105
+ };
106
+ const emptyPlan = () => ({
107
+ requeues: [],
108
+ withdraws: [],
109
+ offers: [],
110
+ clearOffers: [],
111
+ });
112
+ /**
113
+ * Ordered offer: the earliest local member without an open offer gets the
114
+ * seat. A missed window moves that member behind everyone still waiting.
115
+ */
116
+ export const planWaitlistSync = (post, recurrenceId, now = Date.now()) => {
117
+ const event = eventOf(post);
118
+ if (!event?.maxAttendees)
119
+ return emptyPlan();
120
+ const members = orderedWaitlist(post, recurrenceId).map((member) => ({
121
+ ...member,
122
+ }));
123
+ const plan = emptyPlan();
124
+ const joinedNow = new Date(now).toISOString();
125
+ for (const member of members) {
126
+ if (!member.offerExpiresAt || offerOpen(member.offerExpiresAt, now)) {
127
+ continue;
128
+ }
129
+ plan.clearOffers.push({ profileId: member.profileId, recurrenceId });
130
+ plan.requeues.push({
131
+ profileId: member.profileId,
132
+ recurrenceId,
133
+ waitlistJoinedAt: joinedNow,
134
+ });
135
+ member.joinedAt = joinedNow;
136
+ member.offerExpiresAt = undefined;
137
+ }
138
+ members.sort(byJoinedAt);
139
+ const spots = Math.max(0, event.maxAttendees - countYesRsvps(post, recurrenceId));
140
+ const active = members
141
+ .filter((member) => offerOpen(member.offerExpiresAt, now))
142
+ .sort((a, b) => Date.parse(a.offerExpiresAt ?? '') - Date.parse(b.offerExpiresAt ?? ''));
143
+ for (const member of active.slice(spots)) {
144
+ plan.clearOffers.push({ profileId: member.profileId, recurrenceId });
145
+ plan.withdraws.push({
146
+ profileId: member.profileId,
147
+ recurrenceId,
148
+ waitlistJoinedAt: member.joinedAt,
149
+ });
150
+ member.offerExpiresAt = undefined;
151
+ }
152
+ const openings = spots -
153
+ members.filter((member) => offerOpen(member.offerExpiresAt, now)).length;
154
+ if (openings <= 0)
155
+ return plan;
156
+ const expires = new Date(now + waitlistOfferWindowMs(event)).toISOString();
157
+ const offerable = members.filter((member) => member.profileType !== 'federated' &&
158
+ !offerOpen(member.offerExpiresAt, now));
159
+ for (const member of offerable.slice(0, openings)) {
160
+ plan.offers.push({
161
+ profileId: member.profileId,
162
+ recurrenceId,
163
+ waitlistJoinedAt: member.joinedAt,
164
+ offerExpiresAt: expires,
165
+ });
166
+ }
167
+ return plan;
168
+ };
169
+ export const capacityRsvpDecision = (post, profileId, response, occurrenceIds, now = Date.now()) => {
170
+ const event = eventOf(post);
171
+ if (!event?.maxAttendees) {
172
+ return response === 'waitlist'
173
+ ? { ok: false, errorKey: 'error.eventWaitlistNotOpen' }
174
+ : { ok: true, occurrenceIds };
175
+ }
176
+ if (response === 'tentative') {
177
+ return { ok: false, errorKey: 'error.eventTentativeNotAllowed' };
178
+ }
179
+ if (response === 'waitlist') {
180
+ if (event.recurrence && !occurrenceIds?.length) {
181
+ return { ok: false, errorKey: 'error.eventWaitlistNeedsDate' };
182
+ }
183
+ const ids = occurrenceIds ?? [undefined];
184
+ const blocked = ids.some((id) => {
185
+ const current = getEffectiveRsvp(post, profileId, id);
186
+ if (current?.response === 'waitlist')
187
+ return false;
188
+ return !canJoinWaitlist(post, profileId, id, now);
189
+ });
190
+ if (blocked) {
191
+ return { ok: false, errorKey: 'error.eventWaitlistNotOpen' };
192
+ }
193
+ const joinable = ids.filter((id) => getEffectiveRsvp(post, profileId, id)?.response !== 'waitlist');
194
+ if (!joinable.length)
195
+ return { ok: true, occurrenceIds: [] };
196
+ if (joinable.length === 1 && joinable[0] === undefined) {
197
+ return { ok: true, occurrenceIds: undefined };
198
+ }
199
+ return {
200
+ ok: true,
201
+ occurrenceIds: joinable.filter((id) => !!id),
202
+ };
203
+ }
204
+ if (response === 'yes') {
205
+ if (!occurrenceIds?.length && event.recurrence) {
206
+ if (seriesClaimBlocked(post, profileId, new Date(now))) {
207
+ return { ok: false, errorKey: 'error.eventAtCapacity' };
208
+ }
209
+ return { ok: true, occurrenceIds };
210
+ }
211
+ const ids = occurrenceIds ?? [undefined];
212
+ if (ids.some((id) => !canClaimYesRsvp(post, profileId, id, now))) {
213
+ return { ok: false, errorKey: 'error.eventAtCapacity' };
214
+ }
215
+ }
216
+ return { ok: true, occurrenceIds };
217
+ };
@@ -5,6 +5,11 @@ type CapabilitySet = {
5
5
  remove?: string[];
6
6
  };
7
7
  export declare const groupName: (group?: GroupData) => string;
8
+ export type GroupSection = 'posts' | 'events' | 'resources' | 'description';
9
+ /** `#events` selects that tab. Missing or unknown hashes stay on posts. */
10
+ export declare const groupSectionFromHash: (hash: string) => GroupSection;
11
+ /** Shareable group path. Posts is the canonical URL, without a hash. */
12
+ export declare const groupSectionPath: (handle: string, section?: GroupSection) => string;
8
13
  export declare const matchesGroupQuery: (group: GroupData, query: string, queryOptions?: {
9
14
  handle: boolean;
10
15
  displayName: boolean;
@@ -13,6 +13,22 @@ const adminCapabilities = {
13
13
  };
14
14
  const containsIgnoreCase = (candidate, query) => candidate.toLowerCase().indexOf(query.toLowerCase()) > -1;
15
15
  export const groupName = (group) => group?.displayName || group?.handle || '';
16
+ /** `#events` selects that tab. Missing or unknown hashes stay on posts. */
17
+ export const groupSectionFromHash = (hash) => {
18
+ const name = hash.replace(/^#/, '').split(/[/?&]/)[0];
19
+ if (name === 'events' ||
20
+ name === 'resources' ||
21
+ name === 'description' ||
22
+ name === 'posts') {
23
+ return name;
24
+ }
25
+ return 'posts';
26
+ };
27
+ /** Shareable group path. Posts is the canonical URL, without a hash. */
28
+ export const groupSectionPath = (handle, section = 'posts') => {
29
+ const base = `/groups/@${handle.replace(/^@/, '')}`;
30
+ return section === 'posts' ? base : `${base}#${section}`;
31
+ };
16
32
  export const matchesGroupQuery = (group, query, queryOptions = { handle: true, displayName: true, description: false }) => (queryOptions.handle && containsIgnoreCase(group.handle, query)) ||
17
33
  (queryOptions.displayName &&
18
34
  group.displayName &&
@@ -17,6 +17,7 @@ export * from './eventIcs';
17
17
  export * from './eventTime';
18
18
  export * from './timeZone';
19
19
  export * from './eventRecurrence';
20
+ export * from './eventWaitlist';
20
21
  export * from './tokenHelpers';
21
22
  export * from './jamHelpers';
22
23
  export * from './scopeHelpers';
package/dist/lib/index.js CHANGED
@@ -17,6 +17,7 @@ export * from './eventIcs.js';
17
17
  export * from './eventTime.js';
18
18
  export * from './timeZone.js';
19
19
  export * from './eventRecurrence.js';
20
+ export * from './eventWaitlist.js';
20
21
  export * from './tokenHelpers.js';
21
22
  export * from './jamHelpers.js';
22
23
  export * from './scopeHelpers.js';
@@ -28,12 +28,21 @@ export declare const preprocessMarkdownSource: (source: string, mentions?: Menti
28
28
  export declare const matchMentionHandles: (text?: string | null) => string[];
29
29
  /** Readable text for a shortened preview. Drops markdown marks, keeps words. */
30
30
  export declare const markdownPlainText: (markdown?: string | null) => string;
31
+ /**
32
+ * Removes inline images (`![alt](url)` and `![alt]`) from markdown while
33
+ * leaving all other formatting intact. Used to keep abbreviated previews to
34
+ * text+basic formatting and put the full (image-bearing) content in an overlay.
35
+ */
36
+ export declare const stripMarkdownImages: (markdown?: string | null) => string;
31
37
  export declare const firstNWords: (markdown: string | undefined, n: number) => string;
32
38
  /**
33
39
  * Truncates markdown to at most `maxBlocks` block-level regions, then to
34
- * `maxChars` characters at a word boundary. Preserves punctuation and inline
35
- * markdown formatting (links, bold, etc.). An ellipsis is appended only when
36
- * the content was actually shortened.
40
+ * `maxChars` characters at a word boundary. Protected inline constructs — links,
41
+ * images and inline code spans — are never severed: a construct that would not
42
+ * fit whole is omitted (along with what follows), so the result is always valid
43
+ * markdown. Emphasis markers and inline-code backticks left open by a word-boundary
44
+ * cut are closed, so renderers never emit raw markup leaking into copy. An
45
+ * ellipsis is appended only when the content was actually shortened.
37
46
  */
38
47
  export declare const truncateMarkdown: (markdown: string | undefined, maxBlocks: number, maxChars: number) => string;
39
48
  /**