@openpeepshq/common 0.1.45 → 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.
Files changed (126) hide show
  1. package/dist/lib/adminSections.js +1 -1
  2. package/dist/lib/capabilitiesHelpers.d.ts +9 -7
  3. package/dist/lib/capabilitiesHelpers.js +43 -38
  4. package/dist/lib/csvHelpers.js +1 -1
  5. package/dist/lib/eventIcs.d.ts +17 -0
  6. package/dist/lib/eventIcs.js +91 -15
  7. package/dist/lib/eventRecurrence.d.ts +19 -0
  8. package/dist/lib/eventRecurrence.js +82 -20
  9. package/dist/lib/eventTime.d.ts +7 -0
  10. package/dist/lib/eventTime.js +23 -9
  11. package/dist/lib/federationHosts.d.ts +3 -0
  12. package/dist/lib/federationHosts.js +17 -0
  13. package/dist/lib/feedCursor.d.ts +14 -0
  14. package/dist/lib/feedCursor.js +22 -0
  15. package/dist/lib/feedFormat.d.ts +12 -0
  16. package/dist/lib/feedFormat.js +14 -0
  17. package/dist/lib/groupHelpers.js +13 -34
  18. package/dist/lib/groupTemplateHelpers.js +6 -10
  19. package/dist/lib/index.d.ts +7 -0
  20. package/dist/lib/index.js +7 -0
  21. package/dist/lib/jamHelpers.d.ts +11 -2
  22. package/dist/lib/jamHelpers.js +43 -9
  23. package/dist/lib/legacyThemeRoot.d.ts +3 -0
  24. package/dist/lib/legacyThemeRoot.js +10 -0
  25. package/dist/lib/markdownHelpers.d.ts +8 -0
  26. package/dist/lib/markdownHelpers.js +34 -6
  27. package/dist/lib/postHelpers.d.ts +142 -3
  28. package/dist/lib/postHelpers.js +323 -47
  29. package/dist/lib/profileHelpers.d.ts +11 -0
  30. package/dist/lib/profileHelpers.js +34 -10
  31. package/dist/lib/pushHelpers.js +3 -4
  32. package/dist/lib/reactions.d.ts +17 -0
  33. package/dist/lib/reactions.js +37 -0
  34. package/dist/lib/scopeHelpers.d.ts +31 -2
  35. package/dist/lib/scopeHelpers.js +39 -8
  36. package/dist/lib/ssoLoginUrl.js +4 -6
  37. package/dist/lib/ssoProviders.d.ts +72 -0
  38. package/dist/lib/ssoProviders.js +366 -0
  39. package/dist/lib/timeZone.d.ts +9 -0
  40. package/dist/lib/timeZone.js +44 -0
  41. package/dist/lib/tokenHelpers.js +5 -5
  42. package/dist/lib/utils.js +6 -7
  43. package/dist/types/admin-db.js +1 -1
  44. package/dist/types/analytics.d.ts +20 -0
  45. package/dist/types/analytics.js +4 -0
  46. package/dist/types/api.d.ts +41800 -11853
  47. package/dist/types/api.js +53 -5
  48. package/dist/types/auth.d.ts +20 -6
  49. package/dist/types/capabilities.d.ts +15 -1
  50. package/dist/types/capabilities.js +51 -0
  51. package/dist/types/config.d.ts +99 -0
  52. package/dist/types/config.js +56 -4
  53. package/dist/types/db.d.ts +1155 -164
  54. package/dist/types/db.js +14 -0
  55. package/dist/types/export.d.ts +120 -0
  56. package/dist/types/export.js +7 -0
  57. package/dist/types/index.d.ts +2 -0
  58. package/dist/types/index.js +2 -0
  59. package/dist/types/internal.d.ts +2577 -524
  60. package/dist/types/internal.js +6 -0
  61. package/dist/types/models.d.ts +63 -7
  62. package/dist/types/models.js +40 -3
  63. package/dist/types/plugins.d.ts +22 -0
  64. package/dist/types/plugins.js +66 -0
  65. package/dist/types/roleDefaults.d.ts +31 -0
  66. package/dist/types/roleDefaults.js +110 -0
  67. package/dist/types/stats.d.ts +54606 -6065
  68. package/dist/types/stats.js +30 -0
  69. package/dist/types/utils.d.ts +2 -0
  70. package/dist/types/utils.js +1 -3
  71. package/dist/zod.d.ts +1 -0
  72. package/dist/zod.js +1 -0
  73. package/package.json +36 -6
  74. package/src/index.ts +4 -0
  75. package/src/lib/adminSections.ts +84 -0
  76. package/src/lib/attachmentHelpers.ts +14 -0
  77. package/src/lib/capabilitiesHelpers.ts +532 -0
  78. package/src/lib/colors.ts +20 -0
  79. package/src/lib/csvHelpers.ts +31 -0
  80. package/src/lib/eventIcs.ts +265 -0
  81. package/src/lib/eventRecurrence.ts +495 -0
  82. package/src/lib/eventTime.ts +206 -0
  83. package/src/lib/federationHosts.ts +22 -0
  84. package/src/lib/feedCursor.ts +27 -0
  85. package/src/lib/feedFormat.ts +24 -0
  86. package/src/lib/groupHelpers.ts +421 -0
  87. package/src/lib/groupTemplateHelpers.ts +83 -0
  88. package/src/lib/index.ts +29 -0
  89. package/src/lib/jamHelpers.ts +164 -0
  90. package/src/lib/legacyThemeRoot.ts +14 -0
  91. package/src/lib/markdownHelpers.ts +122 -0
  92. package/src/lib/mediaDimensions.ts +21 -0
  93. package/src/lib/postHelpers.ts +783 -0
  94. package/src/lib/profileHelpers.ts +220 -0
  95. package/src/lib/pushHelpers.ts +34 -0
  96. package/src/lib/reactions.ts +52 -0
  97. package/src/lib/reportHelpers.ts +7 -0
  98. package/src/lib/scopeHelpers.ts +176 -0
  99. package/src/lib/ssoLoginUrl.ts +93 -0
  100. package/src/lib/ssoProviders.ts +472 -0
  101. package/src/lib/text.ts +26 -0
  102. package/src/lib/timeZone.ts +52 -0
  103. package/src/lib/tokenHelpers.ts +119 -0
  104. package/src/lib/utils.ts +322 -0
  105. package/src/types/admin-db.ts +74 -0
  106. package/src/types/admin-email.ts +55 -0
  107. package/src/types/admin-jobs.ts +17 -0
  108. package/src/types/admin-performance.ts +20 -0
  109. package/src/types/analytics.ts +318 -0
  110. package/src/types/api.ts +871 -0
  111. package/src/types/apple-app-site-association.ts +54 -0
  112. package/src/types/auth.ts +22 -0
  113. package/src/types/capabilities.ts +280 -0
  114. package/src/types/config.ts +546 -0
  115. package/src/types/db.ts +138 -0
  116. package/src/types/export.ts +10 -0
  117. package/src/types/index.ts +20 -0
  118. package/src/types/internal.ts +338 -0
  119. package/src/types/models.ts +939 -0
  120. package/src/types/notifications.ts +92 -0
  121. package/src/types/plugins.ts +150 -0
  122. package/src/types/roleDefaults.ts +132 -0
  123. package/src/types/stats.ts +195 -0
  124. package/src/types/stripe.ts +45 -0
  125. package/src/types/utils.ts +100 -0
  126. package/src/zod.ts +1 -0
