page.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 IGrantedGroup = {
  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: IGrantedGroup[],
  30. lastUpdateUser: Ref<IUser>,
  31. liker: Ref<IUser>[],
  32. commentCount: number
  33. slackChannels: string,
  34. deleteUser: Ref<IUser>,
  35. deletedAt: Date,
  36. latestRevision?: Ref<IRevision>,
  37. latestRevisionBodyLength?: number,
  38. expandContentWidth?: boolean,
  39. wip?: boolean,
  40. wipExpiredAt?: Date,
  41. }
  42. export type IPagePopulatedToList = Omit<IPageHasId, 'lastUpdateUser'> & {
  43. lastUpdateUser: IUserHasId,
  44. }
  45. export type IPagePopulatedToShowRevision = Omit<IPageHasId, 'lastUpdateUser'|'creator'|'deleteUser'|'grantedGroups'|'revision'|'author'> & {
  46. lastUpdateUser: IUserHasId,
  47. creator: IUserHasId | null,
  48. deleteUser: IUserHasId,
  49. grantedGroups: { type: GroupType, item: IUserGroupHasId }[],
  50. revision: IRevisionHasId,
  51. author: IUserHasId,
  52. }
  53. export const PageGrant = {
  54. GRANT_PUBLIC: 1,
  55. GRANT_RESTRICTED: 2,
  56. GRANT_SPECIFIED: 3, // DEPRECATED
  57. GRANT_OWNER: 4,
  58. GRANT_USER_GROUP: 5,
  59. } as const;
  60. type UnionPageGrantKeys = keyof typeof PageGrant;
  61. export type PageGrant = typeof PageGrant[UnionPageGrantKeys];
  62. export const PageStatus = {
  63. STATUS_PUBLISHED: 'published',
  64. STATUS_DELETED: 'deleted',
  65. } as const;
  66. export type PageStatus = typeof PageStatus[keyof typeof PageStatus];
  67. export type IPageHasId = IPage & HasObjectId;
  68. export type IPageInfo = {
  69. isV5Compatible: boolean,
  70. isEmpty: boolean,
  71. isMovable: boolean,
  72. isDeletable: boolean,
  73. isAbleToDeleteCompletely: boolean,
  74. isRevertible: boolean,
  75. }
  76. export type IPageInfoForEntity = IPageInfo & {
  77. bookmarkCount: number,
  78. sumOfLikers: number,
  79. likerIds: string[],
  80. sumOfSeenUsers: number,
  81. seenUserIds: string[],
  82. contentAge: number,
  83. descendantCount: number,
  84. commentCount: number,
  85. }
  86. export type IPageInfoForOperation = IPageInfoForEntity & {
  87. isBookmarked?: boolean,
  88. isLiked?: boolean,
  89. subscriptionStatus?: SubscriptionStatusType,
  90. }
  91. export type IPageInfoForListing = IPageInfoForEntity & HasRevisionShortbody;
  92. export type IPageInfoAll = IPageInfo | IPageInfoForEntity | IPageInfoForOperation | IPageInfoForListing;
  93. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  94. export const isIPageInfoForEntity = (pageInfo: any | undefined): pageInfo is IPageInfoForEntity => {
  95. return pageInfo != null;
  96. };
  97. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  98. export const isIPageInfoForOperation = (pageInfo: any | undefined): pageInfo is IPageInfoForOperation => {
  99. return pageInfo != null
  100. && isIPageInfoForEntity(pageInfo)
  101. && ('isBookmarked' in pageInfo || 'isLiked' in pageInfo || 'subscriptionStatus' in pageInfo);
  102. };
  103. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  104. export const isIPageInfoForListing = (pageInfo: any | undefined): pageInfo is IPageInfoForListing => {
  105. return pageInfo != null
  106. && isIPageInfoForEntity(pageInfo)
  107. && 'revisionShortBody' in pageInfo;
  108. };
  109. // export type IPageInfoTypeResolver<T extends IPageInfo> =
  110. // T extends HasRevisionShortbody ? IPageInfoForListing :
  111. // T extends { isBookmarked?: boolean } | { isLiked?: boolean } | { subscriptionStatus?: SubscriptionStatusType } ? IPageInfoForOperation :
  112. // T extends { bookmarkCount: number } ? IPageInfoForEntity :
  113. // T extends { isEmpty: number } ? IPageInfo :
  114. // T;
  115. /**
  116. * Union Distribution
  117. * @param pageInfo
  118. * @returns
  119. */
  120. // export const resolvePageInfo = <T extends IPageInfo>(pageInfo: T | undefined): IPageInfoTypeResolver<T> => {
  121. // return <IPageInfoTypeResolver<T>>pageInfo;
  122. // };
  123. export type IDataWithMeta<D = unknown, M = unknown> = {
  124. data: D,
  125. meta?: M,
  126. }
  127. export type IPageWithMeta<M = IPageInfoAll> = IDataWithMeta<IPageHasId, M>;
  128. export type IPageToDeleteWithMeta<T = IPageInfoForEntity | unknown> = IDataWithMeta<HasObjectId & (IPage | { path: string, revision: string | null}), T>;
  129. export type IPageToRenameWithMeta<T = IPageInfoForEntity | unknown> = IPageToDeleteWithMeta<T>;