chore: Add chatwoot:on-message event (#6425)
This commit is contained in:
@@ -23,7 +23,11 @@ import {
|
|||||||
} from './bubbleHelpers';
|
} from './bubbleHelpers';
|
||||||
import { isWidgetColorLighter } from 'shared/helpers/colorHelper';
|
import { isWidgetColorLighter } from 'shared/helpers/colorHelper';
|
||||||
import { dispatchWindowEvent } from 'shared/helpers/CustomEventHelper';
|
import { dispatchWindowEvent } from 'shared/helpers/CustomEventHelper';
|
||||||
import { CHATWOOT_ERROR, CHATWOOT_READY } from '../widget/constants/sdkEvents';
|
import {
|
||||||
|
CHATWOOT_ERROR,
|
||||||
|
CHATWOOT_ON_MESSAGE,
|
||||||
|
CHATWOOT_READY,
|
||||||
|
} from '../widget/constants/sdkEvents';
|
||||||
import { SET_USER_ERROR } from '../widget/constants/errorTypes';
|
import { SET_USER_ERROR } from '../widget/constants/errorTypes';
|
||||||
import { getUserCookieName } from './cookieHelpers';
|
import { getUserCookieName } from './cookieHelpers';
|
||||||
import {
|
import {
|
||||||
@@ -177,7 +181,9 @@ export const IFrameHelper = {
|
|||||||
Cookies.remove(getUserCookieName());
|
Cookies.remove(getUserCookieName());
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
onMessage({ data }) {
|
||||||
|
dispatchWindowEvent({ eventName: CHATWOOT_ON_MESSAGE, data });
|
||||||
|
},
|
||||||
setBubbleLabel(message) {
|
setBubbleLabel(message) {
|
||||||
setBubbleText(window.$chatwoot.launcherTitle || message.label);
|
setBubbleText(window.$chatwoot.launcherTitle || message.label);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
export const CHATWOOT_ERROR = 'chatwoot:error';
|
export const CHATWOOT_ERROR = 'chatwoot:error';
|
||||||
|
export const CHATWOOT_ON_MESSAGE = 'chatwoot:on-message';
|
||||||
export const CHATWOOT_READY = 'chatwoot:ready';
|
export const CHATWOOT_READY = 'chatwoot:ready';
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
import BaseActionCableConnector from '../../shared/helpers/BaseActionCableConnector';
|
import BaseActionCableConnector from '../../shared/helpers/BaseActionCableConnector';
|
||||||
import { playNewMessageNotificationInWidget } from 'widget/helpers/WidgetAudioNotificationHelper';
|
import { playNewMessageNotificationInWidget } from 'widget/helpers/WidgetAudioNotificationHelper';
|
||||||
import { ON_AGENT_MESSAGE_RECEIVED } from '../constants/widgetBusEvents';
|
import { ON_AGENT_MESSAGE_RECEIVED } from '../constants/widgetBusEvents';
|
||||||
|
import { IFrameHelper } from 'widget/helpers/utils';
|
||||||
|
|
||||||
|
const isMessageInActiveConversation = (getters, message) => {
|
||||||
|
const { conversation_id: conversationId } = message;
|
||||||
|
const activeConversationId =
|
||||||
|
getters['conversationAttributes/getConversationParams'].id;
|
||||||
|
return activeConversationId && conversationId !== activeConversationId;
|
||||||
|
};
|
||||||
|
|
||||||
class ActionCableConnector extends BaseActionCableConnector {
|
class ActionCableConnector extends BaseActionCableConnector {
|
||||||
constructor(app, pubsubToken) {
|
constructor(app, pubsubToken) {
|
||||||
@@ -25,15 +33,24 @@ class ActionCableConnector extends BaseActionCableConnector {
|
|||||||
};
|
};
|
||||||
|
|
||||||
onMessageCreated = data => {
|
onMessageCreated = data => {
|
||||||
|
if (isMessageInActiveConversation(this.app.$store.getters, data)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.app.$store
|
this.app.$store
|
||||||
.dispatch('conversation/addOrUpdateMessage', data)
|
.dispatch('conversation/addOrUpdateMessage', data)
|
||||||
.then(() => window.bus.$emit(ON_AGENT_MESSAGE_RECEIVED));
|
.then(() => window.bus.$emit(ON_AGENT_MESSAGE_RECEIVED));
|
||||||
|
|
||||||
|
IFrameHelper.sendMessage({ event: 'onMessage', data });
|
||||||
if (data.sender_type === 'User') {
|
if (data.sender_type === 'User') {
|
||||||
playNewMessageNotificationInWidget();
|
playNewMessageNotificationInWidget();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
onMessageUpdated = data => {
|
onMessageUpdated = data => {
|
||||||
|
if (isMessageInActiveConversation(this.app.$store.getters, data)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.app.$store.dispatch('conversation/addOrUpdateMessage', data);
|
this.app.$store.dispatch('conversation/addOrUpdateMessage', data);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -51,7 +68,13 @@ class ActionCableConnector extends BaseActionCableConnector {
|
|||||||
};
|
};
|
||||||
|
|
||||||
onTypingOn = data => {
|
onTypingOn = data => {
|
||||||
if (data.is_private) {
|
const activeConversationId = this.app.$store.getters[
|
||||||
|
'conversationAttributes/getConversationParams'
|
||||||
|
].id;
|
||||||
|
const isUserTypingOnAnotherConversation =
|
||||||
|
data.conversation && data.conversation.id !== activeConversationId;
|
||||||
|
|
||||||
|
if (isUserTypingOnAnotherConversation || data.is_private) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.clearTimer();
|
this.clearTimer();
|
||||||
|
|||||||
@@ -53,4 +53,9 @@ window.addEventListener('chatwoot:ready', function() {
|
|||||||
window.addEventListener('chatwoot:error', function(e) {
|
window.addEventListener('chatwoot:error', function(e) {
|
||||||
console.log('chatwoot:error', e.detail)
|
console.log('chatwoot:error', e.detail)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
window.addEventListener('chatwoot:on-message', function(e) {
|
||||||
|
console.log('chatwoot:on-message', e.detail)
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user