page-operation.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { pagePathUtils } from '@growi/core';
  2. import { IPageOperationProcessInfo, IPageOperationProcessData } from '~/interfaces/page-operation';
  3. import PageOperation, { PageActionType, PageActionStage, PageOperationDocument } from '~/server/models/page-operation';
  4. import loggerFactory from '~/utils/logger';
  5. import { ObjectIdLike } from '../interfaces/mongoose-utils';
  6. const logger = loggerFactory('growi:services:page-operation');
  7. const { isEitherOfPathAreaOverlap, isPathAreaOverlap, isTrashPage } = pagePathUtils;
  8. const AUTO_UPDATE_INTERVAL_SEC = 5;
  9. const {
  10. Duplicate, Delete, DeleteCompletely, Revert, NormalizeParent,
  11. } = PageActionType;
  12. class PageOperationService {
  13. crowi: any;
  14. constructor(crowi) {
  15. this.crowi = crowi;
  16. }
  17. async init(): Promise<void> {
  18. // cleanup PageOperation documents except ones with actionType: Rename
  19. const types = [Duplicate, Delete, DeleteCompletely, Revert, NormalizeParent];
  20. await PageOperation.deleteByActionTypes(types);
  21. }
  22. /**
  23. * Execute functions that should be run after the express server is ready.
  24. */
  25. async afterExpressServerReady(): Promise<void> {
  26. try {
  27. // execute rename operation
  28. await this.executeAllRenameOperationBySystem();
  29. }
  30. catch (err) {
  31. logger.error(err);
  32. }
  33. }
  34. /**
  35. * Execute renameSubOperation on every page operation for rename ordered by createdAt ASC
  36. */
  37. private async executeAllRenameOperationBySystem(): Promise<void> {
  38. const Page = this.crowi.model('Page');
  39. const pageOps = await PageOperation.find({ actionType: PageActionType.Rename, actionStage: PageActionStage.Sub })
  40. .sort({ createdAt: 'asc' });
  41. if (pageOps.length === 0) return;
  42. for await (const pageOp of pageOps) {
  43. const {
  44. page, toPath, options, user,
  45. } = pageOp;
  46. const renamedPage = await Page.findById(pageOp.page._id);
  47. if (renamedPage == null) {
  48. logger.warn('operating page is not found');
  49. continue;
  50. }
  51. // rename
  52. await this.crowi.pageService.renameSubOperation(page, toPath, user, options, renamedPage, pageOp._id);
  53. }
  54. }
  55. /**
  56. * Check if the operation is operatable
  57. * @param isRecursively Boolean that determines whether the operation is recursive or not
  58. * @param fromPathToOp The path to operate from
  59. * @param toPathToOp The path to operate to
  60. * @returns boolean
  61. */
  62. async canOperate(isRecursively: boolean, fromPathToOp: string | null, toPathToOp: string | null): Promise<boolean> {
  63. const pageOperations = await PageOperation.find();
  64. if (pageOperations.length === 0) {
  65. return true;
  66. }
  67. const fromPaths = pageOperations.map(op => op.fromPath).filter((p): p is string => p != null);
  68. const toPaths = pageOperations.map(op => op.toPath).filter((p): p is string => p != null);
  69. if (isRecursively) {
  70. if (fromPathToOp != null && !isTrashPage(fromPathToOp)) {
  71. const fromFlag = fromPaths.some(p => isEitherOfPathAreaOverlap(p, fromPathToOp));
  72. if (fromFlag) return false;
  73. const toFlag = toPaths.some(p => isEitherOfPathAreaOverlap(p, fromPathToOp));
  74. if (toFlag) return false;
  75. }
  76. if (toPathToOp != null && !isTrashPage(toPathToOp)) {
  77. const fromFlag = fromPaths.some(p => isPathAreaOverlap(p, toPathToOp));
  78. if (fromFlag) return false;
  79. const toFlag = toPaths.some(p => isPathAreaOverlap(p, toPathToOp));
  80. if (toFlag) return false;
  81. }
  82. }
  83. else {
  84. if (fromPathToOp != null && !isTrashPage(fromPathToOp)) {
  85. const fromFlag = fromPaths.some(p => isPathAreaOverlap(p, fromPathToOp));
  86. if (fromFlag) return false;
  87. const toFlag = toPaths.some(p => isPathAreaOverlap(p, fromPathToOp));
  88. if (toFlag) return false;
  89. }
  90. if (toPathToOp != null && !isTrashPage(toPathToOp)) {
  91. const fromFlag = fromPaths.some(p => isPathAreaOverlap(p, toPathToOp));
  92. if (fromFlag) return false;
  93. const toFlag = toPaths.some(p => isPathAreaOverlap(p, toPathToOp));
  94. if (toFlag) return false;
  95. }
  96. }
  97. return true;
  98. }
  99. /**
  100. * Generate object that connects page id with processData of PageOperation.
  101. * The processData is a combination of actionType as a key and information on whether the action is processable as a value.
  102. */
  103. generateProcessInfo(pageOps: PageOperationDocument[]): IPageOperationProcessInfo {
  104. const processInfo: IPageOperationProcessInfo = {};
  105. pageOps.forEach((pageOp) => {
  106. const pageId = pageOp.page._id.toString();
  107. const actionType = pageOp.actionType;
  108. const isProcessable = pageOp.isProcessable();
  109. // processData for processInfo
  110. const processData: IPageOperationProcessData = { [actionType]: { isProcessable } };
  111. // Merge processData if other processData exist
  112. if (processInfo[pageId] != null) {
  113. const otherProcessData = processInfo[pageId];
  114. processInfo[pageId] = { ...otherProcessData, ...processData };
  115. return;
  116. }
  117. // add new process data to processInfo
  118. processInfo[pageId] = processData;
  119. });
  120. return processInfo;
  121. }
  122. /**
  123. * Set interval to update unprocessableExpiryDate every AUTO_UPDATE_INTERVAL_SEC seconds.
  124. * This is used to prevent the same page operation from being processed multiple times at once
  125. */
  126. autoUpdateExpiryDate(operationId: ObjectIdLike): NodeJS.Timeout {
  127. // https://github.com/Microsoft/TypeScript/issues/30128#issuecomment-651877225
  128. const timerObj = global.setInterval(async() => {
  129. await PageOperation.extendExpiryDate(operationId);
  130. }, AUTO_UPDATE_INTERVAL_SEC * 1000);
  131. return timerObj;
  132. }
  133. clearAutoUpdateInterval(timerObj: NodeJS.Timeout): void {
  134. clearInterval(timerObj);
  135. }
  136. }
  137. export default PageOperationService;