@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.
- package/dist/lib/adminSections.d.ts +4 -0
- package/dist/lib/adminSections.js +5 -0
- package/dist/lib/attachmentHelpers.js +5 -1
- package/dist/lib/capabilitiesHelpers.d.ts +9 -0
- package/dist/lib/capabilitiesHelpers.js +9 -0
- package/dist/lib/csvHelpers.d.ts +2 -0
- package/dist/lib/csvHelpers.js +13 -0
- package/dist/lib/eventRecurrence.d.ts +11 -1
- package/dist/lib/eventRecurrence.js +23 -13
- package/dist/lib/eventWaitlist.d.ts +58 -0
- package/dist/lib/eventWaitlist.js +217 -0
- package/dist/lib/groupHelpers.d.ts +5 -0
- package/dist/lib/groupHelpers.js +16 -0
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +1 -0
- package/dist/lib/markdownHelpers.d.ts +12 -3
- package/dist/lib/markdownHelpers.js +115 -6
- package/dist/lib/postHelpers.d.ts +27 -5
- package/dist/lib/postHelpers.js +73 -11
- package/dist/lib/profileHelpers.d.ts +12 -2
- package/dist/lib/profileHelpers.js +19 -2
- package/dist/lib/resources.d.ts +29 -1
- package/dist/lib/resources.js +74 -2
- package/dist/lib/utils.js +11 -11
- package/dist/types/admin-livekit.d.ts +23 -0
- package/dist/types/admin-livekit.js +18 -0
- package/dist/types/admin-media.d.ts +41 -0
- package/dist/types/admin-media.js +20 -0
- package/dist/types/api.d.ts +19945 -3526
- package/dist/types/api.js +16 -10
- package/dist/types/capabilities.d.ts +2 -2
- package/dist/types/capabilities.js +3 -0
- package/dist/types/config.d.ts +15 -0
- package/dist/types/config.js +14 -2
- package/dist/types/db.d.ts +65 -22
- package/dist/types/forbiddenHandles.d.ts +28 -0
- package/dist/types/forbiddenHandles.js +75 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/internal.d.ts +163 -44
- package/dist/types/models.d.ts +46 -14
- package/dist/types/models.js +38 -28
- package/dist/types/notifications.d.ts +2 -0
- package/dist/types/notifications.js +4 -2
- package/dist/types/roleDefaults.js +109 -95
- package/dist/types/stats.d.ts +348 -108
- package/package.json +7 -6
- package/src/lib/adminSections.ts +5 -0
- package/src/lib/attachmentHelpers.ts +18 -12
- package/src/lib/capabilitiesHelpers.ts +15 -0
- package/src/lib/csvHelpers.ts +19 -3
- package/src/lib/eventRecurrence.ts +33 -19
- package/src/lib/eventWaitlist.ts +325 -0
- package/src/lib/groupHelpers.ts +25 -0
- package/src/lib/index.ts +1 -0
- package/src/lib/markdownHelpers.ts +121 -7
- package/src/lib/postHelpers.ts +102 -13
- package/src/lib/profileHelpers.ts +31 -2
- package/src/lib/resources.ts +105 -2
- package/src/lib/utils.ts +40 -31
- package/src/types/admin-livekit.ts +24 -0
- package/src/types/admin-media.ts +29 -0
- package/src/types/api.ts +20 -10
- package/src/types/auth.ts +2 -2
- package/src/types/capabilities.ts +3 -0
- package/src/types/config.ts +19 -10
- package/src/types/forbiddenHandles.ts +103 -0
- package/src/types/index.ts +2 -0
- package/src/types/models.ts +70 -33
- package/src/types/notifications.ts +7 -7
- package/src/types/roleDefaults.ts +115 -96
- package/src/types/stripe.ts +3 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openpeepshq/common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -52,15 +52,16 @@
|
|
|
52
52
|
"!**/*.test.ts"
|
|
53
53
|
],
|
|
54
54
|
"devDependencies": {
|
|
55
|
+
"@eslint/js": "^9.19.0",
|
|
55
56
|
"@types/node": "^24.1.12",
|
|
56
57
|
"@types/validator": "^13.15.10",
|
|
57
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
58
|
-
"@typescript-eslint/parser": "^8.
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^8.72.0",
|
|
59
|
+
"@typescript-eslint/parser": "^8.72.0",
|
|
59
60
|
"@vitest/coverage-v8": "^2.1.8",
|
|
60
61
|
"@vitest/ui": "^2.1.8",
|
|
61
62
|
"concurrently": "^9.2.1",
|
|
62
63
|
"eslint": "^10.1.0",
|
|
63
|
-
"eslint-config-prettier": "^
|
|
64
|
+
"eslint-config-prettier": "^9.1.0",
|
|
64
65
|
"i18next": "^26.0.3",
|
|
65
66
|
"livekit-client": "^2.19.2",
|
|
66
67
|
"prettier": "^3.8.1",
|
|
@@ -70,8 +71,8 @@
|
|
|
70
71
|
"vitest": "^2.1.8"
|
|
71
72
|
},
|
|
72
73
|
"scripts": {
|
|
73
|
-
"lint": "
|
|
74
|
-
"format": "prettier --write .",
|
|
74
|
+
"lint": "eslint .",
|
|
75
|
+
"format": "prettier --write --ignore-path ../../.prettierignore .",
|
|
75
76
|
"clean": "rm -rf tsconfig.tsbuildinfo dist",
|
|
76
77
|
"typecheck": "tsc --noEmit",
|
|
77
78
|
"build": "tsc && tsc-alias",
|
package/src/lib/adminSections.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
export const getAttachmentType = (file: File) => {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
if (file.name.endsWith('.mkv')) {
|
|
3
|
+
return 'video';
|
|
4
|
+
}
|
|
5
|
+
if (
|
|
6
|
+
file.name.endsWith('.pdf') ||
|
|
7
|
+
file.name.endsWith('.doc') ||
|
|
8
|
+
file.name.endsWith('.docx') ||
|
|
9
|
+
file.name.endsWith('.odt') ||
|
|
10
|
+
file.name.endsWith('.txt')
|
|
11
|
+
) {
|
|
12
|
+
return 'document';
|
|
13
|
+
}
|
|
8
14
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
+
const mimeTypeFirstPart = file.type.split('/')[0];
|
|
16
|
+
if (mimeTypeFirstPart === 'application' || mimeTypeFirstPart === 'text') {
|
|
17
|
+
return 'document';
|
|
18
|
+
}
|
|
19
|
+
return mimeTypeFirstPart;
|
|
20
|
+
};
|
|
@@ -82,6 +82,21 @@ export const checkRoleCapabilities = (
|
|
|
82
82
|
mergeCapabilities(roles.map((r) => r.capabilities)),
|
|
83
83
|
);
|
|
84
84
|
|
|
85
|
+
/** Instance roles gate group creation; pending and limited members lack it. */
|
|
86
|
+
export const canCreateGroup = (roles: Role[] = []) =>
|
|
87
|
+
checkRoleCapabilities(roles, ['core-groups-create']).success;
|
|
88
|
+
|
|
89
|
+
/** Same gate as `POST /admin/posts/:postId/announce` (`core-posts-announce`). */
|
|
90
|
+
export const canAnnouncePosts = (roles: Role[] = []) =>
|
|
91
|
+
checkRoleCapabilities(roles, ['core-posts-announce']).success;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Announcements notify the whole community, so composers offer them only for
|
|
95
|
+
* community-wide visibility (public or local).
|
|
96
|
+
*/
|
|
97
|
+
export const isCommunityWideVisibility = (visibility?: string) =>
|
|
98
|
+
visibility === 'public' || visibility === 'local';
|
|
99
|
+
|
|
85
100
|
/** Any of these matching `roles` shows the Administration row (submenu is separate UX). */
|
|
86
101
|
export const ADMIN_SIDEBAR_MENU_CAPABILITIES = [
|
|
87
102
|
'admin',
|
package/src/lib/csvHelpers.ts
CHANGED
|
@@ -18,6 +18,24 @@ export const toCsvRow = (
|
|
|
18
18
|
values: Array<string | number | null | undefined | boolean>,
|
|
19
19
|
): string => values.map(escapeCsvCell).join(',');
|
|
20
20
|
|
|
21
|
+
/** Filesystem-safe `<event name>-<export time>-rsvps.csv`. */
|
|
22
|
+
export const rsvpExportFilename = (
|
|
23
|
+
eventName: string | undefined,
|
|
24
|
+
exportedAt: Date,
|
|
25
|
+
): string => {
|
|
26
|
+
const name =
|
|
27
|
+
(eventName ?? '')
|
|
28
|
+
.normalize('NFKC')
|
|
29
|
+
.replace(/[^\p{L}\p{N}]+/gu, '-')
|
|
30
|
+
.replace(/^-+|-+$/g, '')
|
|
31
|
+
.slice(0, 80) || 'event';
|
|
32
|
+
const stamp = exportedAt
|
|
33
|
+
.toISOString()
|
|
34
|
+
.replace(/\.\d{3}Z$/, 'Z')
|
|
35
|
+
.replace(/:/g, '-');
|
|
36
|
+
return `${name}-${stamp}-rsvps.csv`;
|
|
37
|
+
};
|
|
38
|
+
|
|
21
39
|
export const formatMemberExportCustomFields = (
|
|
22
40
|
fields: ReadonlyArray<{ name: string; value: string }> | undefined,
|
|
23
41
|
): string => {
|
|
@@ -25,7 +43,5 @@ export const formatMemberExportCustomFields = (
|
|
|
25
43
|
return '';
|
|
26
44
|
}
|
|
27
45
|
|
|
28
|
-
return JSON.stringify(
|
|
29
|
-
fields.map(({ name, value }) => ({ name, value })),
|
|
30
|
-
);
|
|
46
|
+
return JSON.stringify(fields.map(({ name, value }) => ({ name, value })));
|
|
31
47
|
};
|
|
@@ -32,16 +32,6 @@ const WEEKDAY_ORDER: RecurrenceWeekday[] = [
|
|
|
32
32
|
|
|
33
33
|
export const OCCURRENCE_HORIZON_MS = 365 * 24 * 60 * 60 * 1000;
|
|
34
34
|
|
|
35
|
-
const WEEKDAY_INDEX: Record<RecurrenceWeekday, number> = {
|
|
36
|
-
SU: 0,
|
|
37
|
-
MO: 1,
|
|
38
|
-
TU: 2,
|
|
39
|
-
WE: 3,
|
|
40
|
-
TH: 4,
|
|
41
|
-
FR: 5,
|
|
42
|
-
SA: 6,
|
|
43
|
-
};
|
|
44
|
-
|
|
45
35
|
const INDEX_WEEKDAY: RecurrenceWeekday[] = [
|
|
46
36
|
'SU',
|
|
47
37
|
'MO',
|
|
@@ -236,15 +226,6 @@ export const occurrencesForIndex = (
|
|
|
236
226
|
});
|
|
237
227
|
};
|
|
238
228
|
|
|
239
|
-
export const listRsvpOccurrences = (
|
|
240
|
-
event: Event,
|
|
241
|
-
now = new Date(),
|
|
242
|
-
): ExpandedOccurrence[] =>
|
|
243
|
-
expandEventOccurrences(event, {
|
|
244
|
-
from: now,
|
|
245
|
-
to: new Date(now.getTime() + OCCURRENCE_HORIZON_MS),
|
|
246
|
-
}).filter((occurrence) => !occurrence.cancelled);
|
|
247
|
-
|
|
248
229
|
/** Matches the agenda: a missing end is one hour after start, not open forever. */
|
|
249
230
|
const IMPLIED_EVENT_DURATION_MS = 60 * 60 * 1000;
|
|
250
231
|
|
|
@@ -262,6 +243,20 @@ export const occurrenceHasEnded = (
|
|
|
262
243
|
return endMs < now.getTime();
|
|
263
244
|
};
|
|
264
245
|
|
|
246
|
+
/** Occurrences that have not ended, including one that is already underway. */
|
|
247
|
+
export const listRsvpOccurrences = (
|
|
248
|
+
event: Event,
|
|
249
|
+
now = new Date(),
|
|
250
|
+
): ExpandedOccurrence[] =>
|
|
251
|
+
expandEventOccurrences(event, {
|
|
252
|
+
from: new Date(now.getTime() - OCCURRENCE_HORIZON_MS),
|
|
253
|
+
to: new Date(now.getTime() + OCCURRENCE_HORIZON_MS),
|
|
254
|
+
}).filter(
|
|
255
|
+
(occurrence) =>
|
|
256
|
+
!occurrence.cancelled &&
|
|
257
|
+
!occurrenceHasEnded(occurrence.start, occurrence.end, now),
|
|
258
|
+
);
|
|
259
|
+
|
|
265
260
|
const findExpandedOccurrence = (
|
|
266
261
|
event: Event,
|
|
267
262
|
recurrenceId: string,
|
|
@@ -356,6 +351,25 @@ export const defaultRsvpRecurrenceId = (
|
|
|
356
351
|
return listRsvpOccurrences(event, now)[0]?.recurrenceId;
|
|
357
352
|
};
|
|
358
353
|
|
|
354
|
+
/**
|
|
355
|
+
* Occurrence a jam-start notification should show. The id the jam was opened
|
|
356
|
+
* with wins; otherwise the occurrence still open at `at`. A series must not
|
|
357
|
+
* fall back to its first date.
|
|
358
|
+
*/
|
|
359
|
+
export const occurrenceForJamStart = (
|
|
360
|
+
event: Event,
|
|
361
|
+
options?: { recurrenceId?: string; at?: Date },
|
|
362
|
+
): ExpandedOccurrence | undefined => {
|
|
363
|
+
if (!event.recurrence) return undefined;
|
|
364
|
+
const at = options?.at ?? new Date();
|
|
365
|
+
const requested = options?.recurrenceId?.trim();
|
|
366
|
+
if (requested) {
|
|
367
|
+
const match = findExpandedOccurrence(event, requested);
|
|
368
|
+
if (match && !match.cancelled) return match;
|
|
369
|
+
}
|
|
370
|
+
return listRsvpOccurrences(event, at)[0];
|
|
371
|
+
};
|
|
372
|
+
|
|
359
373
|
export const previewUpcomingOccurrences = (
|
|
360
374
|
event: Event,
|
|
361
375
|
count = 3,
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import type { Event, PublicPost, PublicRsvp, RsvpResponse } from '../types';
|
|
2
|
+
import { listRsvpOccurrences } from './eventRecurrence';
|
|
3
|
+
import {
|
|
4
|
+
calculateEffectiveRsvps,
|
|
5
|
+
countYesRsvps,
|
|
6
|
+
getEffectiveRsvp,
|
|
7
|
+
normalizeWaitlistOfferMinutes,
|
|
8
|
+
DEFAULT_WAITLIST_OFFER_MINUTES,
|
|
9
|
+
} from './postHelpers';
|
|
10
|
+
|
|
11
|
+
export type WaitlistMember = {
|
|
12
|
+
profileId: string;
|
|
13
|
+
profileType: PublicRsvp['profile']['type'];
|
|
14
|
+
joinedAt: string;
|
|
15
|
+
offerExpiresAt?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type WaitlistEntryWrite = {
|
|
19
|
+
profileId: string;
|
|
20
|
+
recurrenceId?: string;
|
|
21
|
+
waitlistJoinedAt: string;
|
|
22
|
+
offerExpiresAt?: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type WaitlistSyncPlan = {
|
|
26
|
+
/** Expired offers move to the back of the line. */
|
|
27
|
+
requeues: WaitlistEntryWrite[];
|
|
28
|
+
/** Offers withdrawn because the seat was taken. Position is kept. */
|
|
29
|
+
withdraws: WaitlistEntryWrite[];
|
|
30
|
+
offers: WaitlistEntryWrite[];
|
|
31
|
+
clearOffers: { profileId: string; recurrenceId?: string }[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type CapacityRsvpDecision =
|
|
35
|
+
| { ok: true; occurrenceIds?: string[] }
|
|
36
|
+
| { ok: false; errorKey: string };
|
|
37
|
+
|
|
38
|
+
const offerOpen = (offerExpiresAt: string | undefined, now: number) =>
|
|
39
|
+
!!offerExpiresAt && Date.parse(offerExpiresAt) > now;
|
|
40
|
+
|
|
41
|
+
const byJoinedAt = (a: WaitlistMember, b: WaitlistMember) =>
|
|
42
|
+
Date.parse(a.joinedAt) - Date.parse(b.joinedAt) ||
|
|
43
|
+
a.profileId.localeCompare(b.profileId);
|
|
44
|
+
|
|
45
|
+
const eventOf = (post: PublicPost) =>
|
|
46
|
+
post.data?.type === 'event' ? post.data : undefined;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Waitlist state is a local RSVP entry, not an ActivityPub activity. Remote
|
|
50
|
+
* actors are not offered seats: the claim link and response window use local
|
|
51
|
+
* in-app, email, and push delivery.
|
|
52
|
+
*/
|
|
53
|
+
export const orderedWaitlist = (
|
|
54
|
+
post: PublicPost,
|
|
55
|
+
recurrenceId?: string,
|
|
56
|
+
): WaitlistMember[] =>
|
|
57
|
+
calculateEffectiveRsvps(post, recurrenceId)
|
|
58
|
+
.filter((rsvp) => rsvp.response === 'waitlist')
|
|
59
|
+
.map((rsvp) => ({
|
|
60
|
+
profileId: rsvp.profile.id,
|
|
61
|
+
profileType: rsvp.profile.type,
|
|
62
|
+
joinedAt: rsvp.waitlistJoinedAt ?? rsvp.createdAt,
|
|
63
|
+
offerExpiresAt: rsvp.offerExpiresAt,
|
|
64
|
+
}))
|
|
65
|
+
.sort(byJoinedAt);
|
|
66
|
+
|
|
67
|
+
export const waitlistOfferWindowMs = (event: Event) =>
|
|
68
|
+
(normalizeWaitlistOfferMinutes(event.waitlistOfferMinutes) ??
|
|
69
|
+
DEFAULT_WAITLIST_OFFER_MINUTES) * 60_000;
|
|
70
|
+
|
|
71
|
+
export const offerStillOpen = (
|
|
72
|
+
rsvp: Pick<PublicRsvp, 'offerExpiresAt'> | undefined,
|
|
73
|
+
now = Date.now(),
|
|
74
|
+
) => offerOpen(rsvp?.offerExpiresAt, now);
|
|
75
|
+
|
|
76
|
+
/** Seats a member who is not already attending can still take. */
|
|
77
|
+
export const canClaimYesRsvp = (
|
|
78
|
+
post: PublicPost,
|
|
79
|
+
profileId: string,
|
|
80
|
+
recurrenceId?: string,
|
|
81
|
+
now = Date.now(),
|
|
82
|
+
) => {
|
|
83
|
+
const event = eventOf(post);
|
|
84
|
+
if (!event?.maxAttendees) return true;
|
|
85
|
+
const current = getEffectiveRsvp(post, profileId, recurrenceId);
|
|
86
|
+
if (current?.response === 'yes') return true;
|
|
87
|
+
if (countYesRsvps(post, recurrenceId) >= event.maxAttendees) return false;
|
|
88
|
+
const waiting = orderedWaitlist(post, recurrenceId);
|
|
89
|
+
const mine = waiting.find((member) => member.profileId === profileId);
|
|
90
|
+
if (mine && offerOpen(mine.offerExpiresAt, now)) return true;
|
|
91
|
+
return !waiting.some((member) => member.profileType !== 'federated');
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const canJoinWaitlist = (
|
|
95
|
+
post: PublicPost,
|
|
96
|
+
profileId: string,
|
|
97
|
+
recurrenceId?: string,
|
|
98
|
+
now = Date.now(),
|
|
99
|
+
) => {
|
|
100
|
+
const event = eventOf(post);
|
|
101
|
+
if (!event?.maxAttendees) return false;
|
|
102
|
+
const current = getEffectiveRsvp(post, profileId, recurrenceId);
|
|
103
|
+
if (
|
|
104
|
+
current?.response === 'yes' ||
|
|
105
|
+
current?.response === 'removed' ||
|
|
106
|
+
current?.response === 'waitlist' ||
|
|
107
|
+
current?.response === 'tentative'
|
|
108
|
+
) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
return !canClaimYesRsvp(post, profileId, recurrenceId, now);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export const seriesHasClaimableOccurrence = (
|
|
115
|
+
post: PublicPost,
|
|
116
|
+
profileId: string,
|
|
117
|
+
now = new Date(),
|
|
118
|
+
) => {
|
|
119
|
+
const event = eventOf(post);
|
|
120
|
+
if (!event) return false;
|
|
121
|
+
if (!event.recurrence) {
|
|
122
|
+
return canClaimYesRsvp(post, profileId, undefined, now.getTime());
|
|
123
|
+
}
|
|
124
|
+
const occurrences = listRsvpOccurrences(event, now);
|
|
125
|
+
if (!occurrences.length) {
|
|
126
|
+
return canClaimYesRsvp(post, profileId, undefined, now.getTime());
|
|
127
|
+
}
|
|
128
|
+
return occurrences.some((occurrence) =>
|
|
129
|
+
canClaimYesRsvp(post, profileId, occurrence.recurrenceId, now.getTime()),
|
|
130
|
+
);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export const seriesClaimBlocked = (
|
|
134
|
+
post: PublicPost,
|
|
135
|
+
profileId: string,
|
|
136
|
+
now = new Date(),
|
|
137
|
+
) => {
|
|
138
|
+
const event = eventOf(post);
|
|
139
|
+
if (!event?.maxAttendees) return false;
|
|
140
|
+
if (!event.recurrence) {
|
|
141
|
+
return !canClaimYesRsvp(post, profileId, undefined, now.getTime());
|
|
142
|
+
}
|
|
143
|
+
const occurrences = listRsvpOccurrences(event, now);
|
|
144
|
+
if (!occurrences.length) {
|
|
145
|
+
return !canClaimYesRsvp(post, profileId, undefined, now.getTime());
|
|
146
|
+
}
|
|
147
|
+
return occurrences.some(
|
|
148
|
+
(occurrence) =>
|
|
149
|
+
!canClaimYesRsvp(post, profileId, occurrence.recurrenceId, now.getTime()),
|
|
150
|
+
);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/** Public seat count. Seats held for the waitlist are not available. */
|
|
154
|
+
export const availableSpots = (
|
|
155
|
+
post: PublicPost,
|
|
156
|
+
recurrenceId?: string,
|
|
157
|
+
): number | undefined => {
|
|
158
|
+
const event = eventOf(post);
|
|
159
|
+
if (!event?.maxAttendees) return undefined;
|
|
160
|
+
if (event.recurrence && recurrenceId === undefined) return undefined;
|
|
161
|
+
const open = event.maxAttendees - countYesRsvps(post, recurrenceId);
|
|
162
|
+
if (open <= 0) return 0;
|
|
163
|
+
const localWaiting = orderedWaitlist(post, recurrenceId).some(
|
|
164
|
+
(member) => member.profileType !== 'federated',
|
|
165
|
+
);
|
|
166
|
+
if (localWaiting) return 0;
|
|
167
|
+
return open;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export const waitlistClaimPath = (postId: string, recurrenceId?: string) => {
|
|
171
|
+
const query = new URLSearchParams({ claim: '1' });
|
|
172
|
+
if (recurrenceId) query.set('recurrenceId', recurrenceId);
|
|
173
|
+
return `/posts/${postId}?${query.toString()}`;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
export type WaitlistOfferNotice = {
|
|
177
|
+
offerExpiresAt: string;
|
|
178
|
+
recurrenceId?: string;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
export const isWaitlistOfferNotice = (
|
|
182
|
+
data: unknown,
|
|
183
|
+
): data is WaitlistOfferNotice => {
|
|
184
|
+
if (!data || typeof data !== 'object') return false;
|
|
185
|
+
const notice = data as WaitlistOfferNotice;
|
|
186
|
+
return typeof notice.offerExpiresAt === 'string';
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const emptyPlan = (): WaitlistSyncPlan => ({
|
|
190
|
+
requeues: [],
|
|
191
|
+
withdraws: [],
|
|
192
|
+
offers: [],
|
|
193
|
+
clearOffers: [],
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Ordered offer: the earliest local member without an open offer gets the
|
|
198
|
+
* seat. A missed window moves that member behind everyone still waiting.
|
|
199
|
+
*/
|
|
200
|
+
export const planWaitlistSync = (
|
|
201
|
+
post: PublicPost,
|
|
202
|
+
recurrenceId: string | undefined,
|
|
203
|
+
now = Date.now(),
|
|
204
|
+
): WaitlistSyncPlan => {
|
|
205
|
+
const event = eventOf(post);
|
|
206
|
+
if (!event?.maxAttendees) return emptyPlan();
|
|
207
|
+
|
|
208
|
+
const members = orderedWaitlist(post, recurrenceId).map((member) => ({
|
|
209
|
+
...member,
|
|
210
|
+
}));
|
|
211
|
+
const plan = emptyPlan();
|
|
212
|
+
const joinedNow = new Date(now).toISOString();
|
|
213
|
+
|
|
214
|
+
for (const member of members) {
|
|
215
|
+
if (!member.offerExpiresAt || offerOpen(member.offerExpiresAt, now)) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
plan.clearOffers.push({ profileId: member.profileId, recurrenceId });
|
|
219
|
+
plan.requeues.push({
|
|
220
|
+
profileId: member.profileId,
|
|
221
|
+
recurrenceId,
|
|
222
|
+
waitlistJoinedAt: joinedNow,
|
|
223
|
+
});
|
|
224
|
+
member.joinedAt = joinedNow;
|
|
225
|
+
member.offerExpiresAt = undefined;
|
|
226
|
+
}
|
|
227
|
+
members.sort(byJoinedAt);
|
|
228
|
+
|
|
229
|
+
const spots = Math.max(
|
|
230
|
+
0,
|
|
231
|
+
event.maxAttendees - countYesRsvps(post, recurrenceId),
|
|
232
|
+
);
|
|
233
|
+
const active = members
|
|
234
|
+
.filter((member) => offerOpen(member.offerExpiresAt, now))
|
|
235
|
+
.sort(
|
|
236
|
+
(a, b) =>
|
|
237
|
+
Date.parse(a.offerExpiresAt ?? '') - Date.parse(b.offerExpiresAt ?? ''),
|
|
238
|
+
);
|
|
239
|
+
for (const member of active.slice(spots)) {
|
|
240
|
+
plan.clearOffers.push({ profileId: member.profileId, recurrenceId });
|
|
241
|
+
plan.withdraws.push({
|
|
242
|
+
profileId: member.profileId,
|
|
243
|
+
recurrenceId,
|
|
244
|
+
waitlistJoinedAt: member.joinedAt,
|
|
245
|
+
});
|
|
246
|
+
member.offerExpiresAt = undefined;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const openings =
|
|
250
|
+
spots -
|
|
251
|
+
members.filter((member) => offerOpen(member.offerExpiresAt, now)).length;
|
|
252
|
+
if (openings <= 0) return plan;
|
|
253
|
+
|
|
254
|
+
const expires = new Date(now + waitlistOfferWindowMs(event)).toISOString();
|
|
255
|
+
const offerable = members.filter(
|
|
256
|
+
(member) =>
|
|
257
|
+
member.profileType !== 'federated' &&
|
|
258
|
+
!offerOpen(member.offerExpiresAt, now),
|
|
259
|
+
);
|
|
260
|
+
for (const member of offerable.slice(0, openings)) {
|
|
261
|
+
plan.offers.push({
|
|
262
|
+
profileId: member.profileId,
|
|
263
|
+
recurrenceId,
|
|
264
|
+
waitlistJoinedAt: member.joinedAt,
|
|
265
|
+
offerExpiresAt: expires,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
return plan;
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
export const capacityRsvpDecision = (
|
|
272
|
+
post: PublicPost,
|
|
273
|
+
profileId: string,
|
|
274
|
+
response: RsvpResponse,
|
|
275
|
+
occurrenceIds: string[] | undefined,
|
|
276
|
+
now = Date.now(),
|
|
277
|
+
): CapacityRsvpDecision => {
|
|
278
|
+
const event = eventOf(post);
|
|
279
|
+
if (!event?.maxAttendees) {
|
|
280
|
+
return response === 'waitlist'
|
|
281
|
+
? { ok: false, errorKey: 'error.eventWaitlistNotOpen' }
|
|
282
|
+
: { ok: true, occurrenceIds };
|
|
283
|
+
}
|
|
284
|
+
if (response === 'tentative') {
|
|
285
|
+
return { ok: false, errorKey: 'error.eventTentativeNotAllowed' };
|
|
286
|
+
}
|
|
287
|
+
if (response === 'waitlist') {
|
|
288
|
+
if (event.recurrence && !occurrenceIds?.length) {
|
|
289
|
+
return { ok: false, errorKey: 'error.eventWaitlistNeedsDate' };
|
|
290
|
+
}
|
|
291
|
+
const ids = occurrenceIds ?? [undefined];
|
|
292
|
+
const blocked = ids.some((id) => {
|
|
293
|
+
const current = getEffectiveRsvp(post, profileId, id);
|
|
294
|
+
if (current?.response === 'waitlist') return false;
|
|
295
|
+
return !canJoinWaitlist(post, profileId, id, now);
|
|
296
|
+
});
|
|
297
|
+
if (blocked) {
|
|
298
|
+
return { ok: false, errorKey: 'error.eventWaitlistNotOpen' };
|
|
299
|
+
}
|
|
300
|
+
const joinable = ids.filter(
|
|
301
|
+
(id) => getEffectiveRsvp(post, profileId, id)?.response !== 'waitlist',
|
|
302
|
+
);
|
|
303
|
+
if (!joinable.length) return { ok: true, occurrenceIds: [] };
|
|
304
|
+
if (joinable.length === 1 && joinable[0] === undefined) {
|
|
305
|
+
return { ok: true, occurrenceIds: undefined };
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
ok: true,
|
|
309
|
+
occurrenceIds: joinable.filter((id): id is string => !!id),
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
if (response === 'yes') {
|
|
313
|
+
if (!occurrenceIds?.length && event.recurrence) {
|
|
314
|
+
if (seriesClaimBlocked(post, profileId, new Date(now))) {
|
|
315
|
+
return { ok: false, errorKey: 'error.eventAtCapacity' };
|
|
316
|
+
}
|
|
317
|
+
return { ok: true, occurrenceIds };
|
|
318
|
+
}
|
|
319
|
+
const ids = occurrenceIds ?? [undefined];
|
|
320
|
+
if (ids.some((id) => !canClaimYesRsvp(post, profileId, id, now))) {
|
|
321
|
+
return { ok: false, errorKey: 'error.eventAtCapacity' };
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return { ok: true, occurrenceIds };
|
|
325
|
+
};
|
package/src/lib/groupHelpers.ts
CHANGED
|
@@ -37,6 +37,31 @@ const containsIgnoreCase = (candidate: string, query: string) =>
|
|
|
37
37
|
export const groupName = (group?: GroupData) =>
|
|
38
38
|
group?.displayName || group?.handle || '';
|
|
39
39
|
|
|
40
|
+
export type GroupSection = 'posts' | 'events' | 'resources' | 'description';
|
|
41
|
+
|
|
42
|
+
/** `#events` selects that tab. Missing or unknown hashes stay on posts. */
|
|
43
|
+
export const groupSectionFromHash = (hash: string): GroupSection => {
|
|
44
|
+
const name = hash.replace(/^#/, '').split(/[/?&]/)[0];
|
|
45
|
+
if (
|
|
46
|
+
name === 'events' ||
|
|
47
|
+
name === 'resources' ||
|
|
48
|
+
name === 'description' ||
|
|
49
|
+
name === 'posts'
|
|
50
|
+
) {
|
|
51
|
+
return name;
|
|
52
|
+
}
|
|
53
|
+
return 'posts';
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/** Shareable group path. Posts is the canonical URL, without a hash. */
|
|
57
|
+
export const groupSectionPath = (
|
|
58
|
+
handle: string,
|
|
59
|
+
section: GroupSection = 'posts',
|
|
60
|
+
): string => {
|
|
61
|
+
const base = `/groups/@${handle.replace(/^@/, '')}`;
|
|
62
|
+
return section === 'posts' ? base : `${base}#${section}`;
|
|
63
|
+
};
|
|
64
|
+
|
|
40
65
|
export const matchesGroupQuery = (
|
|
41
66
|
group: GroupData,
|
|
42
67
|
query: string,
|
package/src/lib/index.ts
CHANGED
|
@@ -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';
|