From f2516ba877af1e52a30847b3d20c8afa7e15bdd9 Mon Sep 17 00:00:00 2001 From: Zhou Yuntao Date: Fri, 17 Feb 2017 23:51:58 +0800 Subject: [PATCH 1/2] implement /gif command (using rightgif) --- .meteor/packages | 1 + .meteor/versions | 1 + packages/rocketchat-i18n/i18n/en.i18n.json | 3 ++ packages/rocketchat-i18n/i18n/zh.i18n.json | 5 ++- .../rocketchat-slashcommand-gif/gif-client.js | 10 +++++ .../rocketchat-slashcommand-gif/gif-server.js | 42 +++++++++++++++++++ .../rocketchat-slashcommand-gif/package.js | 17 ++++++++ 7 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 packages/rocketchat-slashcommand-gif/gif-client.js create mode 100644 packages/rocketchat-slashcommand-gif/gif-server.js create mode 100644 packages/rocketchat-slashcommand-gif/package.js diff --git a/.meteor/packages b/.meteor/packages index 220130fc15e0..ec3ef5c8dd12 100644 --- a/.meteor/packages +++ b/.meteor/packages @@ -97,6 +97,7 @@ rocketchat:slackbridge rocketchat:slashcommands-archive rocketchat:slashcommands-asciiarts rocketchat:slashcommands-create +rocketchat:slashcommands-gif rocketchat:slashcommands-invite rocketchat:slashcommands-invite-all rocketchat:slashcommands-join diff --git a/.meteor/versions b/.meteor/versions index f6fc57d3a566..08b47e19829e 100644 --- a/.meteor/versions +++ b/.meteor/versions @@ -183,6 +183,7 @@ rocketchat:slackbridge@0.0.1 rocketchat:slashcommands-archive@0.0.1 rocketchat:slashcommands-asciiarts@0.0.1 rocketchat:slashcommands-create@0.0.1 +rocketchat:slashcommands-gif@0.0.1 rocketchat:slashcommands-invite@0.0.1 rocketchat:slashcommands-invite-all@0.0.1 rocketchat:slashcommands-join@0.0.1 diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 859ee90dfe43..3df82ec42c87 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -573,6 +573,7 @@ "From_Email": "From Email", "From_email_warning": "Warning: The field From is subject to your mail server settings.", "General": "General", + "gif_keyword": "Keyword for the gif", "github_no_public_email": "You don't have any email as public email in your GitHub account", "Give_a_unique_name_for_the_custom_oauth": "Give a unique name for the custom oauth", "Give_the_application_a_name_This_will_be_seen_by_your_users": "Give the application a name. This will be seen by your users.", @@ -1256,6 +1257,8 @@ "SlackBridge_Out_Enabled": "SlackBridge Out Enabled", "SlackBridge_Out_Enabled_Description": "Choose whether SlackBridge should also send your messages back to Slack", "SlackBridge_start": "@%s has started a SlackBridge import at `#%s`. We'll let you know when it's finished.", + "Slash_Gif_Description": "Display a gif provided by rightgif according to your keyword", + "Slash_Gif_Failed": "Failed to get gif from rightgif due to network problem or incorrect keyword", "Slash_Gimme_Description": "Displays ༼ つ ◕_◕ ༽つ before your message", "Slash_LennyFace_Description": "Displays ( ͡° ͜ʖ ͡°) after your message", "Slash_Shrug_Description": "Displays ¯\\_(ツ)_/¯ after your message", diff --git a/packages/rocketchat-i18n/i18n/zh.i18n.json b/packages/rocketchat-i18n/i18n/zh.i18n.json index 145471928d18..96d9932c01d7 100644 --- a/packages/rocketchat-i18n/i18n/zh.i18n.json +++ b/packages/rocketchat-i18n/i18n/zh.i18n.json @@ -452,6 +452,7 @@ "From_Email": "从电子邮件", "From_email_warning": "警告From 字段来自于邮件服务器的设置。", "General": "通用", + "gif_keyword": "动图关键词", "github_no_public_email": "在您的GitHub上帐户中,您没有设置任何电子邮件作为公共电子邮件地址。", "Give_a_unique_name_for_the_custom_oauth": "请给自定义 OAuth 设置一个唯一的名称", "Give_the_application_a_name_This_will_be_seen_by_your_users": "给该应用设置一个名称。该名称将被您的用户看到。", @@ -999,6 +1000,8 @@ "Site_Url": "网站地址", "Site_Url_Description": "例如:https://chat.domain.com/", "Skip": "跳过", + "Slash_Gif_Description": "根据你给的关键字显示一张动图(使用rightgif服务)", + "Slash_Gif_Failed": "获取动图失败!可能是网络问题,或者是你的关键词找不到 :(", "Slash_Gimme_Description": "在您的消息前显示 ༼ つ ◕_◕ ༽つ", "Slash_LennyFace_Description": "在您的消息后显示 ( ͡° ͜ʖ ͡°)", "Slash_Shrug_Description": "显示¯\\ _(ツ)_ /¯在您的消息后", @@ -1257,4 +1260,4 @@ "Your_mail_was_sent_to_s": "您的邮件已发送至 %s", "Your_password_is_wrong": "密码错误!", "Your_push_was_sent_to_s_devices": "您的推送已被送到 %s 台设备" -} \ No newline at end of file +} diff --git a/packages/rocketchat-slashcommand-gif/gif-client.js b/packages/rocketchat-slashcommand-gif/gif-client.js new file mode 100644 index 000000000000..57f458ed270a --- /dev/null +++ b/packages/rocketchat-slashcommand-gif/gif-client.js @@ -0,0 +1,10 @@ +/* +* Gif is a named function that will replace /gimme commands +* @param {Object} message - The message object +*/ + + +RocketChat.slashCommands.add('gif', undefined, { + description: 'Slash_Gif_Description', + params: 'gif_keyword' +}); diff --git a/packages/rocketchat-slashcommand-gif/gif-server.js b/packages/rocketchat-slashcommand-gif/gif-server.js new file mode 100644 index 000000000000..a7f7a9e54f08 --- /dev/null +++ b/packages/rocketchat-slashcommand-gif/gif-server.js @@ -0,0 +1,42 @@ +/* +* Gif is a named function that will replace /gimme commands +* @param {Object} message - The message object +*/ + + +function Gif(command, params, item) { + if (command === 'gif') { + let msg; + let url = 'https://rightgif.com/search/web'; + + msg = item; + msg.msg = ''; + HTTP.post(url, { + headers: { + "content-type": "application/json" + }, + data: { + text: params + } + }, function(err, response) { + if (err || response.statusCode != 200 || response.data.url === 'https://rightgif.com/public/images/error.gif') { + let currentUser = Meteor.users.findOne(Meteor.userId()); + RocketChat.Notifications.notifyUser(Meteor.userId(), 'message', { + _id: Random.id(), + rid: msg.rid, + ts: new Date(), + msg: TAPi18n.__('Slash_Gif_Failed', null, currentUser.language) + }); + } else { + msg.attachments = [ {title: params, image_url: response.data.url} ]; + msg.groupable = false; + Meteor.call('sendMessage', msg); + } + }); + } +} + +RocketChat.slashCommands.add('gif', Gif, { + description: 'Slash_Gif_Description', + params: 'gif_keyword' +}); diff --git a/packages/rocketchat-slashcommand-gif/package.js b/packages/rocketchat-slashcommand-gif/package.js new file mode 100644 index 000000000000..bcee94db6a21 --- /dev/null +++ b/packages/rocketchat-slashcommand-gif/package.js @@ -0,0 +1,17 @@ +Package.describe({ + name: 'rocketchat:slashcommands-gif', + version: '0.0.1', + summary: 'Show gif from rightgif', + git: '' +}); + +Package.onUse(function(api) { + api.use([ + 'rocketchat:lib' + ]); + + api.use('ecmascript'); + + api.addFiles('gif-server.js', ['server']); + api.addFiles('gif-client.js', ['client']); +}); From ebddc5c9e5582599284d9eebe5ca49fa65747953 Mon Sep 17 00:00:00 2001 From: Yuntao Zhou Date: Sat, 18 Feb 2017 12:44:47 +0800 Subject: [PATCH 2/2] optimize code style --- .../rocketchat-slashcommand-gif/gif-server.js | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/rocketchat-slashcommand-gif/gif-server.js b/packages/rocketchat-slashcommand-gif/gif-server.js index a7f7a9e54f08..75ce416bd2fd 100644 --- a/packages/rocketchat-slashcommand-gif/gif-server.js +++ b/packages/rocketchat-slashcommand-gif/gif-server.js @@ -6,31 +6,29 @@ function Gif(command, params, item) { if (command === 'gif') { - let msg; - let url = 'https://rightgif.com/search/web'; + const url = 'https://rightgif.com/search/web'; - msg = item; - msg.msg = ''; + item.msg = ''; HTTP.post(url, { headers: { - "content-type": "application/json" + 'content-type': 'application/json' }, data: { text: params } }, function(err, response) { - if (err || response.statusCode != 200 || response.data.url === 'https://rightgif.com/public/images/error.gif') { - let currentUser = Meteor.users.findOne(Meteor.userId()); + if (err || response.statusCode !== 200 || response.data.url === 'https://rightgif.com/public/images/error.gif') { + const currentUser = Meteor.users.findOne(Meteor.userId()); RocketChat.Notifications.notifyUser(Meteor.userId(), 'message', { _id: Random.id(), - rid: msg.rid, + rid: item.rid, ts: new Date(), msg: TAPi18n.__('Slash_Gif_Failed', null, currentUser.language) }); } else { - msg.attachments = [ {title: params, image_url: response.data.url} ]; - msg.groupable = false; - Meteor.call('sendMessage', msg); + item.attachments = [ {title: params, image_url: response.data.url} ]; + item.groupable = false; + Meteor.call('sendMessage', item); } }); }