Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVE] Accept Slash Commands via Action Buttons when msg_in_chat_window: true #13009

Merged
merged 1 commit into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 45 additions & 37 deletions packages/rocketchat-ui/client/lib/chatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,43 +259,8 @@ ChatMessages = class ChatMessages {
this.hasValue.set(false);
this.stopTyping(rid);

// Check if message starts with /command
if (msg[0] === '/') {
const match = msg.match(/^\/([^\s]+)(?:\s+(.*))?$/m);
if (match) {
let command;
if (RocketChat.slashCommands.commands[match[1]]) {
const commandOptions = RocketChat.slashCommands.commands[match[1]];
command = match[1];
const param = match[2] || '';

if (!commandOptions.permission || RocketChat.authz.hasAtLeastOnePermission(commandOptions.permission, Session.get('openedRoom'))) {
if (commandOptions.clientOnly) {
commandOptions.callback(command, param, msgObject);
} else {
Meteor.call('slashCommand', { cmd: command, params: param, msg: msgObject }, (err, result) => typeof commandOptions.result === 'function' && commandOptions.result(err, result, { cmd: command, params: param, msg: msgObject }));
}

return;
}
}

if (!RocketChat.settings.get('Message_AllowUnrecognizedSlashCommand')) {
const invalidCommandMsg = {
_id: Random.id(),
rid,
ts: new Date,
msg: TAPi18n.__('No_such_command', { command: match[1] }),
u: {
username: RocketChat.settings.get('InternalHubot_Username'),
},
private: true,
};

ChatMessage.upsert({ _id: invalidCommandMsg._id }, invalidCommandMsg);
return;
}
}
if (this.processSlashCommand(msgObject)) {
return;
}

Meteor.call('sendMessage', msgObject);
Expand All @@ -319,6 +284,49 @@ ChatMessages = class ChatMessages {
}
}

processSlashCommand(msgObject) {
// Check if message starts with /command
if (msgObject.msg[0] === '/') {
const match = msgObject.msg.match(/^\/([^\s]+)(?:\s+(.*))?$/m);
if (match) {
let command;
if (RocketChat.slashCommands.commands[match[1]]) {
const commandOptions = RocketChat.slashCommands.commands[match[1]];
command = match[1];
const param = match[2] || '';

if (!commandOptions.permission || RocketChat.authz.hasAtLeastOnePermission(commandOptions.permission, Session.get('openedRoom'))) {
if (commandOptions.clientOnly) {
commandOptions.callback(command, param, msgObject);
} else {
Meteor.call('slashCommand', { cmd: command, params: param, msg: msgObject }, (err, result) => typeof commandOptions.result === 'function' && commandOptions.result(err, result, { cmd: command, params: param, msg: msgObject }));
}

return true;
}
}

if (!RocketChat.settings.get('Message_AllowUnrecognizedSlashCommand')) {
const invalidCommandMsg = {
_id: Random.id(),
rid: msgObject.rid,
ts: new Date,
msg: TAPi18n.__('No_such_command', { command: match[1] }),
u: {
username: RocketChat.settings.get('InternalHubot_Username'),
},
private: true,
};

ChatMessage.upsert({ _id: invalidCommandMsg._id }, invalidCommandMsg);
return true;
}
}
}

return false;
}

confirmDeleteMsg(message, done = function() {}) {
if (RocketChat.MessageTypes.isSystemMessage(message)) { return; }
modal.open({
Expand Down
5 changes: 5 additions & 0 deletions packages/rocketchat-ui/client/views/app/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,11 @@ Template.room.events({
return;
}
RocketChat.promises.run('onClientBeforeSendMessage', msgObject).then((msgObject) => {
const chatMessages = window.chatMessages[rid];
if (chatMessages && chatMessages.processSlashCommand(msgObject)) {
return;
}

Meteor.call('sendMessage', msgObject);
});
},
Expand Down