mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-03 04:27:53 +00:00
feat: Add audio alert for incoming messages on dashboard (#1738)
Fixes: #1345
This commit is contained in:
committed by
GitHub
parent
ca4a766b82
commit
9e8a943ec7
@@ -1,9 +1,69 @@
|
||||
import { MESSAGE_TYPE } from 'shared/constants/messages';
|
||||
const notificationAudio = require('shared/assets/audio/ding.mp3');
|
||||
import axios from 'axios';
|
||||
|
||||
export const playNotificationAudio = () => {
|
||||
try {
|
||||
new Audio(notificationAudio).play();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
// error
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
};
|
||||
|
||||
const shouldPlayAudio = data => {
|
||||
const { conversation_id: currentConvId } = window.WOOT.$route.params;
|
||||
const currentUserId = window.WOOT.$store.getters.getCurrentUserID;
|
||||
const {
|
||||
conversation_id: incomingConvId,
|
||||
sender_id: senderId,
|
||||
message_type: messageType,
|
||||
} = data;
|
||||
const isFromCurrentUser = currentUserId === senderId;
|
||||
|
||||
const playAudio =
|
||||
currentConvId !== incomingConvId &&
|
||||
!isFromCurrentUser &&
|
||||
messageType === MESSAGE_TYPE.INCOMING;
|
||||
return playAudio;
|
||||
};
|
||||
|
||||
export const newMessageNotification = data => {
|
||||
const {
|
||||
enable_audio_alerts: enableAudioAlerts = false,
|
||||
} = window.WOOT.$store.getters.getUISettings;
|
||||
if (!enableAudioAlerts) return false;
|
||||
|
||||
if (document.hidden) {
|
||||
window.playAudioAlert();
|
||||
} else {
|
||||
const playAudio = shouldPlayAudio(data);
|
||||
if (playAudio) {
|
||||
window.playAudioAlert();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user