feat: Add settings for audio alert notifications (#2415)
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
@@ -1,16 +1,7 @@
|
||||
import { MESSAGE_TYPE } from 'shared/constants/messages';
|
||||
const notificationAudio = require('shared/assets/audio/ding.mp3');
|
||||
import axios from 'axios';
|
||||
import { showBadgeOnFavicon } from './faviconHelper';
|
||||
|
||||
export const playNotificationAudio = () => {
|
||||
try {
|
||||
new Audio(notificationAudio).play();
|
||||
} catch (error) {
|
||||
// error
|
||||
}
|
||||
};
|
||||
|
||||
export const getAlertAudio = async () => {
|
||||
window.playAudioAlert = () => {};
|
||||
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
||||
@@ -35,11 +26,21 @@ export const getAlertAudio = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
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,
|
||||
isDocHiddden
|
||||
isDocHidden
|
||||
) => {
|
||||
const {
|
||||
conversation_id: incomingConvId,
|
||||
@@ -51,29 +52,44 @@ export const shouldPlayAudio = (
|
||||
|
||||
const playAudio =
|
||||
!isFromCurrentUser && (messageType === MESSAGE_TYPE.INCOMING || isPrivate);
|
||||
|
||||
if (isDocHiddden) return playAudio;
|
||||
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 isDocHiddden = document.hidden;
|
||||
|
||||
const { conversation_id: incomingConvId } = data;
|
||||
const currentConv =
|
||||
window.WOOT.$store.getters.getConversationById(incomingConvId) || {};
|
||||
const assigneeId = getAssigneeFromNotification(currentConv);
|
||||
const isDocHidden = document.hidden;
|
||||
const {
|
||||
enable_audio_alerts: enableAudioAlerts = false,
|
||||
} = window.WOOT.$store.getters.getUISettings;
|
||||
|
||||
const playAudio = shouldPlayAudio(
|
||||
data,
|
||||
currentConvId,
|
||||
currentUserId,
|
||||
isDocHiddden
|
||||
isDocHidden
|
||||
);
|
||||
|
||||
if (enableAudioAlerts && playAudio) {
|
||||
const isNotificationEnabled = notificationEnabled(
|
||||
enableAudioAlerts,
|
||||
currentUserId,
|
||||
assigneeId
|
||||
);
|
||||
if (playAudio && isNotificationEnabled) {
|
||||
window.playAudioAlert();
|
||||
showBadgeOnFavicon();
|
||||
}
|
||||
|
||||
@@ -2,7 +2,11 @@
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
|
||||
import { shouldPlayAudio } from '../AudioNotificationHelper';
|
||||
import {
|
||||
shouldPlayAudio,
|
||||
notificationEnabled,
|
||||
getAssigneeFromNotification,
|
||||
} from '../AudioNotificationHelper';
|
||||
|
||||
describe('shouldPlayAudio', () => {
|
||||
describe('Document active', () => {
|
||||
@@ -107,3 +111,41 @@ describe('shouldPlayAudio', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
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