Skip to content

Commit

Permalink
Make all channel/group calls answer to roomName, except rename
Browse files Browse the repository at this point in the history
In .rename, having both name and roomName parameters would be confusing.

Also:
Sync groups.close error message with channels.close
Sync and fix error message in groups.open that was using a parameter that
couldn't be there before this patch.
Extract the bodyParams/queryParams logic.
  • Loading branch information
reist committed May 15, 2017
1 parent ebaccee commit eab7677
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 85 deletions.
1 change: 1 addition & 0 deletions packages/rocketchat-api/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Package.onUse(function(api) {
api.addFiles('server/settings.js', 'server');

//Register v1 helpers
api.addFiles('server/v1/helpers/requestParams.js', 'server');
api.addFiles('server/v1/helpers/getPaginationItems.js', 'server');
api.addFiles('server/v1/helpers/getUserFromParams.js', 'server');
api.addFiles('server/v1/helpers/isUserFromParams.js', 'server');
Expand Down
64 changes: 32 additions & 32 deletions packages/rocketchat-api/server/v1/channels.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//Returns the channel IF found otherwise it will return the failure of why it didn't. Check the `statusCode` property
function findChannelByIdOrName({ roomId, roomName, checkedArchived = true }) {
if ((!roomId || !roomId.trim()) && (!roomName || !roomName.trim())) {
function findChannelByIdOrName({ params, checkedArchived = true }) {
if ((!params.roomId || !params.roomId.trim()) && (!params.roomName || !params.roomName.trim())) {
throw new Meteor.Error('error-roomid-param-not-provided', 'The parameter "roomId" or "roomName" is required');
}

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 (params.roomId) {
room = RocketChat.models.Rooms.findOneById(params.roomId, { fields: RocketChat.API.v1.defaultFieldsToExclude });
} else if (params.roomName) {
room = RocketChat.models.Rooms.findOneByName(params.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 }`);
throw new Meteor.Error('error-room-not-found', 'The required "roomId" or "roomName" param provided does not match any channel');
}

if (checkedArchived && room.archived) {
Expand All @@ -24,7 +24,7 @@ function findChannelByIdOrName({ roomId, roomName, checkedArchived = true }) {

RocketChat.API.v1.addRoute('channels.addAll', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

Meteor.runAsUser(this.userId, () => {
Meteor.call('addAllUserToRoom', findResult._id, this.bodyParams.activeUsersOnly);
Expand All @@ -38,7 +38,7 @@ RocketChat.API.v1.addRoute('channels.addAll', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.addModerator', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

const user = this.getUserFromParams();

Expand All @@ -52,7 +52,7 @@ RocketChat.API.v1.addRoute('channels.addModerator', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.addOwner', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

const user = this.getUserFromParams();

Expand All @@ -66,7 +66,7 @@ RocketChat.API.v1.addRoute('channels.addOwner', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.archive', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

Meteor.runAsUser(this.userId, () => {
Meteor.call('archiveRoom', findResult._id);
Expand All @@ -78,7 +78,7 @@ RocketChat.API.v1.addRoute('channels.archive', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.cleanHistory', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

if (!this.bodyParams.latest) {
return RocketChat.API.v1.failure('Body parameter "latest" is required.');
Expand Down Expand Up @@ -106,7 +106,7 @@ RocketChat.API.v1.addRoute('channels.cleanHistory', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.close', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId, checkedArchived: false });
const findResult = findChannelByIdOrName({ params: this.requestParams(), checkedArchived: false });

const sub = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(findResult._id, this.userId);

Expand Down Expand Up @@ -162,7 +162,7 @@ RocketChat.API.v1.addRoute('channels.create', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.delete', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId, roomName: this.bodyParams.roomName, checkedArchived: false });
const findResult = findChannelByIdOrName({ params: this.requestParams(), checkedArchived: false });

//The find method returns either with the group or the failur

Expand All @@ -182,7 +182,7 @@ RocketChat.API.v1.addRoute('channels.getIntegrations', { authRequired: true }, {
return RocketChat.API.v1.unauthorized();
}

const findResult = findChannelByIdOrName({ roomId: this.queryParams.roomId, checkedArchived: false });
const findResult = findChannelByIdOrName({ params: this.requestParams(), checkedArchived: false });

let includeAllPublicChannels = true;
if (typeof this.queryParams.includeAllPublicChannels !== 'undefined') {
Expand Down Expand Up @@ -222,7 +222,7 @@ RocketChat.API.v1.addRoute('channels.getIntegrations', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.history', { authRequired: true }, {
get() {
const findResult = findChannelByIdOrName({ roomId: this.queryParams.roomId, checkedArchived: false });
const findResult = findChannelByIdOrName({ params: this.requestParams(), checkedArchived: false });

let latestDate = new Date();
if (this.queryParams.latest) {
Expand Down Expand Up @@ -262,7 +262,7 @@ RocketChat.API.v1.addRoute('channels.history', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.info', { authRequired: true }, {
get() {
const findResult = findChannelByIdOrName({ roomId: this.queryParams.roomId, roomName: this.queryParams.roomName, checkedArchived: false });
const findResult = findChannelByIdOrName({ params: this.requestParams(), checkedArchived: false });

return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude })
Expand All @@ -272,7 +272,7 @@ RocketChat.API.v1.addRoute('channels.info', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.invite', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

const user = this.getUserFromParams();

Expand All @@ -288,7 +288,7 @@ RocketChat.API.v1.addRoute('channels.invite', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.join', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

Meteor.runAsUser(this.userId, () => {
Meteor.call('joinRoom', findResult._id, this.bodyParams.joinCode);
Expand All @@ -302,7 +302,7 @@ RocketChat.API.v1.addRoute('channels.join', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.kick', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

const user = this.getUserFromParams();

Expand All @@ -318,7 +318,7 @@ RocketChat.API.v1.addRoute('channels.kick', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.leave', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

Meteor.runAsUser(this.userId, () => {
Meteor.call('leaveRoom', findResult._id);
Expand Down Expand Up @@ -414,7 +414,7 @@ RocketChat.API.v1.addRoute('channels.online', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.open', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId, checkedArchived: false });
const findResult = findChannelByIdOrName({ params: this.requestParams(), checkedArchived: false });

const sub = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(findResult._id, this.userId);

Expand All @@ -436,7 +436,7 @@ RocketChat.API.v1.addRoute('channels.open', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.removeModerator', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

const user = this.getUserFromParams();

Expand All @@ -450,7 +450,7 @@ RocketChat.API.v1.addRoute('channels.removeModerator', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.removeOwner', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

const user = this.getUserFromParams();

Expand All @@ -468,7 +468,7 @@ RocketChat.API.v1.addRoute('channels.rename', { authRequired: true }, {
return RocketChat.API.v1.failure('The bodyParam "name" is required');
}

const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: { 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 @@ -490,7 +490,7 @@ RocketChat.API.v1.addRoute('channels.setDescription', { authRequired: true }, {
return RocketChat.API.v1.failure('The bodyParam "description" is required');
}

const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

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 @@ -512,7 +512,7 @@ RocketChat.API.v1.addRoute('channels.setJoinCode', { authRequired: true }, {
return RocketChat.API.v1.failure('The bodyParam "joinCode" is required');
}

const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

Meteor.runAsUser(this.userId, () => {
Meteor.call('saveRoomSettings', findResult._id, 'joinCode', this.bodyParams.joinCode);
Expand All @@ -530,7 +530,7 @@ RocketChat.API.v1.addRoute('channels.setPurpose', { authRequired: true }, {
return RocketChat.API.v1.failure('The bodyParam "purpose" is required');
}

const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

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 @@ -552,7 +552,7 @@ RocketChat.API.v1.addRoute('channels.setReadOnly', { authRequired: true }, {
return RocketChat.API.v1.failure('The bodyParam "readOnly" is required');
}

const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

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 @@ -574,7 +574,7 @@ RocketChat.API.v1.addRoute('channels.setTopic', { authRequired: true }, {
return RocketChat.API.v1.failure('The bodyParam "topic" is required');
}

const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

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 @@ -596,7 +596,7 @@ RocketChat.API.v1.addRoute('channels.setType', { authRequired: true }, {
return RocketChat.API.v1.failure('The bodyParam "type" is required');
}

const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId });
const findResult = findChannelByIdOrName({ params: this.requestParams() });

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 @@ -614,7 +614,7 @@ RocketChat.API.v1.addRoute('channels.setType', { authRequired: true }, {

RocketChat.API.v1.addRoute('channels.unarchive', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ roomId: this.bodyParams.roomId, checkedArchived: false });
const findResult = findChannelByIdOrName({ params: this.requestParams(), checkedArchived: false });

if (!findResult.archived) {
return RocketChat.API.v1.failure(`The channel, ${ findResult.name }, is not archived`);
Expand Down
Loading

0 comments on commit eab7677

Please sign in to comment.