From 7adc06031259b237b09be3c95d115cc84c62f0f6 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Fri, 18 Dec 2015 16:32:37 +0100 Subject: [PATCH] add GET group --- website/src/controllers/api-v3/chat.js | 2 +- website/src/controllers/api-v3/groups.js | 54 ++++++++++++++++++++++++ website/src/models/group.js | 2 +- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 website/src/controllers/api-v3/groups.js diff --git a/website/src/controllers/api-v3/chat.js b/website/src/controllers/api-v3/chat.js index e94546ddac..1cf68464d1 100644 --- a/website/src/controllers/api-v3/chat.js +++ b/website/src/controllers/api-v3/chat.js @@ -34,7 +34,7 @@ api.getChat = { if (groupId === 'party' || user.party._id === groupId) { query = {type: 'party', _id: user.party._id}; - } else if (user.guilds.indexOf(groupId)) { + } else if (user.guilds.indexOf(groupId) !== -1) { query = {type: 'guild', _id: groupId}; } else { query = {type: 'guild', privacy: 'public', _id: groupId}; diff --git a/website/src/controllers/api-v3/groups.js b/website/src/controllers/api-v3/groups.js new file mode 100644 index 0000000000..477ba2a101 --- /dev/null +++ b/website/src/controllers/api-v3/groups.js @@ -0,0 +1,54 @@ +import { authWithHeaders } from '../../middlewares/api-v3/auth'; +import cron from '../../middlewares/api-v3/cron'; +import { model as Group } from '../../models/group'; +import { + NotFound, +} from '../../libs/api-v3/errors'; + +let api = {}; + +/** + * @api {get} /groups/:groupId Get group + * @apiVersion 3.0.0 + * @apiName GetGroup + * @apiGroup Group + * + * @apiParam {UUID} groupId The group _id + * + * @apiSuccess {Object} group The group object + */ +api.getGroup = { + method: 'GET', + url: '/groups/:groupId', + 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() + .then(group => { + if (!group) throw new NotFound(res.t('groupNotFound')); + + res.respond(200, group); + }) + .catch(next); + }, +}; + +export default api; diff --git a/website/src/models/group.js b/website/src/models/group.js index 07f9ec9b16..c28334dd17 100644 --- a/website/src/models/group.js +++ b/website/src/models/group.js @@ -127,7 +127,7 @@ schema.post('remove', function postRemoveGroup (group) { firebase.deleteGroup(group._id); }); -/*schema.methods.toJSON = function groupToJSON () { +/* schema.methods.toJSON = function groupToJSON () { let doc = this.toObject(); // removeDuplicates(doc); doc._isMember = this._isMember; // TODO ?