forked from vegeta999/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveUserPreferences.js
More file actions
109 lines (95 loc) · 3.81 KB
/
Copy pathsaveUserPreferences.js
File metadata and controls
109 lines (95 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { Users, Subscriptions } from '../../app/models';
Meteor.methods({
saveUserPreferences(settings) {
const keys = {
language: Match.Optional(String),
newRoomNotification: Match.Optional(String),
newMessageNotification: Match.Optional(String),
clockMode: Match.Optional(Number),
useEmojis: Match.Optional(Boolean),
convertAsciiEmoji: Match.Optional(Boolean),
saveMobileBandwidth: Match.Optional(Boolean),
collapseMediaByDefault: Match.Optional(Boolean),
autoImageLoad: Match.Optional(Boolean),
emailNotificationMode: Match.Optional(String),
unreadAlert: Match.Optional(Boolean),
notificationsSoundVolume: Match.Optional(Number),
desktopNotifications: Match.Optional(String),
mobileNotifications: Match.Optional(String),
enableAutoAway: Match.Optional(Boolean),
highlights: Match.Optional([String]),
desktopNotificationDuration: Match.Optional(Number),
messageViewMode: Match.Optional(Number),
hideUsernames: Match.Optional(Boolean),
hideRoles: Match.Optional(Boolean),
hideAvatars: Match.Optional(Boolean),
hideFlexTab: Match.Optional(Boolean),
sendOnEnter: Match.Optional(String),
idleTimeLimit: Match.Optional(Number),
sidebarShowFavorites: Match.Optional(Boolean),
sidebarShowUnread: Match.Optional(Boolean),
sidebarSortby: Match.Optional(String),
sidebarViewMode: Match.Optional(String),
sidebarHideAvatar: Match.Optional(Boolean),
sidebarGroupByType: Match.Optional(Boolean),
sidebarShowDiscussion: Match.Optional(Boolean),
muteFocusedConversations: Match.Optional(Boolean),
};
check(settings, Match.ObjectIncluding(keys));
const user = Meteor.user();
if (!user) {
return false;
}
const {
desktopNotifications: oldDesktopNotifications,
mobileNotifications: oldMobileNotifications,
emailNotificationMode: oldEmailNotifications,
} = (user.settings && user.settings.preferences) || {};
if (user.settings == null) {
Users.clearSettings(user._id);
}
if (settings.language != null) {
Users.setLanguage(user._id, settings.language);
}
// Keep compatibility with old values
if (settings.emailNotificationMode === 'all') {
settings.emailNotificationMode = 'mentions';
} else if (settings.emailNotificationMode === 'disabled') {
settings.emailNotificationMode = 'nothing';
}
if (settings.idleTimeLimit != null && settings.idleTimeLimit < 60) {
throw new Meteor.Error('invalid-idle-time-limit-value', 'Invalid idleTimeLimit');
}
Users.setPreferences(user._id, settings);
// propagate changed notification preferences
Meteor.defer(() => {
if (settings.desktopNotifications && oldDesktopNotifications !== settings.desktopNotifications) {
if (settings.desktopNotifications === 'default') {
Subscriptions.clearDesktopNotificationUserPreferences(user._id);
} else {
Subscriptions.updateDesktopNotificationUserPreferences(user._id, settings.desktopNotifications);
}
}
if (settings.mobileNotifications && oldMobileNotifications !== settings.mobileNotifications) {
if (settings.mobileNotifications === 'default') {
Subscriptions.clearMobileNotificationUserPreferences(user._id);
} else {
Subscriptions.updateMobileNotificationUserPreferences(user._id, settings.mobileNotifications);
}
}
if (settings.emailNotificationMode && oldEmailNotifications !== settings.emailNotificationMode) {
if (settings.emailNotificationMode === 'default') {
Subscriptions.clearEmailNotificationUserPreferences(user._id);
} else {
Subscriptions.updateEmailNotificationUserPreferences(user._id, settings.emailNotificationMode);
}
}
if (Array.isArray(settings.highlights)) {
Subscriptions.updateUserHighlights(user._id, settings.highlights);
}
});
return true;
},
});