page.ts 4.9 KB

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