Creators of documents are automatically added as watchers. Added a patch creation email for watchers.

This commit is contained in:
Scott Erickson 2014-04-17 17:09:01 -07:00
parent 21e8d7b26f
commit ed0c7e1412
8 changed files with 46 additions and 13 deletions

View file

@ -56,6 +56,7 @@ module.exports = class SaveVersionModal extends ModalView
}
errors = patch.validate()
forms.applyErrorsToForm(@$el, errors) if errors
patch.set 'editPath', document.location.pathname
res = patch.save()
return unless res
@enableModalInProgress(@$el)

View file

@ -233,6 +233,9 @@ module.exports = class Handler
return @sendBadInputError(res, err.errors) if err?.valid is false
return @sendDatabaseError(res, err) if err
@sendSuccess(res, @formatEntity(req, document))
@onPostSuccess(req, document)
onPostSuccess: (req, doc) ->
###
TODO: think about pulling some common stuff out of postFirstVersion/postNewVersion
@ -248,7 +251,6 @@ module.exports = class Handler
document.set('original', document._id)
document.set('creator', req.user._id)
@saveChangesToDocument req, document, (err) =>
console.log 'saved new version', document.toObject()
return @sendBadInputError(res, err.errors) if err?.valid is false
return @sendDatabaseError(res, err) if err
@sendSuccess(res, @formatEntity(req, document))
@ -299,7 +301,9 @@ module.exports = class Handler
parentDocument.makeNewMajorVersion(updatedObject, done)
makeNewInstance: (req) ->
new @modelClass({})
model = new @modelClass({})
model.set 'watchers', [req.user.get('_id')] if @modelClass.schema.is_patchable
model
validateDocumentInput: (input) ->
tv4 = require('tv4').tv4

View file

@ -42,6 +42,7 @@ PatchSchema.pre 'save', (next) ->
patches = _.clone patches
patches.push @_id
document.set 'patches', patches, {strict: false}
@targetLoaded = document
document.save (err) -> next(err)
module.exports = mongoose.model('patch', PatchSchema)

View file

@ -1,8 +1,11 @@
Patch = require('./Patch')
User = require '../users/User'
Handler = require('../commons/Handler')
schema = require '../../app/schemas/models/patch'
{handlers} = require '../commons/mapping'
mongoose = require('mongoose')
log = require 'winston'
sendwithus = require '../sendwithus'
PatchHandler = class PatchHandler extends Handler
modelClass: Patch
@ -50,6 +53,28 @@ PatchHandler = class PatchHandler extends Handler
patch.update {$set:{status:newStatus}}, {}, ->
target.update {$pull:{patches:patch.get('_id')}}, {}, ->
@sendSuccess(res, null)
onPostSuccess: (req, doc) ->
log.error "Error sending patch created: could not find the loaded target on the patch object." unless doc.targetLoaded
return unless doc.targetLoaded
watchers = doc.targetLoaded.get('watchers')
return unless watchers?.length
User.find({_id:{$in:watchers}}).select({email:1, name:1}).exec (err, watchers) =>
for watcher in watchers
@sendPatchCreatedEmail req.user, watcher, doc, doc.targetLoaded, req.body.editPath
sendPatchCreatedEmail: (patchCreator, watcher, patch, target, editPath) ->
# return if watcher._id is patchCreator._id
context =
email_id: sendwithus.templates.patch_created
recipient:
address: watcher.get('email')
name: watcher.get('name')
email_data:
doc_name: target.get('name') or '???'
submitter_name: patchCreator.get('name') or '???'
doc_link: "http://codecombat.com#{editPath}"
commit_message: patch.get('commitMessage')
sendwithus.api.send context, (err, result) ->
module.exports = new PatchHandler()

View file

@ -14,3 +14,4 @@ if config.unittest
module.exports.templates =
welcome_email: 'utnGaBHuSU4Hmsi7qrAypU'
ladder_update_email: 'JzaZxf39A4cKMxpPZUfWy4'
patch_created: 'tem_xhxuNosLALsizTNojBjNcL'

