@openpeepshq/common 0.6.0 → 0.6.1

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.
@@ -264,6 +264,7 @@ export declare const profileResourceTypeSchema: z.ZodEnum<{
264
264
  groups: "groups";
265
265
  reports: "reports";
266
266
  webhooks: "webhooks";
267
+ conversations: "conversations";
267
268
  }>;
268
269
  export type ProfileResourceType = z.infer<typeof profileResourceTypeSchema>;
269
270
  export declare const serviceResourceTypeSchema: z.ZodEnum<{
@@ -288,6 +289,7 @@ export declare const resourceSchema: z.ZodObject<{
288
289
  groups: "groups";
289
290
  reports: "reports";
290
291
  webhooks: "webhooks";
292
+ conversations: "conversations";
291
293
  analytics: "analytics";
292
294
  render: "render";
293
295
  }>;
@@ -321,6 +323,7 @@ export declare const scopeSchema: z.ZodObject<{
321
323
  groups: "groups";
322
324
  reports: "reports";
323
325
  webhooks: "webhooks";
326
+ conversations: "conversations";
324
327
  analytics: "analytics";
325
328
  render: "render";
326
329
  }>;
@@ -410,6 +413,7 @@ export declare const profileDataSchema: z.ZodObject<{
410
413
  groups: "groups";
411
414
  reports: "reports";
412
415
  webhooks: "webhooks";
416
+ conversations: "conversations";
413
417
  analytics: "analytics";
414
418
  render: "render";
415
419
  }>;
@@ -466,6 +470,7 @@ export declare const profileSchema: z.ZodObject<{
466
470
  groups: "groups";
467
471
  reports: "reports";
468
472
  webhooks: "webhooks";
473
+ conversations: "conversations";
469
474
  analytics: "analytics";
470
475
  render: "render";
471
476
  }>;
@@ -539,6 +544,7 @@ export declare const accountWithMetaSchema: z.ZodObject<{
539
544
  groups: "groups";
540
545
  reports: "reports";
541
546
  webhooks: "webhooks";
547
+ conversations: "conversations";
542
548
  analytics: "analytics";
543
549
  render: "render";
544
550
  }>;
@@ -648,6 +654,7 @@ export declare const followSchema: z.ZodObject<{
648
654
  groups: "groups";
649
655
  reports: "reports";
650
656
  webhooks: "webhooks";
657
+ conversations: "conversations";
651
658
  analytics: "analytics";
652
659
  render: "render";
653
660
  }>;
@@ -707,6 +714,7 @@ export declare const followSchema: z.ZodObject<{
707
714
  groups: "groups";
708
715
  reports: "reports";
709
716
  webhooks: "webhooks";
717
+ conversations: "conversations";
710
718
  analytics: "analytics";
711
719
  render: "render";
712
720
  }>;
@@ -153,6 +153,7 @@ const profileResourceTypes = [
153
153
  'reports',
154
154
  'webhooks',
155
155
  'notifications',
156
+ 'conversations',
156
157
  ];
157
158
  export const profileResourceTypeSchema = z.enum(profileResourceTypes);
158
159
  const serviceResourceTypes = [
package/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
2
  "name": "@openpeepshq/common",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "dependencies": {
8
8
  "@asteasolutions/zod-to-openapi": "^8.5.0",
9
+ "@date-fns/tz": "^1.5.0",
9
10
  "date-fns": "^4.1.0",
10
11
  "ical-generator": "^10.2.0",
11
12
  "iso-639-1": "^3.1.5",
12
13
  "jose": "^6.2.2",
14
+ "timezones-ical-library": "^2.4.0",
13
15
  "type-fest": "^5.5.0",
14
16
  "validator": "^13.15.35",
15
- "zod": "^4.4.3"
17
+ "zod": "^4.6.6"
16
18
  },
17
19
  "main": "./dist/index.js",
18
20
  "types": "./dist/index.d.ts",
@@ -57,8 +59,8 @@
57
59
  "@types/validator": "^13.15.10",
58
60
  "@typescript-eslint/eslint-plugin": "^8.72.0",
59
61
  "@typescript-eslint/parser": "^8.72.0",
60
- "@vitest/coverage-v8": "^2.1.8",
61
- "@vitest/ui": "^2.1.8",
62
+ "@vitest/coverage-v8": "^2.1.9",
63
+ "@vitest/ui": "^2.1.9",
62
64
  "concurrently": "^9.2.1",
63
65
  "eslint": "^10.1.0",
64
66
  "eslint-config-prettier": "^9.1.0",
@@ -68,7 +70,7 @@
68
70
  "tsc-alias": "^1.8.16",
69
71
  "typed-emitter": "^2.1.0",
70
72
  "typescript": "^6.0.2",
71
- "vitest": "^2.1.8"
73
+ "vitest": "^2.1.9"
72
74
  },
73
75
  "scripts": {
74
76
  "lint": "eslint .",
@@ -3,9 +3,12 @@ import ical, {
3
3
  ICalEventRepeatingFreq,
4
4
  ICalWeekday,
5
5
  } from 'ical-generator';
6
+ import { TZDate } from '@date-fns/tz';
7
+ import { tzlib_get_ical_block } from 'timezones-ical-library';
6
8
  import type { Event, PublicPost, RecurrenceWeekday } from '../types';
7
9
  import { effectiveEventTimes } from './eventRecurrence';
8
10
  import { getJamUrl } from './jamHelpers';
11
+ import { isIanaTimeZone } from './timeZone';
9
12
 
10
13
  const utcDateFromYmd = (yyyyMmDd: string): Date => {
11
14
  const [y, m, d] = yyyyMmDd.split('-').map(Number);
@@ -18,6 +21,45 @@ const exclusiveEndAfterInclusiveDay = (yyyyMmDd: string): Date => {
18
21
  return dt;
19
22
  };
20
23
 
24
+ /**
25
+ * The event's stored IANA zone when it is one; ical-generator writes any
26
+ * `timezone` string verbatim into `TZID=`, so non-IANA values fall back to
27
+ * UTC output here.
28
+ */
29
+ const eventTimeZone = (event: Event): string | undefined =>
30
+ isIanaTimeZone(event.timeZone) ? event.timeZone : undefined;
31
+
32
+ /**
33
+ * A zone-aware Date: ical-generator formats a TZDate's wall-clock components
34
+ * in the event's zone, so the host's local zone (and its DST gaps) never
35
+ * shifts the exported time.
36
+ */
37
+ const instantInZone = (iso: string, timeZone?: string): Date =>
38
+ timeZone ? new TZDate(iso, timeZone) : new Date(iso);
39
+
40
+ /** The default 1-hour end keeps the event's zone (or UTC). */
41
+ const defaultEnd = (start: Date, timeZone?: string): Date =>
42
+ timeZone
43
+ ? new TZDate(start.getTime() + 60 * 60 * 1000, timeZone)
44
+ : new Date(start.getTime() + 60 * 60 * 1000);
45
+
46
+ /**
47
+ * RFC 5545 requires a VTIMEZONE for every TZID used. ical-generator only
48
+ * emits one via a calendar-level timezone, which in v10 also re-zones
49
+ * UNTIL and DTSTAMP (both of which must stay UTC), so the component is
50
+ * spliced in before the first VEVENT instead.
51
+ */
52
+ const withVtimezone = (calendarText: string, timeZone: string): string => {
53
+ const [block] = tzlib_get_ical_block(timeZone) as [string, string];
54
+ const normalized = block.replace(/\r?\n/g, '\r\n').trim() + '\r\n';
55
+ const veventIndex = calendarText.indexOf('BEGIN:VEVENT');
56
+ return (
57
+ calendarText.slice(0, veventIndex) +
58
+ normalized +
59
+ calendarText.slice(veventIndex)
60
+ );
61
+ };
62
+
21
63
  const originFromUrl = (url?: string): string | undefined => {
22
64
  if (!url) {
23
65
  return undefined;
@@ -67,12 +109,12 @@ const repeatingWeekday: Record<RecurrenceWeekday, ICalWeekday> = {
67
109
  SU: ICalWeekday.SU,
68
110
  };
69
111
 
70
- const repeatingOptions = (event: Event) => {
112
+ const repeatingOptions = (event: Event, timeZone?: string) => {
71
113
  const recurrence = event.recurrence;
72
114
  if (!recurrence) return undefined;
73
115
  const exclude = (event.exceptions ?? [])
74
116
  .filter((exception) => exception.cancelled)
75
- .map((exception) => new Date(exception.recurrenceId));
117
+ .map((exception) => instantInZone(exception.recurrenceId, timeZone));
76
118
  return {
77
119
  freq: repeatingFreq[recurrence.freq],
78
120
  interval: recurrence.interval ?? 1,
@@ -104,7 +146,10 @@ export const buildEventIcs = (
104
146
  const summary = event.name?.trim() || 'Event';
105
147
  const description = event.content?.trim();
106
148
  const location = eventLocation(post, event, options?.postUrl);
107
- const repeating = repeatingOptions(event);
149
+ // Timed events keep the event's own zone; all-day events use DATE values
150
+ // and carry no zone.
151
+ const timeZone = event.wholeDay ? undefined : eventTimeZone(event);
152
+ const repeating = repeatingOptions(event, timeZone);
108
153
 
109
154
  if (event.wholeDay) {
110
155
  const startYmd = event.start.slice(0, 10);
@@ -121,14 +166,15 @@ export const buildEventIcs = (
121
166
  ...(repeating ? { repeating } : {}),
122
167
  });
123
168
  } else {
124
- const start = new Date(event.start);
169
+ const start = instantInZone(event.start, timeZone);
125
170
  const end = event.end
126
- ? new Date(event.end)
127
- : new Date(start.getTime() + 60 * 60 * 1000);
171
+ ? instantInZone(event.end, timeZone)
172
+ : defaultEnd(start, timeZone);
128
173
  calendar.createEvent({
129
174
  id: `openpeeps-event-${post.id}@openpeepshq`,
130
175
  start,
131
176
  end,
177
+ ...(timeZone ? { timezone: timeZone } : {}),
132
178
  summary,
133
179
  ...(description ? { description } : {}),
134
180
  ...(location ? { location } : {}),
@@ -139,23 +185,27 @@ export const buildEventIcs = (
139
185
 
140
186
  for (const exception of event.exceptions ?? []) {
141
187
  if (exception.cancelled || !exception.start) continue;
142
- const start = new Date(exception.start);
188
+ const start = instantInZone(exception.start, timeZone);
143
189
  const end = exception.end
144
- ? new Date(exception.end)
145
- : new Date(start.getTime() + 60 * 60 * 1000);
190
+ ? instantInZone(exception.end, timeZone)
191
+ : defaultEnd(start, timeZone);
146
192
  calendar.createEvent({
147
193
  id: `openpeeps-event-${post.id}-${exception.recurrenceId}@openpeepshq`,
148
194
  start,
149
195
  end,
196
+ ...(timeZone ? { timezone: timeZone } : {}),
150
197
  summary,
151
198
  ...(description ? { description } : {}),
152
199
  ...(location ? { location } : {}),
153
200
  ...(link ? { url: link } : {}),
154
- recurrenceId: new Date(exception.recurrenceId),
201
+ recurrenceId: instantInZone(exception.recurrenceId, timeZone),
155
202
  });
156
203
  }
157
204
 
158
- return calendar.toString() + '\r\n';
205
+ const calendarText = calendar.toString();
206
+ return (
207
+ (timeZone ? withVtimezone(calendarText, timeZone) : calendarText) + '\r\n'
208
+ );
159
209
  };
160
210
 
161
211
  const addOccurrenceEvent = (
@@ -171,6 +221,7 @@ const addOccurrenceEvent = (
171
221
  const location = eventLocation(post, event, options?.postUrl);
172
222
  const times = effectiveEventTimes(event, recurrenceId);
173
223
  const id = `openpeeps-event-${post.id}-${recurrenceId}@openpeepshq`;
224
+ const timeZone = event.wholeDay ? undefined : eventTimeZone(event);
174
225
 
175
226
  if (event.wholeDay) {
176
227
  const startYmd = times.start.slice(0, 10);
@@ -188,14 +239,15 @@ const addOccurrenceEvent = (
188
239
  return;
189
240
  }
190
241
 
191
- const start = new Date(times.start);
242
+ const start = instantInZone(times.start, timeZone);
192
243
  const end = times.end
193
- ? new Date(times.end)
194
- : new Date(start.getTime() + 60 * 60 * 1000);
244
+ ? instantInZone(times.end, timeZone)
245
+ : defaultEnd(start, timeZone);
195
246
  calendar.createEvent({
196
247
  id,
197
248
  start,
198
249
  end,
250
+ ...(timeZone ? { timezone: timeZone } : {}),
199
251
  summary,
200
252
  ...(description ? { description } : {}),
201
253
  ...(location ? { location } : {}),
@@ -227,7 +279,11 @@ export const buildRsvpIcs = (
227
279
  addOccurrenceEvent(calendar, post, event, recurrenceId, options);
228
280
  }
229
281
 
230
- return calendar.toString() + '\r\n';
282
+ const timeZone = event.wholeDay ? undefined : eventTimeZone(event);
283
+ const calendarText = calendar.toString();
284
+ return (
285
+ (timeZone ? withVtimezone(calendarText, timeZone) : calendarText) + '\r\n'
286
+ );
231
287
  };
232
288
 
233
289
  export const rsvpConfirmationContent = (
package/src/lib/index.ts CHANGED
@@ -29,3 +29,4 @@ export * from './ssoLoginUrl';
29
29
  export * from './ssoProviders';
30
30
  export * from './federationHosts';
31
31
  export * from './reactions';
32
+ export * from './videoEmbed';
@@ -0,0 +1,116 @@
1
+ const YOUTUBE_ID_RE = /^[A-Za-z0-9_-]{6,15}$/;
2
+ const VIMEO_ID_RE = /^\d{6,}$/;
3
+ const YOUTUBE_ORIGIN = 'https://www.youtube-nocookie.com';
4
+ const VIMEO_ORIGIN = 'https://player.vimeo.com';
5
+
6
+ export interface VideoEmbed {
7
+ provider: 'youtube' | 'vimeo';
8
+ embedUrl: string;
9
+ /** Origin of `embedUrl`, for same-origin checks in WebView guards. */
10
+ origin: string;
11
+ }
12
+
13
+ /**
14
+ * Maps a YouTube `t=`/`start=` timestamp to seconds. Accepts `42`, `42s`,
15
+ * `1:30`, `1m30s`, and `1h2m3s` forms.
16
+ */
17
+ const parseYouTubeStart = (raw: string): number | undefined => {
18
+ const trimmed = raw.trim();
19
+ if (trimmed === '') return undefined;
20
+ if (/^\d+$/.test(trimmed)) return Number(trimmed);
21
+ const compact = trimmed.match(/^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$/);
22
+ if (compact) {
23
+ const [, hours, minutes, seconds] = compact;
24
+ if (!hours && !minutes && !seconds) return undefined;
25
+ return (
26
+ Number(hours ?? 0) * 3600 +
27
+ Number(minutes ?? 0) * 60 +
28
+ Number(seconds ?? 0)
29
+ );
30
+ }
31
+ const parts = trimmed.split(':');
32
+ if (parts.length > 3 || parts.some((part) => !/^\d+$/.test(part))) {
33
+ return undefined;
34
+ }
35
+ return parts.reduce((total, part) => total * 60 + Number(part), 0);
36
+ };
37
+
38
+ const youtubeEmbed = (id: string, parsed: URL): VideoEmbed => {
39
+ const rawStart =
40
+ parsed.searchParams.get('t') ?? parsed.searchParams.get('start');
41
+ const start = rawStart ? parseYouTubeStart(rawStart) : undefined;
42
+ return {
43
+ provider: 'youtube',
44
+ embedUrl:
45
+ `${YOUTUBE_ORIGIN}/embed/${id}` +
46
+ (start !== undefined && start > 0 ? `?start=${start}` : ''),
47
+ origin: YOUTUBE_ORIGIN,
48
+ };
49
+ };
50
+
51
+ /**
52
+ * Embed info for plain video URLs on supported providers (YouTube
53
+ * watch/share/shorts/embed, Vimeo). Lets the client render a playable
54
+ * player instead of a link card.
55
+ */
56
+ export const videoEmbedForUrl = (url: string): VideoEmbed | undefined => {
57
+ let parsed: URL;
58
+ try {
59
+ parsed = new URL(url);
60
+ } catch {
61
+ return undefined;
62
+ }
63
+ const host = parsed.hostname.replace(/^(www|m)\./, '');
64
+
65
+ if (host === 'youtu.be') {
66
+ const id = parsed.pathname.split('/')[1] ?? '';
67
+ return YOUTUBE_ID_RE.test(id) ? youtubeEmbed(id, parsed) : undefined;
68
+ }
69
+
70
+ if (host === 'youtube.com') {
71
+ const segments = parsed.pathname.split('/').filter(Boolean);
72
+ let id: string | undefined;
73
+ if (segments[0] === 'watch') {
74
+ id = parsed.searchParams.get('v') ?? undefined;
75
+ } else if (segments[0] === 'shorts' || segments[0] === 'embed') {
76
+ id = segments[1];
77
+ }
78
+ return id && YOUTUBE_ID_RE.test(id) ? youtubeEmbed(id, parsed) : undefined;
79
+ }
80
+
81
+ if (host === 'vimeo.com' || host === 'player.vimeo.com') {
82
+ const segments = parsed.pathname.split('/').filter(Boolean);
83
+ let id: string | undefined;
84
+ let hash: string | undefined;
85
+ if (host === 'player.vimeo.com') {
86
+ if (segments[0] === 'video') {
87
+ id = segments[1];
88
+ hash = parsed.searchParams.get('h') ?? undefined;
89
+ }
90
+ } else if (segments.length === 1) {
91
+ id = segments[0];
92
+ } else if (segments.length === 2 && VIMEO_ID_RE.test(segments[0])) {
93
+ // `vimeo.com/<id>/<hash>` — unlisted videos carry the privacy hash
94
+ // in the path.
95
+ id = segments[0];
96
+ hash = segments[1];
97
+ } else if (segments.length === 3 && segments[0] === 'channels') {
98
+ id = segments[2];
99
+ } else if (
100
+ segments.length === 4 &&
101
+ segments[0] === 'groups' &&
102
+ segments[2] === 'videos'
103
+ ) {
104
+ id = segments[3];
105
+ }
106
+ if (!id || !VIMEO_ID_RE.test(id)) return undefined;
107
+ return {
108
+ provider: 'vimeo',
109
+ embedUrl:
110
+ `${VIMEO_ORIGIN}/video/${id}?dnt=1` + (hash ? `&h=${hash}` : ''),
111
+ origin: VIMEO_ORIGIN,
112
+ };
113
+ }
114
+
115
+ return undefined;
116
+ };
@@ -235,6 +235,7 @@ const profileResourceTypes = [
235
235
  'reports',
236
236
  'webhooks',
237
237
  'notifications',
238
+ 'conversations',
238
239
  ] as const;
239
240
 
240
241
  export const profileResourceTypeSchema = z.enum(profileResourceTypes);