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: Prevent app's bridges from overriding lastMsg prop on rooms col #29756

Merged
merged 9 commits into from
Jul 10, 2023
5 changes: 5 additions & 0 deletions .changeset/dirty-crabs-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

fix: Prevent app's bridges from overriding the lastMsg prop which further was affecting Omni-Visitor abandonment feature for app
19 changes: 18 additions & 1 deletion apps/meteor/app/apps/server/converters/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ export class AppRoomsConverter {
let v;
if (room.visitor) {
const visitor = await LivechatVisitors.findOneById(room.visitor.id);

const lastMessageTs = room?.visitor?.lastMessageTs;
const phone = room?.visitor?.channelPhone;
v = {
_id: visitor._id,
username: visitor.username,
token: visitor.token,
status: visitor.status || 'online',
...(lastMessageTs && { lastMessageTs }),
...(phone && { phone }),
};
}

Expand Down Expand Up @@ -172,9 +177,21 @@ export class AppRoomsConverter {
return undefined;
}

const { lastMessageTs, phone } = v;

delete room.v;

return this.orch.getConverters().get('visitors').convertById(v._id);
return {
...(await this.orch.getConverters().get('visitors').convertById(v._id)),
// Note: room.v is not just visitor, it also contains channel related visitor data
// so we need to pass this data to the converter
// So suppose you have a contact whom we're contacting using SMS via 2 phone no's,
// let's call X and Y. Then if the contact sends a message using X phone number,
// then room.v.phoneNo would be X and correspondingly we'll store the timestamp of
// the last message from this visitor from X phone no on room.v.lastMessageTs
...(phone && { channelPhone: phone }),
...(lastMessageTs && { lastMessageTs }),
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
};
},
department: async (room) => {
const { departmentId } = room;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ callbacks.add(
if (!(isOmnichannelRoom(room) && room.v.token)) {
return message;
}
if (message.t) {
return message;
}
if (message.token) {
await LivechatRooms.setVisitorLastMessageTimestampByRoomId(room._id, message.ts);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core-typings/src/IInquiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export enum LivechatInquiryStatus {
OPEN = 'open',
}

// This is a subset of the IVisitor interface + channel related fields
// IMPORTANT: If you're adding a new field here, make sure to update the
// apps-engine's room converter to include it too
export interface IVisitor {
_id: string;
username: string;
Expand Down
Loading