From 8426c20d344424f435b934125aab5a04c44d98b9 Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 12 Aug 2024 15:40:29 +0200 Subject: [PATCH] [Glitch] Fix all streamed notification types being stored without filtering Port a7b718c31adbfee9015c84bc2379129df612a5f2 to glitch-soc Signed-off-by: Claire --- .../glitch/actions/notification_groups.ts | 27 ++++++++++++++++++- .../flavours/glitch/actions/streaming.js | 2 +- .../flavours/glitch/api_types/statuses.ts | 26 ++++++++++++++++-- .../glitch/reducers/notification_groups.ts | 14 +++++----- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/app/javascript/flavours/glitch/actions/notification_groups.ts b/app/javascript/flavours/glitch/actions/notification_groups.ts index e034792275..2f0174d117 100644 --- a/app/javascript/flavours/glitch/actions/notification_groups.ts +++ b/app/javascript/flavours/glitch/actions/notification_groups.ts @@ -15,6 +15,7 @@ import type { NotificationGap } from 'flavours/glitch/reducers/notification_grou import { selectSettingsNotificationsExcludedTypes, selectSettingsNotificationsQuickFilterActive, + selectSettingsNotificationsShows, } from 'flavours/glitch/selectors/settings'; import type { AppDispatch } from 'flavours/glitch/store'; import { @@ -104,7 +105,31 @@ export const fetchNotificationsGap = createDataLoadingThunk( export const processNewNotificationForGroups = createAppAsyncThunk( 'notificationGroups/processNew', - (notification: ApiNotificationJSON, { dispatch }) => { + (notification: ApiNotificationJSON, { dispatch, getState }) => { + const state = getState(); + const activeFilter = selectSettingsNotificationsQuickFilterActive(state); + const notificationShows = selectSettingsNotificationsShows(state); + + const showInColumn = + activeFilter === 'all' + ? notificationShows[notification.type] + : activeFilter === notification.type; + + if (!showInColumn) return; + + if ( + (notification.type === 'mention' || notification.type === 'update') && + notification.status.filtered + ) { + const filters = notification.status.filtered.filter((result) => + result.filter.context.includes('notifications'), + ); + + if (filters.some((result) => result.filter.filter_action === 'hide')) { + return; + } + } + dispatchAssociatedRecords(dispatch, [notification]); return notification; diff --git a/app/javascript/flavours/glitch/actions/streaming.js b/app/javascript/flavours/glitch/actions/streaming.js index 7b006c1be7..2d69542fc7 100644 --- a/app/javascript/flavours/glitch/actions/streaming.js +++ b/app/javascript/flavours/glitch/actions/streaming.js @@ -104,7 +104,7 @@ export const connectTimelineStream = (timelineId, channelName, params = {}, opti const notificationJSON = JSON.parse(data.payload); dispatch(updateNotifications(notificationJSON, messages, locale)); // TODO: remove this once the groups feature replaces the previous one - if(getState().notificationGroups.groups.length > 0) { + if(getState().settings.getIn(['notifications', 'groupingBeta'], false)) { dispatch(processNewNotificationForGroups(notificationJSON)); } break; diff --git a/app/javascript/flavours/glitch/api_types/statuses.ts b/app/javascript/flavours/glitch/api_types/statuses.ts index 9de86e7fa6..c84ecc7afc 100644 --- a/app/javascript/flavours/glitch/api_types/statuses.ts +++ b/app/javascript/flavours/glitch/api_types/statuses.ts @@ -58,6 +58,29 @@ export interface ApiPreviewCardJSON { authors: ApiPreviewCardAuthorJSON[]; } +export type FilterContext = + | 'home' + | 'notifications' + | 'public' + | 'thread' + | 'account'; + +export interface ApiFilterJSON { + id: string; + title: string; + context: FilterContext; + expires_at: string; + filter_action: 'warn' | 'hide'; + keywords?: unknown[]; // TODO: FilterKeywordSerializer + statuses?: unknown[]; // TODO: FilterStatusSerializer +} + +export interface ApiFilterResultJSON { + filter: ApiFilterJSON; + keyword_matches: string[]; + status_matches: string[]; +} + export interface ApiStatusJSON { id: string; created_at: string; @@ -80,8 +103,7 @@ export interface ApiStatusJSON { bookmarked?: boolean; pinned?: boolean; - // filtered: FilterResult[] - filtered: unknown; // TODO + filtered?: ApiFilterResultJSON[]; content?: string; text?: string; diff --git a/app/javascript/flavours/glitch/reducers/notification_groups.ts b/app/javascript/flavours/glitch/reducers/notification_groups.ts index 4dedfe27a8..a721e4008d 100644 --- a/app/javascript/flavours/glitch/reducers/notification_groups.ts +++ b/app/javascript/flavours/glitch/reducers/notification_groups.ts @@ -387,12 +387,14 @@ export const notificationGroupsReducer = createReducer( }) .addCase(processNewNotificationForGroups.fulfilled, (state, action) => { const notification = action.payload; - processNewNotification( - usePendingItems ? state.pendingGroups : state.groups, - notification, - ); - updateLastReadId(state); - trimNotifications(state); + if (notification) { + processNewNotification( + usePendingItems ? state.pendingGroups : state.groups, + notification, + ); + updateLastReadId(state); + trimNotifications(state); + } }) .addCase(disconnectTimeline, (state, action) => { if (action.payload.timeline === 'home') {