WIP(chat): v3 controller

This commit is contained in:
Sabe Jones
2015-12-16 17:29:20 -05:00
parent 18c49493e0
commit 21f93c9399
2 changed files with 52 additions and 1 deletions

View File

@@ -27,5 +27,7 @@
"positionRequired": "\"position\" is required and must be a number.", "positionRequired": "\"position\" is required and must be a number.",
"cantMoveCompletedTodo": "Can't move a completed todo.", "cantMoveCompletedTodo": "Can't move a completed todo.",
"directionUpDown": "\"direction\" is required and must be 'up' or 'down'", "directionUpDown": "\"direction\" is required and must be 'up' or 'down'",
"alreadyTagged": "The task is already tagged with given tag." "alreadyTagged": "The task is already tagged with given tag.",
"groupIdRequired": "\"groupId\" must be a valid UUID",
"groupNotFound": "Group not found."
} }

View File

@@ -0,0 +1,49 @@
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/chat Get chat messages from a group
* @apiVersion 3.0.0
* @apiName GetChat
* @apiGroup Chat
*
* @apiParam {UUID} groupId The group _id
*
* @apiSuccess {Array} chat An array of chat messages
*/
api.getChat = {
method: 'GET',
url: '/groups/:groupId/chat',
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 = groupId === 'party' ?
Group.findOne({type: 'party', members: {$in: [user._id]}}) :
Group.findOne({$or: [
{_id: groupId, privacy: 'public'},
{_id: groupId, privacy: 'private', members: {$in: [user._id]}},
]});
query.exec()
.then((group) => {
if (!group) throw new NotFound(res.t('groupNotFound'));
res.respond(200, group.chat);
})
.catch(next);
},
};
export default api;