parent
1f74b78351
commit
e3dd15a769
@ -1,3 +1,6 @@ |
||||
Template.integrations.helpers |
||||
hasPermission: -> |
||||
return RocketChat.authz.hasAllPermission 'manage-integrations' |
||||
|
||||
integrations: -> |
||||
return ChatIntegrations.find() |
||||
|
||||
@ -0,0 +1,78 @@ |
||||
Api = new Restivus |
||||
enableCors: false |
||||
apiPath: 'hooks/' |
||||
auth: |
||||
user: -> |
||||
user = RocketChat.models.Users.findOne |
||||
_id: @request.params.userId |
||||
'services.resume.loginTokens.hashedToken': @request.params.token |
||||
|
||||
return user: user |
||||
|
||||
|
||||
Api.addRoute ':integrationId/:userId/:token', authRequired: true, |
||||
post: -> |
||||
integration = RocketChat.models.Integrations.findOne(@urlParams.integrationId) |
||||
user = RocketChat.models.Users.findOne(@userId) |
||||
|
||||
channel = @bodyParams.channel or integration.channel |
||||
channelType = channel[0] |
||||
channel = channel.substr(1) |
||||
|
||||
switch channelType |
||||
when '#' |
||||
room = RocketChat.models.Rooms.findOne |
||||
$or: [ |
||||
{_id: channel} |
||||
{name: channel} |
||||
] |
||||
|
||||
if not room? |
||||
return {} = |
||||
statusCode: 400 |
||||
body: |
||||
success: false |
||||
error: 'invalid-channel' |
||||
|
||||
rid = room._id |
||||
Meteor.runAsUser user._id, -> |
||||
Meteor.call 'joinRoom', room._id |
||||
|
||||
when '@' |
||||
roomUser = RocketChat.models.Users.findOne |
||||
$or: [ |
||||
{_id: channel} |
||||
{username: channel} |
||||
] |
||||
|
||||
if not roomUser? |
||||
return {} = |
||||
statusCode: 400 |
||||
body: |
||||
success: false |
||||
error: 'invalid-channel' |
||||
|
||||
rid = [user._id, roomUser._id].sort().join('') |
||||
room = RocketChat.models.Rooms.findOne(rid) |
||||
|
||||
if not room |
||||
Meteor.runAsUser user._id, -> |
||||
Meteor.call 'createDirectMessage', roomUser._id |
||||
room = RocketChat.models.Rooms.findOne(rid) |
||||
|
||||
else |
||||
return {} = |
||||
statusCode: 400 |
||||
body: |
||||
success: false |
||||
error: 'invalid-channel-type' |
||||
|
||||
message = |
||||
msg: @bodyParams.text |
||||
|
||||
RocketChat.sendMessage user, message, room, {} |
||||
|
||||
return {} = |
||||
statusCode: 200 |
||||
body: |
||||
success: true |
||||
@ -0,0 +1,57 @@ |
||||
Meteor.methods |
||||
addIntegration: (integration) -> |
||||
if not _.isString(integration.channel) |
||||
throw new Meteor.Error 'invalid_channel', '[methods] addIntegration -> channel must be string' |
||||
|
||||
if integration.channel.trim() is '' |
||||
throw new Meteor.Error 'invalid_channel', '[methods] addIntegration -> channel can\'t be empty' |
||||
|
||||
if integration.channel[0] not in ['@', '#'] |
||||
throw new Meteor.Error 'invalid_channel', '[methods] addIntegration -> channel should start with # or @' |
||||
|
||||
if not _.isString(integration.username) |
||||
throw new Meteor.Error 'invalid_username', '[methods] addIntegration -> username must be string' |
||||
|
||||
if integration.username.trim() is '' |
||||
throw new Meteor.Error 'invalid_username', '[methods] addIntegration -> username can\'t be empty' |
||||
|
||||
record = undefined |
||||
switch integration.channel[0] |
||||
when '#' |
||||
record = RocketChat.models.Rooms.findOne |
||||
$or: [ |
||||
{_id: integration.channel} |
||||
{name: integration.channel} |
||||
] |
||||
when '@' |
||||
record = RocketChat.models.Users.findOne |
||||
$or: [ |
||||
{_id: integration.channel} |
||||
{username: integration.channel} |
||||
] |
||||
|
||||
if record is undefined |
||||
throw new Meteor.Error 'channel_does_not_exists', "[methods] addIntegration -> 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] addIntegration -> The username does not exists" |
||||
|
||||
stampedToken = Accounts._generateStampedLoginToken() |
||||
hashStampedToken = Accounts._hashStampedToken(stampedToken) |
||||
|
||||
updateObj = |
||||
$push: |
||||
'services.resume.loginTokens': |
||||
hashedToken: hashStampedToken.hashedToken |
||||
integration: true |
||||
|
||||
integration.token = hashStampedToken.hashedToken |
||||
integration.userId = user._id |
||||
|
||||
RocketChat.models.Users.update {_id: user._id}, updateObj |
||||
|
||||
RocketChat.models.Integrations.insert integration |
||||
|
||||
return integration |
||||
@ -0,0 +1,8 @@ |
||||
Meteor.methods |
||||
deleteIntegration: (integrationId) -> |
||||
if not RocketChat.models.Integrations.findOne(integrationId)? |
||||
throw new Meteor.Error 'invalid_integration', '[methods] addIntegration -> integration not found' |
||||
|
||||
RocketChat.models.Integrations.remove _id: integrationId |
||||
|
||||
return true |
||||
@ -0,0 +1,38 @@ |
||||
Meteor.methods |
||||
updateIntegration: (integrationId, integration) -> |
||||
if not _.isString(integration.channel) |
||||
throw new Meteor.Error 'invalid_channel', '[methods] addIntegration -> channel must be string' |
||||
|
||||
if integration.channel.trim() is '' |
||||
throw new Meteor.Error 'invalid_channel', '[methods] addIntegration -> channel can\'t be empty' |
||||
|
||||
if integration.channel[0] not in ['@', '#'] |
||||
throw new Meteor.Error 'invalid_channel', '[methods] addIntegration -> channel should start with # or @' |
||||
|
||||
if not RocketChat.models.Integrations.findOne(integrationId)? |
||||
throw new Meteor.Error 'invalid_integration', '[methods] addIntegration -> integration not found' |
||||
|
||||
record = undefined |
||||
switch integration.channel[0] |
||||
when '#' |
||||
record = RocketChat.models.Rooms.findOne |
||||
$or: [ |
||||
{_id: integration.channel} |
||||
{name: integration.channel} |
||||
] |
||||
when '@' |
||||
record = RocketChat.models.Users.findOne |
||||
$or: [ |
||||
{_id: integration.channel} |
||||
{username: integration.channel} |
||||
] |
||||
|
||||
if record is undefined |
||||
throw new Meteor.Error 'channel_does_not_exists', "[methods] addIntegration -> The channel does not exists" |
||||
|
||||
RocketChat.models.Integrations.update integrationId, |
||||
$set: |
||||
name: integration.name |
||||
channel: integration.channel |
||||
|
||||
return RocketChat.models.Integrations.findOne(integrationId) |
||||
@ -0,0 +1,13 @@ |
||||
RocketChat.models.Integrations = new class extends RocketChat.models._Base |
||||
constructor: -> |
||||
@_initModel 'integrations' |
||||
|
||||
|
||||
# FIND |
||||
# findByRole: (role, options) -> |
||||
# query = |
||||
# roles: role |
||||
|
||||
# return @find query, options |
||||
|
||||
# CREATE |
||||
@ -0,0 +1,8 @@ |
||||
Meteor.publish 'integrations', -> |
||||
unless @userId |
||||
return @ready() |
||||
|
||||
if not RocketChat.authz.hasPermission @userId, 'manage-integrations' |
||||
throw new Meteor.Error "not-authorized" |
||||
|
||||
return RocketChat.models.Integrations.find() |
||||
Loading…
Reference in new issue