mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-03 08:21:07 +00:00
Merge pull request #6171 from crookedneighbor/flag_messages_in_api
Flag messages in api
This commit is contained in:
commit
a7f0a2a8ca
5 changed files with 185 additions and 7 deletions
|
|
@ -82,6 +82,132 @@ describe('GET /groups/:id', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
context('flagged messages', () => {
|
||||||
|
let group;
|
||||||
|
|
||||||
|
let chat1 = {
|
||||||
|
id: 'chat1',
|
||||||
|
text: 'chat 1',
|
||||||
|
flags: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
let chat2 = {
|
||||||
|
id: 'chat2',
|
||||||
|
text: 'chat 2',
|
||||||
|
flags: {},
|
||||||
|
flagCount: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
let chat3 = {
|
||||||
|
id: 'chat3',
|
||||||
|
text: 'chat 3',
|
||||||
|
flags: {
|
||||||
|
'user-id': true,
|
||||||
|
},
|
||||||
|
flagCount: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
let chat4 = {
|
||||||
|
id: 'chat4',
|
||||||
|
text: 'chat 4',
|
||||||
|
flags: {
|
||||||
|
'user-id': true,
|
||||||
|
'other-user-id': true,
|
||||||
|
},
|
||||||
|
flagCount: 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
let chat5 = {
|
||||||
|
id: 'chat5',
|
||||||
|
text: 'chat 5',
|
||||||
|
flags: {
|
||||||
|
'user-id': true,
|
||||||
|
'other-user-id': true,
|
||||||
|
'yet-another-user-id': true,
|
||||||
|
},
|
||||||
|
flagCount: 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
return createAndPopulateGroup({
|
||||||
|
groupDetails: {
|
||||||
|
name: 'test guild',
|
||||||
|
type: 'guild',
|
||||||
|
privacy: 'public',
|
||||||
|
chat: [
|
||||||
|
chat1,
|
||||||
|
chat2,
|
||||||
|
chat3,
|
||||||
|
chat4,
|
||||||
|
chat5,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}).then((res) => {
|
||||||
|
group = res.group;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('non-admin', () => {
|
||||||
|
let api;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
return generateUser().then((user) => {
|
||||||
|
api = requester(user);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not include messages with a flag count of 2 or greater', () => {
|
||||||
|
return api.get(`/groups/${group._id}`).then((_group) => {
|
||||||
|
expect(_group.chat).to.have.lengthOf(3);
|
||||||
|
expect(_group.chat[0].id).to.eql(chat1.id);
|
||||||
|
expect(_group.chat[1].id).to.eql(chat2.id);
|
||||||
|
expect(_group.chat[2].id).to.eql(chat3.id);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not include user ids in flags object', () => {
|
||||||
|
return api.get(`/groups/${group._id}`).then((_group) => {
|
||||||
|
let chatWithOneFlag = _group.chat[2];
|
||||||
|
expect(chatWithOneFlag.id).to.eql(chat3.id);
|
||||||
|
expect(chat3.flags).to.eql({ 'user-id': true });
|
||||||
|
expect(chatWithOneFlag.flags).to.eql({});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('admin', () => {
|
||||||
|
let api;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
return generateUser({
|
||||||
|
'contributor.admin': true,
|
||||||
|
}).then((user) => {
|
||||||
|
api = requester(user);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('includes all messages', () => {
|
||||||
|
return api.get(`/groups/${group._id}`).then((_group) => {
|
||||||
|
expect(_group.chat).to.have.lengthOf(5);
|
||||||
|
expect(_group.chat[0].id).to.eql(chat1.id);
|
||||||
|
expect(_group.chat[1].id).to.eql(chat2.id);
|
||||||
|
expect(_group.chat[2].id).to.eql(chat3.id);
|
||||||
|
expect(_group.chat[3].id).to.eql(chat4.id);
|
||||||
|
expect(_group.chat[4].id).to.eql(chat5.id);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('includes user ids in flags object', () => {
|
||||||
|
return api.get(`/groups/${group._id}`).then((_group) => {
|
||||||
|
let chatWithOneFlag = _group.chat[2];
|
||||||
|
expect(chatWithOneFlag.id).to.eql(chat3.id);
|
||||||
|
expect(chat3.flags).to.eql({ 'user-id': true });
|
||||||
|
expect(chatWithOneFlag.flags).to.eql(chat3.flags);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
context('Non-member of a public guild', () => {
|
context('Non-member of a public guild', () => {
|
||||||
let leader, nonMember, createdGroup;
|
let leader, nonMember, createdGroup;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ describe('POST /groups/:id/chat/:id/flag', () => {
|
||||||
return api.get(`/groups/${group._id}/chat`);
|
return api.get(`/groups/${group._id}/chat`);
|
||||||
}).then((messages) => {
|
}).then((messages) => {
|
||||||
let message = messages[0];
|
let message = messages[0];
|
||||||
expect(message.flags[user._id]).to.eql(true);
|
expect(message.flagCount).to.eql(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -99,4 +99,42 @@ describe('POST /groups/:id/chat/:id/flag', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
context('admin flagging a message', () => {
|
||||||
|
let group, member, message, user;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
return createAndPopulateGroup({
|
||||||
|
groupDetails: {
|
||||||
|
type: 'guild',
|
||||||
|
privacy: 'public',
|
||||||
|
},
|
||||||
|
leaderDetails: {
|
||||||
|
'contributor.admin': true,
|
||||||
|
balance: 10,
|
||||||
|
},
|
||||||
|
members: 1,
|
||||||
|
}).then((res) => {
|
||||||
|
group = res.group;
|
||||||
|
user = res.leader;
|
||||||
|
member = res.members[0];
|
||||||
|
|
||||||
|
return requester(member)
|
||||||
|
.post(`/groups/${group._id}/chat`, null, { message: 'Group member message', });
|
||||||
|
}).then((res) => {
|
||||||
|
message = res.message;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets flagCount to 5', () => {
|
||||||
|
let api = requester(user);
|
||||||
|
|
||||||
|
return api.post(`/groups/${group._id}/chat/${message.id}/flag`).then((messages) => {
|
||||||
|
return api.get(`/groups/${group._id}/chat`);
|
||||||
|
}).then((messages) => {
|
||||||
|
let message = messages[0];
|
||||||
|
expect(message.flagCount).to.eql(5);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -149,6 +149,11 @@ api.get = function(req, res, next) {
|
||||||
// so that users with no party don't get a 404 on every access to the site
|
// so that users with no party don't get a 404 on every access to the site
|
||||||
return res.json(group);
|
return res.json(group);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!user.contributor.admin) {
|
||||||
|
_purgeFlagInfoFromChat(group);
|
||||||
|
}
|
||||||
|
|
||||||
//Since we have a limit on how many members are populate to the group, we want to make sure the user is always in the group
|
//Since we have a limit on how many members are populate to the group, we want to make sure the user is always in the group
|
||||||
var userInGroup = _.find(group.members, function(member){ return member._id == user._id; });
|
var userInGroup = _.find(group.members, function(member){ return member._id == user._id; });
|
||||||
//If the group is private or the group is a party, then the user must be a member of the group based on access restrictions above
|
//If the group is private or the group is a party, then the user must be a member of the group based on access restrictions above
|
||||||
|
|
@ -247,11 +252,17 @@ api.update = function(req, res, next) {
|
||||||
|
|
||||||
// TODO remove from api object?
|
// TODO remove from api object?
|
||||||
api.attachGroup = function(req, res, next) {
|
api.attachGroup = function(req, res, next) {
|
||||||
|
var user = res.locals.user;
|
||||||
var gid = req.params.gid;
|
var gid = req.params.gid;
|
||||||
var q = (gid == 'party') ? Group.findOne({type: 'party', members: {'$in': [res.locals.user._id]}}) : Group.findById(gid);
|
var q = (gid == 'party') ? Group.findOne({type: 'party', members: {'$in': [res.locals.user._id]}}) : Group.findById(gid);
|
||||||
q.exec(function(err, group){
|
q.exec(function(err, group){
|
||||||
if(err) return next(err);
|
if(err) return next(err);
|
||||||
if(!group) return res.json(404, {err: shared.i18n.t('messageGroupNotFound')});
|
if(!group) return res.json(404, {err: shared.i18n.t('messageGroupNotFound')});
|
||||||
|
|
||||||
|
if (!user.contributor.admin) {
|
||||||
|
_purgeFlagInfoFromChat(group);
|
||||||
|
}
|
||||||
|
|
||||||
res.locals.group = group;
|
res.locals.group = group;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
@ -271,6 +282,7 @@ api.getChat = function(req, res, next) {
|
||||||
q.exec(function(err, group){
|
q.exec(function(err, group){
|
||||||
if (err) return next(err);
|
if (err) return next(err);
|
||||||
if (!group && gid!=='party') return res.json(404,{err: shared.i18n.t('messageGroupNotFound')});
|
if (!group && gid!=='party') return res.json(404,{err: shared.i18n.t('messageGroupNotFound')});
|
||||||
|
|
||||||
res.json(res.locals.group.chat);
|
res.json(res.locals.group.chat);
|
||||||
gid = null;
|
gid = null;
|
||||||
});
|
});
|
||||||
|
|
@ -1087,3 +1099,10 @@ api.questLeave = function(req, res, next) {
|
||||||
return next(error);
|
return next(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _purgeFlagInfoFromChat(group) {
|
||||||
|
group.chat = _.filter(group.chat, function(message) { return !message.flagCount || message.flagCount < 2; });
|
||||||
|
_.each(group.chat, function (message) {
|
||||||
|
message.flags = {};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -475,11 +475,6 @@ GroupSchema.methods.leave = function(user, keep, mainCb){
|
||||||
|
|
||||||
GroupSchema.methods.toJSON = function() {
|
GroupSchema.methods.toJSON = function() {
|
||||||
var doc = this.toObject();
|
var doc = this.toObject();
|
||||||
if(doc.chat){
|
|
||||||
doc.chat.forEach(function(msg){
|
|
||||||
msg.flags = {};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
mixin chatMessages(inbox)
|
mixin chatMessages(inbox)
|
||||||
ul.list-unstyled.tavern-chat
|
ul.list-unstyled.tavern-chat
|
||||||
- var ngRepeat = inbox ? 'message in user.inbox.messages | toArray:true | orderBy:"sort":true' : 'message in group.chat track by message.id'
|
- var ngRepeat = inbox ? 'message in user.inbox.messages | toArray:true | orderBy:"sort":true' : 'message in group.chat track by message.id'
|
||||||
li.chat-message(ng-repeat=ngRepeat, ng-class=':: {highlight: isUserMentioned(user,message) || message.uuid=="system", "own-message": user._id == message.uuid}', ng-if="!message.flagCount || message.flagCount < 2 || user.contributor.admin")
|
li.chat-message(ng-repeat=ngRepeat, ng-class=':: {highlight: isUserMentioned(user,message) || message.uuid=="system", "own-message": user._id == message.uuid}')
|
||||||
span.pull-right.text-danger(ng-if="user.contributor.admin && message.flagCount > 0")
|
span.pull-right.text-danger(ng-if="user.contributor.admin && message.flagCount > 0")
|
||||||
| {{message.flagCount > 1 ? "Message Hidden" : "1 flag"}}
|
| {{message.flagCount > 1 ? "Message Hidden" : "1 flag"}}
|
||||||
.scrollable-message(ng-class='{"transparent": message.sent || message.flags[user._id] || (user.contributor.admin && message.flagCount > 1)}')
|
.scrollable-message(ng-class='{"transparent": message.sent || message.flags[user._id] || (user.contributor.admin && message.flagCount > 1)}')
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue