3 var Promise
= require('bluebird');
4 var StatusError
= require('statuserror');
5 var userSettingsService
= require('gitter-web-user-settings');
6 var userDefaultFlagsService
= require('gitter-web-rooms/lib/user-default-flags-service');
7 var userDefaultFlagsUpdateService
= require('gitter-web-rooms/lib/user-default-flags-update-service');
9 function standardHandler(settingsKey
) {
12 return userSettingsService
.getUserSettings(user
._id
, settingsKey
);
14 set: function(user
, value
) {
15 return userSettingsService
.setUserSettings(user
._id
, settingsKey
, value
).return(value
);
20 function cleanLeftRoomSettings(value
) {
21 // These are no longer stored...
27 * Each setting key can have it's own handler.
28 * This maps the setting key to a read and write handler.
29 * If the user attempts to write to an unknown key, we throw a
36 return userSettingsService
.getUserSettings(user
._id
, 'leftRoomMenu').then(function(result
) {
37 cleanLeftRoomSettings(result
);
41 set: function(user
, value
) {
42 cleanLeftRoomSettings(value
);
43 return userSettingsService
.setUserSettings(user
._id
, 'leftRoomMenu', value
).return(value
);
46 rightToolbar
: standardHandler('rightToolbar'),
47 userTheme
: standardHandler('userTheme'),
50 lang
: standardHandler('lang'),
52 /* Unread Emails Opt-out */
53 unread_notifications_optout
: {
55 return userSettingsService
56 .getUserSettings(user
._id
, 'unread_notifications_optout')
57 .then(function(value
) {
61 set: function(user
, value
) {
62 return userSettingsService
63 .setUserSettings(user
._id
, 'unread_notifications_optout', value
? 1 : 0)
70 /* Default Notification Settings Mode */
73 return userDefaultFlagsService
.getDefaultFlagDetailsForUserId(user
._id
);
75 set: function(user
, value
) {
76 var mode
= value
.mode
;
77 var override
= value
.override
;
79 return userDefaultFlagsUpdateService
80 .updateDefaultModeForUser(user
, mode
, override
)
82 return userDefaultFlagsService
.getDefaultFlagDetailsForUserId(user
._id
);
88 function mapHandlers(keys
) {
89 return keys
.map(function(key
) {
90 var handler
= HANDLERS
[key
];
91 if (!handler
) throw new StatusError(404);
96 function getSetting(user
, key
) {
97 var handler
= HANDLERS
[key
];
98 if (!handler
) throw new StatusError(404);
99 return handler
.get(user
).then(function(f
) {
105 * Returns multiple setting keys as a hash
107 function getSettings(user
, keys
) {
108 var handlers
= mapHandlers(keys
);
109 return Promise
.map(handlers
, function(handler
) {
110 return handler
.get(user
);
111 }).then(function(results
) {
112 return keys
.reduce(function(memo
, key
, index
) {
113 memo
[key
] = results
[index
];
120 * Updates a single key
122 function updateSetting(user
, key
, value
) {
123 var handler
= HANDLERS
[key
];
124 if (!handler
) throw new StatusError(404);
126 return handler
.set(user
, value
);
130 * Updates multiple key settings from a hash
131 * All keys need to be valid, otherwise we throw
134 function updateSettings(user
, valuesHash
) {
135 var keys
= Object
.keys(valuesHash
);
136 var handlers
= mapHandlers(keys
);
138 return Promise
.map(handlers
, function(handler
, index
) {
139 var key
= keys
[index
];
140 var value
= valuesHash
[key
];
141 return handler
.set(user
, value
);
142 }).then(function(results
) {
143 return keys
.reduce(function(memo
, key
, index
) {
144 memo
[key
] = results
[index
];
151 getSetting
: getSetting
,
152 getSettings
: getSettings
,
153 updateSetting
: updateSetting
,
154 updateSettings
: updateSettings