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: Deletion of 1st message in a thread causes notification to be up without showing why. #34165

Merged
merged 16 commits into from
Dec 20, 2024
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
5 changes: 5 additions & 0 deletions .changeset/ninety-bulldogs-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": minor
Gustrb marked this conversation as resolved.
Show resolved Hide resolved
---

Fixes an issue where removing the only thread message of a message would show as if there were unread messages.
Gustrb marked this conversation as resolved.
Show resolved Hide resolved
27 changes: 23 additions & 4 deletions apps/meteor/app/lib/server/functions/deleteMessage.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { AppEvents, Apps } from '@rocket.chat/apps';
import { api, Message } from '@rocket.chat/core-services';
import type { AtLeast, IMessage, IUser } from '@rocket.chat/core-typings';
import { Messages, Rooms, Uploads, Users, ReadReceipts } from '@rocket.chat/models';
import type { AtLeast, IMessage, IRoom, IUser } from '@rocket.chat/core-typings';
Gustrb marked this conversation as resolved.
Show resolved Hide resolved
import { Messages, Rooms, Uploads, Users, ReadReceipts, Subscriptions } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';

import { callbacks } from '../../../../lib/callbacks';
import { canDeleteMessageAsync } from '../../../authorization/server/functions/canDeleteMessage';
import { FileUpload } from '../../../file-upload/server';
import { settings } from '../../../settings/server';
import { notifyOnRoomChangedById, notifyOnMessageChange } from '../lib/notifyListener';
import { notifyOnRoomChangedById, notifyOnMessageChange, notifyOnSubscriptionChangedByRoomIdAndUserId } from '../lib/notifyListener';

export const deleteMessageValidatingPermission = async (message: AtLeast<IMessage, '_id'>, userId: IUser['_id']): Promise<void> => {
if (!message?._id) {
Expand Down Expand Up @@ -51,7 +51,7 @@ export async function deleteMessage(message: IMessage, user: IUser): Promise<voi
}

if (deletedMsg?.tmid) {
Gustrb marked this conversation as resolved.
Show resolved Hide resolved
await Messages.decreaseReplyCountById(deletedMsg.tmid, -1);
await deleteThreadMessage(deletedMsg as Required<Pick<IMessage, 'tmid'>>, user, room);
Gustrb marked this conversation as resolved.
Show resolved Hide resolved
}

const files = (message.files || [message.file]).filter(Boolean); // Keep compatibility with old messages
Expand Down Expand Up @@ -107,3 +107,22 @@ export async function deleteMessage(message: IMessage, user: IUser): Promise<voi
void bridges.getListenerBridge().messageEvent(AppEvents.IPostMessageDeleted, deletedMsg, user);
}
}

async function deleteThreadMessage(message: Required<Pick<IMessage, 'tmid'>>, user: IUser, room: IRoom | null): Promise<void> {
Gustrb marked this conversation as resolved.
Show resolved Hide resolved
await Messages.decreaseReplyCountById(message.tmid, -1);

if (room) {
const { modifiedCount } = await Subscriptions.removeUnreadThreadsByRoomId(room._id, [message.tmid]);
if (modifiedCount > 0) {
void notifyOnSubscriptionChangedByRoomIdAndUserId(room._id, user._id);
Gustrb marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Check if this was the last reply in the thread
const thread = await Messages.findOneById(message.tmid, { projection: { tcount: 1 } });
Gustrb marked this conversation as resolved.
Show resolved Hide resolved
if (thread?.tcount === 0) {
void notifyOnMessageChange({
id: message.tmid,
});
}
}
4 changes: 3 additions & 1 deletion apps/meteor/tests/data/chat.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export const sendSimpleMessage = ({
roomId,
text = 'test message',
tmid,
userCredentials = credentials,
}: {
roomId: IRoom['_id'];
text?: string;
tmid?: IMessage['_id'];
userCredentials?: Credentials;
}) => {
if (!roomId) {
throw new Error('"roomId" is required in "sendSimpleMessage" test helper');
Expand All @@ -28,7 +30,7 @@ export const sendSimpleMessage = ({
message.tmid = tmid;
}

return request.post(api('chat.sendMessage')).set(credentials).send({ message });
return request.post(api('chat.sendMessage')).set(userCredentials).send({ message });
};

export const sendMessage = ({
Expand Down
44 changes: 44 additions & 0 deletions apps/meteor/tests/end-to-end/api/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,50 @@ describe('[Chat]', () => {
})
.end(done);
});

describe('when deleting a thread message', () => {
let otherUser: TestUser<IUser>;
let otherUserCredentials: Credentials;
let parentThreadId: IMessage['_id'];

before(async () => {
otherUser = await createUser();
otherUserCredentials = await login(otherUser.username, password);
parentThreadId = (await sendSimpleMessage({ roomId: testChannel._id })).body.message._id;
});

after(() => Promise.all([deleteUser(otherUser), deleteMessage({ msgId: parentThreadId, roomId: testChannel._id })]));

it('should reset the unread counter if the message was removed', async () => {
Gustrb marked this conversation as resolved.
Show resolved Hide resolved
const { body } = await sendSimpleMessage({ roomId: testChannel._id, tmid: parentThreadId, userCredentials: otherUserCredentials });
const msgId = body.message._id;
await request
.post(api('chat.delete'))
.set(credentials)
.send({
roomId: testChannel._id,
msgId,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
});
await request
.get(api('subscriptions.getOne'))
.set(credentials)
.query({
roomId: testChannel._id,
})
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body.subscription).to.have.property('tunread');
expect(res.body.subscription.tunread).to.be.an('array');
expect(res.body.subscription.tunread).to.deep.equal([]);
});
});
Gustrb marked this conversation as resolved.
Show resolved Hide resolved
});
});

describe('/chat.search', () => {
Expand Down
Loading