page.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. isDeletable: boolean,
  69. isAbleToDeleteCompletely: boolean,
  70. isRevertible: boolean,
  71. }
  72. export type IPageInfoForEntity = IPageInfo & {
  73. bookmarkCount: number,
  74. sumOfLikers: number,
  75. likerIds: string[],
  76. sumOfSeenUsers: number,
  77. seenUserIds: string[],
  78. contentAge: number,
  79. descendantCount: number,
  80. commentCount: number,
  81. }
  82. export type IPageInfoForOperation = IPageInfoForEntity & {
  83. isBookmarked?: boolean,
  84. isLiked?: boolean,
  85. subscriptionStatus?: SubscriptionStatusType,
  86. }
  87. export type IPageInfoForListing = IPageInfoForEntity & HasRevisionShortbody;
  88. export type IPageInfoAll = IPageInfo | IPageInfoForEntity | IPageInfoForOperation | IPageInfoForListing;
  89. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  90. export const isIPageInfoForEntity = (pageInfo: any | undefined): pageInfo is IPageInfoForEntity => {
  91. return pageInfo != null;
  92. };
  93. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  94. export const isIPageInfoForOperation = (pageInfo: any | undefined): pageInfo is IPageInfoForOperation => {
  95. return pageInfo != null
  96. && isIPageInfoForEntity(pageInfo)
  97. && ('isBookmarked' in pageInfo || 'isLiked' in pageInfo || 'subscriptionStatus' in pageInfo);
  98. };
  99. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  100. export const isIPageInfoForListing = (pageInfo: any | undefined): pageInfo is IPageInfoForListing => {
  101. return pageInfo != null
  102. && isIPageInfoForEntity(pageInfo)
  103. && 'revisionShortBody' in pageInfo;
  104. };
  105. // export type IPageInfoTypeResolver<T extends IPageInfo> =
  106. // T extends HasRevisionShortbody ? IPageInfoForListing :
  107. // T extends { isBookmarked?: boolean } | { isLiked?: boolean } | { subscriptionStatus?: SubscriptionStatusType } ? IPageInfoForOperation :
  108. // T extends { bookmarkCount: number } ? IPageInfoForEntity :
  109. // T extends { isEmpty: number } ? IPageInfo :
  110. // T;
  111. /**
  112. * Union Distribution
  113. * @param pageInfo
  114. * @returns
  115. */
  116. // export const resolvePageInfo = <T extends IPageInfo>(pageInfo: T | undefined): IPageInfoTypeResolver<T> => {
  117. // return <IPageInfoTypeResolver<T>>pageInfo;
  118. // };
  119. export type IDataWithMeta<D = unknown, M = unknown> = {
  120. data: D,
  121. meta?: M,
  122. }
  123. export type IPageWithMeta<M = IPageInfoAll> = IDataWithMeta<IPageHasId, M>;
  124. export type IPageToDeleteWithMeta<T = IPageInfoForEntity | unknown> = IDataWithMeta<HasObjectId & (IPage | { path: string, revision: string | null}), T>;
  125. export type IPageToRenameWithMeta<T = IPageInfoForEntity | unknown> = IPageToDeleteWithMeta<T>;