page.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import type { Ref } from './common';
  2. import type { HasObjectId } from './has-object-id';
  3. import type {
  4. HasRevisionShortbody,
  5. IRevision,
  6. IRevisionHasId,
  7. } from './revision';
  8. import type { SubscriptionStatusType } from './subscription';
  9. import type { ITag } from './tag';
  10. import type { IUser, IUserGroup, IUserGroupHasId, IUserHasId } from './user';
  11. export const GroupType = {
  12. userGroup: 'UserGroup',
  13. externalUserGroup: 'ExternalUserGroup',
  14. } as const;
  15. export type GroupType = (typeof GroupType)[keyof typeof GroupType];
  16. export type IGrantedGroup = {
  17. type: GroupType;
  18. item: Ref<IUserGroup>;
  19. };
  20. export type IPage = {
  21. path: string;
  22. status: string;
  23. revision?: Ref<IRevision>;
  24. tags: Ref<ITag>[];
  25. creator?: Ref<IUser>;
  26. createdAt: Date;
  27. updatedAt: Date;
  28. seenUsers: Ref<IUser>[];
  29. parent: Ref<IPage> | null;
  30. descendantCount: number;
  31. isEmpty: boolean;
  32. grant: PageGrant;
  33. grantedUsers: Ref<IUser>[];
  34. grantedGroups: IGrantedGroup[];
  35. lastUpdateUser?: Ref<IUser>;
  36. liker: Ref<IUser>[];
  37. commentCount: number;
  38. slackChannels: string;
  39. deleteUser: Ref<IUser>;
  40. deletedAt: Date;
  41. latestRevision?: Ref<IRevision>;
  42. latestRevisionBodyLength?: number;
  43. expandContentWidth?: boolean;
  44. wip?: boolean;
  45. ttlTimestamp?: Date;
  46. };
  47. export type IPagePopulatedToShowRevision = Omit<
  48. IPageHasId,
  49. | 'lastUpdateUser'
  50. | 'creator'
  51. | 'deleteUser'
  52. | 'grantedGroups'
  53. | 'revision'
  54. | 'author'
  55. > & {
  56. lastUpdateUser?: IUserHasId;
  57. creator?: IUserHasId;
  58. deleteUser: IUserHasId;
  59. grantedGroups: { type: GroupType; item: IUserGroupHasId }[];
  60. revision?: IRevisionHasId;
  61. author: IUserHasId;
  62. };
  63. export const PageGrant = {
  64. GRANT_PUBLIC: 1,
  65. GRANT_RESTRICTED: 2,
  66. GRANT_SPECIFIED: 3, // DEPRECATED
  67. GRANT_OWNER: 4,
  68. GRANT_USER_GROUP: 5,
  69. } as const;
  70. type UnionPageGrantKeys = keyof typeof PageGrant;
  71. export type PageGrant = (typeof PageGrant)[UnionPageGrantKeys];
  72. export const PageStatus = {
  73. STATUS_PUBLISHED: 'published',
  74. STATUS_DELETED: 'deleted',
  75. } as const;
  76. export type PageStatus = (typeof PageStatus)[keyof typeof PageStatus];
  77. export type IPageHasId = IPage & HasObjectId;
  78. export type IPageInfo = {
  79. isV5Compatible: boolean;
  80. isEmpty: boolean;
  81. isMovable: boolean;
  82. isDeletable: boolean;
  83. isAbleToDeleteCompletely: boolean;
  84. isRevertible: boolean;
  85. };
  86. export type IPageInfoForEntity = IPageInfo & {
  87. bookmarkCount: number;
  88. sumOfLikers: number;
  89. likerIds: string[];
  90. sumOfSeenUsers: number;
  91. seenUserIds: string[];
  92. contentAge: number;
  93. descendantCount: number;
  94. commentCount: number;
  95. };
  96. export type IPageInfoForOperation = IPageInfoForEntity & {
  97. isBookmarked?: boolean;
  98. isLiked?: boolean;
  99. subscriptionStatus?: SubscriptionStatusType;
  100. };
  101. export type IPageInfoForListing = IPageInfoForEntity & HasRevisionShortbody;
  102. export type IPageInfoAll =
  103. | IPageInfo
  104. | IPageInfoForEntity
  105. | IPageInfoForOperation
  106. | IPageInfoForListing;
  107. export const isIPageInfo = (
  108. // biome-ignore lint/suspicious/noExplicitAny: ignore
  109. pageInfo: any | undefined,
  110. ): pageInfo is IPageInfo => {
  111. return (
  112. pageInfo != null && pageInfo instanceof Object && 'isEmpty' in pageInfo
  113. );
  114. };
  115. export const isIPageInfoForEntity = (
  116. // biome-ignore lint/suspicious/noExplicitAny: ignore
  117. pageInfo: any | undefined,
  118. ): pageInfo is IPageInfoForEntity => {
  119. return isIPageInfo(pageInfo) && pageInfo.isEmpty === false;
  120. };
  121. export const isIPageInfoForOperation = (
  122. // biome-ignore lint/suspicious/noExplicitAny: ignore
  123. pageInfo: any | undefined,
  124. ): pageInfo is IPageInfoForOperation => {
  125. return (
  126. isIPageInfoForEntity(pageInfo) &&
  127. ('isBookmarked' in pageInfo ||
  128. 'isLiked' in pageInfo ||
  129. 'subscriptionStatus' in pageInfo)
  130. );
  131. };
  132. export const isIPageInfoForListing = (
  133. // biome-ignore lint/suspicious/noExplicitAny: ignore
  134. pageInfo: any | undefined,
  135. ): pageInfo is IPageInfoForListing => {
  136. return isIPageInfoForEntity(pageInfo) && 'revisionShortBody' in pageInfo;
  137. };
  138. // export type IPageInfoTypeResolver<T extends IPageInfo> =
  139. // T extends HasRevisionShortbody ? IPageInfoForListing :
  140. // T extends { isBookmarked?: boolean } | { isLiked?: boolean } | { subscriptionStatus?: SubscriptionStatusType } ? IPageInfoForOperation :
  141. // T extends { bookmarkCount: number } ? IPageInfoForEntity :
  142. // T extends { isEmpty: number } ? IPageInfo :
  143. // T;
  144. /**
  145. * Union Distribution
  146. * @param pageInfo
  147. * @returns
  148. */
  149. // export const resolvePageInfo = <T extends IPageInfo>(pageInfo: T | undefined): IPageInfoTypeResolver<T> => {
  150. // return <IPageInfoTypeResolver<T>>pageInfo;
  151. // };
  152. export type IDataWithMeta<D = unknown, M = unknown> = {
  153. data: D;
  154. meta?: M;
  155. };
  156. export type IPageWithMeta<M = IPageInfoAll> = IDataWithMeta<IPageHasId, M>;
  157. export type IPageToDeleteWithMeta<T = IPageInfoForEntity | unknown> =
  158. IDataWithMeta<
  159. HasObjectId & (IPage | { path: string; revision: string | null }),
  160. T
  161. >;
  162. export type IPageToRenameWithMeta<T = IPageInfoForEntity | unknown> =
  163. IPageToDeleteWithMeta<T>;