Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / api / v1 / user / troupe-settings.js
blob143b1ee13593fa41c3595c9885ea96e1f6f53759
1 'use strict';
3 var Promise = require('bluebird');
4 var StatusError = require('statuserror');
5 var roomMembershipService = require('gitter-web-rooms/lib/room-membership-service');
6 var userDefaultFlagsService = require('gitter-web-rooms/lib/user-default-flags-service');
7 var userRoomModeUpdateService = require('gitter-web-rooms/lib/user-room-mode-update-service');
9 function generateResponse(userId, troupeId) {
10 return Promise.join(
11 roomMembershipService.getMembershipDetails(userId, troupeId),
12 userDefaultFlagsService.getDefaultFlagDetailsForUserId(userId),
13 function(details, defaults) {
14 if (!details) throw new StatusError(404);
16 return {
17 push: details.mode, // REMOVE THIS
18 mode: details.mode,
19 lurk: details.lurk, // ALSO deprecated
21 unread: details.unread,
22 activity: details.activity,
23 mention: details.mention,
24 announcement: details.announcement,
25 desktop: details.desktop,
26 mobile: details.mobile,
28 default: details.default,
29 defaultSettings: defaults
34 /**
35 * TODO: REMOVE THIS WHOLE RESOURCE AND UPDATE THIS VIA THE USER TROUPE
36 * with { mode: x }
38 module.exports = {
39 id: 'setting',
41 show: function(req) {
42 var userId = req.resourceUser.id;
43 var troupeId = req.params.userTroupeId;
44 var setting = req.params.setting;
46 if (setting !== 'notifications') throw new StatusError(404);
48 return generateResponse(userId, troupeId);
51 update: function(req) {
52 var userId = req.resourceUser.id;
53 var troupeId = req.params.userTroupeId;
54 var setting = req.params.setting;
56 if (setting !== 'notifications') throw new StatusError(404);
58 var settings = req.body;
59 var mode = settings && (settings.mode || settings.push);
61 if (!mode) throw new StatusError(400, 'Illegal notifications mode');
62 return userRoomModeUpdateService
63 .setModeForUserInRoom(req.resourceUser, troupeId, mode)
64 .then(function() {
65 return generateResponse(userId, troupeId);
66 });