From d9c6df0e4765d181c863d03cab88e58b07ce5c6a Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Thu, 21 Jul 2016 15:56:41 +0100 Subject: [PATCH] start adding the ProjectInvite workflow. --- .../CollaboratorsEmailHandler.coffee | 18 +++++++++++++++- .../CollaboratorsInviteHandler.coffee | 21 +++++++++++++++++++ .../coffee/Features/Email/EmailBuilder.coffee | 20 ++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/services/web/app/coffee/Features/Collaborators/CollaboratorsEmailHandler.coffee b/services/web/app/coffee/Features/Collaborators/CollaboratorsEmailHandler.coffee index e9beb1bb43..f5d08054d4 100644 --- a/services/web/app/coffee/Features/Collaborators/CollaboratorsEmailHandler.coffee +++ b/services/web/app/coffee/Features/Collaborators/CollaboratorsEmailHandler.coffee @@ -3,6 +3,7 @@ EmailHandler = require("../Email/EmailHandler") Settings = require "settings-sharelatex" module.exports = + notifyUserOfProjectShare: (project_id, email, callback)-> Project .findOne(_id: project_id ) @@ -22,4 +23,19 @@ module.exports = "rs=ci" # referral source = collaborator invite ].join("&") owner: project.owner_ref - EmailHandler.sendEmail "projectSharedWithYou", emailOptions, callback \ No newline at end of file + EmailHandler.sendEmail "projectSharedWithYou", emailOptions, callback + + notifyUserOfProjectInvite: (project_id, email, invite, callback)-> + Project + .findOne(_id: project_id ) + .select("name owner_ref") + .populate('owner_ref') + .exec (err, project)-> + emailOptions = + to: email + replyTo: project.owner_ref.email + project: + name: project.name + inviteUrl: "#{Settings.siteUrl}/project/#{project._id}/invite/token/#{invite.token}" + owner: project.owner_ref + EmailHandler.sendEmail "projectInvite", emailOptions, callback diff --git a/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee b/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee index 2d9932c093..9812a9b987 100644 --- a/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee +++ b/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteHandler.coffee @@ -1,5 +1,6 @@ UserCreator = require('../User/UserCreator') Project = require("../../models/Project").Project +ProjectInvite = require("../../models/ProjectInvite").ProjectInvite mimelib = require("mimelib") logger = require('logger-sharelatex') UserGetter = require "../User/UserGetter" @@ -8,10 +9,30 @@ CollaboratorsEmailHandler = require "./CollaboratorsEmailHandler" Async = require "async" PrivilegeLevels = require "../Authorization/PrivilegeLevels" Errors = require "../Errors/Errors" +Crypto = require 'crypto' module.experts = CollaboratorsInviteHandler = inviteToProject: (projectId, sendingUserId, email, privileges, callback=(err,invite)->) -> + logger.log {projectId, sendingUserId, email, privileges}, "adding invite" + Crypto.randomBytes 24, (err, buffer) -> + if err? + logger.err {err, projectId, sendingUserId, email}, "error generating random token" + return callback(err) + token = buffer.toString('hex') + invite = new ProjectInvite { + email: email + token: token + sendingUserId: sendingUserId + projectId: projectId + privileges: privileges + } + ProjectInvite.save (err) -> + if err? + logger.err {err, projectId, sendingUserId, email}, "error saving token" + return callback(err) + CollaboratorsEmailHandler.notifyUserOfProjectInvite projectId, email, invite + callback(null, invite) revokeInvite: (projectId, inviteId, callback=(err)->) -> diff --git a/services/web/app/coffee/Features/Email/EmailBuilder.coffee b/services/web/app/coffee/Features/Email/EmailBuilder.coffee index 4bcf0c671d..dab9fda839 100644 --- a/services/web/app/coffee/Features/Email/EmailBuilder.coffee +++ b/services/web/app/coffee/Features/Email/EmailBuilder.coffee @@ -87,6 +87,26 @@ templates.projectSharedWithYou =

#{settings.appName}

""" +templates.projectInvite = + subject: _.template "<%= owner.email %> wants to share <%= project.name %> with you" + layout: NotificationEmailLayout + type:"notification" + compiledTemplate: _.template """ +

Hi, <%= owner.email %> wants to share '<%= project.name %>' with you

+
+
+
+ + + View Invite + + +
+
+
+

Thank you

+

#{settings.appName}

+""" templates.completeJoinGroupAccount = subject: _.template "Verify Email to join <%= group_name %> group"