feat(flag-message): add server methods

This commit is contained in:
Matteo Pagliazzi
2014-12-07 14:35:09 +01:00
parent e49e8f5765
commit ae6e0255c6
7 changed files with 67 additions and 24 deletions
+16
View File
@@ -256,6 +256,22 @@ api.deleteChatMessage = function(req, res, next){
});
}
api.flagChatMessage = function(req, res, next){
var user = res.locals.user
var group = res.locals.group;
var message = _.find(group.chat, {id: req.params.mid});
if(!message) return res.json(404, {err: "Message not found!"});
if(message.uuid == user._id) return res.json(401, {err: "Can't report your own message."});
message.flags[user._id] = true;
group.markModified('chat');
group.save(function(err,_saved){
if(err) return next(err);
return res.send(204);
});
}
api.seenMessage = function(req,res,next){
// Skip the auth step, we want this to be fast. If !found with uuid/token, then it just doesn't save
// Check for req.params.gid to exist
+13 -1
View File
@@ -99,7 +99,7 @@ var chatDefaults = module.exports.chatDefaults = function(msg,user){
text: msg,
timestamp: +new Date,
likes: {},
flags: {} // for @paglias, @crookedneighbor added this.
flags: {}
};
if (user) {
_.defaults(message, {
@@ -317,6 +317,18 @@ GroupSchema.statics.bossQuest = function(user, progress, cb) {
})
}
GroupSchema.methods.toJSON = function() {
var doc = this.toObject();
if(doc.chat){
doc.chat.forEach(function(msg){
msg.flags = {};
});
}
return doc;
};
module.exports.schema = GroupSchema;
var Group = module.exports.model = mongoose.model("Group", GroupSchema);
+11
View File
@@ -577,6 +577,17 @@ module.exports = (swagger, v2) ->
middleware: [auth.auth, i18n.getUserLanguage, groups.attachGroup]
action: groups.likeChatMessage
"/groups/{gid}/chat/{mid}/flag":
spec:
method: 'POST'
description: "Flag a chat message"
parameters: [
path 'gid','Group id','string'
path 'mid','Message id','string'
]
middleware: [auth.auth, i18n.getUserLanguage, groups.attachGroup]
action: groups.flagChatMessage
# ---------------------------------
# Members
# ---------------------------------