@openpeepshq/common 0.3.0 → 0.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/common",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "sideEffects": false,
package/src/lib/index.ts CHANGED
@@ -12,6 +12,7 @@ export * from './groupTemplateHelpers';
12
12
  export * from './colors';
13
13
  export * from './markdownHelpers';
14
14
  export * from './attachmentHelpers';
15
+ export * from './resources';
15
16
  export * from './eventIcs';
16
17
  export * from './eventTime';
17
18
  export * from './timeZone';
@@ -117,6 +117,48 @@ export const firstNWords = (markdown: string | undefined, n: number) => {
117
117
  return firstTwelveWords.join(' ');
118
118
  };
119
119
 
120
+ /**
121
+ * Truncates markdown to at most `maxBlocks` block-level regions, then to
122
+ * `maxChars` characters at a word boundary. Preserves punctuation and inline
123
+ * markdown formatting (links, bold, etc.). An ellipsis is appended only when
124
+ * the content was actually shortened.
125
+ */
126
+ export const truncateMarkdown = (
127
+ markdown: string | undefined,
128
+ maxBlocks: number,
129
+ maxChars: number,
130
+ ): string => {
131
+ if (!markdown) return '';
132
+
133
+ const blocks = markdown
134
+ .replace(/\r\n/g, '\n')
135
+ .split(/\n{2,}/)
136
+ .map((block) => block.trim())
137
+ .filter(Boolean);
138
+
139
+ if (blocks.length === 0) return '';
140
+
141
+ const hadMoreBlocks = blocks.length > maxBlocks;
142
+ const content = blocks.slice(0, maxBlocks).join('\n\n');
143
+
144
+ if (content.length <= maxChars) {
145
+ return hadMoreBlocks ? `${content}…` : content;
146
+ }
147
+
148
+ const candidate = content.slice(0, maxChars);
149
+ const lastBreak = Math.max(
150
+ candidate.lastIndexOf(' '),
151
+ candidate.lastIndexOf('\n'),
152
+ candidate.lastIndexOf('\t'),
153
+ );
154
+
155
+ if (lastBreak > 0) {
156
+ return `${content.slice(0, lastBreak).trimEnd()}…`;
157
+ }
158
+
159
+ return `${candidate}…`;
160
+ };
161
+
120
162
  /** Average adult reading speed (words per minute). */
121
163
  const DEFAULT_WORDS_PER_MINUTE = 210;
122
164
 
