diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index 55a870b526b..be7de10766b 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', '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.', 'error-channels-setdefault-missing-default-param'); + } + + 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()) { diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 758da563dd9..177b4c0c8c1 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -1003,6 +1003,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", diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index 35f610b784d..e54d8928511 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);