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 4 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
13 changes: 13 additions & 0 deletions app/models/server/models/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,19 @@ export class Rooms extends Base {
return this.update(query, update);
}

decreaseMessageCountById(_id, count) {
if (count == null) { count = -1; }
const query = { _id };

const update = {
$inc: {
msgs: count,
},
};

return this.update(query, update);
}

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