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

Improvements/outgoing hooks #1744

Merged
merged 16 commits into from
Dec 23, 2015
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
4 changes: 2 additions & 2 deletions packages/rocketchat-authorization/server/startup.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ Meteor.startup ->
roles : ['admin']}

{ _id: 'manage-integrations',
roles : ['admin']}
roles : ['admin', 'bot']}
]

#alanning:roles
roles = _.pluck(Roles.getAllRoles().fetch(), 'name');

for permission in permissions
RocketChat.models.Permissions.upsert( permission._id, {$setOnInsert : permission })
RocketChat.models.Permissions.upsert( permission._id, {$set: permission })
for role in permission.roles
unless role in roles
Roles.createRole role
Expand Down
10 changes: 10 additions & 0 deletions packages/rocketchat-integrations/client/route.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ FlowRouter.route '/admin/integrations/incoming/:id?',
pageTitle: t('Integration_Incoming_WebHook')
pageTemplate: 'integrationsIncoming'
params: params


FlowRouter.route '/admin/integrations/outgoing/:id?',
name: 'admin-integrations-outgoing'
action: (params) ->
BlazeLayout.render 'main',
center: 'pageSettingsContainer'
pageTitle: t('Integration_Outgoing_WebHook')
pageTemplate: 'integrationsOutgoing'
params: params
50 changes: 35 additions & 15 deletions packages/rocketchat-integrations/client/views/integrations.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,46 @@
<div class="section">
<div class="admin-integrations-new-panel">
{{#each integrations}}
<a href="{{pathFor "admin-integrations-incoming" id=_id}}">
<div class="admin-integrations-new-item">
<i class="icon-login"></i>
<div class="admin-integrations-new-item-body">
<div class="admin-integrations-new-item-title">
Incoming WebHook {{#if name}}- {{name}}{{/if}}
</div>
<div class="admin-integrations-new-item-description">
{{{_ "Post_to_s_as_s" channel username}}}
</div>
<div class="admin-integrations-new-item-description">
{{{_ "Created_at_s_by_s" (dateFormated _createdAt) _createdBy.username}}}
{{#if $eq type 'webhook-incoming'}}
<a href="{{pathFor "admin-integrations-incoming" id=_id}}">
<div class="admin-integrations-new-item">
<i class="icon-login"></i>
<div class="admin-integrations-new-item-body">
<div class="admin-integrations-new-item-title">
Incoming WebHook {{#if name}}- {{name}}{{/if}}
</div>
<div class="admin-integrations-new-item-description">
{{{_ "Post_to_s_as_s" channel username}}}
</div>
<div class="admin-integrations-new-item-description">
{{{_ "Created_at_s_by_s" (dateFormated _createdAt) _createdBy.username}}}
</div>
</div>
<i class="icon-angle-right"></i>
</div>
<i class="icon-angle-right"></i>
</div>
</a>
</a>
{{/if}}
{{else}}
<h1>{{_ "There_is_no_integrations"}}</h1>
{{/each}}
{{#each integrations}}
{{#if $eq type 'webhook-outgoing'}}
<a href="{{pathFor "admin-integrations-outgoing" id=_id}}">
<div class="admin-integrations-new-item">
<i class="icon-login"></i>
<div class="admin-integrations-new-item-body">
<div class="admin-integrations-new-item-title">
Outgoing WebHook {{#if name}}- {{name}}{{/if}}
</div>
<div class="admin-integrations-new-item-description">
{{{_ "Created_at_s_by_s" (dateFormated _createdAt) _createdBy.username}}}
</div>
</div>
<i class="icon-angle-right"></i>
</div>
</a>
{{/if}}
{{/each}}
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Template.integrationsIncoming.events
closeOnConfirm: false
html: false
, ->
Meteor.call "deleteIntegration", params.id, (err, data) ->
Meteor.call "deleteIncomingIntegration", params.id, (err, data) ->
swal
title: t('Deleted')
text: t('Your_entry_has_been_deleted')
Expand Down Expand Up @@ -141,16 +141,15 @@ Template.integrationsIncoming.events

params = Template.instance().data.params?()
if params?.id?
Meteor.call "updateIntegration", params.id, integration, (err, data) ->
Meteor.call "updateIncomingIntegration", params.id, integration, (err, data) ->
if err?
return toastr.error TAPi18n.__(err.error)

toastr.success TAPi18n.__("Integration_updated")
else
integration.type = 'webhook-incoming'
integration.username = username

Meteor.call "addIntegration", integration, (err, data) ->
Meteor.call "addIncomingIntegration", integration, (err, data) ->
if err?
return toastr.error TAPi18n.__(err.error)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<div>
<input type="text" name="emoji" value="{{data.emoji}}" placeholder="{{_ 'Optional'}}" />
<div class="settings-description">{{_ "You_can_use_an_emoji_as_avatar"}}</div>
<div class="settings-description">{{_ "Example_s" ":ghost:"}}</div>
<div class="settings-description">{{{_ "Example_s" ":ghost:"}}}</div>
</div>
</div>
{{#if data.token}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<i class="icon-angle-right"></i>
</div>
</a>
<!-- <a href="{{pathFor "admin-integrations-incoming"}}">
<a href="{{pathFor "admin-integrations-outgoing"}}">
<div class="admin-integrations-new-item">
<i class="icon-logout"></i>
<div class="admin-integrations-new-item-body">
Expand All @@ -33,7 +33,7 @@
</div>
<i class="icon-angle-right"></i>
</div>
</a> -->
</a>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
Template.integrationsOutgoing.onCreated ->
@record = new ReactiveVar
username: 'rocket.cat'
token: Random.id(24)


Template.integrationsOutgoing.helpers

join: (arr, sep) ->
if not arr?.join?
return arr

return arr.join sep

hasPermission: ->
return RocketChat.authz.hasAllPermission 'manage-integrations'

data: ->
params = Template.instance().data.params?()

if params?.id?
data = ChatIntegrations.findOne({_id: params.id})
if data?
if not data.token?
data.token = Random.id(24)
return data

return Template.instance().record.curValue

example: ->
record = Template.instance().record.get()
return {} =
_id: Random.id()
alias: record.alias
emoji: record.emoji
avatar: record.avatar
msg: 'Response text'
bot:
i: Random.id()
groupable: false
attachments: [{
title: "Rocket.Chat"
title_link: "https://rocket.chat"
text: "Rocket.Chat, the best open source chat"
image_url: "https://rocket.chat/images/mockup.png"
color: "#764FA5"
}]
ts: new Date
u:
_id: Random.id()
username: record.username

exampleJson: ->
record = Template.instance().record.get()
data =
username: record.alias
icon_emoji: record.emoji
icon_url: record.avatar
text: 'Response text'
attachments: [{
title: "Rocket.Chat"
title_link: "https://rocket.chat"
text: "Rocket.Chat, the best open source chat"
image_url: "https://rocket.chat/images/mockup.png"
color: "#764FA5"
}]

for key, value of data
delete data[key] if value in [null, ""]

return hljs.highlight('json', JSON.stringify(data, null, 2)).value


Template.integrationsOutgoing.events
"blur input": (e, t) ->
t.record.set
name: $('[name=name]').val().trim()
alias: $('[name=alias]').val().trim()
emoji: $('[name=emoji]').val().trim()
avatar: $('[name=avatar]').val().trim()
channel: $('[name=channel]').val().trim()
username: $('[name=username]').val().trim()
triggerWords: $('[name=triggerWords]').val().trim()
urls: $('[name=urls]').val().trim()
token: $('[name=token]').val().trim()


"click .submit > .delete": ->
params = Template.instance().data.params()

swal
title: t('Are_you_sure')
text: t('You_will_not_be_able_to_recover')
type: 'warning'
showCancelButton: true
confirmButtonColor: '#DD6B55'
confirmButtonText: t('Yes_delete_it')
cancelButtonText: t('Cancel')
closeOnConfirm: false
html: false
, ->
Meteor.call "deleteOutgoingIntegration", params.id, (err, data) ->
swal
title: t('Deleted')
text: t('Your_entry_has_been_deleted')
type: 'success'
timer: 1000
showConfirmButton: false

FlowRouter.go "admin-integrations"

"click .submit > .save": ->
name = $('[name=name]').val().trim()
alias = $('[name=alias]').val().trim()
emoji = $('[name=emoji]').val().trim()
avatar = $('[name=avatar]').val().trim()
channel = $('[name=channel]').val().trim()
username = $('[name=username]').val().trim()
triggerWords = $('[name=triggerWords]').val().trim()
urls = $('[name=urls]').val().trim()
token = $('[name=token]').val().trim()

if username is ''
return toastr.error TAPi18n.__("The_username_is_required")

triggerWords = triggerWords.split(',')
for triggerWord, index in triggerWords
triggerWords[index] = triggerWord.trim()
delete triggerWords[index] if triggerWord.trim() is ''

triggerWords = _.without triggerWords, [undefined]

if triggerWords.length is 0 and channel.trim() is ''
return toastr.error TAPi18n.__("You should inform at least one trigger word if you do not inform a channel")

urls = urls.split('\n')
for url, index in urls
urls[index] = url.trim()
delete urls[index] if url.trim() is ''

urls = _.without urls, [undefined]

if urls.length is 0
return toastr.error TAPi18n.__("You_should_inform_one_url_at_least")

integration =
channel: channel
username: username
alias: alias if alias isnt ''
emoji: emoji if emoji isnt ''
avatar: avatar if avatar isnt ''
name: name if name isnt ''
triggerWords: triggerWords if triggerWords isnt ''
urls: urls if urls isnt ''
token: token if token isnt ''

params = Template.instance().data.params?()
if params?.id?
Meteor.call "updateOutgoingIntegration", params.id, integration, (err, data) ->
if err?
return toastr.error TAPi18n.__(err.error)

toastr.success TAPi18n.__("Integration_updated")
else
Meteor.call "addOutgoingIntegration", integration, (err, data) ->
if err?
return toastr.error TAPi18n.__(err.error)

toastr.success TAPi18n.__("Integration_added")
FlowRouter.go "admin-integrations-outgoing", {id: data._id}
Loading