@openpeepshq/common 0.6.4 → 0.8.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/contentLength.d.ts +25 -0
- package/dist/lib/contentLength.js +118 -0
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +1 -0
- package/dist/lib/postHelpers.d.ts +23 -1
- package/dist/lib/postHelpers.js +41 -0
- package/dist/types/analytics.d.ts +3 -3
- package/dist/types/api.d.ts +40 -40
- package/dist/types/api.js +2 -2
- package/dist/types/db.d.ts +8 -8
- package/dist/types/internal.d.ts +14 -14
- package/dist/types/models.d.ts +373 -3
- package/dist/types/models.js +42 -8
- package/dist/types/stats.d.ts +36 -36
- package/package.json +1 -1
- package/src/lib/contentLength.ts +139 -0
- package/src/lib/index.ts +1 -0
- package/src/lib/postHelpers.ts +91 -1
- package/src/types/api.ts +2 -1
- package/src/types/models.ts +65 -15
package/src/types/models.ts
CHANGED
|
@@ -3,6 +3,7 @@ import validator from 'validator';
|
|
|
3
3
|
import ISO6391 from 'iso-639-1';
|
|
4
4
|
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
|
|
5
5
|
import { isEndBeforeStart } from '../lib/eventTime';
|
|
6
|
+
import { hasVisibleText, postContentWithinBudget } from '../lib/contentLength';
|
|
6
7
|
import { groupRelationships } from './capabilities';
|
|
7
8
|
import { capabilitiesSchema } from './config';
|
|
8
9
|
import {
|
|
@@ -498,23 +499,43 @@ export const customEmojiSchema = z
|
|
|
498
499
|
.openapi('CustomEmoji');
|
|
499
500
|
export type CustomEmoji = z.infer<typeof customEmojiSchema>;
|
|
500
501
|
|
|
501
|
-
|
|
502
|
-
.
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
502
|
+
const noteBaseSchema = z.object({
|
|
503
|
+
type: z.literal('note'),
|
|
504
|
+
content: z.string().optional(),
|
|
505
|
+
sensitive: z.boolean().optional(),
|
|
506
|
+
spoilerText: z.string().optional(),
|
|
507
|
+
attachments: z.array(mediaAttachmentDataSchema).optional(),
|
|
508
|
+
customEmojis: z.array(customEmojiSchema).optional(),
|
|
509
|
+
language: z.enum(ISO6391.getAllCodes() as [string, ...string[]]).optional(),
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
// A note must carry something a reader can see: visible text or an
|
|
513
|
+
// attachment. Whitespace-only text renders nothing, so it is not enough.
|
|
514
|
+
const hasContentRefine = {
|
|
515
|
+
message: 'posts.form.contentRequired',
|
|
516
|
+
path: ['content'],
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
// Note/question carry the visible-text + raw budgets; the longer post
|
|
520
|
+
// flavors (article, resource, event) extend the base without them.
|
|
521
|
+
const contentBudgetRefine = {
|
|
522
|
+
message: 'posts.form.contentTooLong',
|
|
523
|
+
path: ['content'],
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
// The refines are write-side checks only: they must live on the input
|
|
527
|
+
// schemas the write endpoints validate against, not on the members of
|
|
528
|
+
// `postDataUnionSchema`. That union also validates API *responses*
|
|
529
|
+
// (`publicPostSchema`, reply/entry data), where a contentless note is
|
|
530
|
+
// legitimate — repost shells and system "left" notes have neither
|
|
531
|
+
// content nor attachments, and output validation must not reject them.
|
|
532
|
+
export const noteSchema = noteBaseSchema.openapi('Note');
|
|
512
533
|
export type Note = z.infer<typeof noteSchema>;
|
|
513
534
|
|
|
514
535
|
/** Poll choices are stored short so they fit the vote UI on small screens. */
|
|
515
536
|
export const POLL_OPTION_MAX_LENGTH = 30;
|
|
516
537
|
|
|
517
|
-
export const questionSchema =
|
|
538
|
+
export const questionSchema = noteBaseSchema
|
|
518
539
|
.extend({
|
|
519
540
|
type: z.literal('question'),
|
|
520
541
|
options: z
|
|
@@ -531,7 +552,23 @@ export const questionSchema = noteSchema
|
|
|
531
552
|
|
|
532
553
|
export type Question = z.infer<typeof questionSchema>;
|
|
533
554
|
|
|
534
|
-
export const
|
|
555
|
+
export const noteInputSchema = noteBaseSchema
|
|
556
|
+
.refine(
|
|
557
|
+
(data) =>
|
|
558
|
+
hasVisibleText(data.content) || (data.attachments?.length ?? 0) > 0,
|
|
559
|
+
hasContentRefine,
|
|
560
|
+
)
|
|
561
|
+
.refine((data) => postContentWithinBudget(data.content), contentBudgetRefine);
|
|
562
|
+
|
|
563
|
+
export const questionInputSchema = questionSchema
|
|
564
|
+
.refine(
|
|
565
|
+
(data) =>
|
|
566
|
+
hasVisibleText(data.content) || (data.attachments?.length ?? 0) > 0,
|
|
567
|
+
hasContentRefine,
|
|
568
|
+
)
|
|
569
|
+
.refine((data) => postContentWithinBudget(data.content), contentBudgetRefine);
|
|
570
|
+
|
|
571
|
+
export const articleSchema = noteBaseSchema
|
|
535
572
|
.extend({
|
|
536
573
|
type: z.literal('article'),
|
|
537
574
|
image: z.url().optional(),
|
|
@@ -544,7 +581,7 @@ export type Article = z.infer<typeof articleSchema>;
|
|
|
544
581
|
/** Resource titles are library headings, so they stay much shorter than the description. */
|
|
545
582
|
export const RESOURCE_TITLE_MAX_LENGTH = 200;
|
|
546
583
|
|
|
547
|
-
export const resourcePostSchema =
|
|
584
|
+
export const resourcePostSchema = noteBaseSchema
|
|
548
585
|
.extend({
|
|
549
586
|
type: z.literal('resource'),
|
|
550
587
|
title: z.string().max(RESOURCE_TITLE_MAX_LENGTH),
|
|
@@ -607,7 +644,7 @@ export type EventOccurrenceException = z.infer<
|
|
|
607
644
|
typeof eventOccurrenceExceptionSchema
|
|
608
645
|
>;
|
|
609
646
|
|
|
610
|
-
export const eventSchema =
|
|
647
|
+
export const eventSchema = noteBaseSchema
|
|
611
648
|
.extend({
|
|
612
649
|
type: z.literal('event'),
|
|
613
650
|
name: z.string().optional(),
|
|
@@ -646,6 +683,19 @@ export const postDataUnionSchema = z.discriminatedUnion('type', [
|
|
|
646
683
|
|
|
647
684
|
export type PostDataUnion = z.infer<typeof postDataUnionSchema>;
|
|
648
685
|
|
|
686
|
+
// Write-side union: the server POST/PUT endpoints validate *incoming*
|
|
687
|
+
// post payloads with this. The budget/requirement refines must not sit
|
|
688
|
+
// on the response-facing `postDataUnionSchema` members (see above).
|
|
689
|
+
export const postDataInputUnionSchema = z.discriminatedUnion('type', [
|
|
690
|
+
noteInputSchema,
|
|
691
|
+
questionInputSchema,
|
|
692
|
+
eventSchema,
|
|
693
|
+
articleSchema,
|
|
694
|
+
resourcePostSchema,
|
|
695
|
+
]);
|
|
696
|
+
|
|
697
|
+
export type PostDataInputUnion = z.infer<typeof postDataInputUnionSchema>;
|
|
698
|
+
|
|
649
699
|
export const postDataSchema = z.object({
|
|
650
700
|
type: postTypeSchema,
|
|
651
701
|
visibility: visibilityTypeSchema,
|