Skip to content

Commit

Permalink
Save audio notification correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Dec 22, 2020
1 parent 006cab5 commit b699ebb
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 200 deletions.
2 changes: 1 addition & 1 deletion app/lib/server/functions/notifications/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function shouldNotifyAudio({
roomType,
isThread,
}) {
if (disableAllMessageNotifications && audioNotifications == null && !hasReplyToThread) {
if (disableAllMessageNotifications && audioNotifications == null && !isHighlighted && !hasMentionToUser && !hasReplyToThread) {
return false;
}

Expand Down
29 changes: 13 additions & 16 deletions app/lib/server/lib/sendNotificationsOnMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,19 @@ export const sendNotification = async ({
return;
}

const user = Users.findOneById(subscription.u._id, {
fields: {
active: 1,
emails: 1,
language: 1,
status: 1,
statusConnection: 1,
username: 1,
settings: 1,
},
});

const { settings, ...restUser } = user;

if (!subscription.receiver) {
subscription.receiver = [restUser];
subscription.receiver = [
Users.findOneById(subscription.u._id, {
fields: {
active: 1,
emails: 1,
language: 1,
status: 1,
statusConnection: 1,
username: 1,
},
}),
];
}

const [receiver] = subscription.receiver;
Expand Down Expand Up @@ -87,7 +84,7 @@ export const sendNotification = async ({
disableAllMessageNotifications,
status: receiver.status,
statusConnection: receiver.statusConnection,
audioNotifications: audioNotifications || settings?.preferences?.audioNotifications,
audioNotifications,
hasMentionToAll,
hasMentionToHere,
isHighlighted,
Expand Down
156 changes: 15 additions & 141 deletions app/models/server/models/Subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,6 @@ export class Subscriptions extends Base {
return this.find(query, options);
}

updateAudioNotificationsById(_id, audioNotifications) {
const query = {
_id,
};

const update = {};

if (audioNotifications === 'default') {
update.$unset = { audioNotifications: 1 };
} else {
update.$set = { audioNotifications };
}

return this.update(query, update);
}

updateAudioNotificationValueById(_id, audioNotificationValue) {
const query = {
_id,
Expand All @@ -160,68 +144,26 @@ export class Subscriptions extends Base {
return this.update(query, update);
}

updateDesktopNotificationsById(_id, desktopNotifications) {
updateNotificationsPrefById(_id, notificationPref, notificationField, notificationPrefOrigin) {
const query = {
_id,
};

const update = {};

if (desktopNotifications === null) {
if (notificationPref === null) {
update.$unset = {
desktopNotifications: 1,
desktopPrefOrigin: 1,
[notificationField]: 1,
[notificationPrefOrigin]: 1,
};
} else {
update.$set = {
desktopNotifications: desktopNotifications.value,
desktopPrefOrigin: desktopNotifications.origin,
[notificationField]: notificationPref.value,
[notificationPrefOrigin]: notificationPref.origin,
};
}

return this.update(query, update);
}

updateMobilePushNotificationsById(_id, mobilePushNotifications) {
const query = {
_id,
};

const update = {};

if (mobilePushNotifications === null) {
update.$unset = {
mobilePushNotifications: 1,
mobilePrefOrigin: 1,
};
} else {
update.$set = {
mobilePushNotifications: mobilePushNotifications.value,
mobilePrefOrigin: mobilePushNotifications.origin,
};
}

return this.update(query, update);
}

updateEmailNotificationsById(_id, emailNotifications) {
const query = {
_id,
};

const update = {};

if (emailNotifications === null) {
update.$unset = {
emailNotifications: 1,
emailPrefOrigin: 1,
};
} else {
update.$set = {
emailNotifications: emailNotifications.value,
emailPrefOrigin: emailNotifications.origin,
};
}
console.log('updateNotificationsPrefById', JSON.stringify({ query, update }, null, 2));

return this.update(query, update);
}
Expand Down Expand Up @@ -1147,102 +1089,34 @@ export class Subscriptions extends Base {
return this.update(query, update, { multi: true });
}

clearDesktopNotificationUserPreferences(userId) {
clearNotificationUserPreferences(userId, notificationField, notificationOriginField) {
const query = {
'u._id': userId,
desktopPrefOrigin: 'user',
[notificationOriginField]: 'user',
};

const update = {
$unset: {
desktopNotifications: 1,
desktopPrefOrigin: 1,
},
};

return this.update(query, update, { multi: true });
}

updateDesktopNotificationUserPreferences(userId, desktopNotifications) {
const query = {
'u._id': userId,
desktopPrefOrigin: {
$ne: 'subscription',
},
};

const update = {
$set: {
desktopNotifications,
desktopPrefOrigin: 'user',
},
};

return this.update(query, update, { multi: true });
}

clearMobileNotificationUserPreferences(userId) {
const query = {
'u._id': userId,
mobilePrefOrigin: 'user',
};

const update = {
$unset: {
mobilePushNotifications: 1,
mobilePrefOrigin: 1,
},
};

return this.update(query, update, { multi: true });
}

updateMobileNotificationUserPreferences(userId, mobilePushNotifications) {
const query = {
'u._id': userId,
mobilePrefOrigin: {
$ne: 'subscription',
},
};

const update = {
$set: {
mobilePushNotifications,
mobilePrefOrigin: 'user',
},
};

return this.update(query, update, { multi: true });
}

clearEmailNotificationUserPreferences(userId) {
const query = {
'u._id': userId,
emailPrefOrigin: 'user',
};

const update = {
$unset: {
emailNotifications: 1,
emailPrefOrigin: 1,
[notificationOriginField]: 1,
[notificationField]: 1,
},
};

return this.update(query, update, { multi: true });
}

updateEmailNotificationUserPreferences(userId, emailNotifications) {
updateNotificationUserPreferences(userId, userPref, notificationField, notificationOriginField) {
const query = {
'u._id': userId,
emailPrefOrigin: {
[notificationOriginField]: {
$ne: 'subscription',
},
};

const update = {
$set: {
emailNotifications,
emailPrefOrigin: 'user',
[notificationField]: userPref,
[notificationOriginField]: 'user',
},
};

Expand Down
39 changes: 13 additions & 26 deletions app/push-notifications/server/methods/saveNotificationSettings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';

import { Subscriptions } from '../../../models';
import { Subscriptions } from '../../../models/server';
import { getUserNotificationPreference } from '../../../utils';

Meteor.methods({
Expand All @@ -13,39 +13,26 @@ Meteor.methods({
check(field, String);
check(value, String);

const getNotificationPrefValue = (field, value) => {
if (value === 'default') {
const userPref = getUserNotificationPreference(Meteor.userId(), field);
return userPref.origin === 'server' ? null : userPref;
}
return { value, origin: 'subscription' };
};

const notifications = {
audioNotifications: {
updateMethod: (subscription, value) => Subscriptions.updateAudioNotificationsById(subscription._id, value),
updateMethod: (subscription, value) => Subscriptions.updateNotificationsPrefById(subscription._id, getNotificationPrefValue('audio', value), 'audioNotifications', 'audioPrefOrigin'),
},
desktopNotifications: {
updateMethod: (subscription, value) => {
if (value === 'default') {
const userPref = getUserNotificationPreference(Meteor.userId(), 'desktop');
Subscriptions.updateDesktopNotificationsById(subscription._id, userPref.origin === 'server' ? null : userPref);
} else {
Subscriptions.updateDesktopNotificationsById(subscription._id, { value, origin: 'subscription' });
}
},
updateMethod: (subscription, value) => Subscriptions.updateNotificationsPrefById(subscription._id, getNotificationPrefValue('desktop', value), 'desktopNotifications', 'desktopPrefOrigin'),
},
mobilePushNotifications: {
updateMethod: (subscription, value) => {
if (value === 'default') {
const userPref = getUserNotificationPreference(Meteor.userId(), 'mobile');
Subscriptions.updateMobilePushNotificationsById(subscription._id, userPref.origin === 'server' ? null : userPref);
} else {
Subscriptions.updateMobilePushNotificationsById(subscription._id, { value, origin: 'subscription' });
}
},
updateMethod: (subscription, value) => Subscriptions.updateNotificationsPrefById(subscription._id, getNotificationPrefValue('mobile', value), 'mobilePushNotifications', 'mobilePrefOrigin'),
},
emailNotifications: {
updateMethod: (subscription, value) => {
if (value === 'default') {
const userPref = getUserNotificationPreference(Meteor.userId(), 'email');
Subscriptions.updateEmailNotificationsById(subscription._id, userPref.origin === 'server' ? null : userPref);
} else {
Subscriptions.updateEmailNotificationsById(subscription._id, { value, origin: 'subscription' });
}
},
updateMethod: (subscription, value) => Subscriptions.updateNotificationsPrefById(subscription._id, getNotificationPrefValue('email', value), 'emailNotifications', 'emailPrefOrigin'),
},
unreadAlert: {
updateMethod: (subscription, value) => Subscriptions.updateUnreadAlertById(subscription._id, value),
Expand Down
6 changes: 6 additions & 0 deletions app/utils/lib/getDefaultSubscriptionPref.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const getDefaultSubscriptionPref = (userPref) => {
desktopNotifications,
mobileNotifications,
emailNotificationMode,
audioNotifications,
highlights,
} = (userPref.settings && userPref.settings.preferences) || {};

Expand All @@ -27,5 +28,10 @@ export const getDefaultSubscriptionPref = (userPref) => {
subscription.emailPrefOrigin = 'user';
}

if (audioNotifications && audioNotifications !== 'default') {
subscription.audioNotifications = audioNotifications;
subscription.audioPrefOrigin = 'user';
}

return subscription;
};
3 changes: 2 additions & 1 deletion app/utils/lib/getUserNotificationPreference.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ export const getUserNotificationPreference = (user, pref) => {

let preferenceKey;
switch (pref) {
case 'audio': preferenceKey = 'audioNotifications'; break;
case 'desktop': preferenceKey = 'desktopNotifications'; break;
case 'mobile': preferenceKey = 'mobileNotifications'; break;
case 'email': preferenceKey = 'emailNotificationMode'; break;
}

if (user && user.settings && user.settings.preferences && user.settings.preferences[preferenceKey] !== 'default') {
if (user?.settings?.preferences && typeof user.settings.preferences[preferenceKey] !== 'undefined' && user.settings.preferences[preferenceKey] !== 'default') {
return {
value: user.settings.preferences[preferenceKey],
origin: 'user',
Expand Down
Loading

0 comments on commit b699ebb

Please sign in to comment.