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] Message preview not available for queued chats #25092

Merged
merged 14 commits into from
Apr 22, 2022
26 changes: 13 additions & 13 deletions apps/meteor/app/apps/server/converters/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,24 @@ export class AppMessagesConverter {

const newMessage = {
_id: message.id || Random.id(),
tmid: message.threadId,
...('threadId' in message && { tmid: message.threadId }),
rid: room._id,
u,
msg: message.text,
ts: message.createdAt || new Date(),
_updatedAt: message.updatedAt || new Date(),
editedBy,
editedAt: message.editedAt,
emoji: message.emoji,
avatar: message.avatarUrl,
alias: message.alias,
customFields: message.customFields,
groupable: message.groupable,
attachments,
reactions: message.reactions,
parseUrls: message.parseUrls,
blocks: message.blocks,
token: message.token,
...(editedBy && { editedBy }),
...('editedAt' in message && { editedAt: message.editedAt }),
...('emoji' in message && { emoji: message.emoji }),
...('avatarUrl' in message && { avatar: message.avatarUrl }),
...('alias' in message && { alias: message.alias }),
...('customFields' in message && { customFields: message.customFields }),
...('groupable' in message && { groupable: message.groupable }),
...(attachments && { attachments }),
...('reactions' in message && { reactions: message.reactions }),
...('parseUrls' in message && { parseUrls: message.parseUrls }),
...('blocks' in message && { blocks: message.blocks }),
...('token' in message && { token: message.token }),
};

return Object.assign(newMessage, message._unmappedProperties_);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { isOmnichannelRoom, isEditedMessage } from '@rocket.chat/core-typings';

import { callbacks } from '../../../../lib/callbacks';
import { LivechatInquiry } from '../../../models/server/raw';
import { settings } from '../../../settings/server';
import { RoutingManager } from '../lib/RoutingManager';

callbacks.add(
'afterSaveMessage',
async (message, room) => {
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
if (!isOmnichannelRoom(room)) {
return message;
}

// skip callback if message was edited
if (isEditedMessage(message)) {
return message;
}

if (!RoutingManager.getConfig().showQueue) {
// since last message is only getting used on UI as preview message when queue is enabled
return message;
}

if (!settings.get('Store_Last_Message')) {
return message;
}

await LivechatInquiry.setLastMessageByRoomId(room._id, message);

return message;
},
callbacks.priority.LOW,
'save-last-message-to-inquiry',
);
1 change: 1 addition & 0 deletions apps/meteor/app/livechat/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import './hooks/saveLastVisitorMessageTs';
import './hooks/markRoomNotResponded';
import './hooks/sendTranscriptOnClose';
import './hooks/saveContactLastChat';
import './hooks/saveLastMessageToInquiry';
import './methods/addAgent';
import './methods/addManager';
import './methods/changeLivechatStatus';
Expand Down
8 changes: 6 additions & 2 deletions apps/meteor/app/models/server/raw/LivechatInquiry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FindOneOptions, MongoDistinctPreferences } from 'mongodb';
import { ILivechatInquiryRecord, LivechatInquiryStatus } from '@rocket.chat/core-typings';
import { FindOneOptions, MongoDistinctPreferences, UpdateWriteOpResult } from 'mongodb';
import { IMessage, ILivechatInquiryRecord, LivechatInquiryStatus } from '@rocket.chat/core-typings';

import { BaseRaw } from './BaseRaw';

Expand Down Expand Up @@ -30,4 +30,8 @@ export class LivechatInquiryRaw extends BaseRaw<ILivechatInquiryRecord> {
const updated = await this.findOneAndUpdate({ _id: inquiryId }, { $set: { department } }, { returnDocument: 'after' });
return updated.value;
}

async setLastMessageByRoomId(rid: string, message: IMessage): Promise<UpdateWriteOpResult> {
return this.updateOne({ rid }, { $set: { lastMessage: message } });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ const propsAreEqual = (prevProps, nextProps) => {
if (prevProps.room._updatedAt?.toISOString() !== nextProps.room._updatedAt?.toISOString()) {
return false;
}
if (prevProps.room.lastMessage?._updatedAt?.toISOString() !== nextProps.room.lastMessage?._updatedAt?.toISOString()) {
if (
typeof prevProps.lastMessage?._updatedAt === 'object' &&
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
typeof nextProps.lastMessage?._updatedAt === 'object' &&
prevProps.lastMessage?._updatedAt?.toISOString() !== nextProps.lastMessage?._updatedAt?.toISOString()
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
) {
return false;
}
if (prevProps.room.alert !== nextProps.room.alert) {
Expand Down
48 changes: 25 additions & 23 deletions packages/core-typings/src/IInquiry.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
import type { IRocketChatRecord } from "./IRocketChatRecord";
import type { IMessage } from './IMessage';
import type { IRocketChatRecord } from './IRocketChatRecord';

export interface IInquiry {
_id: string;
_updatedAt?: Date;
department?: string;
_id: string;
_updatedAt?: Date;
department?: string;
}

export enum LivechatInquiryStatus {
QUEUED = "queued",
TAKEN = "taken",
READY = "ready",
QUEUED = 'queued',
TAKEN = 'taken',
READY = 'ready',
}

export interface IVisitor {
_id: string;
username: string;
token: string;
status: string;
_id: string;
username: string;
token: string;
status: string;
}

export interface ILivechatInquiryRecord extends IRocketChatRecord {
rid: string;
name: string;
ts: Date;
message: string;
status: LivechatInquiryStatus;
v: IVisitor;
t: "l";
queueOrder: number;
estimatedWaitingTimeQueue: number;
estimatedServiceTimeAt: string;
department: string;
estimatedInactivityCloseTimeAt: Date;
rid: string;
name: string;
ts: Date;
message: string;
status: LivechatInquiryStatus;
v: IVisitor;
t: 'l';
queueOrder: number;
estimatedWaitingTimeQueue: number;
estimatedServiceTimeAt: string;
department: string;
estimatedInactivityCloseTimeAt: Date;
lastMessage?: IMessage & { token?: string };
}