Просмотр исходного кода

Merge pull request #4464 from weseek/feat/78834-fetch-in-app-notification-status

Feat/78834 fetch in app notification status
Shun Miyazawa 4 лет назад
Родитель
Сommit
900745ee85

+ 4 - 22
packages/app/src/components/InAppNotification/InAppNotificationDropdown.tsx

@@ -4,6 +4,7 @@ import {
 } from 'reactstrap';
 } from 'reactstrap';
 import PropTypes from 'prop-types';
 import PropTypes from 'prop-types';
 import AppContainer from '~/client/services/AppContainer';
 import AppContainer from '~/client/services/AppContainer';
+import { toastError } from '~/client/util/apiNotification';
 import { withUnstatedContainers } from '../UnstatedUtils';
 import { withUnstatedContainers } from '../UnstatedUtils';
 import { InAppNotification as IInAppNotification } from '../../interfaces/in-app-notification';
 import { InAppNotification as IInAppNotification } from '../../interfaces/in-app-notification';
 // import DropdownMenu from './InAppNotificationDropdown/DropdownMenu';
 // import DropdownMenu from './InAppNotificationDropdown/DropdownMenu';
@@ -33,43 +34,24 @@ const InAppNotificationDropdown: FC = (props) => {
   useEffect(() => {
   useEffect(() => {
     initializeSocket(props);
     initializeSocket(props);
     fetchNotificationList(props);
     fetchNotificationList(props);
-    // fetchNotificationStatus();
   }, []);
   }, []);
 
 
   const initializeSocket = (props) => {
   const initializeSocket = (props) => {
     console.log(props);
     console.log(props);
 
 
     const socket = props.socketIoContainer.getSocket();
     const socket = props.socketIoContainer.getSocket();
-    socket.on('comment updated', (data: { user: string }) => {
+    socket.on('commentUpdated', (data: { userId: string, count: number }) => {
       // eslint-disable-next-line no-console
       // eslint-disable-next-line no-console
       console.log('socketData', data);
       console.log('socketData', data);
 
 
-      if (props.me === data.user) {
-        // TODO: Fetch notification status by #78563
+      if (props.me === data.userId) {
+        // TODO: Fetch notification list by #78557
         // fetchNotificationList();
         // fetchNotificationList();
 
 
-        // TODO: Fetch notification list by #78557
-        // fetchNotificationStatus();
       }
       }
     });
     });
   };
   };
 
 
-
-  /**
-    * TODO: Fetch notification status by #78563
-    */
-  // async fetchNotificationStatus() {
-  //   try {
-  //     const { count = null } = await this.props.crowi.apiGet('/notification.status');
-  //     if (count !== null && count !== this.state.count) {
-  //       this.setState({ count });
-  //     }
-  //   }
-  //   catch (err) {
-  //     // TODO: error handling
-  //   }
-  // }
-
   const updateNotificationStatus = () => {
   const updateNotificationStatus = () => {
     try {
     try {
       // await this.props.crowi.apiPost('/notification.read');
       // await this.props.crowi.apiPost('/notification.read');

+ 1 - 1
packages/app/src/server/models/comment.js

@@ -73,7 +73,7 @@ module.exports = function(crowi) {
       { $set: { comment, isMarkdown } },
       { $set: { comment, isMarkdown } },
     );
     );
 
 
-    await commentEvent.emit('update', commentData.creator);
+    await commentEvent.emit('update', commentData.creator, commentData.page);
 
 
     return commentData;
     return commentData;
   };
   };

+ 2 - 3
packages/app/src/server/routes/apiv3/in-app-notification.ts

@@ -32,10 +32,9 @@ module.exports = (crowi) => {
   });
   });
 
 
   router.get('/status', accessTokenParser, loginRequiredStrictly, async(req, res) => {
   router.get('/status', accessTokenParser, loginRequiredStrictly, async(req, res) => {
-    const user = req.user;
-
+    const userId = req.user._id;
     try {
     try {
-      const count = await InAppNotification.getUnreadCountByUser(user._id);
+      const count = await inAppNotificationService.getUnreadCountByUser(userId);
       const result = { count };
       const result = { count };
       return res.apiv3(result);
       return res.apiv3(result);
     }
     }

+ 2 - 2
packages/app/src/server/service/comment.ts

@@ -51,11 +51,11 @@ class CommentService {
     });
     });
 
 
     // update
     // update
-    this.commentEvent.on('update', (user) => {
+    this.commentEvent.on('update', (userId, pageId) => {
       this.commentEvent.onUpdate();
       this.commentEvent.onUpdate();
       const { inAppNotificationService } = this.crowi;
       const { inAppNotificationService } = this.crowi;
 
 
-      inAppNotificationService.emitSocketIo(user);
+      inAppNotificationService.emitSocketIo(userId, pageId);
     });
     });
 
 
     // remove
     // remove

+ 11 - 2
packages/app/src/server/service/in-app-notification.ts

@@ -7,6 +7,7 @@ import {
 import { ActivityDocument } from '~/server/models/activity';
 import { ActivityDocument } from '~/server/models/activity';
 
 
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
+import { RoomPrefix, getRoomNameWithId } from '../util/socket-io-helpers';
 
 
 const logger = loggerFactory('growi:service:inAppNotification');
 const logger = loggerFactory('growi:service:inAppNotification');
 
 
@@ -26,12 +27,20 @@ export default class InAppNotificationService {
     this.crowi = crowi;
     this.crowi = crowi;
     this.socketIoService = crowi.socketIoService;
     this.socketIoService = crowi.socketIoService;
     this.activityEvent = crowi.event('activity');
     this.activityEvent = crowi.event('activity');
+
+    this.getUnreadCountByUser = this.getUnreadCountByUser.bind(this);
   }
   }
 
 
 
 
-  emitSocketIo = async(user) => {
+  emitSocketIo = async(userId, pageId) => {
     if (this.socketIoService.isInitialized) {
     if (this.socketIoService.isInitialized) {
-      await this.socketIoService.getDefaultSocket().emit('comment updated', { user });
+      const count = await this.getUnreadCountByUser(userId);
+
+      // emit to the room for each page
+      await this.socketIoService.getDefaultSocket()
+        .in(getRoomNameWithId(RoomPrefix.PAGE, pageId))
+        .except(getRoomNameWithId(RoomPrefix.USER, userId))
+        .emit('commentUpdated', { userId, count });
     }
     }
   }
   }