View file

@ -64,13 +64,13 @@ GLOBAL.unittest = {}
unittest.users = unittest.users or {}
unittest.getNormalJoe = (done, force) ->
unittest.getUser('normal@jo.com', 'food', done, force)
unittest.getUser('Joe', 'normal@jo.com', 'food', done, force)
unittest.getOtherSam = (done, force) ->
unittest.getUser('other@sam.com', 'beer', done, force)
unittest.getUser('Sam', 'other@sam.com', 'beer', done, force)
unittest.getAdmin = (done, force) ->
unittest.getUser('admin@afc.com', '80yqxpb38j', done, force)
unittest.getUser('Admin', 'admin@afc.com', '80yqxpb38j', done, force)
unittest.getUser = (email, password, done, force) ->
unittest.getUser = (name, email, password, done, force) ->
# Creates the user if it doesn't already exist.
return done(unittest.users[email]) if unittest.users[email] and not force
@ -81,6 +81,7 @@ unittest.getUser = (email, password, done, force) ->
throw err if err
User.findOne({email:email}).exec((err, user) ->
user.set('permissions', if password is '80yqxpb38j' then [ 'admin' ] else [])
user.set('name', name)
user.save (err) ->
wrapUpGetUser(email, user, done)
)
@ -88,7 +89,6 @@ unittest.getUser = (email, password, done, force) ->
form = req.form()
form.append('email', email)
form.append('password', password)
wrapUpGetUser = (email, user, done) ->
unittest.users[email] = user

View file

@ -16,13 +16,14 @@ describe '/db/patch', ->
patch =
commitMessage: 'Accept this patch!'
delta: {name:['test']}
editPath: '/who/knows/yes'
target:
id:null
collection: 'article'
it 'creates an Article to patch', (done) ->
loginAdmin ->
request.post {uri:articleURL, json:patch}, (err, res, body) ->
request.post {uri:articleURL, json:article}, (err, res, body) ->
articles[0] = body
patch.target.id = articles[0]._id
done()
@ -56,24 +57,24 @@ describe '/db/patch', ->
it 'allows you to set yourself as watching', (done) ->
watchingURL = getURL("/db/article/#{articles[0]._id}/watch")
request.put {uri: watchingURL, json: {on:true}}, (err, res, body) ->
expect(body.watchers[0]).toBeDefined()
expect(body.watchers[1]).toBeDefined()
done()
it 'added the watcher to the target document', (done) ->
Article.findOne({}).exec (err, article) ->
expect(article.toObject().watchers[0]).toBeDefined()
expect(article.toObject().watchers[1]).toBeDefined()
done()
it 'does not add duplicate watchers', (done) ->
watchingURL = getURL("/db/article/#{articles[0]._id}/watch")
request.put {uri: watchingURL, json: {on:true}}, (err, res, body) ->
expect(body.watchers.length).toBe(1)
expect(body.watchers.length).toBe(2)
done()
it 'allows removing yourself', (done) ->
watchingURL = getURL("/db/article/#{articles[0]._id}/watch")
request.put {uri: watchingURL, json: {on:false}}, (err, res, body) ->
expect(body.watchers.length).toBe(0)
expect(body.watchers.length).toBe(1)
done()
it 'allows the submitter to withdraw the pull request', (done) ->

View file

@ -112,7 +112,7 @@ ghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghl
unittest.getNormalJoe (joe) ->
req = request.put getURL(urlUser), (err, res) ->
expect(res.statusCode).toBe(200)
unittest.getUser('New@email.com', 'null', (joe) ->
unittest.getUser('Wilhelm', 'New@email.com', 'null', (joe) ->
expect(joe.get('name')).toBe('Wilhelm')
expect(joe.get('emailLower')).toBe('new@email.com')
expect(joe.get('email')).toBe('New@email.com')