page.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. expandContentWidth?: boolean,
  33. }
  34. export type IPagePopulatedToList = Omit<IPageHasId, 'lastUpdateUser'> & {
  35. lastUpdateUser: IUserHasId,
  36. }
  37. export type IPagePopulatedToShowRevision = Omit<IPageHasId, 'lastUpdateUser'|'creator'|'deleteUser'|'grantedGroup'|'revision'|'author'> & {
  38. lastUpdateUser: IUserHasId,
  39. creator: IUserHasId,
  40. deleteUser: IUserHasId,
  41. grantedGroup: IUserGroupHasId,
  42. revision: IRevisionHasId,
  43. author: IUserHasId,
  44. }
  45. export const PageGrant = {
  46. GRANT_PUBLIC: 1,
  47. GRANT_RESTRICTED: 2,
  48. GRANT_SPECIFIED: 3, // DEPRECATED
  49. GRANT_OWNER: 4,
  50. GRANT_USER_GROUP: 5,
  51. } as const;
  52. type UnionPageGrantKeys = keyof typeof PageGrant;
  53. export type PageGrant = typeof PageGrant[UnionPageGrantKeys];
  54. /**
  55. * Neither pages with grant `GRANT_RESTRICTED` nor `GRANT_SPECIFIED` can be on a page tree.
  56. */
  57. export type PageGrantCanBeOnTree = typeof PageGrant[Exclude<UnionPageGrantKeys, 'GRANT_RESTRICTED' | 'GRANT_SPECIFIED'>];
  58. export const PageStatus = {
  59. STATUS_PUBLISHED: 'published',
  60. STATUS_DELETED: 'deleted',
  61. } as const;
  62. export type PageStatus = typeof PageStatus[keyof typeof PageStatus];
  63. export type IPageHasId = IPage & HasObjectId;
  64. export type IPageInfo = {
  65. isV5Compatible: boolean,
  66. isEmpty: boolean,
  67. isMovable: 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. }
  80. export type IPageInfoForOperation = IPageInfoForEntity & {
  81. isBookmarked?: boolean,
  82. isLiked?: boolean,
  83. subscriptionStatus?: SubscriptionStatusType,
  84. }
  85. export type IPageInfoForListing = IPageInfoForEntity & HasRevisionShortbody;
  86. export type IPageInfoAll = IPageInfo | IPageInfoForEntity | IPageInfoForOperation | IPageInfoForListing;
  87. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  88. export const isIPageInfoForEntity = (pageInfo: any | undefined): pageInfo is IPageInfoForEntity => {
  89. return pageInfo != null;
  90. };
  91. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  92. export const isIPageInfoForOperation = (pageInfo: any | undefined): pageInfo is IPageInfoForOperation => {
  93. return pageInfo != null
  94. && isIPageInfoForEntity(pageInfo)
  95. && ('isBookmarked' in pageInfo || 'isLiked' in pageInfo || 'subscriptionStatus' in pageInfo);
  96. };
  97. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  98. export const isIPageInfoForListing = (pageInfo: any | undefined): pageInfo is IPageInfoForListing => {
  99. return pageInfo != null
  100. && isIPageInfoForEntity(pageInfo)
  101. && 'revisionShortBody' in pageInfo;
  102. };
  103. // export type IPageInfoTypeResolver<T extends IPageInfo> =
  104. // T extends HasRevisionShortbody ? IPageInfoForListing :
  105. // T extends { isBookmarked?: boolean } | { isLiked?: boolean } | { subscriptionStatus?: SubscriptionStatusType } ? IPageInfoForOperation :
  106. // T extends { bookmarkCount: number } ? IPageInfoForEntity :
  107. // T extends { isEmpty: number } ? IPageInfo :
  108. // T;
  109. /**
  110. * Union Distribution
  111. * @param pageInfo
  112. * @returns
  113. */
  114. // export const resolvePageInfo = <T extends IPageInfo>(pageInfo: T | undefined): IPageInfoTypeResolver<T> => {
  115. // return <IPageInfoTypeResolver<T>>pageInfo;
  116. // };
  117. export type IDataWithMeta<D = unknown, M = unknown> = {
  118. data: D,
  119. meta?: M,
  120. }
  121. export type IPageWithMeta<M = IPageInfoAll> = IDataWithMeta<IPageHasId, M>;
  122. export type IPageToDeleteWithMeta<T = IPageInfoForEntity | unknown> = IDataWithMeta<HasObjectId & (IPage | { path: string, revision: string | null}), T>;
  123. export type IPageToRenameWithMeta<T = IPageInfoForEntity | unknown> = IPageToDeleteWithMeta<T>;