@@ -0,0 +1,88 @@
1
+ import type { MediaAttachmentData, ResourceKind } from '../types/models';
2
+
3
+ export const RESOURCE_KINDS = [
4
+ 'video',
5
+ 'file',
6
+ 'gallery',
7
+ 'audio',
8
+ 'link',
9
+ ] as const satisfies readonly ResourceKind[];
10
+
11
+ export const attachmentMediaKind = (
12
+ att: Pick<MediaAttachmentData, 'type' | 'meta'>,
13
+ ): 'image' | 'video' | 'audio' | 'file' => {
14
+ const mime = att.meta?.mimetype ?? '';
15
+ const type = att.type ?? '';
16
+ if (type === 'image' || mime.startsWith('image/')) return 'image';
17
+ if (type === 'video' || mime.startsWith('video/')) return 'video';
18
+ if (type === 'audio' || mime.startsWith('audio/')) return 'audio';
19
+ return 'file';
20
+ };
21
+
22
+ export const resourceHasRequiredMedia = (resource: {
23
+ resourceKind: ResourceKind;
24
+ url?: string;
25
+ attachments?: MediaAttachmentData[];
26
+ }): boolean => {
27
+ if (resource.resourceKind === 'link') return Boolean(resource.url);
28
+ const need =
29
+ resource.resourceKind === 'gallery'
30
+ ? 'image'
31
+ : resource.resourceKind === 'video'
32
+ ? 'video'
33
+ : resource.resourceKind === 'audio'
34
+ ? 'audio'
35
+ : 'file';
36
+ return (resource.attachments ?? []).some(
37
+ (att) => attachmentMediaKind(att) === need,
38
+ );
39
+ };
40
+
41
+ export type CategoryNode = {
42
+ name: string;
43
+ path: string[];
44
+ children: CategoryNode[];
45
+ };
46
+
47
+ export const categoryTreeFromPaths = (
48
+ paths: readonly (readonly string[])[],
49
+ ): CategoryNode[] => {
50
+ const root: CategoryNode[] = [];
51
+ for (const path of paths) {
52
+ let level = root;
53
+ const acc: string[] = [];
54
+ for (const name of path) {
55
+ const segment = name.trim();
56
+ if (!segment) continue;
57
+ acc.push(segment);
58
+ let node = level.find((entry) => entry.name === segment);
59
+ if (!node) {
60
+ node = { name: segment, path: [...acc], children: [] };
61
+ level.push(node);
62
+ }
63
+ level = node.children;
64
+ }
65
+ }
66
+ return root;
67
+ };
68
+
69
+ export const pathStartsWith = (
70
+ path: readonly string[],
71
+ prefix: readonly string[],
72
+ ): boolean =>
73
+ prefix.length <= path.length &&
74
+ prefix.every((segment, index) => path[index] === segment);
75
+
76
+ export const normalizeResourceTags = (
77
+ tags: readonly string[] | undefined,
78
+ ): string[] => {
79
+ const seen = new Set<string>();
80
+ const out: string[] = [];
81
+ for (const raw of tags ?? []) {
82
+ const tag = raw.replace(/^#/, '').trim().toLowerCase();
83
+ if (!tag || seen.has(tag)) continue;
84
+ seen.add(tag);
85
+ out.push(tag);
86
+ }
87
+ return out;
88
+ };
@@ -117,6 +117,7 @@ export const analyticsPostTypeKeySchema = z.enum([
117
117
  'note',
118
118
  'poll',
119
119
  'event',
120
+ 'resource',
120
121
  ]);
121
122
  export type AnalyticsPostTypeKey = z.infer<typeof analyticsPostTypeKeySchema>;
122
123
 
@@ -136,6 +137,7 @@ export const analyticsStackedPointSchema = z.object({
136
137
  note: z.number(),
137
138
  poll: z.number(),
138
139
  event: z.number(),
140
+ resource: z.number(),
139
141
  });
140
142
  export type AnalyticsStackedPoint = z.infer<typeof analyticsStackedPointSchema>;
141
143
 
package/src/types/api.ts CHANGED
@@ -745,6 +745,9 @@ export const chronologicalInfiniteQueryParamsSchema = z.object({
745
745
  start: z.string().optional(),
746
746
  limit: z.coerce.number().optional(),
747
747
  format: z.enum(['threaded', 'linear']).optional(),
748
+ groupId: z.string().optional(),
749
+ /** Query-string flag; fetch params only allow string | number. */
750
+ ungrouped: z.literal('true').optional(),
748
751
  });
749
752
  export type ChronologicalInfiniteQueryParams =
750
753
  | z.infer<typeof chronologicalInfiniteQueryParamsSchema>
@@ -83,6 +83,7 @@ export const groupCapabilities = [
83
83
  'core-posts-create-event',
84
84
  'core-posts-create-poll',
85
85
  'core-posts-create-article',
86
+ 'core-posts-create-resource',
86
87
  ] as const;
87
88
 
88
89
  /** Caps an owner can toggle per relationship in the group editor. */
@@ -185,6 +186,10 @@ export const roleCapabilities = [
185
186
  'core-posts-create-article-public',
186
187
  'core-posts-create-article-direct',
187
188
  'core-posts-create-article-private',
189
+ 'core-posts-create-resource-local',
190
+ 'core-posts-create-resource-public',
191
+ 'core-posts-create-resource-direct',
192
+ 'core-posts-create-resource-private',
188
193
  'core-posts-read-local',
189
194
  'core-posts-announce',
190
195
  'core-posts-delete',
@@ -404,9 +404,24 @@ export const visibilityTypeSchema = z.enum(visibilityTypeValues);
404
404
 
405
405
  export type VisibilityType = z.infer<typeof visibilityTypeSchema>;
406
406
 
407
- export const postTypeSchema = z.enum(['note', 'question', 'event', 'article']);
407
+ export const postTypeSchema = z.enum([
408
+ 'note',
409
+ 'question',
410
+ 'event',
411
+ 'article',
412
+ 'resource',
413
+ ]);
408
414
  export type PostType = z.infer<typeof postTypeSchema>;
409
415
 
416
+ export const resourceKindSchema = z.enum([
417
+ 'video',
418
+ 'file',
419
+ 'gallery',
420
+ 'audio',
421
+ 'link',
422
+ ]);
423
+ export type ResourceKind = z.infer<typeof resourceKindSchema>;
424
+
410
425
  export const mediaAttachmentStatusSchema = z.enum([
411
426
  'processing',
412
427
  'ready',
@@ -508,6 +523,18 @@ export const articleSchema = noteSchema
508
523
 
509
524
  export type Article = z.infer<typeof articleSchema>;
510
525
 
526
+ export const resourcePostSchema = noteSchema
527
+ .extend({
528
+ type: z.literal('resource'),
529
+ title: z.string(),
530
+ tags: z.array(z.string().min(1)).optional(),
531
+ categoryPath: z.array(z.string().min(1)).optional(),
532
+ resourceKind: resourceKindSchema,
533
+ url: z.url().optional(),
534
+ })
535
+ .openapi('ResourcePost');
536
+ export type ResourcePost = z.infer<typeof resourcePostSchema>;
537
+
511
538
  export const eventIsoDatetimeSchema = z.iso.datetime({
512
539
  offset: true,
513
540
  precision: 3,
@@ -583,6 +610,7 @@ export const postDataUnionSchema = z.discriminatedUnion('type', [
583
610
  questionSchema,
584
611
  eventSchema,
585
612
  articleSchema,
613
+ resourcePostSchema,
586
614
  ]);
587
615
 
588
616
  export type PostDataUnion = z.infer<typeof postDataUnionSchema>;