diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-08-29 22:34:54 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-08-29 22:34:54 -0400 |
| commit | 01755335859b19756f6d64a2f8a10ae98abceb5f (patch) | |
| tree | d5ba0258769f85fff5512498b04c3c037daecf79 /server/lib/api/collaborator.js | |
| parent | 2235c34e498499b8141e835998b962067583a0ce (diff) | |
| parent | 851ddfd46abb7f944c1a6b7f198b5fd8cabd4c13 (diff) | |
merge
Diffstat (limited to 'server/lib/api/collaborator.js')
| -rw-r--r-- | server/lib/api/collaborator.js | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/server/lib/api/collaborator.js b/server/lib/api/collaborator.js new file mode 100644 index 0000000..1fda01b --- /dev/null +++ b/server/lib/api/collaborator.js @@ -0,0 +1,89 @@ +/* jshint node: true */ + +var _ = require('lodash'), + auth = require('../auth'), + util = require('../util'), + upload = require('../upload'), + config = require('../../../config.json'), + User = require('../schemas/User'), + Collaborator = require('../schemas/Collaborator'), + Project = require('../schemas/Project'); + +var collaborator = { + + join: function(req, res){ + var nonce = req.params.nonce + if (! nonce || ! nonce.length) { return res.json({ error: "invalid invite code" }) } + Collaborator.findOne({ nonce: nonce }, function(err, collaborator){ + if (err || ! collaborator) { return res.json({ error: "can't find collaborator" }) } + collaborator.user_id = req.user._id + collaborator.nonce = "" + collaborator.save(function(err, collaborator){ + Project.findOne({ _id: collaborator.project_id }, function(err, project){ + if (err || ! project) { return res.json({ error: err }) } + res.redirect("/project/" + project.slug + "/edit") + }) + }) + }) + }, + + // + + index: function(req, res){ + if (! req.project) { + return res.json({ error: "can't find project" }) + } + if (String(req.project.user_id) !== String(req.user._id)) { return res.json({ error: "insufficient permission" }) } + Collaborator.find({ project_id: req.project._id }, function(err, collaborators){ + var user_ids = _.pluck(collaborators, "user_id").filter(function(id){ return !! id }) + User.find({ _id: user_ids }, "username displayName photo", function(err, users){ + if (! user_ids) { + return res.json(collaborators) + } + var userIndex = _.indexBy(users, '_id') + collaborators = collaborators.map(function(collaborator){ + var obj = collaborator.toObject() + obj.user = userIndex[ obj.user_id ] + return obj + }) + collaborators.unshift( { user: req.user.toObject(), owner: true } ) + res.json(collaborators) + }) + }) + }, + + create: function(req, res){ + if (! req.project) { + return res.json({ error: "can't find project" }) + } + var data = util.cleanQuery(req.body) + data.email = util.trim( util.sanitize( data.email ) ) + data.project_id = req.project._id + delete data.user_id + + Collaborator.makeNonce(function(nonce){ + data.nonce = nonce + + new Collaborator(data).save(function(err, collaborator){ + if (err || ! collaborator) { return res.json({ error: err }) } + console.log(collaborator) + res.json(collaborator) + auth.mail.collaborator(req.project, req.user, collaborator, function(){}) + }) + }) + }, + + destroy: function(req, res){ + if (! req.project) { + return res.json({ error: "can't find project" }) + } + if (String(req.project.user_id) !== String(req.user._id)) { + return res.json({ error: "insufficient permission" }) + } + Collaborator.remove({ _id: req.body._id }, function(err){ + res.json({ status: "OK" }) + }) + } +} + +module.exports = collaborator |
