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 {
  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?: Ref<IUser>,
  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. ttlTimestamp?: Date
  41. }
  42. export type IPagePopulatedToShowRevision = Omit<IPageHasId, 'lastUpdateUser'|'creator'|'deleteUser'|'grantedGroups'|'revision'|'author'> & {
  43. lastUpdateUser?: IUserHasId,
  44. creator?: IUserHasId,
  45. deleteUser: IUserHasId,
  46. grantedGroups: { type: GroupType, item: IUserGroupHasId }[],
  47. revision?: IRevisionHasId,
  48. author: IUserHasId,
  49. }
  50. export const PageGrant = {
  51. GRANT_PUBLIC: 1,
  52. GRANT_RESTRICTED: 2,
  53. GRANT_SPECIFIED: 3, // DEPRECATED
  54. GRANT_OWNER: 4,
  55. GRANT_USER_GROUP: 5,
  56. } as const;
  57. type UnionPageGrantKeys = keyof typeof PageGrant;
  58. export type PageGrant = typeof PageGrant[UnionPageGrantKeys];
  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. descendantCount: number,
  81. commentCount: number,
  82. }
  83. export type IPageInfoForOperation = IPageInfoForEntity & {
  84. isBookmarked?: boolean,
  85. isLiked?: boolean,
  86. subscriptionStatus?: SubscriptionStatusType,
  87. }
  88. export type IPageInfoForListing = IPageInfoForEntity & HasRevisionShortbody;
  89. export type IPageInfoAll = IPageInfo | IPageInfoForEntity | IPageInfoForOperation | IPageInfoForListing;
  90. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  91. export const isIPageInfoForEntity = (pageInfo: any | undefined): pageInfo is IPageInfoForEntity => {
  92. return pageInfo != null;
  93. };
  94. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  95. export const isIPageInfoForOperation = (pageInfo: any | undefined): pageInfo is IPageInfoForOperation => {
  96. return pageInfo != null
  97. && isIPageInfoForEntity(pageInfo)
  98. && ('isBookmarked' in pageInfo || 'isLiked' in pageInfo || 'subscriptionStatus' in pageInfo);
  99. };
  100. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  101. export const isIPageInfoForListing = (pageInfo: any | undefined): pageInfo is IPageInfoForListing => {
  102. return pageInfo != null
  103. && isIPageInfoForEntity(pageInfo)
  104. && 'revisionShortBody' in pageInfo;
  105. };
  106. // export type IPageInfoTypeResolver<T extends IPageInfo> =
  107. // T extends HasRevisionShortbody ? IPageInfoForListing :
  108. // T extends { isBookmarked?: boolean } | { isLiked?: boolean } | { subscriptionStatus?: SubscriptionStatusType } ? IPageInfoForOperation :
  109. // T extends { bookmarkCount: number } ? IPageInfoForEntity :
  110. // T extends { isEmpty: number } ? IPageInfo :
  111. // T;
  112. /**
  113. * Union Distribution
  114. * @param pageInfo
  115. * @returns
  116. */
  117. // export const resolvePageInfo = <T extends IPageInfo>(pageInfo: T | undefined): IPageInfoTypeResolver<T> => {
  118. // return <IPageInfoTypeResolver<T>>pageInfo;
  119. // };
  120. export type IDataWithMeta<D = unknown, M = unknown> = {
  121. data: D,
  122. meta?: M,
  123. }
  124. export type IPageWithMeta<M = IPageInfoAll> = IDataWithMeta<IPageHasId, M>;
  125. export type IPageToDeleteWithMeta<T = IPageInfoForEntity | unknown> = IDataWithMeta<HasObjectId & (IPage | { path: string, revision: string | null}), T>;
  126. export type IPageToRenameWithMeta<T = IPageInfoForEntity | unknown> = IPageToDeleteWithMeta<T>;