Răsfoiți Sursa

Implement page update activity generation rules

arvid-e 1 lună în urmă
părinte
comite
f548d62214
1 a modificat fișierele cu 35 adăugiri și 23 ștergeri
  1. 35 23
      apps/app/src/server/routes/apiv3/page/update-page.ts

+ 35 - 23
apps/app/src/server/routes/apiv3/page/update-page.ts

@@ -118,29 +118,41 @@ export const updatePageHandlersFactory = (crowi: Crowi): RequestHandler[] => {
       await yjsService.syncWithTheLatestRevisionForce(req.body.pageId);
     }
 
-    // REMINDER: values I need
-    // - The user's latest edit was on current page or not
-    // - Time since last edit on current page (5 min)
-    // - If it is the first edit
-
-    // persist activity
-    const creator =
-      updatedPage.creator != null
-        ? getIdForRef(updatedPage.creator)
-        : undefined;
-    const parameters = {
-      targetModel: SupportedTargetModel.MODEL_PAGE,
-      target: updatedPage,
-      action: SupportedAction.ACTION_PAGE_UPDATE,
-    };
-    const activityEvent = crowi.events.activity;
-    activityEvent.emit(
-      'update',
-      res.locals.activity._id,
-      parameters,
-      { path: updatedPage.path, creator },
-      preNotifyService.generatePreNotify,
-    );
+    const suppressEditWindow = 5 * 60 * 1000; // 5 minutes
+    const minimumRevisionForActivity = 2;
+
+    const isLastUpdatedUser =
+      updatedPage.lastUpdateUser?.toString() === req.user.toString();
+
+    const timeSinceLastUpdate =
+      (Date.now() - updatedPage.updatedAt.getTime()) / (1000 * 60);
+
+    const isOutsideSuppressionWindow = timeSinceLastUpdate > suppressEditWindow;
+    const hasEnoughRevisions = updatedPage.__v > minimumRevisionForActivity;
+
+    const shouldGenerateUpdateActivity =
+      !isLastUpdatedUser || (isOutsideSuppressionWindow && hasEnoughRevisions);
+
+    if (shouldGenerateUpdateActivity) {
+      // persist activity
+      const creator =
+        updatedPage.creator != null
+          ? getIdForRef(updatedPage.creator)
+          : undefined;
+      const parameters = {
+        targetModel: SupportedTargetModel.MODEL_PAGE,
+        target: updatedPage,
+        action: SupportedAction.ACTION_PAGE_UPDATE,
+      };
+      const activityEvent = crowi.events.activity;
+      activityEvent.emit(
+        'update',
+        res.locals.activity._id,
+        parameters,
+        { path: updatedPage.path, creator },
+        preNotifyService.generatePreNotify,
+      );
+    }
 
     // global notification
     try {