page.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 IPageNotFoundInfo = {
  79. isNotFound: true;
  80. isForbidden: boolean;
  81. isEmpty?: true;
  82. };
  83. export type IPageInfo = {
  84. isNotFound: boolean;
  85. isV5Compatible: boolean;
  86. isEmpty: boolean;
  87. isMovable: boolean;
  88. isDeletable: boolean;
  89. isAbleToDeleteCompletely: boolean;
  90. isRevertible: boolean;
  91. bookmarkCount: number;
  92. };
  93. export type IPageInfoForEntity = Omit<IPageInfo, 'isNotFound' | 'isEmpty'> & {
  94. isNotFound: false;
  95. isEmpty: false;
  96. sumOfLikers: number;
  97. likerIds: string[];
  98. sumOfSeenUsers: number;
  99. seenUserIds: string[];
  100. contentAge: number;
  101. descendantCount: number;
  102. commentCount: number;
  103. latestRevisionId: Ref<IRevision>;
  104. };
  105. export type IPageInfoForOperation = IPageInfoForEntity & {
  106. isBookmarked?: boolean;
  107. isLiked?: boolean;
  108. subscriptionStatus?: SubscriptionStatusType;
  109. };
  110. export type IPageInfoForListing = IPageInfoForEntity & HasRevisionShortbody;
  111. export type IPageInfoExt =
  112. | IPageInfo
  113. | IPageInfoForEntity
  114. | IPageInfoForOperation
  115. | IPageInfoForListing;
  116. export const isIPageNotFoundInfo = (
  117. // biome-ignore lint/suspicious/noExplicitAny: ignore
  118. pageInfo: any | undefined,
  119. ): pageInfo is IPageNotFoundInfo => {
  120. return (
  121. pageInfo != null &&
  122. pageInfo instanceof Object &&
  123. pageInfo.isNotFound === true
  124. );
  125. };
  126. export const isIPageInfo = (
  127. // biome-ignore lint/suspicious/noExplicitAny: ignore
  128. pageInfo: any | undefined,
  129. ): pageInfo is IPageInfo => {
  130. return (
  131. pageInfo != null && pageInfo instanceof Object && 'isEmpty' in pageInfo
  132. );
  133. };
  134. export const isIPageInfoForEntity = (
  135. // biome-ignore lint/suspicious/noExplicitAny: ignore
  136. pageInfo: any | undefined,
  137. ): pageInfo is IPageInfoForEntity => {
  138. return isIPageInfo(pageInfo) && pageInfo.isEmpty === false;
  139. };
  140. export const isIPageInfoForOperation = (
  141. // biome-ignore lint/suspicious/noExplicitAny: ignore
  142. pageInfo: any | undefined,
  143. ): pageInfo is IPageInfoForOperation => {
  144. return (
  145. isIPageInfoForEntity(pageInfo) &&
  146. ('isBookmarked' in pageInfo ||
  147. 'isLiked' in pageInfo ||
  148. 'subscriptionStatus' in pageInfo)
  149. );
  150. };
  151. export const isIPageInfoForListing = (
  152. // biome-ignore lint/suspicious/noExplicitAny: ignore
  153. pageInfo: any | undefined,
  154. ): pageInfo is IPageInfoForListing => {
  155. return isIPageInfoForEntity(pageInfo) && 'revisionShortBody' in pageInfo;
  156. };
  157. export type IDataWithMeta<D = unknown, M = unknown> = {
  158. data: D;
  159. meta?: M;
  160. };
  161. export type IDataWithRequiredMeta<D = unknown, M = unknown> = IDataWithMeta<
  162. D,
  163. M
  164. > & { meta: M };
  165. export type IPageWithMeta<M = IPageInfoExt> = IDataWithMeta<IPageHasId, M>;
  166. export type IPageToDeleteWithMeta<T = IPageInfoForEntity | unknown> =
  167. IDataWithMeta<
  168. HasObjectId & (IPage | { path: string; revision: string | null }),
  169. T
  170. >;
  171. export type IPageToRenameWithMeta<T = IPageInfoForEntity | unknown> =
  172. IPageToDeleteWithMeta<T>;