@openpeepshq/common 0.6.4 → 0.7.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.
@@ -1,4 +1,4 @@
1
- import { Answer, AnswerWithPublicProfile, AuthorizationData, CapabilitiesConfig, Event, GroupWithMeta, PostType, PostDataUnion, PublicPost, PublicProfile, PublicRsvp, RSVP, Thread, VisibilityType } from '../types';
1
+ import { Answer, AnswerWithPublicProfile, AuthorizationData, CapabilitiesConfig, EntryWithPublicProfile, Event, GroupWithMeta, PostType, PostDataUnion, PublicPost, PublicProfile, PublicRsvp, RSVP, Thread, VisibilityType } from '../types';
2
2
  export declare const buildThreads: (posts: PublicPost[]) => Thread[];
3
3
  export type ThreadPreviewPost = {
4
4
  id: string;
@@ -309,6 +309,7 @@ export declare const HIDDEN_AUTHOR_HANDLE = "hidden";
309
309
  export declare const toHiddenPost: <T extends {
310
310
  data: PostDataUnion;
311
311
  profile: PublicProfile;
312
+ entries?: EntryWithPublicProfile[];
312
313
  }>(post: T) => T & {
313
314
  hidden: true;
314
315
  };
@@ -334,6 +335,27 @@ export declare const isOneToOneDm: (post: Pick<PublicPost, "visibility" | "audie
334
335
  * notes so they remain valid ActivityPub direct messages.
335
336
  */
336
337
  export declare const isConversationLeavePost: (post: Pick<PublicPost, "inReplyToId" | "audience" | "profile">) => boolean;
338
+ export type PostVersionOrder = 'oldest' | 'newest';
339
+ export type PostEditVersion = {
340
+ id: string;
341
+ createdAt: string;
342
+ data: PostDataUnion;
343
+ /** Author snapshot of the entry that produced this version. */
344
+ profile: EntryWithPublicProfile['profile'];
345
+ /** Create entry is the original text. Each edit entry is that save's text. */
346
+ kind: 'original' | 'edit';
347
+ current: boolean;
348
+ };
349
+ /** True when at least one edit has been saved for this post. */
350
+ export declare const postHasEditHistory: (entries: readonly EntryWithPublicProfile[] | undefined) => boolean;
351
+ /**
352
+ * Saved versions of a post. The create entry is the original; each edit entry
353
+ * stores the content written at that save. Oldest first unless `order` is newest.
354
+ * Returns nothing until the post has been edited.
355
+ */
356
+ export declare const postEditVersions: (entries: readonly EntryWithPublicProfile[] | undefined, order?: PostVersionOrder) => PostEditVersion[];
357
+ export declare const postEditVersionKind: (version: Pick<PostEditVersion, "current" | "kind">) => "original" | "edit" | "current";
358
+ export declare const postEditVersionTitle: (data: PostDataUnion) => string | undefined;
337
359
  export declare const isOneToOneWithBlocked: (post: Pick<PublicPost, "visibility" | "audience" | "profile"> & {
338
360
  creatorId?: string;
339
361
  }, viewer: {
@@ -502,6 +502,11 @@ export const toHiddenPost = (post) => ({
502
502
  ...post.data,
503
503
  ...('content' in post.data ? { content: '' } : {}),
504
504
  },
505
+ // create/edit entries each carry a full content snapshot; dropping them
506
+ // keeps the redacted text out of the hidden post's serialized payload.
507
+ entries: post.entries
508
+ ? post.entries.filter((entry) => entry.type !== 'create' && entry.type !== 'edit')
509
+ : post.entries,
505
510
  profile: {
506
511
  ...post.profile,
507
512
  displayName: undefined,
@@ -538,6 +543,42 @@ export const isConversationLeavePost = (post) => {
538
543
  return false;
539
544
  return !post.audience?.some((p) => p.id === authorId);
540
545
  };
546
+ const isSnapshotEntry = (entry) => entry.type === 'create' || entry.type === 'edit';
547
+ /** True when at least one edit has been saved for this post. */
548
+ export const postHasEditHistory = (entries) => !!entries?.some((entry) => entry.type === 'edit');
549
+ /**
550
+ * Saved versions of a post. The create entry is the original; each edit entry
551
+ * stores the content written at that save. Oldest first unless `order` is newest.
552
+ * Returns nothing until the post has been edited.
553
+ */
554
+ export const postEditVersions = (entries, order = 'oldest') => {
555
+ if (!postHasEditHistory(entries))
556
+ return [];
557
+ const chronological = (entries ?? [])
558
+ .map((entry, index) => ({ entry, index }))
559
+ .filter((item) => isSnapshotEntry(item.entry))
560
+ .sort((a, b) => Date.parse(a.entry.createdAt) - Date.parse(b.entry.createdAt) ||
561
+ a.index - b.index);
562
+ const last = chronological.length - 1;
563
+ const versions = chronological.map(({ entry, index }, position) => ({
564
+ id: `${entry.type}-${entry.createdAt}-${index}`,
565
+ createdAt: entry.createdAt,
566
+ data: entry.data,
567
+ profile: entry.profile,
568
+ kind: entry.type === 'create' ? 'original' : 'edit',
569
+ current: position === last,
570
+ }));
571
+ return order === 'newest' ? [...versions].reverse() : versions;
572
+ };
573
+ export const postEditVersionKind = (version) => version.current ? 'current' : version.kind;
574
+ export const postEditVersionTitle = (data) => {
575
+ if (data.type === 'article' || data.type === 'resource') {
576
+ return data.title || undefined;
577
+ }
578
+ if (data.type === 'event')
579
+ return data.name || undefined;
580
+ return undefined;
581
+ };
541
582
  export const isOneToOneWithBlocked = (post, viewer, blockedIds) => {
542
583
  if (!viewer || post.visibility !== 'direct')
543
584
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/common",
3
- "version": "0.6.4",
3
+ "version": "0.7.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -813,7 +813,11 @@ export const getPostActionAvailability = (
813
813
  export const HIDDEN_AUTHOR_HANDLE = 'hidden';
814
814
 
815
815
  export const toHiddenPost = <
816
- T extends { data: PostDataUnion; profile: PublicProfile },
816
+ T extends {
817
+ data: PostDataUnion;
818
+ profile: PublicProfile;
819
+ entries?: EntryWithPublicProfile[];
820
+ },
817
821
  >(
818
822
  post: T,
819
823
  ): T & { hidden: true } =>
@@ -824,6 +828,13 @@ export const toHiddenPost = <
824
828
  ...post.data,
825
829
  ...('content' in post.data ? { content: '' } : {}),
826
830
  },
831
+ // create/edit entries each carry a full content snapshot; dropping them
832
+ // keeps the redacted text out of the hidden post's serialized payload.
833
+ entries: post.entries
834
+ ? post.entries.filter(
835
+ (entry) => entry.type !== 'create' && entry.type !== 'edit',
836
+ )
837
+ : post.entries,
827
838
  profile: {
828
839
  ...post.profile,
829
840
  displayName: undefined,
@@ -874,6 +885,85 @@ export const isConversationLeavePost = (
874
885
  return !post.audience?.some((p) => p.id === authorId);
875
886
  };
876
887
 
888
+ export type PostVersionOrder = 'oldest' | 'newest';
889
+
890
+ export type PostEditVersion = {
891
+ id: string;
892
+ createdAt: string;
893
+ data: PostDataUnion;
894
+ /** Author snapshot of the entry that produced this version. */
895
+ profile: EntryWithPublicProfile['profile'];
896
+ /** Create entry is the original text. Each edit entry is that save's text. */
897
+ kind: 'original' | 'edit';
898
+ current: boolean;
899
+ };
900
+
901
+ type SnapshotEntry = Extract<
902
+ EntryWithPublicProfile,
903
+ { type: 'create' | 'edit' }
904
+ >;
905
+
906
+ const isSnapshotEntry = (
907
+ entry: EntryWithPublicProfile,
908
+ ): entry is SnapshotEntry => entry.type === 'create' || entry.type === 'edit';
909
+
910
+ /** True when at least one edit has been saved for this post. */
911
+ export const postHasEditHistory = (
912
+ entries: readonly EntryWithPublicProfile[] | undefined,
913
+ ): boolean => !!entries?.some((entry) => entry.type === 'edit');
914
+
915
+ /**
916
+ * Saved versions of a post. The create entry is the original; each edit entry
917
+ * stores the content written at that save. Oldest first unless `order` is newest.
918
+ * Returns nothing until the post has been edited.
919
+ */
920
+ export const postEditVersions = (
921
+ entries: readonly EntryWithPublicProfile[] | undefined,
922
+ order: PostVersionOrder = 'oldest',
923
+ ): PostEditVersion[] => {
924
+ if (!postHasEditHistory(entries)) return [];
925
+
926
+ const chronological = (entries ?? [])
927
+ .map((entry, index) => ({ entry, index }))
928
+ .filter((item): item is { entry: SnapshotEntry; index: number } =>
929
+ isSnapshotEntry(item.entry),
930
+ )
931
+ .sort(
932
+ (a, b) =>
933
+ Date.parse(a.entry.createdAt) - Date.parse(b.entry.createdAt) ||
934
+ a.index - b.index,
935
+ );
936
+
937
+ const last = chronological.length - 1;
938
+ const versions = chronological.map(
939
+ ({ entry, index }, position): PostEditVersion => ({
940
+ id: `${entry.type}-${entry.createdAt}-${index}`,
941
+ createdAt: entry.createdAt,
942
+ data: entry.data,
943
+ profile: entry.profile,
944
+ kind: entry.type === 'create' ? 'original' : 'edit',
945
+ current: position === last,
946
+ }),
947
+ );
948
+
949
+ return order === 'newest' ? [...versions].reverse() : versions;
950
+ };
951
+
952
+ export const postEditVersionKind = (
953
+ version: Pick<PostEditVersion, 'current' | 'kind'>,
954
+ ): 'original' | 'edit' | 'current' =>
955
+ version.current ? 'current' : version.kind;
956
+
957
+ export const postEditVersionTitle = (
958
+ data: PostDataUnion,
959
+ ): string | undefined => {
960
+ if (data.type === 'article' || data.type === 'resource') {
961
+ return data.title || undefined;
962
+ }
963
+ if (data.type === 'event') return data.name || undefined;
964
+ return undefined;
965
+ };
966
+
877
967
  export const isOneToOneWithBlocked = (
878
968
  post: Pick<PublicPost, 'visibility' | 'audience' | 'profile'> & {
879
969
  creatorId?: string;