diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-08-25 13:12:27 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-08-25 13:40:12 -0400 |
| commit | 099dfd16940c62e931bf01e7f62b7a45f2b8c654 (patch) | |
| tree | b10d4dc452d01783619966a43ea10decc75e4344 /server/lib/api/collaborator.js | |
| parent | 4ef340497ef24bb2ecacb2c9c4106c24515c874f (diff) | |
collaborators api
Diffstat (limited to 'server/lib/api/collaborator.js')
| -rw-r--r-- | server/lib/api/collaborator.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/server/lib/api/collaborator.js b/server/lib/api/collaborator.js new file mode 100644 index 0000000..4b55f09 --- /dev/null +++ b/server/lib/api/collaborator.js @@ -0,0 +1,85 @@ +/* jshint node: true */ + +var _ = require('lodash'), + util = require('../util'), + upload = require('../upload'), + config = require('../../../config.json'), + Collaborator = require('../schemas/Collaborator'), + Project = require('../schemas/Project'); + +var collaborator = { + + join: function(req, res){ + var nonce = req.query.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.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 (req.project.user_id !== 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 + }) + res.json(collaborators) + }) + }) + }, + + create: function(req, res){ + if (! req.project) { + return res.json({ error: "can't find project" }) + } + var data = util.cleanQuery(req.body) + delete data.user_id + + data.project_id = req.project._id + + Collaborator.makeNonce(function(nonce){ + data.nonce = nonce + new Collaborator(data).save(function(err, collaborator){ + if (err || ! collaborator) { return res.json({ error: err }) } + auth.mail.forgotPassword(req.project, req.user, collaborator, function(){ + res.json(collaborator) + }) + }) + }) + }, + + destroy: function(req, res){ + if (! req.project) { + return res.json({ error: "can't find project" }) + } + if (req.project.user_id !== req.user._id) { + return res.json({ error: "insufficient permission" }) + } + Collaborator.remove({ _id: _id }, function(err){ + res.json({ status: "OK" }) + }) + } +} + +module.exports = collaborator |
