From 9d2d86c5fb297a27b9ba374e584dfb2024c64aad Mon Sep 17 00:00:00 2001 From: vynmera <39674991+vynmera@users.noreply.github.com> Date: Tue, 29 May 2018 22:06:03 +0000 Subject: [PATCH 1/3] Basic functionality --- packages/rocketchat-api/server/v1/channels.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index 55a870b526b63..778f3c0fc7b0e 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -747,6 +747,28 @@ RocketChat.API.v1.addRoute('channels.setCustomFields', { authRequired: true }, { } }); +RocketChat.API.v1.addRoute('channels.setDefault', { authRequired: true }, { + post() { + if (typeof this.bodyParams.default === 'undefined') { + return RocketChat.API.v1.failure('The bodyParam "default" is required'); + } + + const findResult = findChannelByIdOrName({ params: this.requestParams() }); + + if (findResult.default === this.bodyParams.default) { + return RocketChat.API.v1.failure('The channel default setting is the same as what it would be changed to.'); + } + + Meteor.runAsUser(this.userId, () => { + Meteor.call('saveRoomSettings', findResult._id, 'default', this.bodyParams.default.toString()); + }); + + return RocketChat.API.v1.success({ + channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }) + }); + } +}); + RocketChat.API.v1.addRoute('channels.setDescription', { authRequired: true }, { post() { if (!this.bodyParams.description || !this.bodyParams.description.trim()) { From a686c1212b8935b003c7e154133da120e4812cc6 Mon Sep 17 00:00:00 2001 From: vynmera <39674991+vynmera@users.noreply.github.com> Date: Tue, 29 May 2018 22:12:51 +0000 Subject: [PATCH 2/3] Implement tests --- tests/end-to-end/api/02-channels.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index 35f610b784d67..e54d892851143 100644 --- a/tests/end-to-end/api/02-channels.js +++ b/tests/end-to-end/api/02-channels.js @@ -727,6 +727,27 @@ describe('[Channels]', function() { .end(done); }); + it('/channels.setDefault', async(done) => { + const roomInfo = await getRoomInfo(channel._id); + + request.post(api('channels.setDefault')) + .set(credentials) + .send({ + roomId: channel._id, + default: true + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('channel._id'); + expect(res.body).to.have.nested.property('channel.name', `EDITED${ apiPublicChannelName }`); + expect(res.body).to.have.nested.property('channel.t', 'c'); + expect(res.body).to.have.nested.property('channel.msgs', roomInfo.channel.msgs); + }) + .end(done); + }); + it('/channels.leave', async(done) => { const roomInfo = await getRoomInfo(channel._id); From 4e8f873b824b7694376db76d94eaf962da5a3731 Mon Sep 17 00:00:00 2001 From: vynmera <39674991+vynmera@users.noreply.github.com> Date: Wed, 30 May 2018 20:13:31 +0000 Subject: [PATCH 3/3] Address suggested changes --- packages/rocketchat-api/server/v1/channels.js | 4 ++-- packages/rocketchat-i18n/i18n/en.i18n.json | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index 778f3c0fc7b0e..be7de10766bec 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -750,13 +750,13 @@ RocketChat.API.v1.addRoute('channels.setCustomFields', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.setDefault', { authRequired: true }, { post() { if (typeof this.bodyParams.default === 'undefined') { - return RocketChat.API.v1.failure('The bodyParam "default" is required'); + return RocketChat.API.v1.failure('The bodyParam "default" is required', 'error-channels-setdefault-is-same'); } const findResult = findChannelByIdOrName({ params: this.requestParams() }); if (findResult.default === this.bodyParams.default) { - return RocketChat.API.v1.failure('The channel default setting is the same as what it would be changed to.'); + return RocketChat.API.v1.failure('The channel default setting is the same as what it would be changed to.', 'error-channels-setdefault-missing-default-param'); } Meteor.runAsUser(this.userId, () => { diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 7a1b3e0412d1f..3a494cf32b45e 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -730,6 +730,8 @@ "error-avatar-invalid-url": "Invalid avatar URL: __url__", "error-avatar-url-handling": "Error while handling avatar setting from a URL (__url__) for __username__", "error-cant-invite-for-direct-room": "Can't invite user to direct rooms", + "error-channels-setdefault-is-same": "The channel default setting is the same as what it would be changed to.", + "error-channels-setdefault-missing-default-param": "The bodyParam 'default' is required", "error-could-not-change-email": "Could not change email", "error-could-not-change-name": "Could not change name", "error-could-not-change-username": "Could not change username",