Stop showing notification from invite/left rooms (#2267)
authorAjay Bura <32841439+ajbura@users.noreply.github.com>
Wed, 12 Mar 2025 11:50:23 +0000 (22:50 +1100)
committerGitHub <noreply@github.com>
Wed, 12 Mar 2025 11:50:23 +0000 (22:50 +1100)
src/app/pages/client/inbox/Notifications.tsx

index 722ce5d30888b0ff32d0863916a392cc682cfc65..c28b6753c2ef22570074bf2c19977751918becb3 100644 (file)
@@ -26,6 +26,7 @@ import {
 import { useVirtualizer } from '@tanstack/react-virtual';
 import { HTMLReactParserOptions } from 'html-react-parser';
 import { Opts as LinkifyOpts } from 'linkifyjs';
+import { useAtomValue } from 'jotai';
 import { Page, PageContent, PageContentCenter, PageHeader } from '../../../components/page';
 import { useMatrixClient } from '../../../hooks/useMatrixClient';
 import { getMxIdLocalPart, mxcUrlToHttp } from '../../../utils/matrix';
@@ -82,6 +83,7 @@ import { useSpoilerClickHandler } from '../../../hooks/useSpoilerClickHandler';
 import { ScreenSize, useScreenSizeContext } from '../../../hooks/useScreenSize';
 import { BackRouteHandler } from '../../../components/BackRouteHandler';
 import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication';
+import { allRoomsAtom } from '../../../state/room-list/roomList';
 
 type RoomNotificationsGroup = {
   roomId: string;
@@ -94,9 +96,14 @@ type NotificationTimeline = {
 type LoadTimeline = (from?: string) => Promise<void>;
 type SilentReloadTimeline = () => Promise<void>;
 
-const groupNotifications = (notifications: INotification[]): RoomNotificationsGroup[] => {
+const groupNotifications = (
+  notifications: INotification[],
+  allowRooms: Set<string>
+): RoomNotificationsGroup[] => {
   const groups: RoomNotificationsGroup[] = [];
   notifications.forEach((notification) => {
+    if (!allowRooms.has(notification.room_id)) return;
+
     const groupIndex = groups.length - 1;
     const lastAddedGroup: RoomNotificationsGroup | undefined = groups[groupIndex];
     if (lastAddedGroup && notification.room_id === lastAddedGroup.roomId) {
@@ -116,6 +123,9 @@ const useNotificationTimeline = (
   onlyHighlight?: boolean
 ): [NotificationTimeline, LoadTimeline, SilentReloadTimeline] => {
   const mx = useMatrixClient();
+  const allRooms = useAtomValue(allRoomsAtom);
+  const allJoinedRooms = useMemo(() => new Set(allRooms), [allRooms]);
+
   const [notificationTimeline, setNotificationTimeline] = useState<NotificationTimeline>({
     groups: [],
   });
@@ -142,7 +152,7 @@ const useNotificationTimeline = (
         paginationLimit,
         onlyHighlight ? 'highlight' : undefined
       );
-      const groups = groupNotifications(data.notifications);
+      const groups = groupNotifications(data.notifications, allJoinedRooms);
 
       setNotificationTimeline((currentTimeline) => {
         if (currentTimeline.nextToken === from) {
@@ -154,7 +164,7 @@ const useNotificationTimeline = (
         return currentTimeline;
       });
     },
-    [paginationLimit, onlyHighlight, fetchNotifications]
+    [paginationLimit, onlyHighlight, fetchNotifications, allJoinedRooms]
   );
 
   /**
@@ -167,12 +177,12 @@ const useNotificationTimeline = (
       paginationLimit,
       onlyHighlight ? 'highlight' : undefined
     );
-    const groups = groupNotifications(data.notifications);
+    const groups = groupNotifications(data.notifications, allJoinedRooms);
     setNotificationTimeline({
       nextToken: data.next_token,
       groups,
     });
-  }, [paginationLimit, onlyHighlight, fetchNotifications]);
+  }, [paginationLimit, onlyHighlight, fetchNotifications, allJoinedRooms]);
 
   return [notificationTimeline, loadTimeline, silentReloadTimeline];
 };