Skip to content

Commit

Permalink
Merge pull request #6593 from ggazzo/rocketchat-lib2
Browse files Browse the repository at this point in the history
Rocketchat lib2
  • Loading branch information
rodrigok authored Apr 19, 2017
2 parents 161df86 + e0192b7 commit 752aef3
Show file tree
Hide file tree
Showing 21 changed files with 528 additions and 391 deletions.
26 changes: 0 additions & 26 deletions packages/rocketchat-lib/lib/Message.coffee

This file was deleted.

29 changes: 29 additions & 0 deletions packages/rocketchat-lib/lib/Message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
RocketChat.Message = {
parse(msg, language) {
const messageType = RocketChat.MessageTypes.getType(msg);
if (messageType) {
if (messageType.render) {
return messageType.render(msg);
} else if (messageType.template) {
// Render message
return;
} else if (messageType.message) {
if (!language && typeof localStorage !== 'undefined') {
language = localStorage.getItem('userLanguage');
}
const data = (typeof messageType.data === 'function' && messageType.data(msg)) || {};
return TAPi18n.__(messageType.message, data, language);
}
}
if (msg.u && msg.u.username === RocketChat.settings.get('Chatops_Username')) {
msg.html = msg.msg;
return msg.html;
}
msg.html = msg.msg;
if (_.trim(msg.html) !== '') {
msg.html = _.escapeHTML(msg.html);
}
msg.html = msg.html.replace(/\n/gm, '<br/>');
return msg.html;
}
};
112 changes: 0 additions & 112 deletions packages/rocketchat-lib/lib/MessageTypes.coffee

This file was deleted.

167 changes: 167 additions & 0 deletions packages/rocketchat-lib/lib/MessageTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
RocketChat.MessageTypes = new class {
constructor() {
this.types = {};
}

registerType(options) {
return this.types[options.id] = options;
}

getType(message) {
return this.types[message && message.t];
}

isSystemMessage(message) {
const type = this.types[message && message.t];
return type && type.system;
}

};

Meteor.startup(function() {
RocketChat.MessageTypes.registerType({
id: 'r',
system: true,
message: 'Room_name_changed',
data(message) {
return {
room_name: message.msg,
user_by: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'au',
system: true,
message: 'User_added_by',
data(message) {
return {
user_added: message.msg,
user_by: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'ru',
system: true,
message: 'User_removed_by',
data(message) {
return {
user_removed: message.msg,
user_by: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'ul',
system: true,
message: 'User_left',
data(message) {
return {
user_left: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'uj',
system: true,
message: 'User_joined_channel',
data(message) {
return {
user: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'wm',
system: true,
message: 'Welcome',
data(message) {
return {
user: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'rm',
system: true,
message: 'Message_removed',
data(message) {
return {
user: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'rtc',
render(message) {
return RocketChat.callbacks.run('renderRtcMessage', message);
}
});
RocketChat.MessageTypes.registerType({
id: 'user-muted',
system: true,
message: 'User_muted_by',
data(message) {
return {
user_muted: message.msg,
user_by: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'user-unmuted',
system: true,
message: 'User_unmuted_by',
data(message) {
return {
user_unmuted: message.msg,
user_by: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'subscription-role-added',
system: true,
message: '__username__was_set__role__by__user_by_',
data(message) {
return {
username: message.msg,
role: message.role,
user_by: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'subscription-role-removed',
system: true,
message: '__username__is_no_longer__role__defined_by__user_by_',
data(message) {
return {
username: message.msg,
role: message.role,
user_by: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'room-archived',
system: true,
message: 'This_room_has_been_archived_by__username_',
data(message) {
return {
username: message.u.username
};
}
});
RocketChat.MessageTypes.registerType({
id: 'room-unarchived',
system: true,
message: 'This_room_has_been_unarchived_by__username_',
data(message) {
return {
username: message.u.username
};
}
});
});
26 changes: 0 additions & 26 deletions packages/rocketchat-lib/lib/slashCommand.coffee

This file was deleted.

31 changes: 31 additions & 0 deletions packages/rocketchat-lib/lib/slashCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
RocketChat.slashCommands = {
commands: {}
};

RocketChat.slashCommands.add = function(command, callback, options = {}, result) {
RocketChat.slashCommands.commands[command] = {
command,
callback,
params: options.params,
description: options.description,
clientOnly: options.clientOnly || false,
result
};
};

RocketChat.slashCommands.run = function(command, params, item) {
if (RocketChat.slashCommands.commands[command] && RocketChat.slashCommands.commands[command].callback) {
return RocketChat.slashCommands.commands[command].callback(command, params, item);
}
};

Meteor.methods({
slashCommand(command) {
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'slashCommand'
});
}
return RocketChat.slashCommands.run(command.cmd, command.params, command.msg);
}
});
Loading

0 comments on commit 752aef3

Please sign in to comment.