chore: Refactor audio notification helper (#6148)

This commit is contained in:
Pranav Raj S
2022-12-29 20:01:14 -08:00
committed by GitHub
parent 11f6e6bc61
commit cd3b6ebf28
10 changed files with 131 additions and 273 deletions

View File

@@ -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',
});
};