Skip to content
Snippets Groups Projects
Unverified Commit 22758c0d authored by Rodrigo Nascimento's avatar Rodrigo Nascimento
Browse files

API: Improve method findChannelById to throw instead of return error

parent 3d5b3d60
No related branches found
No related tags found
No related merge requests found
//Returns the channel IF found otherwise it will return the failure of why it didn't. Check the `statusCode` property //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 }) { function findChannelById({ roomId, checkedArchived = true }) {
if (!roomId || !roomId.trim()) { if (!roomId || !roomId.trim()) {
return RocketChat.API.v1.failure('The parameter "roomId" is required'); throw new Meteor.Error('error-roomid-param-not-provided', 'The parameter "roomId" is required');
} }
const room = RocketChat.models.Rooms.findOneById(roomId, { fields: RocketChat.API.v1.defaultFieldsToExclude }); const room = RocketChat.models.Rooms.findOneById(roomId, { fields: RocketChat.API.v1.defaultFieldsToExclude });
if (!room || room.t !== 'c') { if (!room || room.t !== 'c') {
return RocketChat.API.v1.failure(`No channel found by the id of: ${roomId}`); throw new Meteor.Error('error-room-not-found', `No channel found by the id of: ${roomId}`);
} }
if (checkedArchived && room.archived) { if (checkedArchived && room.archived) {
return RocketChat.API.v1.failure(`The channel, ${room.name}, is archived`); throw new Meteor.Error('error-room-aquived', `The channel, ${room.name}, is archived`);
} }
return room; return room;
...@@ -21,10 +21,6 @@ RocketChat.API.v1.addRoute('channels.addAll', { authRequired: true }, { ...@@ -21,10 +21,6 @@ RocketChat.API.v1.addRoute('channels.addAll', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
Meteor.call('addAllUserToRoom', findResult._id); Meteor.call('addAllUserToRoom', findResult._id);
}); });
...@@ -39,10 +35,6 @@ RocketChat.API.v1.addRoute('channels.addModerator', { authRequired: true }, { ...@@ -39,10 +35,6 @@ RocketChat.API.v1.addRoute('channels.addModerator', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
const user = this.getUserFromParams(); const user = this.getUserFromParams();
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
...@@ -57,10 +49,6 @@ RocketChat.API.v1.addRoute('channels.addOwner', { authRequired: true }, { ...@@ -57,10 +49,6 @@ RocketChat.API.v1.addRoute('channels.addOwner', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
const user = this.getUserFromParams(); const user = this.getUserFromParams();
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
...@@ -75,10 +63,6 @@ RocketChat.API.v1.addRoute('channels.archive', { authRequired: true }, { ...@@ -75,10 +63,6 @@ RocketChat.API.v1.addRoute('channels.archive', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
Meteor.call('archiveRoom', findResult._id); Meteor.call('archiveRoom', findResult._id);
}); });
...@@ -91,10 +75,6 @@ RocketChat.API.v1.addRoute('channels.cleanHistory', { authRequired: true }, { ...@@ -91,10 +75,6 @@ RocketChat.API.v1.addRoute('channels.cleanHistory', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
if (!this.bodyParams.latest) { if (!this.bodyParams.latest) {
return RocketChat.API.v1.failure('Body parameter "latest" is required.'); return RocketChat.API.v1.failure('Body parameter "latest" is required.');
} }
...@@ -123,10 +103,6 @@ RocketChat.API.v1.addRoute('channels.close', { authRequired: true }, { ...@@ -123,10 +103,6 @@ RocketChat.API.v1.addRoute('channels.close', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false }); const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false });
if (findResult.statusCode) {
return findResult;
}
const sub = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(findResult._id, this.userId); const sub = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(findResult._id, this.userId);
if (!sub) { if (!sub) {
...@@ -179,10 +155,7 @@ RocketChat.API.v1.addRoute('channels.delete', { authRequired: true }, { ...@@ -179,10 +155,7 @@ RocketChat.API.v1.addRoute('channels.delete', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false }); const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false });
//The find method returns either with the group or the failure //The find method returns either with the group or the failur
if (findResult.statusCode) {
return findResult;
}
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
Meteor.call('eraseRoom', findResult._id); Meteor.call('eraseRoom', findResult._id);
...@@ -202,10 +175,6 @@ RocketChat.API.v1.addRoute('channels.getIntegrations', { authRequired: true }, { ...@@ -202,10 +175,6 @@ RocketChat.API.v1.addRoute('channels.getIntegrations', { authRequired: true }, {
const findResult = findChannelById({ roomId: this.queryParams.roomId, checkedArchived: false }); const findResult = findChannelById({ roomId: this.queryParams.roomId, checkedArchived: false });
if (findResult.statusCode) {
return findResult;
}
let includeAllPublicChannels = true; let includeAllPublicChannels = true;
if (typeof this.queryParams.includeAllPublicChannels !== 'undefined') { if (typeof this.queryParams.includeAllPublicChannels !== 'undefined') {
includeAllPublicChannels = this.queryParams.includeAllPublicChannels === 'true'; includeAllPublicChannels = this.queryParams.includeAllPublicChannels === 'true';
...@@ -258,10 +227,6 @@ RocketChat.API.v1.addRoute('channels.history', { authRequired: true }, { ...@@ -258,10 +227,6 @@ RocketChat.API.v1.addRoute('channels.history', { authRequired: true }, {
get: function() { get: function() {
const findResult = findChannelById({ roomId: this.queryParams.roomId, checkedArchived: false }); const findResult = findChannelById({ roomId: this.queryParams.roomId, checkedArchived: false });
if (findResult.statusCode) {
return findResult;
}
let latestDate = new Date(); let latestDate = new Date();
if (this.queryParams.latest) { if (this.queryParams.latest) {
latestDate = new Date(this.queryParams.latest); latestDate = new Date(this.queryParams.latest);
...@@ -302,10 +267,6 @@ RocketChat.API.v1.addRoute('channels.info', { authRequired: true }, { ...@@ -302,10 +267,6 @@ RocketChat.API.v1.addRoute('channels.info', { authRequired: true }, {
get: function() { get: function() {
const findResult = findChannelById({ roomId: this.queryParams.roomId, checkedArchived: false }); const findResult = findChannelById({ roomId: this.queryParams.roomId, checkedArchived: false });
if (findResult.statusCode) {
return findResult;
}
return RocketChat.API.v1.success({ return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude }) channel: RocketChat.models.Rooms.findOneById(findResult._id, { fields: RocketChat.API.v1.defaultFieldsToExclude })
}); });
...@@ -316,10 +277,6 @@ RocketChat.API.v1.addRoute('channels.invite', { authRequired: true }, { ...@@ -316,10 +277,6 @@ RocketChat.API.v1.addRoute('channels.invite', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
const user = this.getUserFromParams(); const user = this.getUserFromParams();
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
...@@ -336,10 +293,6 @@ RocketChat.API.v1.addRoute('channels.join', { authRequired: true }, { ...@@ -336,10 +293,6 @@ RocketChat.API.v1.addRoute('channels.join', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
Meteor.call('joinRoom', findResult._id, this.bodyParams.joinCode); Meteor.call('joinRoom', findResult._id, this.bodyParams.joinCode);
}); });
...@@ -354,10 +307,6 @@ RocketChat.API.v1.addRoute('channels.kick', { authRequired: true }, { ...@@ -354,10 +307,6 @@ RocketChat.API.v1.addRoute('channels.kick', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
const user = this.getUserFromParams(); const user = this.getUserFromParams();
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
...@@ -374,10 +323,6 @@ RocketChat.API.v1.addRoute('channels.leave', { authRequired: true }, { ...@@ -374,10 +323,6 @@ RocketChat.API.v1.addRoute('channels.leave', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
Meteor.call('leaveRoom', findResult._id); Meteor.call('leaveRoom', findResult._id);
}); });
...@@ -441,10 +386,6 @@ RocketChat.API.v1.addRoute('channels.open', { authRequired: true }, { ...@@ -441,10 +386,6 @@ RocketChat.API.v1.addRoute('channels.open', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false }); const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false });
if (findResult.statusCode) {
return findResult;
}
const sub = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(findResult._id, this.userId); const sub = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(findResult._id, this.userId);
if (!sub) { if (!sub) {
...@@ -467,10 +408,6 @@ RocketChat.API.v1.addRoute('channels.removeModerator', { authRequired: true }, { ...@@ -467,10 +408,6 @@ RocketChat.API.v1.addRoute('channels.removeModerator', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
const user = this.getUserFromParams(); const user = this.getUserFromParams();
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
...@@ -485,10 +422,6 @@ RocketChat.API.v1.addRoute('channels.removeOwner', { authRequired: true }, { ...@@ -485,10 +422,6 @@ RocketChat.API.v1.addRoute('channels.removeOwner', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
const user = this.getUserFromParams(); const user = this.getUserFromParams();
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
...@@ -507,10 +440,6 @@ RocketChat.API.v1.addRoute('channels.rename', { authRequired: true }, { ...@@ -507,10 +440,6 @@ RocketChat.API.v1.addRoute('channels.rename', { authRequired: true }, {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
if (findResult.name === this.bodyParams.name) { if (findResult.name === this.bodyParams.name) {
return RocketChat.API.v1.failure('The channel name is the same as what it would be renamed to.'); return RocketChat.API.v1.failure('The channel name is the same as what it would be renamed to.');
} }
...@@ -533,10 +462,6 @@ RocketChat.API.v1.addRoute('channels.setDescription', { authRequired: true }, { ...@@ -533,10 +462,6 @@ RocketChat.API.v1.addRoute('channels.setDescription', { authRequired: true }, {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
if (findResult.description === this.bodyParams.description) { if (findResult.description === this.bodyParams.description) {
return RocketChat.API.v1.failure('The channel description is the same as what it would be changed to.'); return RocketChat.API.v1.failure('The channel description is the same as what it would be changed to.');
} }
...@@ -559,10 +484,6 @@ RocketChat.API.v1.addRoute('channels.setJoinCode', { authRequired: true }, { ...@@ -559,10 +484,6 @@ RocketChat.API.v1.addRoute('channels.setJoinCode', { authRequired: true }, {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
Meteor.call('saveRoomSettings', findResult._id, 'joinCode', this.bodyParams.joinCode); Meteor.call('saveRoomSettings', findResult._id, 'joinCode', this.bodyParams.joinCode);
}); });
...@@ -581,10 +502,6 @@ RocketChat.API.v1.addRoute('channels.setPurpose', { authRequired: true }, { ...@@ -581,10 +502,6 @@ RocketChat.API.v1.addRoute('channels.setPurpose', { authRequired: true }, {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
if (findResult.description === this.bodyParams.purpose) { 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.'); return RocketChat.API.v1.failure('The channel purpose (description) is the same as what it would be changed to.');
} }
...@@ -607,10 +524,6 @@ RocketChat.API.v1.addRoute('channels.setReadOnly', { authRequired: true }, { ...@@ -607,10 +524,6 @@ RocketChat.API.v1.addRoute('channels.setReadOnly', { authRequired: true }, {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
if (findResult.ro === this.bodyParams.readOnly) { 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.'); return RocketChat.API.v1.failure('The channel read only setting is the same as what it would be changed to.');
} }
...@@ -633,10 +546,6 @@ RocketChat.API.v1.addRoute('channels.setTopic', { authRequired: true }, { ...@@ -633,10 +546,6 @@ RocketChat.API.v1.addRoute('channels.setTopic', { authRequired: true }, {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
if (findResult.topic === this.bodyParams.topic) { if (findResult.topic === this.bodyParams.topic) {
return RocketChat.API.v1.failure('The channel topic is the same as what it would be changed to.'); return RocketChat.API.v1.failure('The channel topic is the same as what it would be changed to.');
} }
...@@ -659,10 +568,6 @@ RocketChat.API.v1.addRoute('channels.setType', { authRequired: true }, { ...@@ -659,10 +568,6 @@ RocketChat.API.v1.addRoute('channels.setType', { authRequired: true }, {
const findResult = findChannelById({ roomId: this.bodyParams.roomId }); const findResult = findChannelById({ roomId: this.bodyParams.roomId });
if (findResult.statusCode) {
return findResult;
}
if (findResult.t === this.bodyParams.type) { if (findResult.t === this.bodyParams.type) {
return RocketChat.API.v1.failure('The channel type is the same as what it would be changed to.'); return RocketChat.API.v1.failure('The channel type is the same as what it would be changed to.');
} }
...@@ -681,10 +586,6 @@ RocketChat.API.v1.addRoute('channels.unarchive', { authRequired: true }, { ...@@ -681,10 +586,6 @@ RocketChat.API.v1.addRoute('channels.unarchive', { authRequired: true }, {
post: function() { post: function() {
const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false }); const findResult = findChannelById({ roomId: this.bodyParams.roomId, checkedArchived: false });
if (findResult.statusCode) {
return findResult;
}
if (!findResult.archived) { if (!findResult.archived) {
return RocketChat.API.v1.failure(`The channel, ${findResult.name}, is not archived`); return RocketChat.API.v1.failure(`The channel, ${findResult.name}, is not archived`);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment