Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / services / user-settings-muxer.js
blob7b9fb6ba7092ff4c4f47f3aff8bf787057cab115
1 'use strict';
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) {
10 return {
11 get: function(user) {
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...
22 delete value.groupId;
23 delete value.state;
26 /**
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
30 * 404 error
32 var HANDLERS = {
33 /* Left Room Menu */
34 leftRoomMenu: {
35 get: function(user) {
36 return userSettingsService.getUserSettings(user._id, 'leftRoomMenu').then(function(result) {
37 cleanLeftRoomSettings(result);
38 return result;
39 });
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'),
49 /* Lang */
50 lang: standardHandler('lang'),
52 /* Unread Emails Opt-out */
53 unread_notifications_optout: {
54 get: function(user) {
55 return userSettingsService
56 .getUserSettings(user._id, 'unread_notifications_optout')
57 .then(function(value) {
58 return !!value;
59 });
61 set: function(user, value) {
62 return userSettingsService
63 .setUserSettings(user._id, 'unread_notifications_optout', value ? 1 : 0)
64 .then(function() {
65 return !!value;
66 });
70 /* Default Notification Settings Mode */
71 defaultRoomMode: {
72 get: function(user) {
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)
81 .then(function() {
82 return userDefaultFlagsService.getDefaultFlagDetailsForUserId(user._id);
83 });
88 function mapHandlers(keys) {
89 return keys.map(function(key) {
90 var handler = HANDLERS[key];
91 if (!handler) throw new StatusError(404);
92 return handler;
93 });
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) {
100 return 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];
114 return memo;
115 }, {});
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
132 * a 404
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];
145 return memo;
146 }, {});
150 module.exports = {
151 getSetting: getSetting,
152 getSettings: getSettings,
153 updateSetting: updateSetting,
154 updateSettings: updateSettings