소스 검색

Merge pull request #4705 from weseek/feat/81841-create-notification-when-page-is-deleted

feat: 81841 create notification when page is deleted
Yuki Takei 4 년 전
부모
커밋
48aaefc89b

+ 12 - 2
packages/app/src/components/InAppNotification/InAppNotificationElm.tsx

@@ -11,7 +11,8 @@ interface Props {
   notification: IInAppNotification & HasObjectId
 }
 
-const InAppNotificationElm = (props: Props): JSX.Element => {
+// TODO 81946 Return to not nullable
+const InAppNotificationElm = (props: Props): JSX.Element | null => {
 
   const { notification } = props;
 
@@ -66,7 +67,12 @@ const InAppNotificationElm = (props: Props): JSX.Element => {
   }, []);
 
   const actionUsers = getActionUsers();
-  const pagePath = { path: props.notification.target.path };
+
+  // TODO 81946 Return to not nullable
+  const pagePath = { path: props.notification.target?.path };
+  if (pagePath.path == null) {
+    return null;
+  }
 
   const actionType: string = notification.action;
   let actionMsg: string;
@@ -81,6 +87,10 @@ const InAppNotificationElm = (props: Props): JSX.Element => {
       actionMsg = 'renamed';
       actionIcon = 'icon-action-redo';
       break;
+    case 'PAGE_DELETE':
+      actionMsg = 'deleted';
+      actionIcon = 'icon-trash';
+      break;
     case 'COMMENT_CREATE':
       actionMsg = 'commented on';
       actionIcon = 'icon-bubble';

+ 9 - 3
packages/app/src/server/service/page.js

@@ -54,7 +54,15 @@ class PageService {
       }
     });
 
-    // TODO 81841
+    // delete
+    this.pageEvent.on('delete', async(page, user) => {
+      try {
+        await this.createAndSendNotifications(page, user, ActivityDefine.ACTION_PAGE_DELETE);
+      }
+      catch (err) {
+        logger.error(err);
+      }
+    });
 
     // createMany
     this.pageEvent.on('createMany', this.pageEvent.onCreateMany);
@@ -138,8 +146,6 @@ class PageService {
       await Page.create(path, body, user, { redirectTo: newPagePath });
     }
 
-    this.pageEvent.emit('delete', page, user);
-    this.pageEvent.emit('create', renamedPage, user);
     this.pageEvent.emit('rename', page, user);
 
     return renamedPage;

+ 4 - 0
packages/app/src/server/service/search.js

@@ -68,6 +68,10 @@ class SearchService {
     pageEvent.on('delete', this.delegator.syncPageDeleted.bind(this.delegator));
     pageEvent.on('updateMany', this.delegator.syncPagesUpdated.bind(this.delegator));
     pageEvent.on('syncDescendants', this.delegator.syncDescendantsPagesUpdated.bind(this.delegator));
+    pageEvent.on('rename', () => {
+      this.delegator.syncPageDeleted.bind(this.delegator);
+      this.delegator.syncPageUpdated.bind(this.delegator);
+    });
 
     const bookmarkEvent = this.crowi.event('bookmark');
     bookmarkEvent.on('create', this.delegator.syncBookmarkChanged.bind(this.delegator));

+ 3 - 0
packages/app/src/server/util/activityDefine.ts

@@ -3,6 +3,7 @@ const MODEL_COMMENT = 'Comment';
 
 const ACTION_PAGE_UPDATE = 'PAGE_UPDATE';
 const ACTION_PAGE_RENAME = 'PAGE_RENAME';
+const ACTION_PAGE_DELETE = 'PAGE_DELETE';
 const ACTION_COMMENT_CREATE = 'COMMENT_CREATE';
 const ACTION_COMMENT_UPDATE = 'COMMENT_UPDATE';
 
@@ -18,6 +19,7 @@ const getSupportActionNames = () => {
   return [
     ACTION_PAGE_UPDATE,
     ACTION_PAGE_RENAME,
+    ACTION_PAGE_DELETE,
     ACTION_COMMENT_CREATE,
     ACTION_COMMENT_UPDATE,
   ];
@@ -29,6 +31,7 @@ const activityDefine = {
 
   ACTION_PAGE_UPDATE,
   ACTION_PAGE_RENAME,
+  ACTION_PAGE_DELETE,
   ACTION_COMMENT_CREATE,
   ACTION_COMMENT_UPDATE,
 

+ 6 - 8
packages/app/src/test/integration/service/page.test.js

@@ -351,8 +351,8 @@ describe('PageService', () => {
 
         expect(xssSpy).toHaveBeenCalled();
         expect(renameDescendantsWithStreamSpy).not.toHaveBeenCalled();
-        expect(pageEventSpy).toHaveBeenCalledWith('delete', parentForRename1, testUser2);
-        expect(pageEventSpy).toHaveBeenCalledWith('create', resultPage, testUser2);
+
+        expect(pageEventSpy).toHaveBeenCalledWith('rename', parentForRename1, testUser2);
 
         expect(resultPage.path).toBe('/renamed1');
         expect(resultPage.updatedAt).toEqual(parentForRename1.updatedAt);
@@ -370,8 +370,8 @@ describe('PageService', () => {
 
         expect(xssSpy).toHaveBeenCalled();
         expect(renameDescendantsWithStreamSpy).not.toHaveBeenCalled();
-        expect(pageEventSpy).toHaveBeenCalledWith('delete', parentForRename2, testUser2);
-        expect(pageEventSpy).toHaveBeenCalledWith('create', resultPage, testUser2);
+
+        expect(pageEventSpy).toHaveBeenCalledWith('rename', parentForRename2, testUser2);
 
         expect(resultPage.path).toBe('/renamed2');
         expect(resultPage.updatedAt).toEqual(dateToUse);
@@ -389,8 +389,7 @@ describe('PageService', () => {
 
         expect(xssSpy).toHaveBeenCalled();
         expect(renameDescendantsWithStreamSpy).not.toHaveBeenCalled();
-        expect(pageEventSpy).toHaveBeenCalledWith('delete', parentForRename3, testUser2);
-        expect(pageEventSpy).toHaveBeenCalledWith('create', resultPage, testUser2);
+        expect(pageEventSpy).toHaveBeenCalledWith('rename', parentForRename3, testUser2);
 
         expect(resultPage.path).toBe('/renamed3');
         expect(resultPage.updatedAt).toEqual(parentForRename3.updatedAt);
@@ -413,8 +412,7 @@ describe('PageService', () => {
 
         expect(xssSpy).toHaveBeenCalled();
         expect(renameDescendantsWithStreamSpy).toHaveBeenCalled();
-        expect(pageEventSpy).toHaveBeenCalledWith('delete', parentForRename4, testUser2);
-        expect(pageEventSpy).toHaveBeenCalledWith('create', resultPage, testUser2);
+        expect(pageEventSpy).toHaveBeenCalledWith('rename', parentForRename4, testUser2);
 
         expect(resultPage.path).toBe('/renamed4');
         expect(resultPage.updatedAt).toEqual(parentForRename4.updatedAt);