2021-03-04 13:54:03 +00:00
|
|
|
import { MESSAGE_TYPE } from 'shared/constants/messages';
|
2020-02-29 05:57:02 +00:00
|
|
|
const notificationAudio = require('shared/assets/audio/ding.mp3');
|
2021-03-04 13:54:03 +00:00
|
|
|
import axios from 'axios';
|
2021-04-13 13:55:19 +00:00
|
|
|
import { showBadgeOnFavicon } from './faviconHelper';
|
2020-02-29 05:57:02 +00:00
|
|
|
|
|
|
|
export const playNotificationAudio = () => {
|
|
|
|
try {
|
|
|
|
new Audio(notificationAudio).play();
|
|
|
|
} catch (error) {
|
2021-03-04 13:54:03 +00:00
|
|
|
// error
|
2020-02-29 05:57:02 +00:00
|
|
|
}
|
|
|
|
};
|
2021-03-04 13:54:03 +00:00
|
|
|
|
|
|
|
export const getAlertAudio = async () => {
|
|
|
|
window.playAudioAlert = () => {};
|
|
|
|
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
|
|
const playsound = audioBuffer => {
|
|
|
|
window.playAudioAlert = () => {
|
|
|
|
const source = audioCtx.createBufferSource();
|
|
|
|
source.buffer = audioBuffer;
|
|
|
|
source.connect(audioCtx.destination);
|
|
|
|
source.loop = false;
|
|
|
|
source.start();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await axios.get('/dashboard/audios/ding.mp3', {
|
|
|
|
responseType: 'arraybuffer',
|
|
|
|
});
|
|
|
|
|
|
|
|
audioCtx.decodeAudioData(response.data).then(playsound);
|
|
|
|
} catch (error) {
|
|
|
|
// error
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-08 07:43:13 +00:00
|
|
|
export const shouldPlayAudio = (
|
|
|
|
message,
|
|
|
|
conversationId,
|
|
|
|
userId,
|
|
|
|
isDocHiddden
|
|
|
|
) => {
|
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;
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (isDocHiddden) return playAudio;
|
|
|
|
if (conversationId !== incomingConvId) return playAudio;
|
|
|
|
return false;
|
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;
|
|
|
|
const isDocHiddden = 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,
|
|
|
|
isDocHiddden
|
|
|
|
);
|
|
|
|
|
|
|
|
if (enableAudioAlerts && playAudio) {
|
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
|
|
|
}
|
|
|
|
};
|