From 4021c1d62b6166826459ffb111d9ab148b946280 Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Wed, 28 Mar 2018 17:32:56 -0300 Subject: [PATCH] Renaming channels.notifications Get/Post endpoints to subscriptions.getOne and rooms.saveNotifications --- packages/rocketchat-api/server/v1/channels.js | 39 ------------------- packages/rocketchat-api/server/v1/rooms.js | 23 +++++++++++ .../rocketchat-api/server/v1/subscriptions.js | 22 +++++++++++ tests/end-to-end/api/02-channels.js | 38 ------------------ tests/end-to-end/api/09-rooms.js | 39 ++++++++++++++++++- tests/end-to-end/api/10-subscriptions.js | 31 ++++++++++++++- 6 files changed, 113 insertions(+), 79 deletions(-) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index ef07da8d1df7..26a982af249a 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -847,42 +847,3 @@ RocketChat.API.v1.addRoute('channels.getAllUserMentionsByChannel', { authRequire } }); -RocketChat.API.v1.addRoute('channels.notifications', { authRequired: true }, { - get() { - const { roomId } = this.requestParams(); - - if (!roomId) { - return RocketChat.API.v1.failure('The \'roomId\' param is required'); - } - - const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(roomId, this.userId, { - fields: { - _room: 0, - _user: 0, - $loki: 0 - } - }); - - return RocketChat.API.v1.success({ - subscription - }); - }, - post() { - const saveNotifications = (notifications, roomId) => { - Object.keys(notifications).map((notificationKey) => { - Meteor.runAsUser(this.userId, () => Meteor.call('saveNotificationSettings', roomId, notificationKey, notifications[notificationKey])); - }); - }; - const { roomId, notifications } = this.bodyParams; - - if (!roomId) { - return RocketChat.API.v1.failure('The \'roomId\' param is required'); - } - - if (!notifications || Object.keys(notifications).length === 0) { - return RocketChat.API.v1.failure('The \'notifications\' param is required'); - } - - saveNotifications(notifications, roomId); - } -}); diff --git a/packages/rocketchat-api/server/v1/rooms.js b/packages/rocketchat-api/server/v1/rooms.js index 5e9fa05b23ab..3dd8eebc4d9c 100644 --- a/packages/rocketchat-api/server/v1/rooms.js +++ b/packages/rocketchat-api/server/v1/rooms.js @@ -92,3 +92,26 @@ RocketChat.API.v1.addRoute('rooms.upload/:rid', { authRequired: true }, { return RocketChat.API.v1.success(); } }); + +RocketChat.API.v1.addRoute('rooms.saveNotification', { authRequired: true }, { + post() { + const saveNotifications = (notifications, roomId) => { + Object.keys(notifications).map((notificationKey) => { + Meteor.runAsUser(this.userId, () => Meteor.call('saveNotificationSettings', roomId, notificationKey, notifications[notificationKey])); + }); + }; + const { roomId, notifications } = this.bodyParams; + + if (!roomId) { + return RocketChat.API.v1.failure('The \'roomId\' param is required'); + } + + if (!notifications || Object.keys(notifications).length === 0) { + return RocketChat.API.v1.failure('The \'notifications\' param is required'); + } + + saveNotifications(notifications, roomId); + + return RocketChat.API.v1.success(); + } +}); diff --git a/packages/rocketchat-api/server/v1/subscriptions.js b/packages/rocketchat-api/server/v1/subscriptions.js index 0cc9c89a7dc5..6b17cf66e820 100644 --- a/packages/rocketchat-api/server/v1/subscriptions.js +++ b/packages/rocketchat-api/server/v1/subscriptions.js @@ -25,6 +25,28 @@ RocketChat.API.v1.addRoute('subscriptions.get', { authRequired: true }, { } }); +RocketChat.API.v1.addRoute('subscriptions.getOne', { authRequired: true }, { + get() { + const { roomId } = this.requestParams(); + + if (!roomId) { + return RocketChat.API.v1.failure('The \'roomId\' param is required'); + } + + const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(roomId, this.userId, { + fields: { + _room: 0, + _user: 0, + $loki: 0 + } + }); + + return RocketChat.API.v1.success({ + subscription + }); + } +}); + /** This API is suppose to mark any room as read. diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index 65bbcf98ee78..3b12b625bcaa 100644 --- a/tests/end-to-end/api/02-channels.js +++ b/tests/end-to-end/api/02-channels.js @@ -505,44 +505,6 @@ describe('[Channels]', function() { .end(done); }); - it('GET /channels.notifications', (done) => { - request.get(api('channels.notifications')) - .set(credentials) - .query({ - roomId: channel._id - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('subscription').and.to.be.an('object'); - }) - .end(done); - }); - - it('POST /channels.notifications', (done) => { - request.post(api('channels.notifications')) - .set(credentials) - .send({ - roomId: channel._id, - notifications: { - disableNotifications: '0', - emailNotifications: 'nothing', - audioNotificationValue: 'beep', - desktopNotifications: 'nothing', - desktopNotificationDuration: '2', - audioNotifications: 'all', - mobilePushNotifications: 'mentions' - } - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); - }); - it('/channels.leave', async(done) => { const roomInfo = await getRoomInfo(channel._id); diff --git a/tests/end-to-end/api/09-rooms.js b/tests/end-to-end/api/09-rooms.js index 82410c239935..4a619f77cff1 100644 --- a/tests/end-to-end/api/09-rooms.js +++ b/tests/end-to-end/api/09-rooms.js @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* globals expect */ -import {getCredentials, api, request, credentials } from '../../data/api-data.js'; +import { getCredentials, api, request, credentials } from '../../data/api-data.js'; describe('[Rooms]', function() { this.retries(0); @@ -34,4 +34,41 @@ describe('[Rooms]', function() { }) .end(done); }); + + describe('/rooms.saveNotification:', () => { + let testChannel; + it('create an channel', (done) => { + request.post(api('channels.create')) + .set(credentials) + .send({ + name: `channel.test.${ Date.now() }` + }) + .end((err, res) => { + testChannel = res.body.channel; + done(); + }); + }); + it('/rooms.saveNotification:', (done) => { + request.post(api('rooms.saveNotification')) + .set(credentials) + .send({ + roomId: testChannel._id, + notifications: { + disableNotifications: '0', + emailNotifications: 'nothing', + audioNotificationValue: 'beep', + desktopNotifications: 'nothing', + desktopNotificationDuration: '2', + audioNotifications: 'all', + mobilePushNotifications: 'mentions' + } + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + }); }); diff --git a/tests/end-to-end/api/10-subscriptions.js b/tests/end-to-end/api/10-subscriptions.js index 1f5d5783ff37..24a67ca551b4 100644 --- a/tests/end-to-end/api/10-subscriptions.js +++ b/tests/end-to-end/api/10-subscriptions.js @@ -1,7 +1,7 @@ /* eslint-env mocha */ /* globals expect */ -import {getCredentials, api, request, credentials } from '../../data/api-data.js'; +import { getCredentials, api, request, credentials } from '../../data/api-data.js'; describe('[Subscriptions]', function() { this.retries(0); @@ -36,6 +36,35 @@ describe('[Subscriptions]', function() { .end(done); }); + it('/subscriptions.getOne:', () => { + let testChannel; + it('create an channel', (done) => { + request.post(api('channels.create')) + .set(credentials) + .send({ + name: `channel.test.${ Date.now() }` + }) + .end((err, res) => { + testChannel = res.body.channel; + done(); + }); + }); + it('subscriptions.getOne', (done) => { + request.get(api('subscriptions.getOne')) + .set(credentials) + .query({ + roomId: testChannel._id + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('subscription').and.to.be.an('object'); + }) + .end(done); + }); + }); + describe('[/subscriptions.read]', () => { it('should mark public channels as read', (done) => { request.post(api('subscriptions.read'))