@openpeepshq/common 0.4.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.
- 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 +9 -0
- package/dist/lib/eventRecurrence.js +17 -9
- package/dist/lib/eventWaitlist.d.ts +58 -0
- package/dist/lib/eventWaitlist.js +217 -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 +35 -5
- package/dist/lib/postHelpers.js +85 -10
- package/dist/lib/profileHelpers.d.ts +12 -2
- package/dist/lib/profileHelpers.js +24 -3
- 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 +15 -9
- package/dist/types/capabilities.d.ts +2 -2
- package/dist/types/capabilities.js +3 -0
- package/dist/types/config.d.ts +3 -0
- package/dist/types/config.js +6 -0
- 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 +28 -14
- package/dist/types/models.js +27 -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 +19 -10
- package/src/lib/eventWaitlist.ts +325 -0
- package/src/lib/index.ts +1 -0
- package/src/lib/markdownHelpers.ts +121 -7
- package/src/lib/postHelpers.ts +129 -11
- package/src/lib/profileHelpers.ts +38 -3
- 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 +19 -9
- package/src/types/auth.ts +2 -2
- package/src/types/capabilities.ts +3 -0
- package/src/types/config.ts +6 -0
- package/src/types/forbiddenHandles.ts +103 -0
- package/src/types/index.ts +2 -0
- package/src/types/models.ts +53 -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.11",
|
|
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',
|
|
@@ -361,6 +351,25 @@ export const defaultRsvpRecurrenceId = (
|
|
|
361
351
|
return listRsvpOccurrences(event, now)[0]?.recurrenceId;
|
|
362
352
|
};
|
|
363
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
|
+
|
|
364
373
|
export const previewUpcomingOccurrences = (
|
|
365
374
|
event: Event,
|
|
366
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/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';
|
|
@@ -88,6 +88,19 @@ export const markdownPlainText = (markdown?: string | null): string => {
|
|
|
88
88
|
.trim();
|
|
89
89
|
};
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Removes inline images (`` and `![alt]`) from markdown while
|
|
93
|
+
* leaving all other formatting intact. Used to keep abbreviated previews to
|
|
94
|
+
* text+basic formatting and put the full (image-bearing) content in an overlay.
|
|
95
|
+
*/
|
|
96
|
+
export const stripMarkdownImages = (markdown?: string | null): string => {
|
|
97
|
+
if (!markdown) return '';
|
|
98
|
+
return markdown
|
|
99
|
+
.replace(/\[!\[[^\]]*\]\([^)]*\)\]\([^)]*\)/g, '')
|
|
100
|
+
.replace(/!\[[^\]]*\]\([^)]*\)/g, ' ')
|
|
101
|
+
.replace(/!\[[^\]]*\]/g, '');
|
|
102
|
+
};
|
|
103
|
+
|
|
91
104
|
export const firstNWords = (markdown: string | undefined, n: number) => {
|
|
92
105
|
if (!markdown) return '';
|
|
93
106
|
// Regular expression to match words and markdown links.
|
|
@@ -119,9 +132,12 @@ export const firstNWords = (markdown: string | undefined, n: number) => {
|
|
|
119
132
|
|
|
120
133
|
/**
|
|
121
134
|
* Truncates markdown to at most `maxBlocks` block-level regions, then to
|
|
122
|
-
* `maxChars` characters at a word boundary.
|
|
123
|
-
*
|
|
124
|
-
* the
|
|
135
|
+
* `maxChars` characters at a word boundary. Protected inline constructs — links,
|
|
136
|
+
* images and inline code spans — are never severed: a construct that would not
|
|
137
|
+
* fit whole is omitted (along with what follows), so the result is always valid
|
|
138
|
+
* markdown. Emphasis markers and inline-code backticks left open by a word-boundary
|
|
139
|
+
* cut are closed, so renderers never emit raw markup leaking into copy. An
|
|
140
|
+
* ellipsis is appended only when the content was actually shortened.
|
|
125
141
|
*/
|
|
126
142
|
export const truncateMarkdown = (
|
|
127
143
|
markdown: string | undefined,
|
|
@@ -145,18 +161,116 @@ export const truncateMarkdown = (
|
|
|
145
161
|
return hadMoreBlocks ? `${content}…` : content;
|
|
146
162
|
}
|
|
147
163
|
|
|
148
|
-
|
|
164
|
+
return `${closeOpenInline(sliceMarkdownTo(content, maxChars))}…`;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/** Inline constructs kept whole so truncation never breaks their syntax. */
|
|
168
|
+
const PROTECTED_RE = /(`[^`]+`)|(!\[[^\]]*\]\([^)]*\))|(\[[^\]]*\]\([^)]*\))/g;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Walks `content` accumulating plain text (cuttable at a word boundary) and
|
|
172
|
+
* protected spans (kept whole). A protected span that would not fit in the
|
|
173
|
+
* remaining budget ends the snippet instead of being severed.
|
|
174
|
+
*/
|
|
175
|
+
const sliceMarkdownTo = (content: string, maxChars: number): string => {
|
|
176
|
+
let out = '';
|
|
177
|
+
let pos = 0;
|
|
178
|
+
|
|
179
|
+
while (pos < content.length) {
|
|
180
|
+
PROTECTED_RE.lastIndex = pos;
|
|
181
|
+
const match = PROTECTED_RE.exec(content);
|
|
182
|
+
const spanStart = match ? match.index : content.length;
|
|
183
|
+
|
|
184
|
+
const plain = content.slice(pos, spanStart);
|
|
185
|
+
const room = maxChars - out.length;
|
|
186
|
+
if (room <= 0) break;
|
|
187
|
+
|
|
188
|
+
if (plain.length <= room) {
|
|
189
|
+
out += plain;
|
|
190
|
+
} else {
|
|
191
|
+
out += cutAtWordBoundary(plain, room);
|
|
192
|
+
return out.trimEnd();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (match) {
|
|
196
|
+
const span = match[0];
|
|
197
|
+
if (out.length + span.length <= maxChars) {
|
|
198
|
+
out += span;
|
|
199
|
+
pos = match.index + span.length;
|
|
200
|
+
} else {
|
|
201
|
+
// Whole span doesn't fit — drop it (and the rest) rather than sever it.
|
|
202
|
+
return out.trimEnd();
|
|
203
|
+
}
|
|
204
|
+
} else {
|
|
205
|
+
pos = content.length;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return out.trimEnd();
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const cutAtWordBoundary = (text: string, max: number): string => {
|
|
213
|
+
const candidate = text.slice(0, max);
|
|
149
214
|
const lastBreak = Math.max(
|
|
150
215
|
candidate.lastIndexOf(' '),
|
|
151
216
|
candidate.lastIndexOf('\n'),
|
|
152
217
|
candidate.lastIndexOf('\t'),
|
|
153
218
|
);
|
|
154
|
-
|
|
155
219
|
if (lastBreak > 0) {
|
|
156
|
-
return
|
|
220
|
+
return text.slice(0, lastBreak).trimEnd();
|
|
221
|
+
}
|
|
222
|
+
return candidate;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Best-effort repair: closes emphasis runs and inline-code backticks left
|
|
227
|
+
* dangling by a cut inside their span. List-marker asterisks at the start of a
|
|
228
|
+
* line are treated as plain text, not emphasis.
|
|
229
|
+
*/
|
|
230
|
+
const closeOpenInline = (snippet: string): string => {
|
|
231
|
+
const closers: string[] = [];
|
|
232
|
+
let inCode = false;
|
|
233
|
+
let i = 0;
|
|
234
|
+
|
|
235
|
+
while (i < snippet.length) {
|
|
236
|
+
const ch = snippet[i];
|
|
237
|
+
const atLineStart = i === 0 || snippet[i - 1] === '\n';
|
|
238
|
+
|
|
239
|
+
if (ch === '`') {
|
|
240
|
+
inCode = !inCode;
|
|
241
|
+
i++;
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (inCode) {
|
|
245
|
+
i++;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (!atLineStart && (ch === '*' || ch === '_' || ch === '~')) {
|
|
250
|
+
let j = i + 1;
|
|
251
|
+
while (j < snippet.length && snippet[j] === ch) j++;
|
|
252
|
+
const marker = snippet.slice(i, j);
|
|
253
|
+
// A lone '~' is not emphasis; treat length-1 tilde as plain text.
|
|
254
|
+
if (ch === '~' && marker.length < 2) {
|
|
255
|
+
i++;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
const opener = marker.length >= 2 ? marker.slice(0, 2) : marker;
|
|
259
|
+
if (closers[closers.length - 1] === opener) {
|
|
260
|
+
closers.pop();
|
|
261
|
+
} else {
|
|
262
|
+
closers.push(opener);
|
|
263
|
+
}
|
|
264
|
+
i = j;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
i++;
|
|
157
269
|
}
|
|
158
270
|
|
|
159
|
-
return
|
|
271
|
+
if (closers.length === 0 && !inCode) return snippet;
|
|
272
|
+
const tail = closers.reverse().join('') + (inCode ? '`' : '');
|
|
273
|
+
return `${snippet}${tail}`;
|
|
160
274
|
};
|
|
161
275
|
|
|
162
276
|
/** Average adult reading speed (words per minute). */
|