[FIX] Message update not working in some cases (#22856)

* Fix message update not working

* resolve room object only if required + only add canned-responses-replace-placeholders when the canned-response feature is on

Co-authored-by: Kevin Aleman <kevin.aleman@rocket.chat>
pull/22839/head^2
Murtaza Patrawala 4 years ago committed by GitHub
parent 5dc6eafecc
commit bb4dbfc4b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      ee/app/canned-responses/server/hooks/onMessageSentParsePlaceholder.ts

@ -1,7 +1,8 @@
import get from 'lodash.get';
import { settings } from '../../../../../app/settings/server';
import { callbacks } from '../../../../../app/callbacks/server';
import { Users, LivechatVisitors } from '../../../../../app/models/server';
import { Users, LivechatVisitors, Rooms } from '../../../../../app/models/server';
import { IMessage } from '../../../../../definition/IMessage';
import { IOmnichannelRoom, isOmnichannelRoom } from '../../../../../definition/IRoom';
@ -28,7 +29,12 @@ const placeholderFields = {
},
};
callbacks.add('beforeSaveMessage', (message: IMessage, room: IOmnichannelRoom): any => {
const handleBeforeSaveMessage = (message: IMessage, room: IOmnichannelRoom): any => {
if (!message.msg || message.msg === '') {
return message;
}
room = room?._id ? room : Rooms.findOneById(message.rid);
if (!isOmnichannelRoom(room)) {
return message;
}
@ -51,4 +57,13 @@ callbacks.add('beforeSaveMessage', (message: IMessage, room: IOmnichannelRoom):
message.msg = messageText;
return message;
}, callbacks.priority.LOW, 'canned-responses-replace-placeholders');
};
settings.get('Canned_Responses_Enable', function(_, value) {
if (!value) {
callbacks.remove('beforeSaveMessage', 'canned-responses-replace-placeholders');
return;
}
callbacks.add('beforeSaveMessage', handleBeforeSaveMessage, callbacks.priority.MEDIUM, 'canned-responses-replace-placeholders');
});

Loading…
Cancel
Save