From 6b430e68663076b036dc2993b76b5d3862b2bc79 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Fri, 18 Dec 2015 16:38:10 +0100 Subject: [PATCH] add getGroup static method and refactor controllers to use it --- website/src/controllers/api-v3/chat.js | 14 +------------- website/src/controllers/api-v3/groups.js | 14 +------------- website/src/models/group.js | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/website/src/controllers/api-v3/chat.js b/website/src/controllers/api-v3/chat.js index 1cf68464d1..7a3346d60f 100644 --- a/website/src/controllers/api-v3/chat.js +++ b/website/src/controllers/api-v3/chat.js @@ -23,25 +23,13 @@ api.getChat = { middlewares: [authWithHeaders(), cron], handler (req, res, next) { let user = res.locals.user; - let groupId = req.params.groupId; req.checkParams('groupId', res.t('groupIdRequired')).notEmpty(); let validationErrors = req.validationErrors(); if (validationErrors) return next(validationErrors); - let query; - - if (groupId === 'party' || user.party._id === groupId) { - query = {type: 'party', _id: user.party._id}; - } else if (user.guilds.indexOf(groupId) !== -1) { - query = {type: 'guild', _id: groupId}; - } else { - query = {type: 'guild', privacy: 'public', _id: groupId}; - } - - Group - .findOne(query, 'chat').exec() + Group.getGroup(user, req.params.groupId, 'chat') .then(group => { if (!group) throw new NotFound(res.t('groupNotFound')); diff --git a/website/src/controllers/api-v3/groups.js b/website/src/controllers/api-v3/groups.js index 477ba2a101..7617f3ea99 100644 --- a/website/src/controllers/api-v3/groups.js +++ b/website/src/controllers/api-v3/groups.js @@ -23,25 +23,13 @@ api.getGroup = { middlewares: [authWithHeaders(), cron], handler (req, res, next) { let user = res.locals.user; - let groupId = req.params.groupId; req.checkParams('groupId', res.t('groupIdRequired')).notEmpty(); let validationErrors = req.validationErrors(); if (validationErrors) return next(validationErrors); - let query; - - if (groupId === 'party' || user.party._id === groupId) { - query = {type: 'party', _id: user.party._id}; - } else if (user.guilds.indexOf(groupId) !== -1) { - query = {type: 'guild', _id: groupId}; - } else { - query = {type: 'guild', privacy: 'public', _id: groupId}; - } - - Group - .findOne(query).exec() + Group.getGroup(user, req.params.groupId) .then(group => { if (!group) throw new NotFound(res.t('groupNotFound')); diff --git a/website/src/models/group.js b/website/src/models/group.js index c28334dd17..0fe32464d0 100644 --- a/website/src/models/group.js +++ b/website/src/models/group.js @@ -145,6 +145,20 @@ schema.post('remove', function postRemoveGroup (group) { return doc; };*/ +schema.statics.getGroup = function getGroup (user, groupId, fields) { + let query; + + if (groupId === 'party' || user.party._id === groupId) { + query = {type: 'party', _id: user.party._id}; + } else if (user.guilds.indexOf(groupId) !== -1) { + query = {type: 'guild', _id: groupId}; + } else { + query = {type: 'guild', privacy: 'public', _id: groupId}; + } + + return this.findOne(query, fields).exec(); // TODO catch errors here? +}; + // TODO move to its own model export function chatDefaults (msg, user) { let message = {