@openpeepshq/common 0.1.46 → 0.2.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.js +1 -1
- package/dist/lib/capabilitiesHelpers.js +30 -40
- package/dist/lib/csvHelpers.js +1 -1
- package/dist/lib/eventIcs.js +26 -32
- package/dist/lib/eventRecurrence.d.ts +17 -0
- package/dist/lib/eventRecurrence.js +75 -18
- package/dist/lib/eventTime.js +9 -11
- package/dist/lib/groupHelpers.js +13 -34
- package/dist/lib/groupTemplateHelpers.js +6 -10
- package/dist/lib/jamHelpers.js +6 -9
- package/dist/lib/markdownHelpers.js +2 -3
- package/dist/lib/postHelpers.js +70 -97
- package/dist/lib/profileHelpers.js +17 -24
- package/dist/lib/pushHelpers.js +3 -4
- package/dist/lib/scopeHelpers.js +6 -9
- package/dist/lib/ssoLoginUrl.js +4 -6
- package/dist/lib/ssoProviders.js +35 -39
- package/dist/lib/tokenHelpers.js +5 -5
- package/dist/lib/utils.js +6 -7
- package/dist/types/admin-db.js +1 -1
- package/dist/types/api.d.ts +1 -0
- package/dist/types/api.js +1 -1
- package/dist/types/config.d.ts +3 -0
- package/dist/types/config.js +3 -0
- package/dist/types/models.js +1 -1
- package/dist/types/plugins.js +2 -2
- package/dist/zod.d.ts +1 -0
- package/dist/zod.js +1 -0
- package/package.json +36 -6
- package/src/index.ts +4 -0
- package/src/lib/adminSections.ts +84 -0
- package/src/lib/attachmentHelpers.ts +14 -0
- package/src/lib/capabilitiesHelpers.ts +532 -0
- package/src/lib/colors.ts +20 -0
- package/src/lib/csvHelpers.ts +31 -0
- package/src/lib/eventIcs.ts +265 -0
- package/src/lib/eventRecurrence.ts +495 -0
- package/src/lib/eventTime.ts +206 -0
- package/src/lib/federationHosts.ts +22 -0
- package/src/lib/feedCursor.ts +27 -0
- package/src/lib/feedFormat.ts +24 -0
- package/src/lib/groupHelpers.ts +421 -0
- package/src/lib/groupTemplateHelpers.ts +83 -0
- package/src/lib/index.ts +29 -0
- package/src/lib/jamHelpers.ts +164 -0
- package/src/lib/legacyThemeRoot.ts +14 -0
- package/src/lib/markdownHelpers.ts +122 -0
- package/src/lib/mediaDimensions.ts +21 -0
- package/src/lib/postHelpers.ts +783 -0
- package/src/lib/profileHelpers.ts +220 -0
- package/src/lib/pushHelpers.ts +34 -0
- package/src/lib/reactions.ts +52 -0
- package/src/lib/reportHelpers.ts +7 -0
- package/src/lib/scopeHelpers.ts +176 -0
- package/src/lib/ssoLoginUrl.ts +93 -0
- package/src/lib/ssoProviders.ts +472 -0
- package/src/lib/text.ts +26 -0
- package/src/lib/timeZone.ts +52 -0
- package/src/lib/tokenHelpers.ts +119 -0
- package/src/lib/utils.ts +322 -0
- package/src/types/admin-db.ts +74 -0
- package/src/types/admin-email.ts +55 -0
- package/src/types/admin-jobs.ts +17 -0
- package/src/types/admin-performance.ts +20 -0
- package/src/types/analytics.ts +318 -0
- package/src/types/api.ts +871 -0
- package/src/types/apple-app-site-association.ts +54 -0
- package/src/types/auth.ts +22 -0
- package/src/types/capabilities.ts +280 -0
- package/src/types/config.ts +546 -0
- package/src/types/db.ts +138 -0
- package/src/types/export.ts +10 -0
- package/src/types/index.ts +20 -0
- package/src/types/internal.ts +338 -0
- package/src/types/models.ts +939 -0
- package/src/types/notifications.ts +92 -0
- package/src/types/plugins.ts +150 -0
- package/src/types/roleDefaults.ts +132 -0
- package/src/types/stats.ts +195 -0
- package/src/types/stripe.ts +45 -0
- package/src/types/utils.ts +100 -0
- package/src/zod.ts +1 -0
|
@@ -0,0 +1,783 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Answer,
|
|
3
|
+
AnswerWithPublicProfile,
|
|
4
|
+
AuthorizationData,
|
|
5
|
+
CapabilitiesConfig,
|
|
6
|
+
EntryWithPublicProfile,
|
|
7
|
+
Event,
|
|
8
|
+
GroupWithMeta,
|
|
9
|
+
POLL_OPTION_MAX_LENGTH,
|
|
10
|
+
PostType,
|
|
11
|
+
PostDataUnion,
|
|
12
|
+
PublicPost,
|
|
13
|
+
PublicProfile,
|
|
14
|
+
PublicRsvp,
|
|
15
|
+
Question,
|
|
16
|
+
RSVP,
|
|
17
|
+
Thread,
|
|
18
|
+
VisibilityType,
|
|
19
|
+
visibilityTypeValues,
|
|
20
|
+
} from '../types';
|
|
21
|
+
import { canModerateJam } from './jamHelpers';
|
|
22
|
+
import {
|
|
23
|
+
defaultRsvpRecurrenceId,
|
|
24
|
+
effectiveEventTimes,
|
|
25
|
+
isRecurringEvent,
|
|
26
|
+
listRsvpOccurrences,
|
|
27
|
+
normalizeRecurrenceId,
|
|
28
|
+
sameRecurrenceId,
|
|
29
|
+
} from './eventRecurrence';
|
|
30
|
+
import { formatEventWhen } from './eventTime';
|
|
31
|
+
import {
|
|
32
|
+
checkGroupCapabilities,
|
|
33
|
+
checkPostCapabilities,
|
|
34
|
+
checkRoleCapabilities,
|
|
35
|
+
} from './capabilitiesHelpers';
|
|
36
|
+
import { scopeMatches } from './scopeHelpers';
|
|
37
|
+
import { countBy, dateSorter, groupBy, transformValues } from './utils';
|
|
38
|
+
|
|
39
|
+
const buildThread = (rootPost: PublicPost, postList: PublicPost[]): Thread => ({
|
|
40
|
+
...rootPost,
|
|
41
|
+
children: postList
|
|
42
|
+
.filter((p) => p.inReplyToId === rootPost.id)
|
|
43
|
+
.map((post) => buildThread(post, postList)),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
export const buildThreads = (posts: PublicPost[]) =>
|
|
47
|
+
posts
|
|
48
|
+
.filter((p) => !posts.find((p2) => p2.id === p.inReplyToId))
|
|
49
|
+
.map((post) => buildThread(post, posts))
|
|
50
|
+
.sort(dateSorter());
|
|
51
|
+
|
|
52
|
+
export type ThreadPreviewPost = {
|
|
53
|
+
id: string;
|
|
54
|
+
createdAt: string;
|
|
55
|
+
inReplyToId?: string | null;
|
|
56
|
+
replyTo?: ThreadPreviewPost | null;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export type ThreadPreviewGroup = {
|
|
60
|
+
skippedAncestor: boolean;
|
|
61
|
+
ancestor?: ThreadPreviewPost;
|
|
62
|
+
posts: ThreadPreviewPost[];
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const parentIdOf = (post: ThreadPreviewPost) =>
|
|
66
|
+
post.inReplyToId ?? post.replyTo?.id;
|
|
67
|
+
|
|
68
|
+
export const buildThreadPreview = (
|
|
69
|
+
rootId: string,
|
|
70
|
+
replies: ThreadPreviewPost[],
|
|
71
|
+
): ThreadPreviewGroup[] => {
|
|
72
|
+
const sorted = [...replies].sort(
|
|
73
|
+
(a, b) =>
|
|
74
|
+
a.createdAt.localeCompare(b.createdAt) || a.id.localeCompare(b.id),
|
|
75
|
+
);
|
|
76
|
+
if (!sorted.length) return [];
|
|
77
|
+
|
|
78
|
+
const ids = new Set(sorted.map((post) => post.id));
|
|
79
|
+
const parent = new Map(sorted.map((post) => [post.id, post.id]));
|
|
80
|
+
const find = (id: string): string => {
|
|
81
|
+
const current = parent.get(id) ?? id;
|
|
82
|
+
if (current !== id) {
|
|
83
|
+
const root = find(current);
|
|
84
|
+
parent.set(id, root);
|
|
85
|
+
return root;
|
|
86
|
+
}
|
|
87
|
+
return id;
|
|
88
|
+
};
|
|
89
|
+
const union = (a: string, b: string) => {
|
|
90
|
+
const ra = find(a);
|
|
91
|
+
const rb = find(b);
|
|
92
|
+
if (ra !== rb) parent.set(ra, rb);
|
|
93
|
+
};
|
|
94
|
+
for (const post of sorted) {
|
|
95
|
+
const parentId = parentIdOf(post);
|
|
96
|
+
if (parentId && ids.has(parentId)) union(post.id, parentId);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const grouped = new Map<string, ThreadPreviewPost[]>();
|
|
100
|
+
for (const post of sorted) {
|
|
101
|
+
const key = find(post.id);
|
|
102
|
+
const list = grouped.get(key) ?? [];
|
|
103
|
+
list.push(post);
|
|
104
|
+
grouped.set(key, list);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return [...grouped.values()]
|
|
108
|
+
.map((posts) => {
|
|
109
|
+
const start =
|
|
110
|
+
posts.find((post) => {
|
|
111
|
+
const parentId = parentIdOf(post);
|
|
112
|
+
return !parentId || !ids.has(parentId);
|
|
113
|
+
}) ?? posts[0]!;
|
|
114
|
+
const ordered: ThreadPreviewPost[] = [];
|
|
115
|
+
const used = new Set<string>();
|
|
116
|
+
let current: ThreadPreviewPost | undefined = start;
|
|
117
|
+
while (current && !used.has(current.id)) {
|
|
118
|
+
ordered.push(current);
|
|
119
|
+
used.add(current.id);
|
|
120
|
+
current = posts.find(
|
|
121
|
+
(post) => parentIdOf(post) === current?.id && !used.has(post.id),
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
for (const post of posts) {
|
|
125
|
+
if (!used.has(post.id)) ordered.push(post);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const startParentId = parentIdOf(start);
|
|
129
|
+
const ancestor =
|
|
130
|
+
startParentId && startParentId !== rootId
|
|
131
|
+
? (start.replyTo ?? undefined)
|
|
132
|
+
: undefined;
|
|
133
|
+
const skippedAncestor = !!(
|
|
134
|
+
ancestor &&
|
|
135
|
+
ancestor.inReplyToId &&
|
|
136
|
+
ancestor.inReplyToId !== rootId
|
|
137
|
+
);
|
|
138
|
+
return { skippedAncestor, ancestor, posts: ordered };
|
|
139
|
+
})
|
|
140
|
+
.sort((a, b) => {
|
|
141
|
+
const aKey = a.ancestor?.createdAt ?? a.posts[0]?.createdAt ?? '';
|
|
142
|
+
const bKey = b.ancestor?.createdAt ?? b.posts[0]?.createdAt ?? '';
|
|
143
|
+
return aKey.localeCompare(bKey);
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export const getReactionCount = (post: PublicPost) => {
|
|
148
|
+
return countBy(post.reactions, (r) => r.reaction);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export const countVotes = (optionsCount: number, answers: Answer[]) => {
|
|
152
|
+
const voteCounts: number[] = new Array(optionsCount).fill(0);
|
|
153
|
+
for (const answer of answers) {
|
|
154
|
+
for (const index of answer.selection) {
|
|
155
|
+
if (index < optionsCount) {
|
|
156
|
+
voteCounts[index] = voteCounts[index] + 1;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return voteCounts;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/** Fill blank poll choices with their placeholder label (e.g. "Option 1"). */
|
|
164
|
+
export const resolvePollOptionContents = (
|
|
165
|
+
options: readonly string[],
|
|
166
|
+
fallbackLabel: (index: number) => string,
|
|
167
|
+
): string[] =>
|
|
168
|
+
options.map((text, index) => {
|
|
169
|
+
const trimmed = text.trim();
|
|
170
|
+
return trimmed.length > 0 ? trimmed : fallbackLabel(index);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
/** Longer choices are rejected by `questionSchema`, so block submit instead. */
|
|
174
|
+
export const pollOptionsWithinLimit = (options: readonly string[]): boolean =>
|
|
175
|
+
options.every((text) => text.trim().length <= POLL_OPTION_MAX_LENGTH);
|
|
176
|
+
|
|
177
|
+
export const collectVotes = (
|
|
178
|
+
post: PublicPost,
|
|
179
|
+
): { voteCounts: number[]; votes: AnswerWithPublicProfile[] } => {
|
|
180
|
+
if (post.type !== 'question') {
|
|
181
|
+
return {
|
|
182
|
+
voteCounts: [],
|
|
183
|
+
votes: [],
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const postData = post.data as Question;
|
|
188
|
+
|
|
189
|
+
const sortedVoteEntries = post.entries
|
|
190
|
+
.filter((e) => e.type === 'answer')
|
|
191
|
+
.sort(dateSorter<EntryWithPublicProfile>());
|
|
192
|
+
|
|
193
|
+
const votesByProfile = groupBy<EntryWithPublicProfile, string>(
|
|
194
|
+
sortedVoteEntries,
|
|
195
|
+
(e) => e.profile.id,
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
const lastVoteByProfile = transformValues(
|
|
199
|
+
votesByProfile,
|
|
200
|
+
(votes) => votes.slice(-1)[0],
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
const votes: AnswerWithPublicProfile[] = [...Object.values(lastVoteByProfile)]
|
|
204
|
+
.filter((ve) => (ve.data as Answer).selection.length > 0)
|
|
205
|
+
.map((entry) => ({
|
|
206
|
+
profile: entry.profile,
|
|
207
|
+
selection: entry.data.selection,
|
|
208
|
+
}));
|
|
209
|
+
|
|
210
|
+
const voteCounts = countVotes(postData.options.length, votes);
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
votes,
|
|
214
|
+
voteCounts,
|
|
215
|
+
};
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const latestRsvpPerProfile = (rsvps: PublicRsvp[], recurrenceId?: string) => {
|
|
219
|
+
const scoped = rsvps.filter((rsvp) => {
|
|
220
|
+
if (recurrenceId) {
|
|
221
|
+
if (rsvp.recurrenceId) {
|
|
222
|
+
return sameRecurrenceId(rsvp.recurrenceId, recurrenceId);
|
|
223
|
+
}
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
return true;
|
|
227
|
+
});
|
|
228
|
+
const rsvpsByProfile = groupBy(scoped, (r) => r.profile.id);
|
|
229
|
+
return Object.values(rsvpsByProfile).map((profileRsvps) => {
|
|
230
|
+
const instanceRsvps = recurrenceId
|
|
231
|
+
? profileRsvps.filter((rsvp) =>
|
|
232
|
+
sameRecurrenceId(rsvp.recurrenceId, recurrenceId),
|
|
233
|
+
)
|
|
234
|
+
: [];
|
|
235
|
+
const seriesRsvps = recurrenceId
|
|
236
|
+
? profileRsvps.filter((rsvp) => !rsvp.recurrenceId)
|
|
237
|
+
: profileRsvps;
|
|
238
|
+
const pool =
|
|
239
|
+
recurrenceId && instanceRsvps.length > 0 ? instanceRsvps : seriesRsvps;
|
|
240
|
+
const sorted = pool.sort(dateSorter<PublicRsvp>());
|
|
241
|
+
return sorted[sorted.length - 1];
|
|
242
|
+
});
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
export const calculateEffectiveRsvps = (
|
|
246
|
+
post: PublicPost,
|
|
247
|
+
recurrenceId?: string,
|
|
248
|
+
) =>
|
|
249
|
+
latestRsvpPerProfile(post.rsvps || [], recurrenceId).filter(
|
|
250
|
+
(rsvp): rsvp is PublicRsvp => !!rsvp,
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
export const getEffectiveRsvp = (
|
|
254
|
+
post: PublicPost,
|
|
255
|
+
profileId: string,
|
|
256
|
+
recurrenceId?: string,
|
|
257
|
+
) =>
|
|
258
|
+
calculateEffectiveRsvps(post, recurrenceId).find(
|
|
259
|
+
(r) => r.profile.id === profileId,
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
export const countYesRsvps = (post: PublicPost, recurrenceId?: string) =>
|
|
263
|
+
calculateEffectiveRsvps(post, recurrenceId).filter(
|
|
264
|
+
(r) => r.response === 'yes',
|
|
265
|
+
).length;
|
|
266
|
+
|
|
267
|
+
export const countSeriesYesRsvps = (post: PublicPost) =>
|
|
268
|
+
(post.rsvps ?? []).filter((r) => !r.recurrenceId && r.response === 'yes')
|
|
269
|
+
.length;
|
|
270
|
+
|
|
271
|
+
const attendingResponse = (response?: PublicRsvp['response']) =>
|
|
272
|
+
response === 'yes' || response === 'tentative';
|
|
273
|
+
|
|
274
|
+
/** Current roster: attending guests, plus organizer removals that can be restored. */
|
|
275
|
+
export const listedRsvps = (rsvps: PublicRsvp[]) =>
|
|
276
|
+
rsvps.filter(
|
|
277
|
+
(rsvp) =>
|
|
278
|
+
rsvp.response === 'yes' ||
|
|
279
|
+
rsvp.response === 'tentative' ||
|
|
280
|
+
rsvp.response === 'removed',
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
/** Set only when a member leaves an attending RSVP. A second "no" stays unset. */
|
|
284
|
+
export type RsvpCancelNotice = {
|
|
285
|
+
occurrenceIds: string[];
|
|
286
|
+
series: boolean;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
export const isRsvpCancelNotice = (data: unknown): data is RsvpCancelNotice => {
|
|
290
|
+
if (!data || typeof data !== 'object') return false;
|
|
291
|
+
const notice = data as RsvpCancelNotice;
|
|
292
|
+
return (
|
|
293
|
+
typeof notice.series === 'boolean' &&
|
|
294
|
+
Array.isArray(notice.occurrenceIds) &&
|
|
295
|
+
notice.occurrenceIds.every((id) => typeof id === 'string')
|
|
296
|
+
);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
export const rsvpCancelNotice = (
|
|
300
|
+
post: PublicPost,
|
|
301
|
+
profileId: string,
|
|
302
|
+
response: PublicRsvp['response'],
|
|
303
|
+
occurrenceIds?: string[],
|
|
304
|
+
): RsvpCancelNotice | undefined => {
|
|
305
|
+
if (response !== 'no') return undefined;
|
|
306
|
+
const event = post.data?.type === 'event' ? post.data : undefined;
|
|
307
|
+
if (!event) return undefined;
|
|
308
|
+
|
|
309
|
+
if (occurrenceIds?.length) {
|
|
310
|
+
const canceled = occurrenceIds.filter((id) =>
|
|
311
|
+
attendingResponse(getEffectiveRsvp(post, profileId, id)?.response),
|
|
312
|
+
);
|
|
313
|
+
if (!canceled.length) return undefined;
|
|
314
|
+
return { occurrenceIds: canceled, series: false };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (attendingResponse(getEffectiveRsvp(post, profileId)?.response)) {
|
|
318
|
+
return { occurrenceIds: [], series: isRecurringEvent(event) };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (!event.recurrence) return undefined;
|
|
322
|
+
|
|
323
|
+
const canceled = instanceRsvpIdsForProfile(post, profileId).filter((id) =>
|
|
324
|
+
attendingResponse(getEffectiveRsvp(post, profileId, id)?.response),
|
|
325
|
+
);
|
|
326
|
+
if (!canceled.length) return undefined;
|
|
327
|
+
return { occurrenceIds: canceled, series: false };
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
export type RsvpCancellation = {
|
|
331
|
+
profile: PublicRsvp['profile'];
|
|
332
|
+
recurrenceId?: string;
|
|
333
|
+
canceledAt: string;
|
|
334
|
+
series: boolean;
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
const instanceRsvpKey = (profileId: string, recurrenceId: string) =>
|
|
338
|
+
`${profileId}|${normalizeRecurrenceId(recurrenceId)}`;
|
|
339
|
+
|
|
340
|
+
/** Attending → no transitions, newest first. Later re-RSVPs do not erase them. */
|
|
341
|
+
export const listRsvpCancellations = (post: PublicPost): RsvpCancellation[] => {
|
|
342
|
+
const event = post.data?.type === 'event' ? post.data : undefined;
|
|
343
|
+
const recurring = event ? isRecurringEvent(event) : false;
|
|
344
|
+
const seriesResponse = new Map<string, PublicRsvp['response']>();
|
|
345
|
+
const instanceResponse = new Map<string, PublicRsvp['response']>();
|
|
346
|
+
const records: RsvpCancellation[] = [];
|
|
347
|
+
|
|
348
|
+
for (const rsvp of [...(post.rsvps ?? [])].sort(dateSorter())) {
|
|
349
|
+
const profileId = rsvp.profile.id;
|
|
350
|
+
if (rsvp.response === 'no') {
|
|
351
|
+
if (rsvp.recurrenceId) {
|
|
352
|
+
const key = instanceRsvpKey(profileId, rsvp.recurrenceId);
|
|
353
|
+
const previous = instanceResponse.has(key)
|
|
354
|
+
? instanceResponse.get(key)
|
|
355
|
+
: seriesResponse.get(profileId);
|
|
356
|
+
if (attendingResponse(previous)) {
|
|
357
|
+
records.push({
|
|
358
|
+
profile: rsvp.profile,
|
|
359
|
+
recurrenceId: rsvp.recurrenceId,
|
|
360
|
+
canceledAt: rsvp.createdAt,
|
|
361
|
+
series: false,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
} else if (attendingResponse(seriesResponse.get(profileId))) {
|
|
365
|
+
records.push({
|
|
366
|
+
profile: rsvp.profile,
|
|
367
|
+
canceledAt: rsvp.createdAt,
|
|
368
|
+
series: recurring,
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (rsvp.recurrenceId) {
|
|
374
|
+
instanceResponse.set(
|
|
375
|
+
instanceRsvpKey(profileId, rsvp.recurrenceId),
|
|
376
|
+
rsvp.response,
|
|
377
|
+
);
|
|
378
|
+
} else {
|
|
379
|
+
seriesResponse.set(profileId, rsvp.response);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return records.reverse();
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
export const rsvpCancelWhenLabels = (
|
|
387
|
+
post: PublicPost,
|
|
388
|
+
notice: RsvpCancelNotice,
|
|
389
|
+
): { series: boolean; labels: string[] } => {
|
|
390
|
+
const event = post.data?.type === 'event' ? post.data : undefined;
|
|
391
|
+
if (!event || notice.series) return { series: notice.series, labels: [] };
|
|
392
|
+
const format = (start: string, end?: string) =>
|
|
393
|
+
formatEventWhen(start, {
|
|
394
|
+
end,
|
|
395
|
+
timeZone: event.timeZone,
|
|
396
|
+
allDay: event.wholeDay,
|
|
397
|
+
});
|
|
398
|
+
if (!notice.occurrenceIds.length) {
|
|
399
|
+
return { series: false, labels: [format(event.start, event.end)] };
|
|
400
|
+
}
|
|
401
|
+
return {
|
|
402
|
+
series: false,
|
|
403
|
+
labels: notice.occurrenceIds.map((id) => {
|
|
404
|
+
const times = effectiveEventTimes(event, id);
|
|
405
|
+
return format(times.start, times.end);
|
|
406
|
+
}),
|
|
407
|
+
};
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
export const instanceRsvpIdsForProfile = (
|
|
411
|
+
post: PublicPost,
|
|
412
|
+
profileId: string,
|
|
413
|
+
): string[] => {
|
|
414
|
+
const ids = new Set<string>();
|
|
415
|
+
for (const rsvp of post.rsvps ?? []) {
|
|
416
|
+
if (rsvp.profile.id !== profileId || !rsvp.recurrenceId) continue;
|
|
417
|
+
ids.add(normalizeRecurrenceId(rsvp.recurrenceId));
|
|
418
|
+
}
|
|
419
|
+
return [...ids];
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
export const overlaySeriesRsvpEntries = (
|
|
423
|
+
response: RSVP['response'],
|
|
424
|
+
instanceIds: string[],
|
|
425
|
+
): RSVP[] => [
|
|
426
|
+
{ response },
|
|
427
|
+
...instanceIds.map((recurrenceId) => ({ response, recurrenceId })),
|
|
428
|
+
];
|
|
429
|
+
|
|
430
|
+
export const seriesYesBlockedByCapacity = (
|
|
431
|
+
post: PublicPost,
|
|
432
|
+
profileId: string,
|
|
433
|
+
now = new Date(),
|
|
434
|
+
): boolean => {
|
|
435
|
+
const event = post.data?.type === 'event' ? post.data : undefined;
|
|
436
|
+
const maxAttendees = event?.maxAttendees;
|
|
437
|
+
if (!event || !maxAttendees || !event.recurrence) return false;
|
|
438
|
+
const occurrences = listRsvpOccurrences(event, now);
|
|
439
|
+
// When there are no upcoming occurrences (e.g. a live jam whose current
|
|
440
|
+
// occurrence started before `now`), fall back to the series-level count.
|
|
441
|
+
if (occurrences.length === 0) {
|
|
442
|
+
const current = getEffectiveRsvp(post, profileId);
|
|
443
|
+
if (
|
|
444
|
+
current?.response !== 'yes' &&
|
|
445
|
+
countSeriesYesRsvps(post) >= maxAttendees
|
|
446
|
+
) {
|
|
447
|
+
return true;
|
|
448
|
+
}
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
return occurrences.some((occurrence) => {
|
|
452
|
+
const current = getEffectiveRsvp(post, profileId, occurrence.recurrenceId);
|
|
453
|
+
if (current?.response === 'yes') return false;
|
|
454
|
+
return countYesRsvps(post, occurrence.recurrenceId) >= maxAttendees;
|
|
455
|
+
});
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
export const occurrenceHasCapacity = (
|
|
459
|
+
post: PublicPost,
|
|
460
|
+
recurrenceId?: string,
|
|
461
|
+
profileId?: string,
|
|
462
|
+
): boolean => {
|
|
463
|
+
const event = post.data?.type === 'event' ? post.data : undefined;
|
|
464
|
+
if (!event?.maxAttendees) return true;
|
|
465
|
+
if (profileId) {
|
|
466
|
+
const current = getEffectiveRsvp(post, profileId, recurrenceId);
|
|
467
|
+
if (current?.response === 'yes') return true;
|
|
468
|
+
}
|
|
469
|
+
return countYesRsvps(post, recurrenceId) < event.maxAttendees;
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
/** Series Register stays enabled while any upcoming date still has a seat. */
|
|
473
|
+
export const recurringEventHasOpenOccurrence = (
|
|
474
|
+
post: PublicPost,
|
|
475
|
+
profileId?: string,
|
|
476
|
+
now = new Date(),
|
|
477
|
+
): boolean => {
|
|
478
|
+
const event = post.data?.type === 'event' ? post.data : undefined;
|
|
479
|
+
if (!event) return false;
|
|
480
|
+
if (!event.recurrence) {
|
|
481
|
+
return occurrenceHasCapacity(post, undefined, profileId);
|
|
482
|
+
}
|
|
483
|
+
return listRsvpOccurrences(event, now).some((occurrence) =>
|
|
484
|
+
occurrenceHasCapacity(post, occurrence.recurrenceId, profileId),
|
|
485
|
+
);
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Series-page RSVP for the Register/Cancel CTA: prefer the default/next date,
|
|
490
|
+
* then any attending instance so a partial RSVP still shows Cancel.
|
|
491
|
+
*/
|
|
492
|
+
export const displayRsvpForProfile = (
|
|
493
|
+
post: PublicPost,
|
|
494
|
+
profileId: string,
|
|
495
|
+
options?: {
|
|
496
|
+
recurrenceId?: string;
|
|
497
|
+
lockToOccurrence?: boolean;
|
|
498
|
+
now?: Date;
|
|
499
|
+
},
|
|
500
|
+
): PublicRsvp | undefined => {
|
|
501
|
+
const event = post.data?.type === 'event' ? post.data : undefined;
|
|
502
|
+
const scopedId = options?.lockToOccurrence
|
|
503
|
+
? options.recurrenceId
|
|
504
|
+
: event
|
|
505
|
+
? defaultRsvpRecurrenceId(event, options?.recurrenceId, options?.now)
|
|
506
|
+
: options?.recurrenceId;
|
|
507
|
+
const scoped = getEffectiveRsvp(post, profileId, scopedId);
|
|
508
|
+
if (options?.lockToOccurrence) return scoped;
|
|
509
|
+
if (scoped && scoped.response !== 'no') return scoped;
|
|
510
|
+
if (event?.recurrence) {
|
|
511
|
+
for (const occurrence of listRsvpOccurrences(event, options?.now)) {
|
|
512
|
+
const instance = getEffectiveRsvp(
|
|
513
|
+
post,
|
|
514
|
+
profileId,
|
|
515
|
+
occurrence.recurrenceId,
|
|
516
|
+
);
|
|
517
|
+
if (instance && instance.response !== 'no') return instance;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
return scoped;
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
export const isCapacityEvent = (event: Event) => !!event.maxAttendees;
|
|
524
|
+
|
|
525
|
+
export const parseEventMaxAttendeesInput = (
|
|
526
|
+
raw: string,
|
|
527
|
+
): number | undefined => {
|
|
528
|
+
const trimmed = raw.trim();
|
|
529
|
+
if (!trimmed) return undefined;
|
|
530
|
+
const parsed = Number.parseInt(trimmed, 10);
|
|
531
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
export const withoutEventMaxAttendees = <T extends Event>(event: T): T => {
|
|
535
|
+
const { maxAttendees: _, ...rest } = event;
|
|
536
|
+
return rest as T;
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
export const normalizeEventDataForSave = <T extends Event>(event: T): T => {
|
|
540
|
+
const { maxAttendees } = event;
|
|
541
|
+
if (
|
|
542
|
+
maxAttendees == null ||
|
|
543
|
+
!Number.isFinite(maxAttendees) ||
|
|
544
|
+
maxAttendees < 1
|
|
545
|
+
) {
|
|
546
|
+
return withoutEventMaxAttendees(event);
|
|
547
|
+
}
|
|
548
|
+
return { ...event, maxAttendees: Math.trunc(maxAttendees) };
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
/** Null capacity means it was cleared; strip for validation/output. */
|
|
552
|
+
export const normalizeEventDataFromDb = <T extends Event>(event: T): T => {
|
|
553
|
+
if (event.maxAttendees != null) {
|
|
554
|
+
return event;
|
|
555
|
+
}
|
|
556
|
+
return withoutEventMaxAttendees(event);
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
export const normalizePostDataFromDb = (data: PostDataUnion): PostDataUnion =>
|
|
560
|
+
data.type === 'event' ? normalizeEventDataFromDb(data) : data;
|
|
561
|
+
|
|
562
|
+
/** Nested `data` updates send null to remove maxAttendees. */
|
|
563
|
+
export type EventDbUpdate = Omit<Event, 'maxAttendees'> & {
|
|
564
|
+
maxAttendees?: number | null;
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
export const eventDataForDbUpdate = (
|
|
568
|
+
previous: Event | undefined,
|
|
569
|
+
normalized: Event,
|
|
570
|
+
): EventDbUpdate => {
|
|
571
|
+
if (previous?.maxAttendees != null && !('maxAttendees' in normalized)) {
|
|
572
|
+
return { ...normalized, maxAttendees: null };
|
|
573
|
+
}
|
|
574
|
+
return normalized;
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
export const canManageEventRsvps = (
|
|
578
|
+
profile: Pick<PublicProfile, 'id'> | undefined,
|
|
579
|
+
post: PublicPost,
|
|
580
|
+
) =>
|
|
581
|
+
!!(
|
|
582
|
+
profile &&
|
|
583
|
+
(post.profile.id === profile.id || canModerateJam(profile, post))
|
|
584
|
+
);
|
|
585
|
+
|
|
586
|
+
export type JamCapacityJoinBlock =
|
|
587
|
+
| { blocked: false }
|
|
588
|
+
| { blocked: true; reason: 'full' | 'rsvp-required' | 'removed' };
|
|
589
|
+
|
|
590
|
+
export const getJamCapacityJoinBlock = (
|
|
591
|
+
post: PublicPost,
|
|
592
|
+
profile: Pick<PublicProfile, 'id'> | undefined,
|
|
593
|
+
recurrenceId?: string,
|
|
594
|
+
): JamCapacityJoinBlock => {
|
|
595
|
+
const event = post.data?.type === 'event' ? post.data : undefined;
|
|
596
|
+
if (!event?.maxAttendees || !profile?.id) {
|
|
597
|
+
return { blocked: false };
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
if (canModerateJam(profile, post)) {
|
|
601
|
+
return { blocked: false };
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
const myRsvp = getEffectiveRsvp(post, profile.id, recurrenceId);
|
|
605
|
+
if (myRsvp?.response === 'yes') {
|
|
606
|
+
return { blocked: false };
|
|
607
|
+
}
|
|
608
|
+
if (myRsvp?.response === 'removed') {
|
|
609
|
+
return { blocked: true, reason: 'removed' };
|
|
610
|
+
}
|
|
611
|
+
if (countYesRsvps(post, recurrenceId) >= event.maxAttendees) {
|
|
612
|
+
return { blocked: true, reason: 'full' };
|
|
613
|
+
}
|
|
614
|
+
return { blocked: true, reason: 'rsvp-required' };
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
export const canDeletePost = (
|
|
618
|
+
authData: AuthorizationData,
|
|
619
|
+
post: PublicPost,
|
|
620
|
+
config: CapabilitiesConfig,
|
|
621
|
+
) =>
|
|
622
|
+
checkPostCapabilities(authData, ['core-posts-delete'], post, config).success;
|
|
623
|
+
|
|
624
|
+
export const canCreatePost = (
|
|
625
|
+
authData: AuthorizationData,
|
|
626
|
+
type: PostType,
|
|
627
|
+
visibility: VisibilityType,
|
|
628
|
+
group?: GroupWithMeta,
|
|
629
|
+
) =>
|
|
630
|
+
visibility === 'group'
|
|
631
|
+
? !!group &&
|
|
632
|
+
checkGroupCapabilities(authData, [`core-posts-create-${type}`], group)
|
|
633
|
+
.success
|
|
634
|
+
: checkRoleCapabilities(authData.profile?.roles, [
|
|
635
|
+
`core-posts-create-${type}-${visibility}`,
|
|
636
|
+
]).success &&
|
|
637
|
+
scopeMatches({
|
|
638
|
+
scopes: authData.scopes,
|
|
639
|
+
requiredScope: { scopeLevel: 'write', resource: { type: 'posts' } },
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
export const canCreatePostTypeInAnyGroup = (
|
|
643
|
+
authData: AuthorizationData,
|
|
644
|
+
type: PostType,
|
|
645
|
+
) =>
|
|
646
|
+
scopeMatches({
|
|
647
|
+
scopes: authData.scopes,
|
|
648
|
+
requiredScope: { scopeLevel: 'write', resource: { type: 'groups' } },
|
|
649
|
+
}) &&
|
|
650
|
+
!!authData.profile?.memberships?.some(
|
|
651
|
+
(m) =>
|
|
652
|
+
checkGroupCapabilities(
|
|
653
|
+
authData,
|
|
654
|
+
[`core-posts-create-${type}`],
|
|
655
|
+
m.group as GroupWithMeta,
|
|
656
|
+
).success,
|
|
657
|
+
);
|
|
658
|
+
|
|
659
|
+
export const canCreatePostTypeWithVisibility = (
|
|
660
|
+
authData: AuthorizationData,
|
|
661
|
+
type: PostType,
|
|
662
|
+
visibility: VisibilityType,
|
|
663
|
+
) =>
|
|
664
|
+
visibility === 'group'
|
|
665
|
+
? canCreatePostTypeInAnyGroup(authData, type)
|
|
666
|
+
: checkRoleCapabilities(authData.profile?.roles, [
|
|
667
|
+
`core-posts-create-${type}-${visibility}`,
|
|
668
|
+
]).success &&
|
|
669
|
+
scopeMatches({
|
|
670
|
+
scopes: authData.scopes,
|
|
671
|
+
requiredScope: { scopeLevel: 'write', resource: { type: 'posts' } },
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
export const canCreatePostType = (
|
|
675
|
+
authData: AuthorizationData,
|
|
676
|
+
type: PostType,
|
|
677
|
+
) =>
|
|
678
|
+
visibilityTypeValues.some((visibility) =>
|
|
679
|
+
canCreatePostTypeWithVisibility(authData, type, visibility),
|
|
680
|
+
);
|
|
681
|
+
|
|
682
|
+
export type PostActionAvailability = {
|
|
683
|
+
canReply: boolean;
|
|
684
|
+
canRepost: boolean;
|
|
685
|
+
canReact: boolean;
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* Whether Reply / Repost / Like should look live. Non-members of a group
|
|
690
|
+
* post stay fully disabled (existing feed rule). Each action also follows
|
|
691
|
+
* the capability the matching write endpoint actually checks: feed replies
|
|
692
|
+
* and reposts need create-*, likes need core-posts-react.
|
|
693
|
+
*/
|
|
694
|
+
export const getPostActionAvailability = (
|
|
695
|
+
authData: AuthorizationData,
|
|
696
|
+
post: PublicPost,
|
|
697
|
+
config: CapabilitiesConfig,
|
|
698
|
+
): PostActionAvailability => {
|
|
699
|
+
const isGroupPost = !!post.groupId || !!post.group;
|
|
700
|
+
const membershipExists = !!authData.profile?.memberships?.some(
|
|
701
|
+
(m) => m.group.id === post.group?.id || m.group.id === post.groupId,
|
|
702
|
+
);
|
|
703
|
+
if (isGroupPost && !membershipExists) {
|
|
704
|
+
return { canReply: false, canRepost: false, canReact: false };
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
const canReply =
|
|
708
|
+
checkPostCapabilities(authData, ['core-posts-reply'], post, config)
|
|
709
|
+
.success &&
|
|
710
|
+
canCreatePost(authData, 'note', post.visibility, post.group ?? undefined);
|
|
711
|
+
|
|
712
|
+
const canReact = checkPostCapabilities(
|
|
713
|
+
authData,
|
|
714
|
+
['core-posts-react'],
|
|
715
|
+
post,
|
|
716
|
+
config,
|
|
717
|
+
).success;
|
|
718
|
+
|
|
719
|
+
const canRepost = post.group
|
|
720
|
+
? checkGroupCapabilities(
|
|
721
|
+
authData,
|
|
722
|
+
[`core-posts-create-${post.type}`],
|
|
723
|
+
post.group,
|
|
724
|
+
).success
|
|
725
|
+
: canCreatePost(authData, post.type, post.visibility);
|
|
726
|
+
|
|
727
|
+
return { canReply, canRepost, canReact };
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
/** Sentinel handle for block-hidden reply tombstones (no name/handle leak). */
|
|
731
|
+
export const HIDDEN_AUTHOR_HANDLE = 'hidden';
|
|
732
|
+
|
|
733
|
+
export const toHiddenPost = <
|
|
734
|
+
T extends { data: PostDataUnion; profile: PublicProfile },
|
|
735
|
+
>(
|
|
736
|
+
post: T,
|
|
737
|
+
): T & { hidden: true } =>
|
|
738
|
+
({
|
|
739
|
+
...post,
|
|
740
|
+
hidden: true,
|
|
741
|
+
data: {
|
|
742
|
+
...post.data,
|
|
743
|
+
...('content' in post.data ? { content: '' } : {}),
|
|
744
|
+
},
|
|
745
|
+
profile: {
|
|
746
|
+
...post.profile,
|
|
747
|
+
displayName: undefined,
|
|
748
|
+
handle: HIDDEN_AUTHOR_HANDLE,
|
|
749
|
+
avatar: null,
|
|
750
|
+
header: null,
|
|
751
|
+
bio: undefined,
|
|
752
|
+
},
|
|
753
|
+
}) as T & { hidden: true };
|
|
754
|
+
|
|
755
|
+
export const isHiddenPost = (post: { hidden?: boolean }): boolean =>
|
|
756
|
+
!!post.hidden;
|
|
757
|
+
|
|
758
|
+
const participantIds = (
|
|
759
|
+
post: Pick<PublicPost, 'audience' | 'profile'> & { creatorId?: string },
|
|
760
|
+
): Set<string> => {
|
|
761
|
+
const ids = new Set(
|
|
762
|
+
(post.audience ?? []).map((member) => member.id).filter(Boolean),
|
|
763
|
+
);
|
|
764
|
+
const creatorId = post.creatorId ?? post.profile?.id;
|
|
765
|
+
if (creatorId) ids.add(creatorId);
|
|
766
|
+
return ids;
|
|
767
|
+
};
|
|
768
|
+
|
|
769
|
+
export const isOneToOneWithBlocked = (
|
|
770
|
+
post: Pick<PublicPost, 'visibility' | 'audience' | 'profile'> & {
|
|
771
|
+
creatorId?: string;
|
|
772
|
+
},
|
|
773
|
+
viewer: { id: string } | undefined | null,
|
|
774
|
+
blockedIds?: Set<string> | string[],
|
|
775
|
+
): boolean => {
|
|
776
|
+
if (!viewer || post.visibility !== 'direct') return false;
|
|
777
|
+
const blocked =
|
|
778
|
+
blockedIds instanceof Set ? blockedIds : new Set(blockedIds ?? []);
|
|
779
|
+
if (!blocked.size) return false;
|
|
780
|
+
const others = participantIds(post);
|
|
781
|
+
others.delete(viewer.id);
|
|
782
|
+
return others.size === 1 && blocked.has([...others][0]!);
|
|
783
|
+
};
|