From ea2bffc8be18a7422e8d9bcfddd3ca6cb8ca698c Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Fri, 8 Jan 2016 21:17:48 -0200 Subject: [PATCH] Add apis 'integrations.create' and 'integrations.remove' --- packages/rocketchat-api/server/api.coffee | 2 +- .../server/api/api.coffee | 125 +++++++++--------- 2 files changed, 63 insertions(+), 64 deletions(-) diff --git a/packages/rocketchat-api/server/api.coffee b/packages/rocketchat-api/server/api.coffee index 1f2ee3baeb6..e15db14f1fa 100644 --- a/packages/rocketchat-api/server/api.coffee +++ b/packages/rocketchat-api/server/api.coffee @@ -6,7 +6,7 @@ class API extends Restivus addAuthMethod: (method) -> @authMethods.push method - success: (result) -> + success: (result={}) -> if _.isObject(result) result.success = true diff --git a/packages/rocketchat-integrations/server/api/api.coffee b/packages/rocketchat-integrations/server/api/api.coffee index 706ebe40903..54e564e7264 100644 --- a/packages/rocketchat-integrations/server/api/api.coffee +++ b/packages/rocketchat-integrations/server/api/api.coffee @@ -3,6 +3,9 @@ Api = new Restivus apiPath: 'hooks/' auth: user: -> + if @bodyParams?.payload? + @bodyParams = JSON.parse @bodyParams.payload + user = RocketChat.models.Users.findOne _id: @request.params.userId 'services.resume.loginTokens.hashedToken': decodeURIComponent @request.params.token @@ -12,9 +15,6 @@ Api = new Restivus Api.addRoute ':integrationId/:userId/:token', authRequired: true, post: -> - if @bodyParams?.payload? - @bodyParams = JSON.parse @bodyParams.payload - console.log 'Post integration' console.log '@urlParams', @urlParams console.log '@bodyParams', @bodyParams @@ -107,83 +107,82 @@ Api.addRoute ':integrationId/:userId/:token', authRequired: true, success: true -Api.addRoute 'add/:integrationId/:userId/:token', authRequired: true, - post: -> - console.log 'Add integration' - console.log @bodyParams +createIntegration = (options, user) -> + console.log 'Add integration' + console.log options + + Meteor.runAsUser user._id, => + switch options['event'] + when 'newMessageOnChannel' + options.data ?= {} + + if options.data.channel_name? and options.data.channel_name.indexOf('#') is -1 + options.data.channel_name = '#' + options.data.channel_name + + Meteor.call 'addOutgoingIntegration', + username: 'rocket.cat' + urls: [options.target_url] + name: options.name + channel: options.data.channel_name + triggerWords: options.data.trigger_words + + when 'newMessageToUser' + if options.data.username.indexOf('@') is -1 + options.data.username = '@' + options.data.username + + Meteor.call 'addOutgoingIntegration', + username: 'rocket.cat' + urls: [options.target_url] + name: options.name + channel: options.data.username + triggerWords: options.data.trigger_words - if @bodyParams?.payload? - @bodyParams = JSON.parse @bodyParams.payload + return RocketChat.API.v1.success() + +removeIntegration = (options, user) -> + console.log 'Remove integration' + console.log options + + integrationToRemove = RocketChat.models.Integrations.findOne urls: options.target_url + Meteor.runAsUser user._id, => + Meteor.call 'deleteOutgoingIntegration', integrationToRemove._id + + return RocketChat.API.v1.success() + + +Api.addRoute 'add/:integrationId/:userId/:token', authRequired: true, + post: -> integration = RocketChat.models.Integrations.findOne(@urlParams.integrationId) - user = RocketChat.models.Users.findOne(@userId) if not integration? - return {} = - statusCode: 400 - body: - success: false - error: 'Invalid integraiton id' - - Meteor.runAsUser user._id, => - switch @bodyParams['event'] - when 'newMessageOnChannel' - @bodyParams.data ?= {} - - if @bodyParams.data.channel_name? and @bodyParams.data.channel_name.indexOf('#') is -1 - @bodyParams.data.channel_name = '#' + @bodyParams.data.channel_name - - Meteor.call 'addOutgoingIntegration', - username: 'rocket.cat' - urls: [@bodyParams.target_url] - name: @bodyParams.name - channel: @bodyParams.data.channel_name - triggerWords: @bodyParams.data.trigger_words - - when 'newMessageToUser' - if @bodyParams.data.username.indexOf('@') is -1 - @bodyParams.data.username = '@' + @bodyParams.data.username - - Meteor.call 'addOutgoingIntegration', - username: 'rocket.cat' - urls: [@bodyParams.target_url] - name: @bodyParams.name - channel: @bodyParams.data.username - triggerWords: @bodyParams.data.trigger_words + return RocketChat.API.v1.failure 'Invalid integraiton id' - return {} = - statusCode: 200 - body: - success: true + user = RocketChat.models.Users.findOne(@userId) + + return createIntegration @bodyParams, user Api.addRoute 'remove/:integrationId/:userId/:token', authRequired: true, post: -> - console.log 'Remove integration' - console.log @bodyParams + integration = RocketChat.models.Integrations.findOne(@urlParams.integrationId) - if @bodyParams?.payload? - @bodyParams = JSON.parse @bodyParams.payload + if not integration? + return RocketChat.API.v1.failure 'Invalid integraiton id' - integration = RocketChat.models.Integrations.findOne(@urlParams.integrationId) user = RocketChat.models.Users.findOne(@userId) - if not integration? - return {} = - statusCode: 400 - body: - success: false - error: 'Invalid integraiton id' + return removeIntegration @bodyParams, user - integrationToRemove = RocketChat.models.Integrations.findOne urls: @bodyParams.target_url - Meteor.runAsUser user._id, => - Meteor.call 'deleteOutgoingIntegration', integrationToRemove._id +RocketChat.API.v1.addRoute 'integrations.create', authRequired: true, + post: -> + return createIntegration @bodyParams, @user - return {} = - statusCode: 200 - body: - success: true + +RocketChat.API.v1.addRoute 'integrations.remove', authRequired: true, + post: -> + return removeIntegration @bodyParams, @user Api.addRoute 'sample/:integrationId/:userId/:token', authRequired: true,