diff --git a/packages/rocketchat-api/server/v1/chat.js b/packages/rocketchat-api/server/v1/chat.js index 0b27d26f627..b816e42e8df 100644 --- a/packages/rocketchat-api/server/v1/chat.js +++ b/packages/rocketchat-api/server/v1/chat.js @@ -2,11 +2,11 @@ RocketChat.API.v1.addRoute('chat.delete', { authRequired: true }, { post: function() { try { - check(this.bodyParams, { + check(this.bodyParams, Match.ObjectIncluding({ msgId: String, roomId: String, asUser: Match.Maybe(Boolean) - }); + })); const msg = RocketChat.models.Messages.findOneById(this.bodyParams.msgId, { fields: { u: 1, rid: 1 }}); @@ -35,14 +35,6 @@ RocketChat.API.v1.addRoute('chat.delete', { authRequired: true }, { RocketChat.API.v1.addRoute('chat.postMessage', { authRequired: true }, { post: function() { try { - if (!this.bodyParams.attachments) { - check(this.bodyParams, { - channel: String, - text: String - }); - } - - //TODO: Completely rewrite this? Seems too "magical" const messageReturn = processWebhookMessage(this.bodyParams, this.user)[0]; if (!messageReturn) { diff --git a/packages/rocketchat-api/server/v1/users.js b/packages/rocketchat-api/server/v1/users.js index 7d432716660..1f5fea29d96 100644 --- a/packages/rocketchat-api/server/v1/users.js +++ b/packages/rocketchat-api/server/v1/users.js @@ -116,10 +116,16 @@ RocketChat.API.v1.addRoute('users.info', { authRequired: true }, { RocketChat.API.v1.addRoute('users.list', { authRequired: true }, { get: function() { + let limit = -1; + + if (typeof this.queryParams.limit !== 'undefined') { + limit = parseInt(limit); + } + let result = undefined; try { Meteor.runAsUser(this.userId, () => { - result = Meteor.call('getFullUserData', {}); + result = Meteor.call('getFullUserData', { filter: '', limit }); }); } catch (e) { return RocketChat.API.v1.failure(e.name + ': ' + e.message); diff --git a/packages/rocketchat-integrations/server/processWebhookMessage.js b/packages/rocketchat-integrations/server/processWebhookMessage.js index 56730a50d55..90cd9de64a0 100644 --- a/packages/rocketchat-integrations/server/processWebhookMessage.js +++ b/packages/rocketchat-integrations/server/processWebhookMessage.js @@ -1,5 +1,47 @@ +function retrieveRoomInfo({ userId, channel, ignoreEmpty=false }) { + const room = RocketChat.models.Rooms.findOneByIdOrName(channel); + if (!_.isObject(room) && !ignoreEmpty) { + throw new Meteor.Error('invalid-channel'); + } + + if (room && room.t === 'c') { + Meteor.runAsUser(userId, function() { + return Meteor.call('joinRoom', room._id); + }); + } + + return room; +} + +function retrieveDirectMessageInfo({ userId, channel }) { + const roomUser = RocketChat.models.Users.findOne({ + $or: [ + { + _id: channel + }, { + username: channel + } + ] + }) || {}; + + const rid = [userId, roomUser._id].sort().join(''); + let room = RocketChat.models.Rooms.findOneById({$in: [rid, channel]}); + if (!_.isObject(roomUser) && !_.isObject(room)) { + throw new Meteor.Error('invalid-channel'); + } + + if (!room) { + Meteor.runAsUser(userId, function() { + Meteor.call('createDirectMessage', roomUser.username); + room = RocketChat.models.Rooms.findOneById(rid); + }); + } + + return room; +} + this.processWebhookMessage = function(messageObj, user, defaultValues) { - var attachment, channel, channels, channelType, i, len, message, ref, rid, room, roomUser, ret; + var attachment, channel, channels, channelType, i, len, message, ref, room, ret; ret = []; if (!defaultValues) { @@ -11,7 +53,7 @@ this.processWebhookMessage = function(messageObj, user, defaultValues) { }; } - channel = messageObj.channel || defaultValues.channel; + channel = messageObj.channel || messageObj.roomId || defaultValues.channel; channels = [].concat(channel); @@ -22,41 +64,26 @@ this.processWebhookMessage = function(messageObj, user, defaultValues) { switch (channelType) { case '#': - room = RocketChat.models.Rooms.findOneByIdOrName(channel); - if (!_.isObject(room)) { - throw new Meteor.Error('invalid-channel'); - } - rid = room._id; - if (room.t === 'c') { - Meteor.runAsUser(user._id, function() { - return Meteor.call('joinRoom', room._id); - }); - } + room = retrieveRoomInfo({ userId: user._id, channel }); break; case '@': - roomUser = RocketChat.models.Users.findOne({ - $or: [ - { - _id: channel - }, { - username: channel - } - ] - }) || {}; - rid = [user._id, roomUser._id].sort().join(''); - room = RocketChat.models.Rooms.findOneById({$in: [rid, channel]}); - if (!_.isObject(roomUser) && !_.isObject(room)) { - throw new Meteor.Error('invalid-channel'); - } - if (!room) { - Meteor.runAsUser(user._id, function() { - Meteor.call('createDirectMessage', roomUser.username); - room = RocketChat.models.Rooms.findOneById(rid); - }); - } + room = retrieveDirectMessageInfo({ userId: user._id, channel }); break; default: - throw new Meteor.Error('invalid-channel-type'); + //Try to find the room by id or name if they didn't include the prefix. + room = retrieveRoomInfo({ userId: user._id, channel: channelType + channel, ignoreEmpty: true }); + if (room) { + break; + } + + //We didn't get a room, let's try finding direct messages + room = retrieveDirectMessageInfo({ userId: user._id, channel: channelType + channel }); + if (room) { + break; + } + + //No room, so throw an error + throw new Meteor.Error('invalid-channel'); } if (messageObj.attachments && !_.isArray(messageObj.attachments)) { diff --git a/packages/rocketchat-lib/server/functions/getFullUserData.js b/packages/rocketchat-lib/server/functions/getFullUserData.js index 75a319c171a..afeacd9c483 100644 --- a/packages/rocketchat-lib/server/functions/getFullUserData.js +++ b/packages/rocketchat-lib/server/functions/getFullUserData.js @@ -21,7 +21,7 @@ RocketChat.getFullUserData = function({userId, filter, limit}) { requirePasswordChangeReason: 1, roles: 1 }); - } else { + } else if (limit !== -1) { limit = 1; }