The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/packages/rocketchat-integrations/server/methods/outgoing/updateOutgoingIntegration.c...

84 lines
3.2 KiB

Meteor.methods
updateOutgoingIntegration: (integrationId, integration) ->
if not RocketChat.authz.hasPermission @userId, 'manage-integrations'
throw new Meteor.Error 'not_authorized'
if integration.username.trim() is ''
throw new Meteor.Error 'invalid_username', '[methods] updateOutgoingIntegration -> username can\'t be empty'
if not Match.test integration.urls, [String]
throw new Meteor.Error 'invalid_urls', '[methods] updateOutgoingIntegration -> urls must be an array'
for url, index in integration.urls
delete integration.urls[index] if url.trim() is ''
integration.urls = _.without integration.urls, [undefined]
if integration.urls.length is 0
throw new Meteor.Error 'invalid_urls', '[methods] updateOutgoingIntegration -> urls is required'
if integration.channel?.trim() isnt '' and integration.channel[0] not in ['@', '#']
throw new Meteor.Error 'invalid_channel', '[methods] updateOutgoingIntegration -> channel should start with # or @'
if not integration.token? or integration.token?.trim() is ''
throw new Meteor.Error 'invalid_token', '[methods] updateOutgoingIntegration -> token is required'
if integration.triggerWords?
if not Match.test integration.triggerWords, [String]
throw new Meteor.Error 'invalid_triggerWords', '[methods] updateOutgoingIntegration -> triggerWords must be an array'
for triggerWord, index in integration.triggerWords
delete integration.triggerWords[index] if triggerWord.trim() is ''
integration.triggerWords = _.without integration.triggerWords, [undefined]
if integration.triggerWords.length is 0 and not integration.channel?
throw new Meteor.Error 'invalid_triggerWords', '[methods] updateOutgoingIntegration -> triggerWords is required if channel is empty'
if not RocketChat.models.Integrations.findOne(integrationId)?
throw new Meteor.Error 'invalid_integration', '[methods] updateOutgoingIntegration -> integration not found'
if integration.channel?.trim() isnt ''
record = undefined
channelType = integration.channel[0]
channel = integration.channel.substr(1)
switch channelType
when '#'
record = RocketChat.models.Rooms.findOne
$or: [
{_id: channel}
{name: channel}
]
when '@'
record = RocketChat.models.Users.findOne
$or: [
{_id: channel}
{username: channel}
]
if record is undefined
throw new Meteor.Error 'channel_does_not_exists', "[methods] updateOutgoingIntegration -> The channel does not exists"
user = RocketChat.models.Users.findOne({username: integration.username})
if not user?
throw new Meteor.Error 'user_does_not_exists', "[methods] updateOutgoingIntegration -> The username does not exists"
RocketChat.models.Integrations.update integrationId,
$set:
name: integration.name
avatar: integration.avatar
emoji: integration.emoji
alias: integration.alias
channel: integration.channel
username: integration.username
userId: user._id
urls: integration.urls
token: integration.token
triggerWords: integration.triggerWords
_updatedAt: new Date
_updatedBy: RocketChat.models.Users.findOne @userId, {fields: {username: 1}}
return RocketChat.models.Integrations.findOne(integrationId)