Skip to content

Commit

Permalink
fix: Pruning messages in a room results in an incorrect message count…
Browse files Browse the repository at this point in the history
…er (#29781)
  • Loading branch information
matheusbsilva137 committed Aug 21, 2023
1 parent ce207de commit 9c957b9
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-adults-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fix pruning messages in a room results in an incorrect message counter
52 changes: 36 additions & 16 deletions apps/meteor/server/models/raw/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1376,8 +1376,23 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
query.tcount = { $exists: false };
}

const notCountedMessages = (
await this.find(
{
...query,
$or: [{ _hidden: true }, { editedAt: { $exists: true }, editedBy: { $exists: true }, t: 'rm' }],
},
{
projection: {
_id: 1,
},
limit,
},
).toArray()
).length;

if (!limit) {
const count = (await this.deleteMany(query)).deletedCount;
const count = (await this.deleteMany(query)).deletedCount - notCountedMessages;

if (count) {
// decrease message count
Expand All @@ -1387,22 +1402,27 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
return count;
}

const messagesToDelete = (
await this.find(query, {
projection: {
_id: 1,
},
limit,
}).toArray()
).map(({ _id }) => _id);
const messagesToDelete = await this.find(query, {
projection: {
_id: 1,
_hidden: 1,
t: 1,
editedAt: 1,
editedBy: 1,
},
limit,
}).toArray();

const count = (
await this.deleteMany({
_id: {
$in: messagesToDelete,
},
})
).deletedCount;
const messagesIdsToDelete = messagesToDelete.map(({ _id }) => _id);

const count =
(
await this.deleteMany({
_id: {
$in: messagesIdsToDelete,
},
})
).deletedCount - notCountedMessages;

if (count) {
// decrease message count
Expand Down
45 changes: 44 additions & 1 deletion apps/meteor/tests/end-to-end/api/09-rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect } from 'chai';
import { after, afterEach, before, beforeEach, describe, it } from 'mocha';

import { getCredentials, api, request, credentials } from '../../data/api-data.js';
import { sendSimpleMessage } from '../../data/chat.helper';
import { sendSimpleMessage, deleteMessage } from '../../data/chat.helper';
import { imgURL } from '../../data/interactions.js';
import { updateEEPermission, updatePermission, updateSetting } from '../../data/permissions.helper';
import { closeRoom, createRoom } from '../../data/rooms.helper';
Expand Down Expand Up @@ -383,6 +383,12 @@ describe('[Rooms]', function () {
.end(done);
user = undefined;
});
before(async () => {
await updateSetting('Message_ShowDeletedStatus', true);
});
after(async () => {
await updateSetting('Message_ShowDeletedStatus', false);
});
it('create a public channel', (done) => {
createRoom({ type: 'c', name: `testeChannel${+new Date()}` }).end((err, res) => {
publicChannel = res.body.channel;
Expand Down Expand Up @@ -417,6 +423,43 @@ describe('[Rooms]', function () {
})
.end(done);
});
it('should not count hidden or deleted messages when limit param is not sent', async () => {
const res = await sendSimpleMessage({ roomId: publicChannel._id });
await deleteMessage({ roomId: publicChannel._id, msgId: res.body.message._id });
await request
.post(api('rooms.cleanHistory'))
.set(credentials)
.send({
roomId: publicChannel._id,
latest: '9999-12-31T23:59:59.000Z',
oldest: '0001-01-01T00:00:00.000Z',
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('count', 0);
});
});
it('should not count hidden or deleted messages when limit param is sent', async () => {
const res = await sendSimpleMessage({ roomId: publicChannel._id });
await deleteMessage({ roomId: publicChannel._id, msgId: res.body.message._id });
await request
.post(api('rooms.cleanHistory'))
.set(credentials)
.send({
roomId: publicChannel._id,
latest: '9999-12-31T23:59:59.000Z',
oldest: '0001-01-01T00:00:00.000Z',
limit: 2000,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('count', 0);
});
});
it('should successfully delete an image and thumbnail from public channel', (done) => {
request
.post(api(`rooms.upload/${publicChannel._id}`))
Expand Down

0 comments on commit 9c957b9

Please sign in to comment.