Commit 26c5ce07 authored by Gabriel Engel's avatar Gabriel Engel Committed by GitHub

Merge pull request #5235 from RocketChat/zapier-additions/fixes

Several changes to allow for easier Integrations from others
parents 5930a48e b8498ad7
...@@ -171,7 +171,7 @@ rocketchat:message-pin@0.0.1 ...@@ -171,7 +171,7 @@ rocketchat:message-pin@0.0.1
rocketchat:message-snippet@0.0.1 rocketchat:message-snippet@0.0.1
rocketchat:message-star@0.0.1 rocketchat:message-star@0.0.1
rocketchat:migrations@0.0.1 rocketchat:migrations@0.0.1
rocketchat:oauth2-server@1.4.0 rocketchat:oauth2-server@2.0.0
rocketchat:oauth2-server-config@1.0.0 rocketchat:oauth2-server-config@1.0.0
rocketchat:oembed@0.0.1 rocketchat:oembed@0.0.1
rocketchat:otr@0.0.1 rocketchat:otr@0.0.1
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
RocketChat.API.v1.addRoute('chat.delete', { authRequired: true }, { RocketChat.API.v1.addRoute('chat.delete', { authRequired: true }, {
post: function() { post: function() {
try { try {
check(this.bodyParams, { check(this.bodyParams, Match.ObjectIncluding({
msgId: String, msgId: String,
roomId: String, roomId: String,
asUser: Match.Maybe(Boolean) asUser: Match.Maybe(Boolean)
}); }));
const msg = RocketChat.models.Messages.findOneById(this.bodyParams.msgId, { fields: { u: 1, rid: 1 }}); 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 }, { ...@@ -35,14 +35,6 @@ RocketChat.API.v1.addRoute('chat.delete', { authRequired: true }, {
RocketChat.API.v1.addRoute('chat.postMessage', { authRequired: true }, { RocketChat.API.v1.addRoute('chat.postMessage', { authRequired: true }, {
post: function() { post: function() {
try { 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]; const messageReturn = processWebhookMessage(this.bodyParams, this.user)[0];
if (!messageReturn) { if (!messageReturn) {
......
...@@ -116,10 +116,16 @@ RocketChat.API.v1.addRoute('users.info', { authRequired: true }, { ...@@ -116,10 +116,16 @@ RocketChat.API.v1.addRoute('users.info', { authRequired: true }, {
RocketChat.API.v1.addRoute('users.list', { authRequired: true }, { RocketChat.API.v1.addRoute('users.list', { authRequired: true }, {
get: function() { get: function() {
let limit = -1;
if (typeof this.queryParams.limit !== 'undefined') {
limit = parseInt(this.queryParams.limit);
}
let result = undefined; let result = undefined;
try { try {
Meteor.runAsUser(this.userId, () => { Meteor.runAsUser(this.userId, () => {
result = Meteor.call('getFullUserData', {}); result = Meteor.call('getFullUserData', { filter: '', limit });
}); });
} catch (e) { } catch (e) {
return RocketChat.API.v1.failure(e.name + ': ' + e.message); return RocketChat.API.v1.failure(e.name + ': ' + e.message);
......
function retrieveRoomInfo({ currentUserId, 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(currentUserId, function() {
return Meteor.call('joinRoom', room._id);
});
}
return room;
}
function retrieveDirectMessageInfo({ currentUserId, channel, findByUserIdOnly=false }) {
let roomUser = undefined;
if (findByUserIdOnly) {
roomUser = RocketChat.models.Users.findOneById(channel);
} else {
roomUser = RocketChat.models.Users.findOne({
$or: [{ _id: channel }, { username: channel }]
});
}
if (!_.isObject(roomUser)) {
throw new Meteor.Error('invalid-channel');
}
const rid = [currentUserId, roomUser._id].sort().join('');
let room = RocketChat.models.Rooms.findOneById({ $in: [rid, channel] });
if (!room) {
Meteor.runAsUser(currentUserId, function() {
Meteor.call('createDirectMessage', roomUser.username);
room = RocketChat.models.Rooms.findOneById(rid);
});
}
return room;
}
this.processWebhookMessage = function(messageObj, user, defaultValues) { 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 = []; ret = [];
if (!defaultValues) { if (!defaultValues) {
...@@ -11,7 +54,7 @@ this.processWebhookMessage = function(messageObj, user, defaultValues) { ...@@ -11,7 +54,7 @@ this.processWebhookMessage = function(messageObj, user, defaultValues) {
}; };
} }
channel = messageObj.channel || defaultValues.channel; channel = messageObj.channel || messageObj.roomId || defaultValues.channel;
channels = [].concat(channel); channels = [].concat(channel);
...@@ -22,41 +65,26 @@ this.processWebhookMessage = function(messageObj, user, defaultValues) { ...@@ -22,41 +65,26 @@ this.processWebhookMessage = function(messageObj, user, defaultValues) {
switch (channelType) { switch (channelType) {
case '#': case '#':
room = RocketChat.models.Rooms.findOneByIdOrName(channel); room = retrieveRoomInfo({ currentUserId: user._id, 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);
});
}
break; break;
case '@': case '@':
roomUser = RocketChat.models.Users.findOne({ room = retrieveDirectMessageInfo({ currentUserId: user._id, channel });
$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);
});
}
break; break;
default: 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({ currentUserId: user._id, channel: channelType + channel, ignoreEmpty: true });
if (room) {
break;
}
//We didn't get a room, let's try finding direct messages
room = retrieveDirectMessageInfo({ currentUserId: user._id, channel: channelType + channel, findByUserIdOnly: true });
if (room) {
break;
}
//No room, so throw an error
throw new Meteor.Error('invalid-channel');
} }
if (messageObj.attachments && !_.isArray(messageObj.attachments)) { if (messageObj.attachments && !_.isArray(messageObj.attachments)) {
......
...@@ -21,7 +21,7 @@ RocketChat.getFullUserData = function({userId, filter, limit}) { ...@@ -21,7 +21,7 @@ RocketChat.getFullUserData = function({userId, filter, limit}) {
requirePasswordChangeReason: 1, requirePasswordChangeReason: 1,
roles: 1 roles: 1
}); });
} else { } else if (limit !== -1) {
limit = 1; limit = 1;
} }
......
RocketChat.Migrations.add({
version: 73,
up: function() {
RocketChat.models.Users.find({ 'oauth.athorizedClients': { $exists: true } }, { oauth: 1 }).forEach(function(user) {
RocketChat.models.Users.update({ _id: user._id }, {
$set: {
'oauth.authorizedClients': user.oauth.athorizedClients
},
$unset: {
'oauth.athorizedClients': 1
}
});
});
}
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment