page.ts 4.6 KB

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