فهرست منبع

Merge pull request #4511 from weseek/feat/79710-when-a-comment-is-created-emit-the-user-id-to-the-front-side-for-notification

Feat/79710 when a comment is created emit the user id to the front side for notification
Haku Mizuki 4 سال پیش
والد
کامیت
52389cf39c

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

@@ -19,7 +19,6 @@ const logger = loggerFactory('growi:InAppNotificationDropdown');
 type Props = {
   appContainer: AppContainer,
   socketIoContainer: SocketIoContainer,
-  me: string,
 };
 
 const InAppNotificationDropdown: FC<Props> = (props: Props) => {
@@ -36,10 +35,9 @@ const InAppNotificationDropdown: FC<Props> = (props: Props) => {
   }, []);
 
   const initializeSocket = (props) => {
-    console.log(props);
-
     const socket = props.socketIoContainer.getSocket();
-    socket.on('commentUpdated', (data: { userId: string, count: number }) => {
+    socket.on('notificationUpdated', (data: { userId: string, count: number }) => {
+      setCount(data.count);
       // eslint-disable-next-line no-console
       console.log('socketData', data);
     });

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

@@ -39,6 +39,7 @@ class CommentService {
         let targetUsers: Types.ObjectId[] = [];
         targetUsers = await savedActivity.getNotificationTargetUsers();
 
+        await this.inAppNotificationService.emitSocketIo(targetUsers);
         await this.inAppNotificationService.upsertByActivity(targetUsers, savedActivity);
       }
       catch (err) {
@@ -50,9 +51,10 @@ class CommentService {
     // update
     this.commentEvent.on('update', (userId, pageId) => {
       this.commentEvent.onUpdate();
-      const { inAppNotificationService } = this.crowi;
 
-      inAppNotificationService.emitSocketIo(userId, pageId);
+      // TODO: 79713
+      // const { inAppNotificationService } = this.crowi;
+      // inAppNotificationService.emitSocketIo(userId, pageId);
     });
 
     // remove
@@ -76,6 +78,7 @@ class CommentService {
   createByPageComment = function(comment) {
     const { activityService } = this.crowi;
 
+    // TODO: Changing the action name in Create and Update
 
     const parameters = {
       user: comment.creator,

+ 9 - 8
packages/app/src/server/service/in-app-notification.ts

@@ -29,15 +29,16 @@ export default class InAppNotificationService {
   }
 
 
-  emitSocketIo = async(userId, pageId) => {
+  emitSocketIo = async(targetUsers) => {
     if (this.socketIoService.isInitialized) {
-      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 });
+      targetUsers.forEach(async(userId) => {
+        const count = await this.getUnreadCountByUser(userId);
+
+        // emit to the room for each user
+        await this.socketIoService.getDefaultSocket()
+          .in(getRoomNameWithId(RoomPrefix.USER, userId))
+          .emit('notificationUpdated', { userId, count });
+      });
     }
   }