Skip to content

Commit

Permalink
Make channels.info accept roomName, just like groups.info
Browse files Browse the repository at this point in the history
  • Loading branch information
reist committed Apr 27, 2017
1 parent 1359fff commit 0f029e4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
63 changes: 34 additions & 29 deletions packages/rocketchat-api/server/v1/channels.js
Original file line number Diff line number Diff line change
@@ -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 }`);
Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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.');
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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

Expand All @@ -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') {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 })
Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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.');
Expand All @@ -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.');
Expand All @@ -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);
Expand All @@ -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.');
Expand All @@ -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.');
Expand All @@ -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.');
Expand All @@ -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.');
Expand All @@ -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`);
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-api/server/v1/groups.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down

0 comments on commit 0f029e4

Please sign in to comment.