From c954c5ffece8111bf924bec91e6666b25a66749f Mon Sep 17 00:00:00 2001
From: Matheus Barbosa Silva
<36537004+matheusbsilva137@users.noreply.github.com>
Date: Fri, 29 Oct 2021 14:40:04 -0300
Subject: [PATCH] [FIX] Notifications are not being filtered (#23487)
* Add migration to update push notification setting's value
* Update mobileNotifications preference to pushNotifications
---
app/api/server/v1/users.js | 2 +-
app/utils/lib/getDefaultSubscriptionPref.js | 6 ++---
.../methods/saveUserPreferences.ts | 2 +-
.../PreferencesNotificationsSection.js | 10 +++----
server/methods/saveUserPreferences.js | 10 +++----
server/startup/migrations/index.ts | 1 +
server/startup/migrations/v243.ts | 26 +++++++++++++++++++
tests/data/user.js | 2 +-
8 files changed, 43 insertions(+), 16 deletions(-)
create mode 100644 server/startup/migrations/v243.ts
diff --git a/app/api/server/v1/users.js b/app/api/server/v1/users.js
index 714fc5e9265f..1f3f22e38e6c 100644
--- a/app/api/server/v1/users.js
+++ b/app/api/server/v1/users.js
@@ -593,7 +593,7 @@ API.v1.addRoute('users.setPreferences', { authRequired: true }, {
unreadAlert: Match.Maybe(Boolean),
notificationsSoundVolume: Match.Maybe(Number),
desktopNotifications: Match.Maybe(String),
- mobileNotifications: Match.Maybe(String),
+ pushNotifications: Match.Maybe(String),
enableAutoAway: Match.Maybe(Boolean),
highlights: Match.Maybe(Array),
desktopNotificationRequireInteraction: Match.Maybe(Boolean),
diff --git a/app/utils/lib/getDefaultSubscriptionPref.js b/app/utils/lib/getDefaultSubscriptionPref.js
index 294a7d50a734..0200cb128e33 100644
--- a/app/utils/lib/getDefaultSubscriptionPref.js
+++ b/app/utils/lib/getDefaultSubscriptionPref.js
@@ -3,7 +3,7 @@ export const getDefaultSubscriptionPref = (userPref) => {
const {
desktopNotifications,
- mobileNotifications,
+ pushNotifications,
emailNotificationMode,
highlights,
} = (userPref.settings && userPref.settings.preferences) || {};
@@ -17,8 +17,8 @@ export const getDefaultSubscriptionPref = (userPref) => {
subscription.desktopPrefOrigin = 'user';
}
- if (mobileNotifications && mobileNotifications !== 'default') {
- subscription.mobilePushNotifications = mobileNotifications;
+ if (pushNotifications && pushNotifications !== 'default') {
+ subscription.mobilePushNotifications = pushNotifications;
subscription.mobilePrefOrigin = 'user';
}
diff --git a/client/contexts/ServerContext/methods/saveUserPreferences.ts b/client/contexts/ServerContext/methods/saveUserPreferences.ts
index 1f5f67100c97..7e6b20ad0bf6 100644
--- a/client/contexts/ServerContext/methods/saveUserPreferences.ts
+++ b/client/contexts/ServerContext/methods/saveUserPreferences.ts
@@ -12,7 +12,7 @@ type UserPreferences = {
unreadAlert: boolean;
notificationsSoundVolume: number;
desktopNotifications: string;
- mobileNotifications: string;
+ pushNotifications: string;
enableAutoAway: boolean;
highlights: string[];
messageViewMode: number;
diff --git a/client/views/account/preferences/PreferencesNotificationsSection.js b/client/views/account/preferences/PreferencesNotificationsSection.js
index 615b063d8e3e..6419f8df5ffe 100644
--- a/client/views/account/preferences/PreferencesNotificationsSection.js
+++ b/client/views/account/preferences/PreferencesNotificationsSection.js
@@ -50,7 +50,7 @@ const PreferencesNotificationsSection = ({ onChange, commitRef, ...props }) => {
{
desktopNotificationRequireInteraction: userDesktopNotificationRequireInteraction,
desktopNotifications: userDesktopNotifications,
- mobileNotifications: userMobileNotifications,
+ pushNotifications: userMobileNotifications,
emailNotificationMode: userEmailNotificationMode,
},
onChange,
@@ -59,14 +59,14 @@ const PreferencesNotificationsSection = ({ onChange, commitRef, ...props }) => {
const {
desktopNotificationRequireInteraction,
desktopNotifications,
- mobileNotifications,
+ pushNotifications,
emailNotificationMode,
} = values;
const {
handleDesktopNotificationRequireInteraction,
handleDesktopNotifications,
- handleMobileNotifications,
+ handlePushNotifications,
handleEmailNotificationMode,
} = handlers;
@@ -171,8 +171,8 @@ const PreferencesNotificationsSection = ({ onChange, commitRef, ...props }) => {
{t('Notification_Push_Default_For')}
diff --git a/server/methods/saveUserPreferences.js b/server/methods/saveUserPreferences.js
index bd062f6064b8..ffed9b7adc6d 100644
--- a/server/methods/saveUserPreferences.js
+++ b/server/methods/saveUserPreferences.js
@@ -19,7 +19,7 @@ Meteor.methods({
unreadAlert: Match.Optional(Boolean),
notificationsSoundVolume: Match.Optional(Number),
desktopNotifications: Match.Optional(String),
- mobileNotifications: Match.Optional(String),
+ pushNotifications: Match.Optional(String),
enableAutoAway: Match.Optional(Boolean),
highlights: Match.Optional([String]),
messageViewMode: Match.Optional(Number),
@@ -46,7 +46,7 @@ Meteor.methods({
const {
desktopNotifications: oldDesktopNotifications,
- mobileNotifications: oldMobileNotifications,
+ pushNotifications: oldMobileNotifications,
emailNotificationMode: oldEmailNotifications,
} = (user.settings && user.settings.preferences) || {};
@@ -81,11 +81,11 @@ Meteor.methods({
}
}
- if (settings.mobileNotifications && oldMobileNotifications !== settings.mobileNotifications) {
- if (settings.mobileNotifications === 'default') {
+ if (settings.pushNotifications && oldMobileNotifications !== settings.pushNotifications) {
+ if (settings.pushNotifications === 'default') {
Subscriptions.clearNotificationUserPreferences(user._id, 'mobilePushNotifications', 'mobilePrefOrigin');
} else {
- Subscriptions.updateNotificationUserPreferences(user._id, settings.mobileNotifications, 'mobilePushNotifications', 'mobilePrefOrigin');
+ Subscriptions.updateNotificationUserPreferences(user._id, settings.pushNotifications, 'mobilePushNotifications', 'mobilePrefOrigin');
}
}
diff --git a/server/startup/migrations/index.ts b/server/startup/migrations/index.ts
index 132a7ad3862d..37f25773c4e2 100644
--- a/server/startup/migrations/index.ts
+++ b/server/startup/migrations/index.ts
@@ -66,4 +66,5 @@ import './v239';
import './v240';
import './v241';
import './v242';
+import './v243';
import './xrun';
diff --git a/server/startup/migrations/v243.ts b/server/startup/migrations/v243.ts
new file mode 100644
index 000000000000..89d3f9f03e70
--- /dev/null
+++ b/server/startup/migrations/v243.ts
@@ -0,0 +1,26 @@
+import { addMigration } from '../../lib/migrations';
+import { Settings, Users } from '../../../app/models/server';
+
+addMigration({
+ version: 243,
+ up() {
+ const mobileNotificationsSetting = Settings.findOneById('Accounts_Default_User_Preferences_mobileNotifications');
+
+ Settings.removeById('Accounts_Default_User_Preferences_mobileNotifications');
+ if (mobileNotificationsSetting && mobileNotificationsSetting.value) {
+ Settings.upsert({
+ _id: 'Accounts_Default_User_Preferences_pushNotifications',
+ }, {
+ $set: {
+ value: mobileNotificationsSetting.value,
+ },
+ });
+ }
+
+ Users.update(
+ { 'settings.preferences.mobileNotifications': { $exists: 1 } },
+ { $rename: { 'settings.preferences.mobileNotifications': 'settings.preferences.pushNotifications' } },
+ { multi: true },
+ );
+ },
+});
diff --git a/tests/data/user.js b/tests/data/user.js
index 1a729c10c1d5..7ba380fcd8c4 100644
--- a/tests/data/user.js
+++ b/tests/data/user.js
@@ -20,7 +20,7 @@ export const preferences = {
unreadAlert: true,
notificationsSoundVolume: 100,
desktopNotifications: 'default',
- mobileNotifications: 'default',
+ pushNotifications: 'default',
enableAutoAway: true,
highlights: [],
desktopNotificationRequireInteraction: false,