瀏覽代碼

WIP: notification when user is sent an invite

Shane Kilkelly 10 年之前
父節點
當前提交
9b46c1b1f7

+ 21 - 1
services/web/app/coffee/Features/Collaborators/CollaboratorsInviteController.coffee

@@ -6,6 +6,7 @@ CollaboratorsInviteHandler = require('./CollaboratorsInviteHandler')
 logger = require('logger-sharelatex')
 EmailHelper = require "../Helpers/EmailHelper"
 EditorRealTimeController = require("../Editor/EditorRealTimeController")
+NotificationsBuilder = require("../Notifications/NotificationsBuilder")
 
 
 module.exports = CollaboratorsInviteController =
@@ -19,10 +20,27 @@ module.exports = CollaboratorsInviteController =
 				return next(err)
 			res.json({invites: invites})
 
+	_trySendInviteNotification: (projectId, sendingUser, invite, callback=(err)->) ->
+		email = invite.email
+		UserGetter.getUser {email: email}, {_id: 1}, (err, existingUser) ->
+			if err?
+				logger.err {projectId, email}, "error checking if user exists"
+				return next(err)
+			if existingUser
+				ProjectGetter.getProject projectId, (err, project) ->
+					if err?
+						logger.err {projectId, email}, "error getting project"
+						return next(err)
+					if !project
+						logger.log {projectId}, "no project found while sending notification, returning"
+						return callback()
+					NotificationsBuilder.projectInvite(invite, project, sendingUser, existingUser).create(callback)
+
 	inviteToProject: (req, res, next) ->
 		projectId = req.params.Project_id
 		email = req.body.email
-		sendingUserId = req.session?.user?._id
+		sendingUser = req.session.user
+		sendingUserId = sendingUser._id
 		logger.log {projectId, email, sendingUserId}, "inviting to project"
 		LimitationsManager.canAddXCollaborators projectId, 1, (error, allowed) =>
 			return next(error) if error?
@@ -40,6 +58,8 @@ module.exports = CollaboratorsInviteController =
 					return next(err)
 				logger.log {projectId, email, sendingUserId}, "invite created"
 				EditorRealTimeController.emitToRoom projectId, 'project:membership:changed', {invites: true}
+				# async check if email is for an existing user, send a notification
+				CollaboratorsInviteController._trySendInviteNotification(projectId, sendingUser, invite, ()->)
 				return res.json {invite: invite}
 
 	revokeInvite: (req, res, next) ->

+ 1 - 0
services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee

@@ -7,6 +7,7 @@ PrivilegeLevels = require "../Authorization/PrivilegeLevels"
 Errors = require "../Errors/Errors"
 Crypto = require 'crypto'
 
+
 module.exports = CollaboratorsInviteHandler =
 
 	getAllInvites: (projectId, callback=(err, invites)->) ->

+ 15 - 2
services/web/app/coffee/Features/Notifications/NotificationsBuilder.coffee

@@ -1,12 +1,12 @@
 logger = require("logger-sharelatex")
 NotificationsHandler = require("./NotificationsHandler")
 
-module.exports = 
+module.exports =
 
 	groupPlan: (user, licence)->
 		key : "join-sub-#{licence.subscription_id}"
 		create: (callback = ->)->
-			messageOpts = 
+			messageOpts =
 				groupName: licence.name
 				subscription_id: licence.subscription_id
 			logger.log user_id:user._id, key:key, "creating notification key for user"
@@ -14,3 +14,16 @@ module.exports =
 
 		read: (callback = ->)->
 			NotificationsHandler.markAsReadWithKey user._id, @key, callback
+
+	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"
+			NotificationsHandler.createNotification user._id, @key, "notification_project_invite", messageOpts, callback
+		read:  (callback=()->) ->
+			NotificationsHandler.markAsReadWithKey user._id, @key, callback