@@ -0,0 +1,265 @@
1
+ import ical, {
2
+ ICalCalendarMethod,
3
+ ICalEventRepeatingFreq,
4
+ ICalWeekday,
5
+ } from 'ical-generator';
6
+ import type { Event, PublicPost, RecurrenceWeekday } from '../types';
7
+ import { effectiveEventTimes } from './eventRecurrence';
8
+ import { getJamUrl } from './jamHelpers';
9
+
10
+ const utcDateFromYmd = (yyyyMmDd: string): Date => {
11
+ const [y, m, d] = yyyyMmDd.split('-').map(Number);
12
+ return new Date(Date.UTC(y, m - 1, d));
13
+ };
14
+
15
+ const exclusiveEndAfterInclusiveDay = (yyyyMmDd: string): Date => {
16
+ const dt = utcDateFromYmd(yyyyMmDd);
17
+ dt.setUTCDate(dt.getUTCDate() + 1);
18
+ return dt;
19
+ };
20
+
21
+ const originFromUrl = (url?: string): string | undefined => {
22
+ if (!url) {
23
+ return undefined;
24
+ }
25
+ try {
26
+ return new URL(url).origin;
27
+ } catch {
28
+ return undefined;
29
+ }
30
+ };
31
+
32
+ /** Prefer a joinable place: physical address, jam URL, then external event URL. */
33
+ const eventLocation = (
34
+ post: PublicPost,
35
+ event: Event,
36
+ postUrl?: string,
37
+ ): string | undefined => {
38
+ const physical = event.physicalLocation?.text?.trim();
39
+ if (physical) {
40
+ return physical;
41
+ }
42
+ if (event.jam) {
43
+ const jamUrl = getJamUrl(post.id, originFromUrl(postUrl));
44
+ if (jamUrl.startsWith('http')) {
45
+ return jamUrl;
46
+ }
47
+ }
48
+ return event.url?.trim() || undefined;
49
+ };
50
+
51
+ const repeatingFreq: Record<
52
+ NonNullable<Event['recurrence']>['freq'],
53
+ ICalEventRepeatingFreq
54
+ > = {
55
+ DAILY: ICalEventRepeatingFreq.DAILY,
56
+ WEEKLY: ICalEventRepeatingFreq.WEEKLY,
57
+ MONTHLY: ICalEventRepeatingFreq.MONTHLY,
58
+ };
59
+
60
+ const repeatingWeekday: Record<RecurrenceWeekday, ICalWeekday> = {
61
+ MO: ICalWeekday.MO,
62
+ TU: ICalWeekday.TU,
63
+ WE: ICalWeekday.WE,
64
+ TH: ICalWeekday.TH,
65
+ FR: ICalWeekday.FR,
66
+ SA: ICalWeekday.SA,
67
+ SU: ICalWeekday.SU,
68
+ };
69
+
70
+ const repeatingOptions = (event: Event) => {
71
+ const recurrence = event.recurrence;
72
+ if (!recurrence) return undefined;
73
+ const exclude = (event.exceptions ?? [])
74
+ .filter((exception) => exception.cancelled)
75
+ .map((exception) => new Date(exception.recurrenceId));
76
+ return {
77
+ freq: repeatingFreq[recurrence.freq],
78
+ interval: recurrence.interval ?? 1,
79
+ ...(recurrence.byDay?.length
80
+ ? { byDay: recurrence.byDay.map((day) => repeatingWeekday[day]) }
81
+ : {}),
82
+ ...(recurrence.until ? { until: new Date(recurrence.until) } : {}),
83
+ ...(recurrence.count ? { count: recurrence.count } : {}),
84
+ ...(exclude.length ? { exclude } : {}),
85
+ };
86
+ };
87
+
88
+ export const buildEventIcs = (
89
+ post: PublicPost,
90
+ options?: { postUrl?: string },
91
+ ): string | null => {
92
+ if (post.type !== 'event' || post.data?.type !== 'event') {
93
+ return null;
94
+ }
95
+
96
+ const event = post.data as Event;
97
+ const calendar = ical({
98
+ prodId: '//OpenPeeps//Event//EN',
99
+ method: ICalCalendarMethod.PUBLISH,
100
+ scale: 'GREGORIAN',
101
+ });
102
+
103
+ const link = event.url ?? options?.postUrl;
104
+ const summary = event.name?.trim() || 'Event';
105
+ const description = event.content?.trim();
106
+ const location = eventLocation(post, event, options?.postUrl);
107
+ const repeating = repeatingOptions(event);
108
+
109
+ if (event.wholeDay) {
110
+ const startYmd = event.start.slice(0, 10);
111
+ const lastInclusiveYmd = event.end?.slice(0, 10) ?? startYmd;
112
+ calendar.createEvent({
113
+ id: `openpeeps-event-${post.id}@openpeepshq`,
114
+ allDay: true,
115
+ start: utcDateFromYmd(startYmd),
116
+ end: exclusiveEndAfterInclusiveDay(lastInclusiveYmd),
117
+ summary,
118
+ ...(description ? { description } : {}),
119
+ ...(location ? { location } : {}),
120
+ ...(link ? { url: link } : {}),
121
+ ...(repeating ? { repeating } : {}),
122
+ });
123
+ } else {
124
+ const start = new Date(event.start);
125
+ const end = event.end
126
+ ? new Date(event.end)
127
+ : new Date(start.getTime() + 60 * 60 * 1000);
128
+ calendar.createEvent({
129
+ id: `openpeeps-event-${post.id}@openpeepshq`,
130
+ start,
131
+ end,
132
+ summary,
133
+ ...(description ? { description } : {}),
134
+ ...(location ? { location } : {}),
135
+ ...(link ? { url: link } : {}),
136
+ ...(repeating ? { repeating } : {}),
137
+ });
138
+ }
139
+
140
+ for (const exception of event.exceptions ?? []) {
141
+ if (exception.cancelled || !exception.start) continue;
142
+ const start = new Date(exception.start);
143
+ const end = exception.end
144
+ ? new Date(exception.end)
145
+ : new Date(start.getTime() + 60 * 60 * 1000);
146
+ calendar.createEvent({
147
+ id: `openpeeps-event-${post.id}-${exception.recurrenceId}@openpeepshq`,
148
+ start,
149
+ end,
150
+ summary,
151
+ ...(description ? { description } : {}),
152
+ ...(location ? { location } : {}),
153
+ ...(link ? { url: link } : {}),
154
+ recurrenceId: new Date(exception.recurrenceId),
155
+ });
156
+ }
157
+
158
+ return calendar.toString() + '\r\n';
159
+ };
160
+
161
+ const addOccurrenceEvent = (
162
+ calendar: ReturnType<typeof ical>,
163
+ post: PublicPost,
164
+ event: Event,
165
+ recurrenceId: string,
166
+ options?: { postUrl?: string },
167
+ ) => {
168
+ const link = event.url ?? options?.postUrl;
169
+ const summary = event.name?.trim() || 'Event';
170
+ const description = event.content?.trim();
171
+ const location = eventLocation(post, event, options?.postUrl);
172
+ const times = effectiveEventTimes(event, recurrenceId);
173
+ const id = `openpeeps-event-${post.id}-${recurrenceId}@openpeepshq`;
174
+
175
+ if (event.wholeDay) {
176
+ const startYmd = times.start.slice(0, 10);
177
+ const lastInclusiveYmd = times.end?.slice(0, 10) ?? startYmd;
178
+ calendar.createEvent({
179
+ id,
180
+ allDay: true,
181
+ start: utcDateFromYmd(startYmd),
182
+ end: exclusiveEndAfterInclusiveDay(lastInclusiveYmd),
183
+ summary,
184
+ ...(description ? { description } : {}),
185
+ ...(location ? { location } : {}),
186
+ ...(link ? { url: link } : {}),
187
+ });
188
+ return;
189
+ }
190
+
191
+ const start = new Date(times.start);
192
+ const end = times.end
193
+ ? new Date(times.end)
194
+ : new Date(start.getTime() + 60 * 60 * 1000);
195
+ calendar.createEvent({
196
+ id,
197
+ start,
198
+ end,
199
+ summary,
200
+ ...(description ? { description } : {}),
201
+ ...(location ? { location } : {}),
202
+ ...(link ? { url: link } : {}),
203
+ });
204
+ };
205
+
206
+ /** Attendance ICS: discrete booked dates, or the series definition when none. */
207
+ export const buildRsvpIcs = (
208
+ post: PublicPost,
209
+ options?: { postUrl?: string; occurrenceIds?: string[] },
210
+ ): string | null => {
211
+ const occurrenceIds = options?.occurrenceIds?.filter(Boolean) ?? [];
212
+ if (!occurrenceIds.length) {
213
+ return buildEventIcs(post, { postUrl: options?.postUrl });
214
+ }
215
+ if (post.type !== 'event' || post.data?.type !== 'event') {
216
+ return null;
217
+ }
218
+
219
+ const event = post.data as Event;
220
+ const calendar = ical({
221
+ prodId: '//OpenPeeps//Event//EN',
222
+ method: ICalCalendarMethod.PUBLISH,
223
+ scale: 'GREGORIAN',
224
+ });
225
+
226
+ for (const recurrenceId of occurrenceIds) {
227
+ addOccurrenceEvent(calendar, post, event, recurrenceId, options);
228
+ }
229
+
230
+ return calendar.toString() + '\r\n';
231
+ };
232
+
233
+ export const rsvpConfirmationContent = (
234
+ post: PublicPost,
235
+ options: { postUrl: string; occurrenceIds?: string[] },
236
+ ): {
237
+ ics: string;
238
+ eventName: string;
239
+ start: string;
240
+ end: string | null;
241
+ location: string | null;
242
+ allDay: boolean;
243
+ occurrenceCount: number;
244
+ } | null => {
245
+ if (post.type !== 'event' || post.data?.type !== 'event') {
246
+ return null;
247
+ }
248
+ const ics = buildRsvpIcs(post, options);
249
+ if (!ics) return null;
250
+ const event = post.data as Event;
251
+ const occurrenceIds = options.occurrenceIds?.filter(Boolean) ?? [];
252
+ const times =
253
+ occurrenceIds.length > 0
254
+ ? effectiveEventTimes(event, occurrenceIds[0])
255
+ : { start: event.start, end: event.end };
256
+ return {
257
+ ics,
258
+ eventName: event.name?.trim() || 'Event',
259
+ start: times.start,
260
+ end: times.end ?? null,
261
+ location: event.physicalLocation?.text ?? null,
262
+ allDay: event.wholeDay ?? false,
263
+ occurrenceCount: occurrenceIds.length,
264
+ };
265
+ };