@openpeepshq/common 0.3.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/adminSections.d.ts +4 -0
- package/dist/lib/adminSections.js +5 -0
- package/dist/lib/attachmentHelpers.js +5 -1
- package/dist/lib/capabilitiesHelpers.d.ts +9 -0
- package/dist/lib/capabilitiesHelpers.js +9 -0
- package/dist/lib/csvHelpers.d.ts +2 -0
- package/dist/lib/csvHelpers.js +13 -0
- package/dist/lib/eventRecurrence.d.ts +11 -1
- package/dist/lib/eventRecurrence.js +23 -13
- package/dist/lib/eventWaitlist.d.ts +58 -0
- package/dist/lib/eventWaitlist.js +217 -0
- package/dist/lib/groupHelpers.d.ts +5 -0
- package/dist/lib/groupHelpers.js +16 -0
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +1 -0
- package/dist/lib/markdownHelpers.d.ts +12 -3
- package/dist/lib/markdownHelpers.js +115 -6
- package/dist/lib/postHelpers.d.ts +27 -5
- package/dist/lib/postHelpers.js +73 -11
- package/dist/lib/profileHelpers.d.ts +12 -2
- package/dist/lib/profileHelpers.js +19 -2
- package/dist/lib/resources.d.ts +29 -1
- package/dist/lib/resources.js +74 -2
- package/dist/lib/utils.js +11 -11
- package/dist/types/admin-livekit.d.ts +23 -0
- package/dist/types/admin-livekit.js +18 -0
- package/dist/types/admin-media.d.ts +41 -0
- package/dist/types/admin-media.js +20 -0
- package/dist/types/api.d.ts +19945 -3526
- package/dist/types/api.js +16 -10
- package/dist/types/capabilities.d.ts +2 -2
- package/dist/types/capabilities.js +3 -0
- package/dist/types/config.d.ts +15 -0
- package/dist/types/config.js +14 -2
- package/dist/types/db.d.ts +65 -22
- package/dist/types/forbiddenHandles.d.ts +28 -0
- package/dist/types/forbiddenHandles.js +75 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/internal.d.ts +163 -44
- package/dist/types/models.d.ts +46 -14
- package/dist/types/models.js +38 -28
- package/dist/types/notifications.d.ts +2 -0
- package/dist/types/notifications.js +4 -2
- package/dist/types/roleDefaults.js +109 -95
- package/dist/types/stats.d.ts +348 -108
- package/package.json +7 -6
- package/src/lib/adminSections.ts +5 -0
- package/src/lib/attachmentHelpers.ts +18 -12
- package/src/lib/capabilitiesHelpers.ts +15 -0
- package/src/lib/csvHelpers.ts +19 -3
- package/src/lib/eventRecurrence.ts +33 -19
- package/src/lib/eventWaitlist.ts +325 -0
- package/src/lib/groupHelpers.ts +25 -0
- package/src/lib/index.ts +1 -0
- package/src/lib/markdownHelpers.ts +121 -7
- package/src/lib/postHelpers.ts +102 -13
- package/src/lib/profileHelpers.ts +31 -2
- package/src/lib/resources.ts +105 -2
- package/src/lib/utils.ts +40 -31
- package/src/types/admin-livekit.ts +24 -0
- package/src/types/admin-media.ts +29 -0
- package/src/types/api.ts +20 -10
- package/src/types/auth.ts +2 -2
- package/src/types/capabilities.ts +3 -0
- package/src/types/config.ts +19 -10
- package/src/types/forbiddenHandles.ts +103 -0
- package/src/types/index.ts +2 -0
- package/src/types/models.ts +70 -33
- package/src/types/notifications.ts +7 -7
- package/src/types/roleDefaults.ts +115 -96
- package/src/types/stripe.ts +3 -1
|
@@ -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). */
|
package/src/lib/postHelpers.ts
CHANGED
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
} from './capabilitiesHelpers';
|
|
36
36
|
import { scopeMatches } from './scopeHelpers';
|
|
37
37
|
import { countBy, dateSorter, groupBy, transformValues } from './utils';
|
|
38
|
+
import { truncateText } from './text';
|
|
38
39
|
|
|
39
40
|
const buildThread = (rootPost: PublicPost, postList: PublicPost[]): Thread => ({
|
|
40
41
|
...rootPost,
|
|
@@ -436,8 +437,7 @@ export const seriesYesBlockedByCapacity = (
|
|
|
436
437
|
const maxAttendees = event?.maxAttendees;
|
|
437
438
|
if (!event || !maxAttendees || !event.recurrence) return false;
|
|
438
439
|
const occurrences = listRsvpOccurrences(event, now);
|
|
439
|
-
//
|
|
440
|
-
// occurrence started before `now`), fall back to the series-level count.
|
|
440
|
+
// No open date left (the current one has ended and nothing follows).
|
|
441
441
|
if (occurrences.length === 0) {
|
|
442
442
|
const current = getEffectiveRsvp(post, profileId);
|
|
443
443
|
if (
|
|
@@ -499,6 +499,11 @@ export const displayRsvpForProfile = (
|
|
|
499
499
|
},
|
|
500
500
|
): PublicRsvp | undefined => {
|
|
501
501
|
const event = post.data?.type === 'event' ? post.data : undefined;
|
|
502
|
+
// One-time events have no occurrences. Older claims stamped the start time
|
|
503
|
+
// onto some RSVPs; a newer unscoped waitlist must still be the current one.
|
|
504
|
+
if (event && !event.recurrence) {
|
|
505
|
+
return getEffectiveRsvp(post, profileId);
|
|
506
|
+
}
|
|
502
507
|
const scopedId = options?.lockToOccurrence
|
|
503
508
|
? options.recurrenceId
|
|
504
509
|
: event
|
|
@@ -522,6 +527,9 @@ export const displayRsvpForProfile = (
|
|
|
522
527
|
|
|
523
528
|
export const isCapacityEvent = (event: Event) => !!event.maxAttendees;
|
|
524
529
|
|
|
530
|
+
export const DEFAULT_WAITLIST_OFFER_MINUTES = 60;
|
|
531
|
+
export const MAX_WAITLIST_OFFER_MINUTES = 7 * 24 * 60;
|
|
532
|
+
|
|
525
533
|
export const parseEventMaxAttendeesInput = (
|
|
526
534
|
raw: string,
|
|
527
535
|
): number | undefined => {
|
|
@@ -531,8 +539,25 @@ export const parseEventMaxAttendeesInput = (
|
|
|
531
539
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
|
|
532
540
|
};
|
|
533
541
|
|
|
542
|
+
export const normalizeWaitlistOfferMinutes = (
|
|
543
|
+
value: number | null | undefined,
|
|
544
|
+
): number | undefined => {
|
|
545
|
+
if (value == null || !Number.isFinite(value)) return undefined;
|
|
546
|
+
const minutes = Math.trunc(value);
|
|
547
|
+
if (minutes < 1 || minutes > MAX_WAITLIST_OFFER_MINUTES) return undefined;
|
|
548
|
+
return minutes;
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
export const parseEventWaitlistOfferMinutesInput = (
|
|
552
|
+
raw: string,
|
|
553
|
+
): number | undefined => {
|
|
554
|
+
const trimmed = raw.trim();
|
|
555
|
+
if (!trimmed) return undefined;
|
|
556
|
+
return normalizeWaitlistOfferMinutes(Number.parseInt(trimmed, 10));
|
|
557
|
+
};
|
|
558
|
+
|
|
534
559
|
export const withoutEventMaxAttendees = <T extends Event>(event: T): T => {
|
|
535
|
-
const { maxAttendees: _, ...rest } = event;
|
|
560
|
+
const { maxAttendees: _, waitlistOfferMinutes: __, ...rest } = event;
|
|
536
561
|
return rest as T;
|
|
537
562
|
};
|
|
538
563
|
|
|
@@ -545,33 +570,52 @@ export const normalizeEventDataForSave = <T extends Event>(event: T): T => {
|
|
|
545
570
|
) {
|
|
546
571
|
return withoutEventMaxAttendees(event);
|
|
547
572
|
}
|
|
548
|
-
|
|
573
|
+
const minutes = normalizeWaitlistOfferMinutes(event.waitlistOfferMinutes);
|
|
574
|
+
const { waitlistOfferMinutes: _, ...rest } = event;
|
|
575
|
+
return {
|
|
576
|
+
...rest,
|
|
577
|
+
maxAttendees: Math.trunc(maxAttendees),
|
|
578
|
+
...(minutes != null ? { waitlistOfferMinutes: minutes } : {}),
|
|
579
|
+
} as T;
|
|
549
580
|
};
|
|
550
581
|
|
|
551
582
|
/** Null capacity means it was cleared; strip for validation/output. */
|
|
552
583
|
export const normalizeEventDataFromDb = <T extends Event>(event: T): T => {
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
584
|
+
const { maxAttendees, waitlistOfferMinutes, ...rest } = event;
|
|
585
|
+
return {
|
|
586
|
+
...rest,
|
|
587
|
+
...(maxAttendees != null ? { maxAttendees } : {}),
|
|
588
|
+
...(waitlistOfferMinutes != null ? { waitlistOfferMinutes } : {}),
|
|
589
|
+
} as T;
|
|
557
590
|
};
|
|
558
591
|
|
|
559
592
|
export const normalizePostDataFromDb = (data: PostDataUnion): PostDataUnion =>
|
|
560
593
|
data.type === 'event' ? normalizeEventDataFromDb(data) : data;
|
|
561
594
|
|
|
562
|
-
/** Nested `data` updates send null to remove
|
|
563
|
-
export type EventDbUpdate = Omit<
|
|
595
|
+
/** Nested `data` updates send null to remove cleared capacity fields. */
|
|
596
|
+
export type EventDbUpdate = Omit<
|
|
597
|
+
Event,
|
|
598
|
+
'maxAttendees' | 'waitlistOfferMinutes'
|
|
599
|
+
> & {
|
|
564
600
|
maxAttendees?: number | null;
|
|
601
|
+
waitlistOfferMinutes?: number | null;
|
|
565
602
|
};
|
|
566
603
|
|
|
567
604
|
export const eventDataForDbUpdate = (
|
|
568
605
|
previous: Event | undefined,
|
|
569
606
|
normalized: Event,
|
|
570
607
|
): EventDbUpdate => {
|
|
608
|
+
let next: EventDbUpdate = normalized;
|
|
571
609
|
if (previous?.maxAttendees != null && !('maxAttendees' in normalized)) {
|
|
572
|
-
|
|
610
|
+
next = { ...next, maxAttendees: null };
|
|
611
|
+
}
|
|
612
|
+
if (
|
|
613
|
+
previous?.waitlistOfferMinutes != null &&
|
|
614
|
+
!('waitlistOfferMinutes' in normalized)
|
|
615
|
+
) {
|
|
616
|
+
next = { ...next, waitlistOfferMinutes: null };
|
|
573
617
|
}
|
|
574
|
-
return
|
|
618
|
+
return next;
|
|
575
619
|
};
|
|
576
620
|
|
|
577
621
|
export const canManageEventRsvps = (
|
|
@@ -611,6 +655,16 @@ export const getJamCapacityJoinBlock = (
|
|
|
611
655
|
if (countYesRsvps(post, recurrenceId) >= event.maxAttendees) {
|
|
612
656
|
return { blocked: true, reason: 'full' };
|
|
613
657
|
}
|
|
658
|
+
// A freed seat stays reserved for the waitlist unless this profile holds the offer.
|
|
659
|
+
const waiting = calculateEffectiveRsvps(post, recurrenceId).filter(
|
|
660
|
+
(rsvp) => rsvp.response === 'waitlist' && rsvp.profile.type !== 'federated',
|
|
661
|
+
);
|
|
662
|
+
const mine = waiting.find((rsvp) => rsvp.profile.id === profile.id);
|
|
663
|
+
const offerOpen =
|
|
664
|
+
!!mine?.offerExpiresAt && Date.parse(mine.offerExpiresAt) > Date.now();
|
|
665
|
+
if (waiting.length > 0 && !offerOpen) {
|
|
666
|
+
return { blocked: true, reason: 'full' };
|
|
667
|
+
}
|
|
614
668
|
return { blocked: true, reason: 'rsvp-required' };
|
|
615
669
|
};
|
|
616
670
|
|
|
@@ -755,7 +809,7 @@ export const toHiddenPost = <
|
|
|
755
809
|
export const isHiddenPost = (post: { hidden?: boolean }): boolean =>
|
|
756
810
|
!!post.hidden;
|
|
757
811
|
|
|
758
|
-
const participantIds = (
|
|
812
|
+
export const participantIds = (
|
|
759
813
|
post: Pick<PublicPost, 'audience' | 'profile'> & { creatorId?: string },
|
|
760
814
|
): Set<string> => {
|
|
761
815
|
const ids = new Set(
|
|
@@ -766,6 +820,13 @@ const participantIds = (
|
|
|
766
820
|
return ids;
|
|
767
821
|
};
|
|
768
822
|
|
|
823
|
+
/** Direct message between exactly two participants. */
|
|
824
|
+
export const isOneToOneDm = (
|
|
825
|
+
post: Pick<PublicPost, 'visibility' | 'audience' | 'profile'> & {
|
|
826
|
+
creatorId?: string;
|
|
827
|
+
},
|
|
828
|
+
): boolean => post.visibility === 'direct' && participantIds(post).size === 2;
|
|
829
|
+
|
|
769
830
|
export const isOneToOneWithBlocked = (
|
|
770
831
|
post: Pick<PublicPost, 'visibility' | 'audience' | 'profile'> & {
|
|
771
832
|
creatorId?: string;
|
|
@@ -781,3 +842,31 @@ export const isOneToOneWithBlocked = (
|
|
|
781
842
|
others.delete(viewer.id);
|
|
782
843
|
return others.size === 1 && blocked.has([...others][0]!);
|
|
783
844
|
};
|
|
845
|
+
|
|
846
|
+
/** First usable text field of a post, whitespace-collapsed and truncated. */
|
|
847
|
+
export const messagePreviewText = (
|
|
848
|
+
message: PublicPost,
|
|
849
|
+
maxLength = 160,
|
|
850
|
+
): string => {
|
|
851
|
+
const data = message.data;
|
|
852
|
+
if (!data || typeof data !== 'object') return '';
|
|
853
|
+
const text =
|
|
854
|
+
'content' in data && typeof data.content === 'string' && data.content
|
|
855
|
+
? data.content
|
|
856
|
+
: 'name' in data && typeof data.name === 'string'
|
|
857
|
+
? data.name
|
|
858
|
+
: 'title' in data && typeof data.title === 'string'
|
|
859
|
+
? data.title
|
|
860
|
+
: '';
|
|
861
|
+
return truncateText(text.replace(/\s+/g, ' ').trim(), maxLength);
|
|
862
|
+
};
|
|
863
|
+
|
|
864
|
+
/** Display names of a post's audience minus the viewer, comma-joined. */
|
|
865
|
+
export const conversationLabel = (
|
|
866
|
+
message: PublicPost,
|
|
867
|
+
viewerId?: string,
|
|
868
|
+
): string =>
|
|
869
|
+
(message.audience ?? [])
|
|
870
|
+
.filter((profile) => profile.id !== viewerId)
|
|
871
|
+
.map((profile) => profile.displayName || `@${profile.handle}`)
|
|
872
|
+
.join(', ');
|
|
@@ -12,8 +12,8 @@ const containsIgnoreCase = (candidate: string, query: string) =>
|
|
|
12
12
|
|
|
13
13
|
export const PROFILE_DISPLAY_NAME_MAX_LENGTH = 30;
|
|
14
14
|
|
|
15
|
-
/** Public sentinel shown for soft-deleted
|
|
16
|
-
export const DELETED_AUTHOR_DISPLAY_NAME = 'Deleted
|
|
15
|
+
/** Public sentinel shown for soft-deleted profiles (API + clients). */
|
|
16
|
+
export const DELETED_AUTHOR_DISPLAY_NAME = 'Deleted Profile';
|
|
17
17
|
export const DELETED_AUTHOR_HANDLE = 'deleted';
|
|
18
18
|
|
|
19
19
|
export const clampProfileDisplayName = (
|
|
@@ -28,6 +28,35 @@ export const isDeletedProfile = (
|
|
|
28
28
|
profile: { deletedAt?: string | null } | undefined | null,
|
|
29
29
|
): boolean => !!profile?.deletedAt;
|
|
30
30
|
|
|
31
|
+
// `handle`/`displayName` arrive from untyped DB rows (`rowToModel`),
|
|
32
|
+
// so the hydration helper must accept `unknown`, not just `string | null`.
|
|
33
|
+
type DeletedProfileIdentity = {
|
|
34
|
+
deletedAt?: string | null;
|
|
35
|
+
displayName?: unknown;
|
|
36
|
+
handle?: unknown;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Deleted rows keep their original handle (the column is unique) and may have
|
|
41
|
+
* no display name. Public hydration fills in the sentinel when it is missing.
|
|
42
|
+
*/
|
|
43
|
+
export const withDeletedProfileIdentity = <T extends DeletedProfileIdentity>(
|
|
44
|
+
profile: T,
|
|
45
|
+
): T => {
|
|
46
|
+
if (!isDeletedProfile(profile)) return profile;
|
|
47
|
+
if (
|
|
48
|
+
profile.displayName === DELETED_AUTHOR_DISPLAY_NAME &&
|
|
49
|
+
profile.handle === DELETED_AUTHOR_HANDLE
|
|
50
|
+
) {
|
|
51
|
+
return profile;
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
...profile,
|
|
55
|
+
displayName: DELETED_AUTHOR_DISPLAY_NAME,
|
|
56
|
+
handle: DELETED_AUTHOR_HANDLE,
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
31
60
|
/**
|
|
32
61
|
* Strip PII from a soft-deleted profile for public API / UI surfaces.
|
|
33
62
|
* Keeps id + timestamps (including deletedAt) so clients can detect the state.
|
package/src/lib/resources.ts
CHANGED
|
@@ -1,4 +1,22 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import {
|
|
3
|
+
RESOURCE_TITLE_MAX_LENGTH,
|
|
4
|
+
visibilityTypeValues,
|
|
5
|
+
type MediaAttachmentData,
|
|
6
|
+
type ResourceKind,
|
|
7
|
+
type VisibilityType,
|
|
8
|
+
} from '../types/models';
|
|
9
|
+
|
|
10
|
+
export { RESOURCE_TITLE_MAX_LENGTH };
|
|
11
|
+
|
|
12
|
+
/** Matches `resourcePostSchema.url` (`z.url()`), after trimming. */
|
|
13
|
+
const resourceUrlSchema = z.url();
|
|
14
|
+
|
|
15
|
+
export const isValidResourceUrl = (url: string | undefined): boolean => {
|
|
16
|
+
const trimmed = url?.trim() ?? '';
|
|
17
|
+
if (!trimmed) return false;
|
|
18
|
+
return resourceUrlSchema.safeParse(trimmed).success;
|
|
19
|
+
};
|
|
2
20
|
|
|
3
21
|
export const RESOURCE_KINDS = [
|
|
4
22
|
'video',
|
|
@@ -24,7 +42,9 @@ export const resourceHasRequiredMedia = (resource: {
|
|
|
24
42
|
url?: string;
|
|
25
43
|
attachments?: MediaAttachmentData[];
|
|
26
44
|
}): boolean => {
|
|
27
|
-
if (resource.resourceKind === 'link')
|
|
45
|
+
if (resource.resourceKind === 'link') {
|
|
46
|
+
return isValidResourceUrl(resource.url);
|
|
47
|
+
}
|
|
28
48
|
const need =
|
|
29
49
|
resource.resourceKind === 'gallery'
|
|
30
50
|
? 'image'
|
|
@@ -38,6 +58,89 @@ export const resourceHasRequiredMedia = (resource: {
|
|
|
38
58
|
);
|
|
39
59
|
};
|
|
40
60
|
|
|
61
|
+
export const RESOURCE_FORM_ISSUES = [
|
|
62
|
+
'title',
|
|
63
|
+
'titleLength',
|
|
64
|
+
'url',
|
|
65
|
+
'media',
|
|
66
|
+
'audience',
|
|
67
|
+
'group',
|
|
68
|
+
] as const;
|
|
69
|
+
|
|
70
|
+
export type ResourceFormIssue = (typeof RESOURCE_FORM_ISSUES)[number];
|
|
71
|
+
|
|
72
|
+
export const resourceFormIssues = (input: {
|
|
73
|
+
title?: string;
|
|
74
|
+
resourceKind: ResourceKind;
|
|
75
|
+
url?: string;
|
|
76
|
+
attachments?: MediaAttachmentData[];
|
|
77
|
+
visibility?: string | null;
|
|
78
|
+
audience?: readonly unknown[] | null;
|
|
79
|
+
groupId?: string | null;
|
|
80
|
+
}): ResourceFormIssue[] => {
|
|
81
|
+
const issues: ResourceFormIssue[] = [];
|
|
82
|
+
if (!input.title?.trim()) issues.push('title');
|
|
83
|
+
else if (input.title.length > RESOURCE_TITLE_MAX_LENGTH) {
|
|
84
|
+
issues.push('titleLength');
|
|
85
|
+
}
|
|
86
|
+
if (input.resourceKind === 'link') {
|
|
87
|
+
if (!isValidResourceUrl(input.url)) issues.push('url');
|
|
88
|
+
} else if (!resourceHasRequiredMedia(input)) {
|
|
89
|
+
issues.push('media');
|
|
90
|
+
}
|
|
91
|
+
if (input.visibility === 'direct' && !input.audience?.length) {
|
|
92
|
+
issues.push('audience');
|
|
93
|
+
}
|
|
94
|
+
if (input.visibility === 'group' && !input.groupId) {
|
|
95
|
+
issues.push('group');
|
|
96
|
+
}
|
|
97
|
+
return issues;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const isVisibility = (
|
|
101
|
+
value: string | null | undefined,
|
|
102
|
+
): value is VisibilityType =>
|
|
103
|
+
!!value && (visibilityTypeValues as readonly string[]).includes(value);
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Draft visibility the profile may actually post with. A group-only author
|
|
107
|
+
* starts on Specific Group; a single postable group is preselected. Pass
|
|
108
|
+
* `postableGroupIds: null` while memberships are still loading so an existing
|
|
109
|
+
* group choice is not cleared.
|
|
110
|
+
*/
|
|
111
|
+
export const resourceVisibilityForCapabilities = (input: {
|
|
112
|
+
visibility?: string | null;
|
|
113
|
+
groupId?: string | null;
|
|
114
|
+
allowed: readonly VisibilityType[];
|
|
115
|
+
postableGroupIds: readonly string[] | null;
|
|
116
|
+
}): { visibility: VisibilityType; groupId?: string } => {
|
|
117
|
+
const current = isVisibility(input.visibility) ? input.visibility : 'local';
|
|
118
|
+
const fallback = input.allowed[0];
|
|
119
|
+
if (!fallback) {
|
|
120
|
+
return { visibility: current, groupId: input.groupId ?? undefined };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const keep = input.allowed.includes(current) ? current : fallback;
|
|
124
|
+
if (keep !== 'group') return { visibility: keep };
|
|
125
|
+
|
|
126
|
+
if (input.postableGroupIds == null) {
|
|
127
|
+
return { visibility: 'group', groupId: input.groupId ?? undefined };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const kept =
|
|
131
|
+
input.groupId && input.postableGroupIds.includes(input.groupId)
|
|
132
|
+
? input.groupId
|
|
133
|
+
: undefined;
|
|
134
|
+
return {
|
|
135
|
+
visibility: 'group',
|
|
136
|
+
groupId:
|
|
137
|
+
kept ??
|
|
138
|
+
(input.postableGroupIds.length === 1
|
|
139
|
+
? input.postableGroupIds[0]
|
|
140
|
+
: undefined),
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
41
144
|
export type CategoryNode = {
|
|
42
145
|
name: string;
|
|
43
146
|
path: string[];
|
package/src/lib/utils.ts
CHANGED
|
@@ -64,8 +64,8 @@ export const asyncFilter = async <T>(
|
|
|
64
64
|
|
|
65
65
|
export const passThroughUndefined =
|
|
66
66
|
<Input, Output>(func: (input: Input) => Output) =>
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
(input: Input | undefined) =>
|
|
68
|
+
input && func(input);
|
|
69
69
|
|
|
70
70
|
export const countBy = <T, V extends string | number | symbol>(
|
|
71
71
|
arr: T[],
|
|
@@ -93,8 +93,8 @@ export const groupBy = <T, V extends string | number | symbol>(
|
|
|
93
93
|
|
|
94
94
|
export const dateSorter =
|
|
95
95
|
<T extends { createdAt: string }>(): ((v0: T, v1: T) => number) =>
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
(v0: T, v1: T) =>
|
|
97
|
+
Date.parse(v0.createdAt) - Date.parse(v1.createdAt);
|
|
98
98
|
|
|
99
99
|
export const getUniqueBy = <T, V = string>(
|
|
100
100
|
arr: T[],
|
|
@@ -135,11 +135,11 @@ export const deepSet = <T>(
|
|
|
135
135
|
const keys = Array.isArray(path)
|
|
136
136
|
? path
|
|
137
137
|
: path
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
138
|
+
.replace(/(\w)\[/g, '$1.[')
|
|
139
|
+
.split('.')
|
|
140
|
+
.map((k) =>
|
|
141
|
+
k.includes('[') ? parseInt(k.substring(1, k.length - 1)) : k,
|
|
142
|
+
);
|
|
143
143
|
|
|
144
144
|
let subObject: unknown = obj;
|
|
145
145
|
for (let i = 0; i < keys.length; i++) {
|
|
@@ -150,9 +150,9 @@ export const deepSet = <T>(
|
|
|
150
150
|
// @ts-expect-error deepset is not really typeable
|
|
151
151
|
subObject[currentKey] = subObject[currentKey]
|
|
152
152
|
? // @ts-expect-error deepset is not really typeable
|
|
153
|
-
|
|
153
|
+
subObject[currentKey]
|
|
154
154
|
: // @ts-expect-error deepset is not really typeable
|
|
155
|
-
|
|
155
|
+
isNaN(nextKey)
|
|
156
156
|
? {}
|
|
157
157
|
: [];
|
|
158
158
|
} else {
|
|
@@ -203,35 +203,41 @@ export const formatTimeStamp = (
|
|
|
203
203
|
return new Date(timestamp * 1000).toLocaleDateString();
|
|
204
204
|
};
|
|
205
205
|
|
|
206
|
-
const getMode = (
|
|
206
|
+
const getMode = (
|
|
207
|
+
config: CommunityConfig,
|
|
208
|
+
profileSettings: ProfileSettings | undefined,
|
|
209
|
+
systemTheme: 'dark' | 'light',
|
|
210
|
+
): 'dark' | 'light' => {
|
|
207
211
|
if (profileSettings) {
|
|
208
|
-
if (profileSettings.theme ===
|
|
212
|
+
if (profileSettings.theme === 'system') {
|
|
209
213
|
return systemTheme;
|
|
210
|
-
} else if (profileSettings?.theme ===
|
|
211
|
-
return
|
|
212
|
-
} else if (profileSettings?.theme ===
|
|
213
|
-
return
|
|
214
|
+
} else if (profileSettings?.theme === 'dark') {
|
|
215
|
+
return 'dark';
|
|
216
|
+
} else if (profileSettings?.theme === 'light') {
|
|
217
|
+
return 'light';
|
|
214
218
|
}
|
|
215
219
|
}
|
|
216
220
|
return config.theme.base === 'OpenpeepsDark' ? 'dark' : 'light';
|
|
217
|
-
}
|
|
221
|
+
};
|
|
218
222
|
|
|
219
223
|
export const getTheme = (
|
|
220
224
|
config: CommunityConfig,
|
|
221
225
|
profileSettings?: ProfileSettings,
|
|
222
|
-
systemTheme: 'dark' | 'light' = getSystemTheme()
|
|
226
|
+
systemTheme: 'dark' | 'light' = getSystemTheme(),
|
|
223
227
|
) =>
|
|
224
|
-
getMode(config, profileSettings, systemTheme) === 'dark'
|
|
225
|
-
{ ...config.theme.dark, dark: true }
|
|
226
|
-
{ ...config.theme.light, dark: false };
|
|
228
|
+
getMode(config, profileSettings, systemTheme) === 'dark'
|
|
229
|
+
? { ...config.theme.dark, dark: true }
|
|
230
|
+
: { ...config.theme.light, dark: false };
|
|
227
231
|
|
|
228
232
|
export const getSystemTheme = () => {
|
|
229
|
-
if (typeof window !==
|
|
230
|
-
const systemIsDark = window.matchMedia(
|
|
231
|
-
|
|
233
|
+
if (typeof window !== 'undefined' && window.matchMedia) {
|
|
234
|
+
const systemIsDark = window.matchMedia(
|
|
235
|
+
'(prefers-color-scheme: dark)',
|
|
236
|
+
).matches;
|
|
237
|
+
return systemIsDark ? 'dark' : 'light';
|
|
232
238
|
}
|
|
233
239
|
return 'light';
|
|
234
|
-
}
|
|
240
|
+
};
|
|
235
241
|
|
|
236
242
|
export const getProfileAvatar = (
|
|
237
243
|
profile: PublicProfile | undefined | null,
|
|
@@ -252,7 +258,8 @@ export const getGroupAvatar = (
|
|
|
252
258
|
};
|
|
253
259
|
|
|
254
260
|
export const extractDateFromUUIDv7 = (uuidV7: string): string => {
|
|
255
|
-
const UUID_REGEX =
|
|
261
|
+
const UUID_REGEX =
|
|
262
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
256
263
|
if (!UUID_REGEX.test(uuidV7)) {
|
|
257
264
|
throw new Error('Invalid UUID format');
|
|
258
265
|
}
|
|
@@ -271,13 +278,15 @@ export const extractDateFromUUIDv7 = (uuidV7: string): string => {
|
|
|
271
278
|
|
|
272
279
|
const pad = (num: number, size: number): string => {
|
|
273
280
|
let s = String(num);
|
|
274
|
-
while (s.length < size) {
|
|
281
|
+
while (s.length < size) {
|
|
282
|
+
s = '0' + s;
|
|
283
|
+
}
|
|
275
284
|
return s;
|
|
276
285
|
};
|
|
277
286
|
|
|
278
287
|
return (
|
|
279
288
|
date.getUTCFullYear().toString() +
|
|
280
|
-
pad(date.getUTCMonth() + 1, 2) +
|
|
289
|
+
pad(date.getUTCMonth() + 1, 2) + // months are zero‑based
|
|
281
290
|
pad(date.getUTCDate(), 2) +
|
|
282
291
|
pad(date.getUTCHours(), 2) +
|
|
283
292
|
pad(date.getUTCMinutes(), 2)
|
|
@@ -292,7 +301,7 @@ export const formatSize = (bytes: number) => {
|
|
|
292
301
|
|
|
293
302
|
export const hasVisibilityChanged = (
|
|
294
303
|
previousAudienceSetting: AudienceSetting,
|
|
295
|
-
newAudienceSetting: AudienceSetting
|
|
304
|
+
newAudienceSetting: AudienceSetting,
|
|
296
305
|
): boolean => {
|
|
297
306
|
if (previousAudienceSetting.visibility !== newAudienceSetting.visibility) {
|
|
298
307
|
return true;
|
|
@@ -319,4 +328,4 @@ export const hasVisibilityChanged = (
|
|
|
319
328
|
}
|
|
320
329
|
|
|
321
330
|
return false;
|
|
322
|
-
};
|
|
331
|
+
};
|