mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 23:27:26 +01:00
Added chat post route and initial tests
This commit is contained in:
@@ -39,4 +39,60 @@ api.getChat = {
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @api {post} /groups/:groupId/chat Post chat message to a group
|
||||
* @apiVersion 3.0.0
|
||||
* @apiName PostCat
|
||||
* @apiGroup Chat
|
||||
*
|
||||
* @apiParam {UUID} groupId The group _id
|
||||
* @apiParam {message} message The chat's message
|
||||
* @apiParam {previousMsg} previousMsg The previous chat message which will force a return of the full group chat
|
||||
*
|
||||
* @apiSuccess {Array} chat An array of chat messages
|
||||
*/
|
||||
api.postChat = {
|
||||
method: 'POST',
|
||||
url: '/groups/:groupId/chat',
|
||||
middlewares: [authWithHeaders(), cron],
|
||||
handler (req, res, next) {
|
||||
let user = res.locals.user;
|
||||
let groupId = req.params.groupId;
|
||||
let chatUpdated;
|
||||
|
||||
req.checkParams('groupId', res.t('groupIdRequired')).notEmpty();
|
||||
req.checkBody('message', res.t('messageGroupChatBlankMessage')).notEmpty();
|
||||
|
||||
let validationErrors = req.validationErrors();
|
||||
if (validationErrors) return next(validationErrors);
|
||||
|
||||
Group.getGroup(user, groupId)
|
||||
.then((group) => {
|
||||
if (!group) throw new NotFound(res.t('groupNotFound'));
|
||||
if (group.type !== 'party' && user.flags.chatRevoked) {
|
||||
throw new NotFound('Your chat privileges have been revoked.');
|
||||
}
|
||||
|
||||
let lastClientMsg = req.query.previousMsg;
|
||||
chatUpdated = lastClientMsg && group.chat && group.chat[0] && group.chat[0].id !== lastClientMsg ? true : false;
|
||||
|
||||
group.sendChat(req.body.message, user);
|
||||
|
||||
if (group.type === 'party') {
|
||||
user.party.lastMessageSeen = group.chat[0].id;
|
||||
user.save();
|
||||
}
|
||||
return group.save();
|
||||
})
|
||||
.then((group) => {
|
||||
if (chatUpdated) {
|
||||
res.respond(200, {chat: group.chat});
|
||||
} else {
|
||||
res.respond(200, {message: group.chat[0]});
|
||||
}
|
||||
})
|
||||
.catch(next);
|
||||
},
|
||||
};
|
||||
|
||||
export default api;
|
||||
|
||||
Reference in New Issue
Block a user