codecombat/server/middleware/patchable.coffee

57 lines
1.9 KiB
CoffeeScript
Raw Normal View History

2015-12-16 20:09:22 -05:00
utils = require '../lib/utils'
errors = require '../commons/errors'
wrap = require 'co-express'
Promise = require 'bluebird'
Patch = require '../models/Patch'
mongoose = require 'mongoose'
database = require '../commons/database'
parse = require '../commons/parse'
2015-12-16 20:09:22 -05:00
module.exports =
patches: (Model, options={}) -> wrap (req, res) ->
2015-12-16 20:09:22 -05:00
dbq = Patch.find()
dbq.limit(parse.getLimitFromReq(req))
dbq.skip(parse.getSkipFromReq(req))
dbq.select(parse.getProjectFromReq(req))
2015-12-16 20:09:22 -05:00
doc = yield database.getDocFromHandle(req, Model, {_id: 1})
if not doc
throw new errors.NotFound('Patchable document not found')
2015-12-16 20:09:22 -05:00
query =
$or: [
{'target.original': doc.id }
{'target.original': doc._id }
2015-12-16 20:09:22 -05:00
]
if req.query.status
query.status = req.query.status
if req.user and req.query.creator is req.user.id
query.creator = req.user._id
2015-12-16 20:09:22 -05:00
patches = yield dbq.find(query).sort('-created')
res.status(200).send(patches)
joinWatchers: (Model, options={}) -> wrap (req, res) ->
doc = yield database.getDocFromHandle(req, Model)
2015-12-16 20:09:22 -05:00
if not doc
throw new errors.NotFound('Document not found.')
if not database.hasAccessToDocument(req, doc, 'get')
2015-12-16 20:09:22 -05:00
throw new errors.Forbidden()
updateResult = yield doc.update({ $addToSet: { watchers: req.user.get('_id') }})
if updateResult.nModified
watchers = doc.get('watchers')
watchers.push(req.user.get('_id'))
doc.set('watchers', watchers)
res.status(200).send(doc)
leaveWatchers: (Model, options={}) -> wrap (req, res) ->
doc = yield database.getDocFromHandle(req, Model)
2015-12-16 20:09:22 -05:00
if not doc
throw new errors.NotFound('Document not found.')
updateResult = yield doc.update({ $pull: { watchers: req.user.get('_id') }})
if updateResult.nModified
watchers = doc.get('watchers')
watchers = _.filter watchers, (id) -> not id.equals(req.user._id)
doc.set('watchers', watchers)
res.status(200).send(doc)