2016-06-15 12:38:02 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2016-02-05 09:13:38 -05:00
|
|
|
NotificationsHandler = require("./NotificationsHandler")
|
2018-09-02 08:47:16 -04:00
|
|
|
request = require "request"
|
|
|
|
settings = require "settings-sharelatex"
|
2016-02-05 09:13:38 -05:00
|
|
|
|
2016-08-05 11:11:03 -04:00
|
|
|
module.exports =
|
2016-02-05 09:13:38 -05:00
|
|
|
|
2016-08-11 09:04:11 -04:00
|
|
|
# Note: notification keys should be url-safe
|
|
|
|
|
2016-02-05 09:13:38 -05:00
|
|
|
groupPlan: (user, licence)->
|
|
|
|
key : "join-sub-#{licence.subscription_id}"
|
|
|
|
create: (callback = ->)->
|
2016-08-05 11:11:03 -04:00
|
|
|
messageOpts =
|
2016-02-05 09:13:38 -05:00
|
|
|
groupName: licence.name
|
|
|
|
subscription_id: licence.subscription_id
|
2016-08-31 04:40:40 -04:00
|
|
|
logger.log user_id:user._id, key:@key, "creating notification key for user"
|
2016-08-30 08:07:37 -04:00
|
|
|
NotificationsHandler.createNotification user._id, @key, "notification_group_invite", messageOpts, null, callback
|
2016-02-05 09:13:38 -05:00
|
|
|
|
|
|
|
read: (callback = ->)->
|
2016-02-17 11:24:09 -05:00
|
|
|
NotificationsHandler.markAsReadWithKey user._id, @key, callback
|
2016-08-05 11:11:03 -04:00
|
|
|
|
|
|
|
projectInvite: (invite, project, sendingUser, user) ->
|
|
|
|
key: "project-invite-#{invite._id}"
|
|
|
|
create: (callback=()->) ->
|
|
|
|
messageOpts =
|
|
|
|
userName: sendingUser.first_name
|
|
|
|
projectName: project.name
|
|
|
|
projectId: project._id.toString()
|
|
|
|
token: invite.token
|
|
|
|
logger.log {user_id: user._id, project_id: project._id, invite_id: invite._id, key: @key}, "creating project invite notification for user"
|
2016-08-30 08:07:37 -04:00
|
|
|
NotificationsHandler.createNotification user._id, @key, "notification_project_invite", messageOpts, invite.expires, callback
|
2016-08-05 11:11:03 -04:00
|
|
|
read: (callback=()->) ->
|
2016-08-11 09:04:11 -04:00
|
|
|
NotificationsHandler.markAsReadByKeyOnly @key, callback
|
2018-09-02 08:47:16 -04:00
|
|
|
|
|
|
|
ipMatcherAffiliation: (userId, ip) ->
|
2018-09-02 10:22:45 -04:00
|
|
|
key: "ip-matched-affiliation-#{ip}"
|
|
|
|
create: (callback=()->) ->
|
|
|
|
return null unless settings?.apis?.v1?.url # service is not configured
|
|
|
|
_key = @key
|
|
|
|
request {
|
|
|
|
method: 'GET'
|
|
|
|
url: "#{settings.apis.v1.url}/api/v2/users/ip_matcher/#{userId}"
|
|
|
|
auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass }
|
|
|
|
body: { ip: ip }
|
|
|
|
json: true
|
|
|
|
timeout: 20 * 1000
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return error if error?
|
|
|
|
return null if response.statusCode == 204
|
|
|
|
|
|
|
|
messageOpts =
|
|
|
|
university_id: body.university_id
|
|
|
|
university_name: body.university_name
|
|
|
|
content: body.ad_copy
|
|
|
|
logger.log user_id:userId, key:_key, "creating notification key for user"
|
|
|
|
NotificationsHandler.createNotification userId, _key, "notification_ip_matched_affiliation", messageOpts, null, callback
|
|
|
|
|
|
|
|
read: (callback = ->)->
|
|
|
|
NotificationsHandler.markAsReadWithKey userId, @key, callback
|