fix: `im.counters` returns `null` for unread msgs for user who never opened the DM (#34109)

pull/34038/head^2
Kevin Aleman 1 year ago committed by GitHub
parent 072a749470
commit 211c10dfa4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      .changeset/proud-cups-share.md
  2. 4
      apps/meteor/app/api/server/v1/im.ts
  3. 131
      apps/meteor/tests/end-to-end/api/direct-message.ts

@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---
Fixes `im.counters` endpoint returning `null` on `unread` messages property for users that have never opened the queried DM

@ -195,9 +195,9 @@ API.v1.addRoute(
lm = room?.lm ? new Date(room.lm).toISOString() : new Date(room._updatedAt).toISOString(); // lm is the last message timestamp
if (subscription?.open) {
if (subscription) {
unreads = subscription.unread ?? null;
if (subscription.ls && room.msgs) {
unreads = subscription.unread;
unreadsFrom = new Date(subscription.ls).toISOString(); // last read timestamp
}
userMentions = subscription.userMentions;

@ -343,26 +343,117 @@ describe('[Direct Messages]', () => {
.end(done);
});
it('/im.counters', (done) => {
void request
.get(api('im.counters'))
.set(credentials)
.query({
roomId: directMessage._id,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('joined', true);
expect(res.body).to.have.property('members');
expect(res.body).to.have.property('unreads');
expect(res.body).to.have.property('unreadsFrom');
expect(res.body).to.have.property('msgs');
expect(res.body).to.have.property('latest');
expect(res.body).to.have.property('userMentions');
})
.end(done);
describe('/im.counters', () => {
it('should require auth', async () => {
await request
.get(api('im.counters'))
.expect('Content-Type', 'application/json')
.expect(401)
.expect((res) => {
expect(res.body).to.have.property('status', 'error');
});
});
it('should require a roomId', async () => {
await request
.get(api('im.counters'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
});
});
it('should work with all params right', (done) => {
void request
.get(api('im.counters'))
.set(credentials)
.query({
roomId: directMessage._id,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('joined', true);
expect(res.body).to.have.property('members');
expect(res.body).to.have.property('unreads');
expect(res.body).to.have.property('unreadsFrom');
expect(res.body).to.have.property('msgs');
expect(res.body).to.have.property('latest');
expect(res.body).to.have.property('userMentions');
})
.end(done);
});
describe('with valid room id', () => {
let testDM: IRoom & { rid: IRoom['_id'] };
let user2: TestUser<IUser>;
let userCreds: Credentials;
before(async () => {
user2 = await createUser();
userCreds = await login(user2.username, password);
await request
.post(api('im.create'))
.set(credentials)
.send({
username: user2.username,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
testDM = res.body.room;
});
await request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
text: 'Sample message',
rid: testDM._id,
},
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
});
});
after(async () => {
await request
.post(api('im.delete'))
.set(credentials)
.send({
roomId: testDM._id,
})
.expect(200);
await deleteUser(user2);
});
it('should properly return counters before opening the dm', async () => {
await request
.get(api('im.counters'))
.set(userCreds)
.query({
roomId: testDM._id,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('joined', true);
expect(res.body).to.have.property('members').and.to.be.a('number').and.to.be.eq(2);
expect(res.body).to.have.property('unreads').and.to.be.a('number').and.to.be.eq(1);
expect(res.body).to.have.property('unreadsFrom');
expect(res.body).to.have.property('msgs').and.to.be.a('number').and.to.be.eq(1);
expect(res.body).to.have.property('latest');
expect(res.body).to.have.property('userMentions').and.to.be.a('number').and.to.be.eq(0);
});
});
});
});
describe('[/im.files]', async () => {

Loading…
Cancel
Save