-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add updater on afterOmnichannelSaveMessage (markRoomResponded)…
… hook (#32983) Co-authored-by: Guilherme Gazzo <[email protected]>
- Loading branch information
1 parent
c97b46e
commit 5388eef
Showing
6 changed files
with
118 additions
and
119 deletions.
There are no files selected for viewing
104 changes: 52 additions & 52 deletions
104
apps/meteor/app/livechat/server/hooks/markRoomResponded.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 29 additions & 36 deletions
65
apps/meteor/ee/app/livechat-enterprise/server/hooks/setPredictedVisitorAbandonmentTime.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,46 @@ | ||
import type { IOmnichannelRoom } from '@rocket.chat/core-typings'; | ||
import { isEditedMessage } from '@rocket.chat/core-typings'; | ||
import { LivechatRooms } from '@rocket.chat/models'; | ||
import type { IMessage } from '@rocket.chat/core-typings'; | ||
import { isEditedMessage, isMessageFromVisitor } from '@rocket.chat/core-typings'; | ||
import moment from 'moment'; | ||
|
||
import { markRoomResponded } from '../../../../../app/livechat/server/hooks/markRoomResponded'; | ||
import { settings } from '../../../../../app/settings/server'; | ||
import { callbacks } from '../../../../../lib/callbacks'; | ||
import { setPredictedVisitorAbandonmentTime } from '../lib/Helper'; | ||
|
||
function shouldSaveInactivity(message: IMessage): boolean { | ||
if (message.t || isEditedMessage(message) || isMessageFromVisitor(message)) { | ||
return false; | ||
} | ||
|
||
const abandonedRoomsAction = settings.get('Livechat_abandoned_rooms_action'); | ||
const visitorInactivityTimeout = settings.get<number>('Livechat_visitor_inactivity_timeout'); | ||
|
||
if (!abandonedRoomsAction || abandonedRoomsAction === 'none' || visitorInactivityTimeout <= 0) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
callbacks.remove('afterOmnichannelSaveMessage', 'markRoomResponded'); | ||
|
||
callbacks.add( | ||
'afterOmnichannelSaveMessage', | ||
async (message, { room }) => { | ||
if ( | ||
!settings.get('Livechat_abandoned_rooms_action') || | ||
settings.get('Livechat_abandoned_rooms_action') === 'none' || | ||
settings.get<number>('Livechat_visitor_inactivity_timeout') <= 0 | ||
) { | ||
return message; | ||
} | ||
// skips this callback if the message was edited | ||
if (isEditedMessage(message)) { | ||
return message; | ||
} | ||
// if the message has a type means it is a special message (like the closing comment), so skip it | ||
if (message.t) { | ||
return message; | ||
} | ||
// message from visitor | ||
if (message.token) { | ||
return message; | ||
} | ||
|
||
const latestRoom = await LivechatRooms.findOneById<Pick<IOmnichannelRoom, '_id' | 'responseBy' | 'departmentId'>>(room._id, { | ||
projection: { | ||
_id: 1, | ||
responseBy: 1, | ||
departmentId: 1, | ||
}, | ||
}); | ||
async (message, { room, roomUpdater }) => { | ||
const responseBy = await markRoomResponded(message, room, roomUpdater); | ||
|
||
if (!latestRoom?.responseBy) { | ||
if (!shouldSaveInactivity(message)) { | ||
return message; | ||
} | ||
|
||
if (moment(latestRoom.responseBy.firstResponseTs).isSame(moment(message.ts))) { | ||
await setPredictedVisitorAbandonmentTime(latestRoom); | ||
if (!responseBy) { | ||
return; | ||
} | ||
|
||
return message; | ||
if (moment(responseBy.firstResponseTs).isSame(moment(message.ts))) { | ||
await setPredictedVisitorAbandonmentTime({ ...room, responseBy }, roomUpdater); | ||
} | ||
}, | ||
callbacks.priority.MEDIUM, | ||
'save-visitor-inactivity', | ||
); // This hook priority should always be less than the priority of hook "markRoomResponded" bcs, the room.responseBy.firstMessage property set there is being used here for determining visitor abandonment | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters