@openpeepshq/common 0.4.0 → 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 +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 +27 -5
- package/dist/lib/postHelpers.js +72 -9
- 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 +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 +101 -11
- 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 +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
|
@@ -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";
|
|
@@ -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') ||
|
|
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',
|
package/dist/lib/csvHelpers.d.ts
CHANGED
|
@@ -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;
|
package/dist/lib/csvHelpers.js
CHANGED
|
@@ -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 '';
|
|
@@ -41,6 +41,15 @@ export type RsvpTimeTarget = {
|
|
|
41
41
|
*/
|
|
42
42
|
export declare const rsvpTargetsPastEvent: (event: Event, target?: RsvpTimeTarget, now?: Date) => boolean;
|
|
43
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;
|
|
44
53
|
export declare const previewUpcomingOccurrences: (event: Event, count?: number, now?: Date) => ExpandedOccurrence[];
|
|
45
54
|
export declare const effectiveEventTimes: (event: Event, recurrenceId?: string) => {
|
|
46
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',
|
|
@@ -248,6 +239,23 @@ export const defaultRsvpRecurrenceId = (event, queryId, now = new Date()) => {
|
|
|
248
239
|
return normalizeRecurrenceId(queryId);
|
|
249
240
|
return listRsvpOccurrences(event, now)[0]?.recurrenceId;
|
|
250
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
|
+
};
|
|
251
259
|
export const previewUpcomingOccurrences = (event, count = 3, now = new Date()) => listRsvpOccurrences(event, now).slice(0, count);
|
|
252
260
|
export const effectiveEventTimes = (event, recurrenceId) => {
|
|
253
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
|
+
};
|
package/dist/lib/index.d.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';
|
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 (`` 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.
|
|
35
|
-
*
|
|
36
|
-
* the
|
|
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
|
/**
|
|
@@ -56,6 +56,19 @@ export const markdownPlainText = (markdown) => {
|
|
|
56
56
|
.replace(/\s+/g, ' ')
|
|
57
57
|
.trim();
|
|
58
58
|
};
|
|
59
|
+
/**
|
|
60
|
+
* Removes inline images (`` and `![alt]`) from markdown while
|
|
61
|
+
* leaving all other formatting intact. Used to keep abbreviated previews to
|
|
62
|
+
* text+basic formatting and put the full (image-bearing) content in an overlay.
|
|
63
|
+
*/
|
|
64
|
+
export const stripMarkdownImages = (markdown) => {
|
|
65
|
+
if (!markdown)
|
|
66
|
+
return '';
|
|
67
|
+
return markdown
|
|
68
|
+
.replace(/\[!\[[^\]]*\]\([^)]*\)\]\([^)]*\)/g, '')
|
|
69
|
+
.replace(/!\[[^\]]*\]\([^)]*\)/g, ' ')
|
|
70
|
+
.replace(/!\[[^\]]*\]/g, '');
|
|
71
|
+
};
|
|
59
72
|
export const firstNWords = (markdown, n) => {
|
|
60
73
|
if (!markdown)
|
|
61
74
|
return '';
|
|
@@ -84,9 +97,12 @@ export const firstNWords = (markdown, n) => {
|
|
|
84
97
|
};
|
|
85
98
|
/**
|
|
86
99
|
* Truncates markdown to at most `maxBlocks` block-level regions, then to
|
|
87
|
-
* `maxChars` characters at a word boundary.
|
|
88
|
-
*
|
|
89
|
-
* the
|
|
100
|
+
* `maxChars` characters at a word boundary. Protected inline constructs — links,
|
|
101
|
+
* images and inline code spans — are never severed: a construct that would not
|
|
102
|
+
* fit whole is omitted (along with what follows), so the result is always valid
|
|
103
|
+
* markdown. Emphasis markers and inline-code backticks left open by a word-boundary
|
|
104
|
+
* cut are closed, so renderers never emit raw markup leaking into copy. An
|
|
105
|
+
* ellipsis is appended only when the content was actually shortened.
|
|
90
106
|
*/
|
|
91
107
|
export const truncateMarkdown = (markdown, maxBlocks, maxChars) => {
|
|
92
108
|
if (!markdown)
|
|
@@ -103,12 +119,105 @@ export const truncateMarkdown = (markdown, maxBlocks, maxChars) => {
|
|
|
103
119
|
if (content.length <= maxChars) {
|
|
104
120
|
return hadMoreBlocks ? `${content}…` : content;
|
|
105
121
|
}
|
|
106
|
-
|
|
122
|
+
return `${closeOpenInline(sliceMarkdownTo(content, maxChars))}…`;
|
|
123
|
+
};
|
|
124
|
+
/** Inline constructs kept whole so truncation never breaks their syntax. */
|
|
125
|
+
const PROTECTED_RE = /(`[^`]+`)|(!\[[^\]]*\]\([^)]*\))|(\[[^\]]*\]\([^)]*\))/g;
|
|
126
|
+
/**
|
|
127
|
+
* Walks `content` accumulating plain text (cuttable at a word boundary) and
|
|
128
|
+
* protected spans (kept whole). A protected span that would not fit in the
|
|
129
|
+
* remaining budget ends the snippet instead of being severed.
|
|
130
|
+
*/
|
|
131
|
+
const sliceMarkdownTo = (content, maxChars) => {
|
|
132
|
+
let out = '';
|
|
133
|
+
let pos = 0;
|
|
134
|
+
while (pos < content.length) {
|
|
135
|
+
PROTECTED_RE.lastIndex = pos;
|
|
136
|
+
const match = PROTECTED_RE.exec(content);
|
|
137
|
+
const spanStart = match ? match.index : content.length;
|
|
138
|
+
const plain = content.slice(pos, spanStart);
|
|
139
|
+
const room = maxChars - out.length;
|
|
140
|
+
if (room <= 0)
|
|
141
|
+
break;
|
|
142
|
+
if (plain.length <= room) {
|
|
143
|
+
out += plain;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
out += cutAtWordBoundary(plain, room);
|
|
147
|
+
return out.trimEnd();
|
|
148
|
+
}
|
|
149
|
+
if (match) {
|
|
150
|
+
const span = match[0];
|
|
151
|
+
if (out.length + span.length <= maxChars) {
|
|
152
|
+
out += span;
|
|
153
|
+
pos = match.index + span.length;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
// Whole span doesn't fit — drop it (and the rest) rather than sever it.
|
|
157
|
+
return out.trimEnd();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
pos = content.length;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return out.trimEnd();
|
|
165
|
+
};
|
|
166
|
+
const cutAtWordBoundary = (text, max) => {
|
|
167
|
+
const candidate = text.slice(0, max);
|
|
107
168
|
const lastBreak = Math.max(candidate.lastIndexOf(' '), candidate.lastIndexOf('\n'), candidate.lastIndexOf('\t'));
|
|
108
169
|
if (lastBreak > 0) {
|
|
109
|
-
return
|
|
170
|
+
return text.slice(0, lastBreak).trimEnd();
|
|
171
|
+
}
|
|
172
|
+
return candidate;
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Best-effort repair: closes emphasis runs and inline-code backticks left
|
|
176
|
+
* dangling by a cut inside their span. List-marker asterisks at the start of a
|
|
177
|
+
* line are treated as plain text, not emphasis.
|
|
178
|
+
*/
|
|
179
|
+
const closeOpenInline = (snippet) => {
|
|
180
|
+
const closers = [];
|
|
181
|
+
let inCode = false;
|
|
182
|
+
let i = 0;
|
|
183
|
+
while (i < snippet.length) {
|
|
184
|
+
const ch = snippet[i];
|
|
185
|
+
const atLineStart = i === 0 || snippet[i - 1] === '\n';
|
|
186
|
+
if (ch === '`') {
|
|
187
|
+
inCode = !inCode;
|
|
188
|
+
i++;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (inCode) {
|
|
192
|
+
i++;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (!atLineStart && (ch === '*' || ch === '_' || ch === '~')) {
|
|
196
|
+
let j = i + 1;
|
|
197
|
+
while (j < snippet.length && snippet[j] === ch)
|
|
198
|
+
j++;
|
|
199
|
+
const marker = snippet.slice(i, j);
|
|
200
|
+
// A lone '~' is not emphasis; treat length-1 tilde as plain text.
|
|
201
|
+
if (ch === '~' && marker.length < 2) {
|
|
202
|
+
i++;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
const opener = marker.length >= 2 ? marker.slice(0, 2) : marker;
|
|
206
|
+
if (closers[closers.length - 1] === opener) {
|
|
207
|
+
closers.pop();
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
closers.push(opener);
|
|
211
|
+
}
|
|
212
|
+
i = j;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
i++;
|
|
110
216
|
}
|
|
111
|
-
|
|
217
|
+
if (closers.length === 0 && !inCode)
|
|
218
|
+
return snippet;
|
|
219
|
+
const tail = closers.reverse().join('') + (inCode ? '`' : '');
|
|
220
|
+
return `${snippet}${tail}`;
|
|
112
221
|
};
|
|
113
222
|
/** Average adult reading speed (words per minute). */
|
|
114
223
|
const DEFAULT_WORDS_PER_MINUTE = 210;
|