Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Wrong message count statistics in Admin info page #16680

Merged
merged 7 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/lib/server/functions/deleteMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export const deleteMessage = function(message, user) {
}
}

// decrease message count
Rooms.decreaseMessageCountById(message.rid, 1);

if (showDeletedStatus) {
Messages.setAsDeletedByIdAndUser(message._id, user);
} else {
Expand Down
14 changes: 12 additions & 2 deletions app/models/server/models/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,12 @@ export class Messages extends Base {
}

if (!limit) {
return this.remove(query);
const count = this.remove(query);

// decrease message count
Rooms.decreaseMessageCountById(rid, count);

return count;
}

const messagesToDelete = this.find(query, {
Expand All @@ -925,11 +930,16 @@ export class Messages extends Base {
limit,
}).map(({ _id }) => _id);

return this.remove({
const count = this.remove({
_id: {
$in: messagesToDelete,
},
});

// decrease message count
Rooms.decreaseMessageCountById(rid, count);

return count;
}

removeByUserId(userId) {
Expand Down
7 changes: 5 additions & 2 deletions app/models/server/models/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,7 @@ export class Rooms extends Base {
return this.update(query, update);
}

incMsgCountById(_id, inc) {
if (inc == null) { inc = 1; }
incMsgCountById(_id, inc = 1) {
const query = { _id };

const update = {
Expand Down Expand Up @@ -648,6 +647,10 @@ export class Rooms extends Base {
return this.update(query, update);
}

decreaseMessageCountById(_id, count = 1) {
return this.incMsgCountById(_id, -count);
}

incUsersCountById(_id, inc = 1) {
const query = { _id };

Expand Down
2 changes: 1 addition & 1 deletion app/statistics/server/lib/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ export const statistics = {
statistics.livechatEnabled = settings.get('Livechat_enabled');

// Message statistics
statistics.totalMessages = Messages.find().count();
statistics.totalChannelMessages = _.reduce(Rooms.findByType('c', { fields: { msgs: 1 } }).fetch(), function _countChannelMessages(num, room) { return num + room.msgs; }, 0);
statistics.totalPrivateGroupMessages = _.reduce(Rooms.findByType('p', { fields: { msgs: 1 } }).fetch(), function _countPrivateGroupMessages(num, room) { return num + room.msgs; }, 0);
statistics.totalDirectMessages = _.reduce(Rooms.findByType('d', { fields: { msgs: 1 } }).fetch(), function _countDirectMessages(num, room) { return num + room.msgs; }, 0);
statistics.totalLivechatMessages = _.reduce(Rooms.findByType('l', { fields: { msgs: 1 } }).fetch(), function _countLivechatMessages(num, room) { return num + room.msgs; }, 0);
statistics.totalMessages = statistics.totalChannelMessages + statistics.totalPrivateGroupMessages + statistics.totalDirectMessages + statistics.totalLivechatMessages;

// Federation statistics
const federationOverviewData = federationGetStatistics();
Expand Down