mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-27 00:23:56 +00:00
chore: Refactor audio notification helper (#6148)
This commit is contained in:
@@ -1,8 +1,3 @@
|
||||
import { MESSAGE_TYPE } from 'shared/constants/messages';
|
||||
import { IFrameHelper } from 'widget/helpers/utils';
|
||||
|
||||
import { showBadgeOnFavicon } from './faviconHelper';
|
||||
|
||||
export const initOnEvents = ['click', 'touchstart', 'keypress', 'keydown'];
|
||||
|
||||
export const getAudioContext = () => {
|
||||
@@ -15,19 +10,8 @@ export const getAudioContext = () => {
|
||||
return audioCtx;
|
||||
};
|
||||
|
||||
const getAlertTone = alertType => {
|
||||
if (alertType === 'dashboard') {
|
||||
const {
|
||||
notification_tone: tone,
|
||||
} = window.WOOT.$store.getters.getUISettings;
|
||||
return tone;
|
||||
}
|
||||
return 'ding';
|
||||
};
|
||||
|
||||
export const getAlertAudio = async (baseUrl = '', type = 'dashboard') => {
|
||||
export const getAlertAudio = async (baseUrl = '', requestContext) => {
|
||||
const audioCtx = getAudioContext();
|
||||
|
||||
const playSound = audioBuffer => {
|
||||
window.playAudioAlert = () => {
|
||||
if (audioCtx) {
|
||||
@@ -41,7 +25,7 @@ export const getAlertAudio = async (baseUrl = '', type = 'dashboard') => {
|
||||
};
|
||||
|
||||
if (audioCtx) {
|
||||
const alertTone = getAlertTone(type);
|
||||
const { type = 'dashboard', alertTone = 'ding' } = requestContext || {};
|
||||
const resourceUrl = `${baseUrl}/audio/${type}/${alertTone}.mp3`;
|
||||
const audioRequest = new Request(resourceUrl);
|
||||
|
||||
@@ -56,87 +40,3 @@ export const getAlertAudio = async (baseUrl = '', type = 'dashboard') => {
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const notificationEnabled = (enableAudioAlerts, id, userId) => {
|
||||
if (enableAudioAlerts === 'mine') {
|
||||
return userId === id;
|
||||
}
|
||||
if (enableAudioAlerts === 'all') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
export const shouldPlayAudio = (
|
||||
message,
|
||||
conversationId,
|
||||
userId,
|
||||
isDocHidden
|
||||
) => {
|
||||
const {
|
||||
conversation_id: incomingConvId,
|
||||
sender_id: senderId,
|
||||
message_type: messageType,
|
||||
private: isPrivate,
|
||||
} = message;
|
||||
if (!isDocHidden && messageType === MESSAGE_TYPE.INCOMING) {
|
||||
showBadgeOnFavicon();
|
||||
return false;
|
||||
}
|
||||
const isFromCurrentUser = userId === senderId;
|
||||
|
||||
const playAudio =
|
||||
!isFromCurrentUser && (messageType === MESSAGE_TYPE.INCOMING || isPrivate);
|
||||
if (isDocHidden) return playAudio;
|
||||
if (conversationId !== incomingConvId) return playAudio;
|
||||
return false;
|
||||
};
|
||||
|
||||
export const getAssigneeFromNotification = currentConv => {
|
||||
let id;
|
||||
if (currentConv.meta) {
|
||||
const assignee = currentConv.meta.assignee;
|
||||
if (assignee) {
|
||||
id = assignee.id;
|
||||
}
|
||||
}
|
||||
return id;
|
||||
};
|
||||
|
||||
export const newMessageNotification = data => {
|
||||
const { conversation_id: currentConvId } = window.WOOT.$route.params;
|
||||
const currentUserId = window.WOOT.$store.getters.getCurrentUserID;
|
||||
const { conversation_id: incomingConvId } = data;
|
||||
const currentConv =
|
||||
window.WOOT.$store.getters.getConversationById(incomingConvId) || {};
|
||||
const assigneeId = getAssigneeFromNotification(currentConv);
|
||||
|
||||
const {
|
||||
enable_audio_alerts: enableAudioAlerts = false,
|
||||
always_play_audio_alert: alwaysPlayAudioAlert,
|
||||
} = window.WOOT.$store.getters.getUISettings;
|
||||
const isDocHidden = alwaysPlayAudioAlert ? true : document.hidden;
|
||||
|
||||
const playAudio = shouldPlayAudio(
|
||||
data,
|
||||
currentConvId,
|
||||
currentUserId,
|
||||
isDocHidden
|
||||
);
|
||||
const isNotificationEnabled = notificationEnabled(
|
||||
enableAudioAlerts,
|
||||
currentUserId,
|
||||
assigneeId
|
||||
);
|
||||
|
||||
if (playAudio && isNotificationEnabled) {
|
||||
window.playAudioAlert();
|
||||
showBadgeOnFavicon();
|
||||
}
|
||||
};
|
||||
|
||||
export const playNewMessageNotificationInWidget = () => {
|
||||
IFrameHelper.sendMessage({
|
||||
event: 'playAudio',
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
export const showBadgeOnFavicon = () => {
|
||||
const favicons = document.querySelectorAll('.favicon');
|
||||
|
||||
favicons.forEach(favicon => {
|
||||
const newFileName = `/favicon-badge-${favicon.sizes[[0]]}.png`;
|
||||
favicon.href = newFileName;
|
||||
});
|
||||
};
|
||||
|
||||
export const initFaviconSwitcher = () => {
|
||||
const favicons = document.querySelectorAll('.favicon');
|
||||
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'visible') {
|
||||
favicons.forEach(favicon => {
|
||||
const oldFileName = `/favicon-${favicon.sizes[[0]]}.png`;
|
||||
favicon.href = oldFileName;
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -1,151 +0,0 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
|
||||
import {
|
||||
shouldPlayAudio,
|
||||
notificationEnabled,
|
||||
getAssigneeFromNotification,
|
||||
} from '../AudioNotificationHelper';
|
||||
|
||||
describe('shouldPlayAudio', () => {
|
||||
describe('Document active', () => {
|
||||
it('Retuns true if incoming message', () => {
|
||||
const message = {
|
||||
conversation_id: 10,
|
||||
sender_id: 5,
|
||||
message_type: 0,
|
||||
private: false,
|
||||
};
|
||||
const [conversationId, userId, isDocHiddden] = [1, 2, true];
|
||||
const result = shouldPlayAudio(
|
||||
message,
|
||||
conversationId,
|
||||
userId,
|
||||
isDocHiddden
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
it('Retuns false if outgoing message', () => {
|
||||
const message = {
|
||||
conversation_id: 10,
|
||||
sender_id: 5,
|
||||
message_type: 1,
|
||||
private: false,
|
||||
};
|
||||
const [conversationId, userId, isDocHiddden] = [1, 2, false];
|
||||
const result = shouldPlayAudio(
|
||||
message,
|
||||
conversationId,
|
||||
userId,
|
||||
isDocHiddden
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('Retuns false if from Same sender', () => {
|
||||
const message = {
|
||||
conversation_id: 1,
|
||||
sender_id: 2,
|
||||
message_type: 0,
|
||||
private: false,
|
||||
};
|
||||
const [conversationId, userId, isDocHiddden] = [1, 2, true];
|
||||
const result = shouldPlayAudio(
|
||||
message,
|
||||
conversationId,
|
||||
userId,
|
||||
isDocHiddden
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
it('Retuns true if private message from another agent', () => {
|
||||
const message = {
|
||||
conversation_id: 1,
|
||||
sender_id: 5,
|
||||
message_type: 1,
|
||||
private: true,
|
||||
};
|
||||
const [conversationId, userId, isDocHiddden] = [1, 2, true];
|
||||
const result = shouldPlayAudio(
|
||||
message,
|
||||
conversationId,
|
||||
userId,
|
||||
isDocHiddden
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
describe('Document inactive', () => {
|
||||
it('Retuns true if incoming message', () => {
|
||||
const message = {
|
||||
conversation_id: 1,
|
||||
sender_id: 5,
|
||||
message_type: 0,
|
||||
private: false,
|
||||
};
|
||||
const [conversationId, userId, isDocHiddden] = [1, 2, true];
|
||||
const result = shouldPlayAudio(
|
||||
message,
|
||||
conversationId,
|
||||
userId,
|
||||
isDocHiddden
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
it('Retuns false if outgoing message', () => {
|
||||
const message = {
|
||||
conversation_id: 1,
|
||||
sender_id: 5,
|
||||
message_type: 1,
|
||||
private: false,
|
||||
};
|
||||
const [conversationId, userId, isDocHiddden] = [1, 2, true];
|
||||
const result = shouldPlayAudio(
|
||||
message,
|
||||
conversationId,
|
||||
userId,
|
||||
isDocHiddden
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('notificationEnabled', () => {
|
||||
it('returns true if mine', () => {
|
||||
const [enableAudioAlerts, userId, id] = ['mine', 1, 1];
|
||||
const result = notificationEnabled(enableAudioAlerts, userId, id);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
it('returns true if all', () => {
|
||||
const [enableAudioAlerts, userId, id] = ['all', 1, 2];
|
||||
const result = notificationEnabled(enableAudioAlerts, userId, id);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
it('returns false if none', () => {
|
||||
const [enableAudioAlerts, userId, id] = ['none', 1, 2];
|
||||
const result = notificationEnabled(enableAudioAlerts, userId, id);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
describe('getAssigneeFromNotification', () => {
|
||||
it('Retuns true if gets notification from assignee', () => {
|
||||
const currentConv = {
|
||||
id: 1,
|
||||
accountId: 1,
|
||||
meta: {
|
||||
assignee: {
|
||||
id: 1,
|
||||
name: 'John',
|
||||
},
|
||||
},
|
||||
};
|
||||
const result = getAssigneeFromNotification(currentConv);
|
||||
expect(result).toBe(1);
|
||||
});
|
||||
it('Retuns true if gets notification from assignee is udefined', () => {
|
||||
const currentConv = {};
|
||||
const result = getAssigneeFromNotification(currentConv);
|
||||
expect(result).toBe(undefined);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user