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] Unread Message count not displayed for new messages in Omni-Rooms #27539

Merged
merged 10 commits into from
Jan 31, 2023
4 changes: 3 additions & 1 deletion apps/meteor/app/lib/server/lib/notifyUsersOnMessage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import moment from 'moment';
import { escapeRegExp } from '@rocket.chat/string-helpers';
import { Subscriptions as SubscriptionsRaw } from '@rocket.chat/models';
import { isOmnichannelRoom } from '@rocket.chat/core-typings';

import { Rooms, Subscriptions } from '../../../models/server';
import { settings } from '../../../settings/server';
Expand Down Expand Up @@ -106,7 +107,8 @@ export async function updateUsersSubscriptions(message, room) {
}

// this shouldn't run only if has group mentions because it will already exclude mentioned users from the query
if (!toAll && !toHere && unreadCount === 'all_messages') {
// and this should always run if it's a omnichannel room
if (!toAll && !toHere && (unreadCount === 'all_messages' || isOmnichannelRoom(room))) {
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
await SubscriptionsRaw.incUnreadForRoomIdExcludingUserIds(room._id, [...userIds, message.u._id]);
}
}
Expand Down
10 changes: 8 additions & 2 deletions apps/meteor/tests/data/livechat/department.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ new Promise((resolve, reject) => {
});
});

export const createDepartmentWithAnOnlineAgent = async (): Promise<{department: ILivechatDepartment, agent: IUser}> => {
export const createDepartmentWithAnOnlineAgent = async (): Promise<{department: ILivechatDepartment, agent: {
credentials: { 'X-Auth-Token': string; 'X-User-Id': string; };
user: IUser;
}}> => {
const agent: IUser = await createUser();
const createdUserCredentials = await login(agent.username, password);
await createAgent(agent.username);
Expand All @@ -69,7 +72,10 @@ export const createDepartmentWithAnOnlineAgent = async (): Promise<{department:

return {
department,
agent,
agent: {
credentials: createdUserCredentials,
user: agent,
}
};
};

Expand Down
15 changes: 15 additions & 0 deletions apps/meteor/tests/data/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ISubscription } from "@rocket.chat/core-typings";
import { api, credentials, request } from "./api-data";

export const getSubscriptionForRoom = async (roomId: string, overrideCredential?: { 'X-Auth-Token': string; 'X-User-Id': string; }): Promise<ISubscription> => {
const response = await request
.get(api('subscriptions.getOne'))
.set(overrideCredential || credentials)
.query({ roomId })
.expect('Content-Type', 'application/json')
.expect(200);

const { subscription } = response.body;

return subscription;
}
27 changes: 27 additions & 0 deletions apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { createDepartmentWithAnOnlineAgent } from '../../../data/livechat/depart
import { sleep } from '../../../data/livechat/utils';
import { IS_EE } from '../../../e2e/config/constants';
import { createCustomField } from '../../../data/livechat/custom-fields';
import { getSubscriptionForRoom } from '../../../data/subscriptions';

describe('LIVECHAT - rooms', function () {
this.retries(0);
Expand Down Expand Up @@ -1446,4 +1447,30 @@ describe('LIVECHAT - rooms', function () {
expect(response.body.filters.find((f: IOmnichannelRoom['source']) => f.type === 'api')).to.not.be.undefined;
});
});

describe('it should mark room as unread when a new message is sent', () => {
let room: IOmnichannelRoom;
let visitor: ILivechatVisitor;
let totalMessagesSent = 0;
let departmentWithAgent: Awaited<ReturnType<typeof createDepartmentWithAnOnlineAgent>>;

before(async () => {
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
await updateSetting('Livechat_Routing_Method', 'Auto_Selection');

departmentWithAgent = await createDepartmentWithAnOnlineAgent();
visitor = await createVisitor(departmentWithAgent.department._id);
room = await createLivechatRoom(visitor.token);

await sendMessage(room._id, 'message 1', visitor.token);
await sendMessage(room._id, 'message 2', visitor.token);

// 1st message is for the room creation, so we need to add 1 to the total messages sent
totalMessagesSent = 3;
});

it("room's subscription should have correct unread count", async () => {
const { unread } = await getSubscriptionForRoom(room._id, departmentWithAgent.agent.credentials);
expect(unread).to.equal(totalMessagesSent);
});
});
});