فهرست منبع

succeeded in getting the user-id for notification in the front

Shun Miyazawa 4 سال پیش
والد
کامیت
e446d1e0d8

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

@@ -19,7 +19,7 @@ const logger = loggerFactory('growi:InAppNotificationDropdown');
 type Props = {
   appContainer: AppContainer,
   socketIoContainer: SocketIoContainer,
-  me: string,
+  currentUserId: string,
 };
 
 const InAppNotificationDropdown: FC<Props> = (props: Props) => {
@@ -36,10 +36,11 @@ 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 }) => {
+      if (data.userId === props.currentUserId) {
+        setCount(data.count);
+      }
       // eslint-disable-next-line no-console
       console.log('socketData', data);
     });

+ 1 - 1
packages/app/src/components/Navbar/GrowiNavbar.jsx

@@ -29,7 +29,7 @@ class GrowiNavbar extends React.Component {
     return (
       <>
         <li>
-          <InAppNotificationDropdown />
+          <InAppNotificationDropdown currentUserId={currentUser._id} />
         </li>
 
         <li className="nav-item d-none d-md-block">

+ 2 - 1
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) {
@@ -52,7 +53,7 @@ class CommentService {
       this.commentEvent.onUpdate();
       const { inAppNotificationService } = this.crowi;
 
-      inAppNotificationService.emitSocketIo(userId, pageId);
+      // inAppNotificationService.emitSocketIo(userId, pageId);
     });
 
     // remove

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

@@ -29,15 +29,13 @@ 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);
+        await this.socketIoService.getDefaultSocket()
+          .emit('commentUpdated', { userId, count });
+      });
     }
   }