2021-03-04 13:54:03 +00:00
|
|
|
import { MESSAGE_TYPE } from 'shared/constants/messages';
|
2022-02-28 16:13:24 +00:00
|
|
|
import { IFrameHelper } from 'widget/helpers/utils';
|
|
|
|
|
2021-04-13 13:55:19 +00:00
|
|
|
import { showBadgeOnFavicon } from './faviconHelper';
|
2020-02-29 05:57:02 +00:00
|
|
|
|
2022-04-07 04:53:18 +00:00
|
|
|
export const initOnEvents = ['click', 'touchstart', 'keypress', 'keydown'];
|
2022-10-21 03:05:17 +00:00
|
|
|
|
|
|
|
export const getAudioContext = () => {
|
|
|
|
let audioCtx;
|
|
|
|
try {
|
|
|
|
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
|
|
} catch {
|
|
|
|
// AudioContext is not available.
|
|
|
|
}
|
|
|
|
return audioCtx;
|
|
|
|
};
|
|
|
|
|
2022-05-06 14:49:36 +00:00
|
|
|
export const getAlertAudio = async (baseUrl = '', type = 'dashboard') => {
|
2022-10-21 02:13:20 +00:00
|
|
|
const audioCtx = getAudioContext();
|
|
|
|
|
2021-03-04 13:54:03 +00:00
|
|
|
const playsound = audioBuffer => {
|
|
|
|
window.playAudioAlert = () => {
|
2022-10-21 02:13:20 +00:00
|
|
|
if (audioCtx) {
|
|
|
|
const source = audioCtx.createBufferSource();
|
|
|
|
source.buffer = audioBuffer;
|
|
|
|
source.connect(audioCtx.destination);
|
|
|
|
source.loop = false;
|
|
|
|
source.start();
|
|
|
|
}
|
2021-03-04 13:54:03 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-21 02:13:20 +00:00
|
|
|
if (audioCtx) {
|
|
|
|
const resourceUrl = `${baseUrl}/audio/${type}/ding.mp3`;
|
|
|
|
const audioRequest = new Request(resourceUrl);
|
|
|
|
|
|
|
|
fetch(audioRequest)
|
|
|
|
.then(response => response.arrayBuffer())
|
|
|
|
.then(buffer => {
|
|
|
|
audioCtx.decodeAudioData(buffer).then(playsound);
|
|
|
|
return new Promise(res => res());
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
// error
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-05 06:31:54 +00:00
|
|
|
export const notificationEnabled = (enableAudioAlerts, id, userId) => {
|
|
|
|
if (enableAudioAlerts === 'mine') {
|
|
|
|
return userId === id;
|
|
|
|
}
|
|
|
|
if (enableAudioAlerts === 'all') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-03-08 07:43:13 +00:00
|
|
|
export const shouldPlayAudio = (
|
|
|
|
message,
|
|
|
|
conversationId,
|
|
|
|
userId,
|
2021-07-05 06:31:54 +00:00
|
|
|
isDocHidden
|
2021-03-08 07:43:13 +00:00
|
|
|
) => {
|
2021-03-04 13:54:03 +00:00
|
|
|
const {
|
|
|
|
conversation_id: incomingConvId,
|
|
|
|
sender_id: senderId,
|
|
|
|
message_type: messageType,
|
2021-03-08 07:43:13 +00:00
|
|
|
private: isPrivate,
|
|
|
|
} = message;
|
2022-04-07 04:53:18 +00:00
|
|
|
if (!isDocHidden && messageType === MESSAGE_TYPE.INCOMING) {
|
|
|
|
showBadgeOnFavicon();
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-08 07:43:13 +00:00
|
|
|
const isFromCurrentUser = userId === senderId;
|
2021-03-04 13:54:03 +00:00
|
|
|
|
|
|
|
const playAudio =
|
2021-03-08 07:43:13 +00:00
|
|
|
!isFromCurrentUser && (messageType === MESSAGE_TYPE.INCOMING || isPrivate);
|
2021-07-05 06:31:54 +00:00
|
|
|
if (isDocHidden) return playAudio;
|
2021-03-08 07:43:13 +00:00
|
|
|
if (conversationId !== incomingConvId) return playAudio;
|
|
|
|
return false;
|
2021-03-04 13:54:03 +00:00
|
|
|
};
|
|
|
|
|
2021-07-05 06:31:54 +00:00
|
|
|
export const getAssigneeFromNotification = currentConv => {
|
|
|
|
let id;
|
|
|
|
if (currentConv.meta) {
|
|
|
|
const assignee = currentConv.meta.assignee;
|
|
|
|
if (assignee) {
|
|
|
|
id = assignee.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
};
|
2021-03-04 13:54:03 +00:00
|
|
|
export const newMessageNotification = data => {
|
2021-03-08 07:43:13 +00:00
|
|
|
const { conversation_id: currentConvId } = window.WOOT.$route.params;
|
|
|
|
const currentUserId = window.WOOT.$store.getters.getCurrentUserID;
|
2021-07-05 06:31:54 +00:00
|
|
|
const { conversation_id: incomingConvId } = data;
|
|
|
|
const currentConv =
|
|
|
|
window.WOOT.$store.getters.getConversationById(incomingConvId) || {};
|
|
|
|
const assigneeId = getAssigneeFromNotification(currentConv);
|
|
|
|
const isDocHidden = document.hidden;
|
2021-03-04 13:54:03 +00:00
|
|
|
const {
|
|
|
|
enable_audio_alerts: enableAudioAlerts = false,
|
|
|
|
} = window.WOOT.$store.getters.getUISettings;
|
2021-03-08 07:43:13 +00:00
|
|
|
const playAudio = shouldPlayAudio(
|
|
|
|
data,
|
|
|
|
currentConvId,
|
|
|
|
currentUserId,
|
2021-07-05 06:31:54 +00:00
|
|
|
isDocHidden
|
2021-03-08 07:43:13 +00:00
|
|
|
);
|
2021-07-05 06:31:54 +00:00
|
|
|
const isNotificationEnabled = notificationEnabled(
|
|
|
|
enableAudioAlerts,
|
|
|
|
currentUserId,
|
|
|
|
assigneeId
|
|
|
|
);
|
2022-02-28 16:13:24 +00:00
|
|
|
|
2021-07-05 06:31:54 +00:00
|
|
|
if (playAudio && isNotificationEnabled) {
|
2021-03-04 13:54:03 +00:00
|
|
|
window.playAudioAlert();
|
2021-04-13 13:55:19 +00:00
|
|
|
showBadgeOnFavicon();
|
2021-03-04 13:54:03 +00:00
|
|
|
}
|
|
|
|
};
|
2021-10-18 13:09:04 +00:00
|
|
|
|
|
|
|
export const playNewMessageNotificationInWidget = () => {
|
2022-02-28 16:13:24 +00:00
|
|
|
IFrameHelper.sendMessage({
|
|
|
|
event: 'playAudio',
|
|
|
|
});
|
2021-10-18 13:09:04 +00:00
|
|
|
};
|