From 0f029e42badb74a14ce0e6de0696a81225287fec Mon Sep 17 00:00:00 2001 From: Boris Peterbarg Date: Thu, 27 Apr 2017 19:36:10 +0300 Subject: [PATCH] Make channels.info accept roomName, just like groups.info --- packages/rocketchat-api/server/v1/channels.js | 63 ++++++++++--------- packages/rocketchat-api/server/v1/groups.js | 2 +- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index e145a84f0e14..1de2d82d724b 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -1,10 +1,15 @@ //Returns the channel IF found otherwise it will return the failure of why it didn't. Check the `statusCode` property -function findChannelById({ roomId, checkedArchived = true }) { - if (!roomId || !roomId.trim()) { - throw new Meteor.Error('error-roomid-param-not-provided', 'The parameter "roomId" is required'); +function findChannelByIdOrName({ roomId, roomName, checkedArchived = true }) { + if ((!roomId || !roomId.trim()) && (!roomName || !roomName.trim())) { + throw new Meteor.Error('error-roomid-param-not-provided', 'The parameter "roomId" or "roomName" is required'); } - const room = RocketChat.models.Rooms.findOneById(roomId, { fields: RocketChat.API.v1.defaultFieldsToExclude }); + let room; + if (roomId) { + room = RocketChat.models.Rooms.findOneById(roomId, { fields: RocketChat.API.v1.defaultFieldsToExclude }); + } else if (roomName) { + room = RocketChat.models.Rooms.findOneByName(roomName, { fields: RocketChat.API.v1.defaultFieldsToExclude }); + } if (!room || room.t !== 'c') { throw new Meteor.Error('error-room-not-found', `No channel found by the id of: ${ roomId }`); @@ -19,7 +24,7 @@ function findChannelById({ roomId, checkedArchived = true }) { RocketChat.API.v1.addRoute('channels.addAll', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); Meteor.runAsUser(this.userId, () => { Meteor.call('addAllUserToRoom', findResult._id, this.bodyParams.activeUsersOnly); @@ -33,7 +38,7 @@ RocketChat.API.v1.addRoute('channels.addAll', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.addModerator', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); const user = this.getUserFromParams(); @@ -47,7 +52,7 @@ RocketChat.API.v1.addRoute('channels.addModerator', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.addOwner', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); const user = this.getUserFromParams(); @@ -61,7 +66,7 @@ RocketChat.API.v1.addRoute('channels.addOwner', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.archive', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); Meteor.runAsUser(this.userId, () => { Meteor.call('archiveRoom', findResult._id); @@ -73,7 +78,7 @@ RocketChat.API.v1.addRoute('channels.archive', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.cleanHistory', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); if (!this.bodyParams.latest) { return RocketChat.API.v1.failure('Body parameter "latest" is required.'); @@ -101,7 +106,7 @@ RocketChat.API.v1.addRoute('channels.cleanHistory', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.close', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId, checkedArchived: false }); const sub = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(findResult._id, this.userId); @@ -157,7 +162,7 @@ RocketChat.API.v1.addRoute('channels.create', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.delete', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId, checkedArchived: false }); //The find method returns either with the group or the failur @@ -177,7 +182,7 @@ RocketChat.API.v1.addRoute('channels.getIntegrations', { authRequired: true }, { return RocketChat.API.v1.unauthorized(); } - const findResult = findChannelById({ roomId: this.queryParams.roomId, checkedArchived: false }); + const findResult = findChannelByIdOrName({ roomId: this.queryParams.roomId, checkedArchived: false }); let includeAllPublicChannels = true; if (typeof this.queryParams.includeAllPublicChannels !== 'undefined') { @@ -217,7 +222,7 @@ RocketChat.API.v1.addRoute('channels.getIntegrations', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.history', { authRequired: true }, { get() { - const findResult = findChannelById({ roomId: this.queryParams.roomId, checkedArchived: false }); + const findResult = findChannelByIdOrName({ roomId: this.queryParams.roomId, checkedArchived: false }); let latestDate = new Date(); if (this.queryParams.latest) { @@ -257,7 +262,7 @@ RocketChat.API.v1.addRoute('channels.history', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.info', { authRequired: true }, { get() { - const findResult = findChannelById({ roomId: this.queryParams.roomId, checkedArchived: false }); + const findResult = findChannelByIdOrName({ roomId: this.queryParams.roomId, roomName: this.queryParams.roomName, checkedArchived: false }); return RocketChat.API.v1.success({ channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }) @@ -267,7 +272,7 @@ RocketChat.API.v1.addRoute('channels.info', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.invite', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); const user = this.getUserFromParams(); @@ -283,7 +288,7 @@ RocketChat.API.v1.addRoute('channels.invite', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.join', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); Meteor.runAsUser(this.userId, () => { Meteor.call('joinRoom', findResult._id, this.bodyParams.joinCode); @@ -297,7 +302,7 @@ RocketChat.API.v1.addRoute('channels.join', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.kick', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); const user = this.getUserFromParams(); @@ -313,7 +318,7 @@ RocketChat.API.v1.addRoute('channels.kick', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.leave', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); Meteor.runAsUser(this.userId, () => { Meteor.call('leaveRoom', findResult._id); @@ -409,7 +414,7 @@ RocketChat.API.v1.addRoute('channels.online', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.open', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId, checkedArchived: false }); const sub = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(findResult._id, this.userId); @@ -431,7 +436,7 @@ RocketChat.API.v1.addRoute('channels.open', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.removeModerator', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); const user = this.getUserFromParams(); @@ -445,7 +450,7 @@ RocketChat.API.v1.addRoute('channels.removeModerator', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.removeOwner', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); const user = this.getUserFromParams(); @@ -463,7 +468,7 @@ RocketChat.API.v1.addRoute('channels.rename', { authRequired: true }, { return RocketChat.API.v1.failure('The bodyParam "name" is required'); } - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); if (findResult.name === this.bodyParams.name) { return RocketChat.API.v1.failure('The channel name is the same as what it would be renamed to.'); @@ -485,7 +490,7 @@ RocketChat.API.v1.addRoute('channels.setDescription', { authRequired: true }, { return RocketChat.API.v1.failure('The bodyParam "description" is required'); } - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); if (findResult.description === this.bodyParams.description) { return RocketChat.API.v1.failure('The channel description is the same as what it would be changed to.'); @@ -507,7 +512,7 @@ RocketChat.API.v1.addRoute('channels.setJoinCode', { authRequired: true }, { return RocketChat.API.v1.failure('The bodyParam "joinCode" is required'); } - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); Meteor.runAsUser(this.userId, () => { Meteor.call('saveRoomSettings', findResult._id, 'joinCode', this.bodyParams.joinCode); @@ -525,7 +530,7 @@ RocketChat.API.v1.addRoute('channels.setPurpose', { authRequired: true }, { return RocketChat.API.v1.failure('The bodyParam "purpose" is required'); } - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); if (findResult.description === this.bodyParams.purpose) { return RocketChat.API.v1.failure('The channel purpose (description) is the same as what it would be changed to.'); @@ -547,7 +552,7 @@ RocketChat.API.v1.addRoute('channels.setReadOnly', { authRequired: true }, { return RocketChat.API.v1.failure('The bodyParam "readOnly" is required'); } - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); if (findResult.ro === this.bodyParams.readOnly) { return RocketChat.API.v1.failure('The channel read only setting is the same as what it would be changed to.'); @@ -569,7 +574,7 @@ RocketChat.API.v1.addRoute('channels.setTopic', { authRequired: true }, { return RocketChat.API.v1.failure('The bodyParam "topic" is required'); } - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); if (findResult.topic === this.bodyParams.topic) { return RocketChat.API.v1.failure('The channel topic is the same as what it would be changed to.'); @@ -591,7 +596,7 @@ RocketChat.API.v1.addRoute('channels.setType', { authRequired: true }, { return RocketChat.API.v1.failure('The bodyParam "type" is required'); } - const findResult = findChannelById({ roomId: this.bodyParams.roomId }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId }); if (findResult.t === this.bodyParams.type) { return RocketChat.API.v1.failure('The channel type is the same as what it would be changed to.'); @@ -609,7 +614,7 @@ RocketChat.API.v1.addRoute('channels.setType', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.unarchive', { authRequired: true }, { post() { - const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false }); + const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId, checkedArchived: false }); if (!findResult.archived) { return RocketChat.API.v1.failure(`The channel, ${ findResult.name }, is not archived`); diff --git a/packages/rocketchat-api/server/v1/groups.js b/packages/rocketchat-api/server/v1/groups.js index f05538c4d6d9..10bef449b9c1 100644 --- a/packages/rocketchat-api/server/v1/groups.js +++ b/packages/rocketchat-api/server/v1/groups.js @@ -1,4 +1,4 @@ -//Returns the private group subscription IF found otherwise it will reutrn the failure of why it didn't. Check the `statusCode` property +//Returns the private group subscription IF found otherwise it will return the failure of why it didn't. Check the `statusCode` property function findPrivateGroupByIdOrName({ roomId, roomName, userId, checkedArchived = true }) { if ((!roomId || !roomId.trim()) && (!roomName || !roomName.trim())) { throw new Meteor.Error('error-roomid-param-not-provided', 'The parameter "roomId" or "roomName" is required');