feat: expand live chat and work server tools
This commit is contained in:
52
src/sw.js
52
src/sw.js
@@ -27,6 +27,51 @@ self.addEventListener('message', (event) => {
|
||||
}
|
||||
});
|
||||
|
||||
function normalizeNotificationValue(value) {
|
||||
return typeof value === 'string' ? value.trim() : '';
|
||||
}
|
||||
|
||||
function normalizeNotificationAliases(value) {
|
||||
if (!Array.isArray(value)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return value.map((item) => normalizeNotificationValue(item)).filter(Boolean);
|
||||
}
|
||||
|
||||
function shouldCloseExistingNotification(notification, payload) {
|
||||
const data = payload.data ?? {};
|
||||
const notificationScope = normalizeNotificationValue(data.notificationScope);
|
||||
const notificationSource = normalizeNotificationValue(data.source);
|
||||
const notificationKey = normalizeNotificationValue(data.notificationKey);
|
||||
const notificationAliases = normalizeNotificationAliases(data.notificationAliases);
|
||||
const replaceExistingScope =
|
||||
data.replaceExistingScope === true || normalizeNotificationValue(data.replaceExistingScope).toLowerCase() === 'true';
|
||||
|
||||
if (!notificationScope || (!replaceExistingScope && notificationScope !== 'automation')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const existingData = notification.data && typeof notification.data === 'object' ? notification.data : {};
|
||||
const existingScope = normalizeNotificationValue(existingData.notificationScope);
|
||||
const existingSource = normalizeNotificationValue(existingData.source);
|
||||
const existingNotificationKey = normalizeNotificationValue(existingData.notificationKey);
|
||||
const existingTag = normalizeNotificationValue(notification.tag);
|
||||
const replaceTargets = new Set([
|
||||
notificationScope,
|
||||
notificationSource,
|
||||
notificationKey,
|
||||
...notificationAliases,
|
||||
].filter(Boolean));
|
||||
|
||||
return (
|
||||
replaceTargets.has(existingScope) ||
|
||||
replaceTargets.has(existingSource) ||
|
||||
replaceTargets.has(existingNotificationKey) ||
|
||||
replaceTargets.has(existingTag)
|
||||
);
|
||||
}
|
||||
|
||||
self.addEventListener('push', (event) => {
|
||||
if (!event.data) {
|
||||
return;
|
||||
@@ -46,18 +91,13 @@ self.addEventListener('push', (event) => {
|
||||
|
||||
const title = payload.title || 'AI Code App';
|
||||
const body = payload.body || '새 알림이 도착했습니다.';
|
||||
const notificationScope = payload.data?.notificationScope;
|
||||
const notificationKey =
|
||||
payload.data?.notificationKey ||
|
||||
[payload.threadId ?? 'ai-code-app-notification', payload.data?.eventType ?? 'event', Date.now()].join(':');
|
||||
|
||||
event.waitUntil(
|
||||
self.registration.getNotifications().then((notifications) => {
|
||||
if (notificationScope === 'automation') {
|
||||
notifications
|
||||
.filter((notification) => notification.data?.notificationScope === 'automation')
|
||||
.forEach((notification) => notification.close());
|
||||
}
|
||||
notifications.filter((notification) => shouldCloseExistingNotification(notification, payload)).forEach((notification) => notification.close());
|
||||
|
||||
return self.registration.showNotification(title, {
|
||||
body,
|
||||
|
||||
Reference in New Issue
Block a user