Skip to content

Commit

Permalink
fix: Added check if count is truthy on cleanRoomHistory (#28081)
Browse files Browse the repository at this point in the history
Co-authored-by: Matheus Barbosa Silva <36537004+matheusbsilva137@users.noreply.github.com>
  • Loading branch information
LucianoPierdona and matheusbsilva137 authored May 25, 2023
1 parent c75272b commit 3f58495
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-clouds-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

chore: update room on `cleanRoomHistory` only if any message has been deleted
12 changes: 8 additions & 4 deletions apps/meteor/server/models/raw/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,10 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
if (!limit) {
const count = (await this.deleteMany(query)).deletedCount;

// decrease message count
await Rooms.decreaseMessageCountById(rid, count);
if (count) {
// decrease message count
await Rooms.decreaseMessageCountById(rid, count);
}

return count;
}
Expand All @@ -1377,8 +1379,10 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
})
).deletedCount;

// decrease message count
await Rooms.decreaseMessageCountById(rid, count);
if (count) {
// decrease message count
await Rooms.decreaseMessageCountById(rid, count);
}

return count;
}
Expand Down
121 changes: 121 additions & 0 deletions apps/meteor/tests/end-to-end/api/24-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,127 @@ describe('Meteor.methods', function () {
});
});

describe('[@cleanRoomHistory]', () => {
let rid = false;

let channelName = false;

before('create room', (done) => {
channelName = `methods-test-channel-${Date.now()}`;
request
.post(api('groups.create'))
.set(credentials)
.send({
name: channelName,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.nested.property('group._id');
expect(res.body).to.have.nested.property('group.name', channelName);
expect(res.body).to.have.nested.property('group.t', 'p');
expect(res.body).to.have.nested.property('group.msgs', 0);
rid = res.body.group._id;
})
.end(done);
});

before('send sample message', (done) => {
request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
text: 'Sample message',
rid,
},
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});

before('send another sample message', (done) => {
request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
text: 'Second Sample message',
rid,
},
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});

it('should not change the _updatedAt value when nothing is changed on the room', async () => {
const roomBefore = await request.get(api('groups.info')).set(credentials).query({
roomId: rid,
});

await request
.post(api('rooms.cleanHistory'))
.set(credentials)
.send({
roomId: rid,
latest: '2016-12-09T13:42:25.304Z',
oldest: '2016-08-30T13:42:25.304Z',
excludePinned: false,
filesOnly: false,
ignoreThreads: false,
ignoreDiscussion: false,
})
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
expect(res.body).to.have.a.property('count', 0);
});

const roomAfter = await request.get(api('groups.info')).set(credentials).query({
roomId: rid,
});
expect(roomBefore.body.group._updatedAt).to.be.equal(roomAfter.body.group._updatedAt);
});

it('should change the _updatedAt value when room is cleaned', async () => {
const roomBefore = await request.get(api('groups.info')).set(credentials).query({
roomId: rid,
});

await request
.post(api('rooms.cleanHistory'))
.set(credentials)
.send({
roomId: rid,
latest: '9999-12-31T23:59:59.000Z',
oldest: '0001-01-01T00:00:00.000Z',
excludePinned: false,
filesOnly: false,
ignoreThreads: false,
ignoreDiscussion: false,
})
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
expect(res.body).to.have.a.property('count', 2);
});

const roomAfter = await request.get(api('groups.info')).set(credentials).query({
roomId: rid,
});
expect(roomBefore.body.group._updatedAt).to.not.be.equal(roomAfter.body.group._updatedAt);
});
});

describe('[@loadHistory]', () => {
let rid = false;
let postMessageDate = false;
Expand Down

0 comments on commit 3f58495

Please sign in to comment.