| 1 |
// This is a separate file since the other utils import `config.js`, which depends on `window` |
| 2 |
// and cannot be used in the service worker |
| 3 |
|
| 4 |
import emojisMapped from "./emojisMapped"; |
| 5 |
import { ACTION_HTTP, ACTION_VIEW } from "./actions"; |
| 6 |
|
| 7 |
const toEmojis = (tags) => { |
| 8 |
if (!tags) return []; |
| 9 |
return tags.filter((tag) => tag in emojisMapped).map((tag) => emojisMapped[tag]); |
| 10 |
}; |
| 11 |
|
| 12 |
export const formatTitle = (m) => { |
| 13 |
const emojiList = toEmojis(m.tags); |
| 14 |
if (emojiList.length > 0) { |
| 15 |
return `${emojiList.join(" ")} ${m.title}`; |
| 16 |
} |
| 17 |
return m.title; |
| 18 |
}; |
| 19 |
|
| 20 |
const formatTitleWithDefault = (m, fallback) => { |
| 21 |
if (m.title) { |
| 22 |
return formatTitle(m); |
| 23 |
} |
| 24 |
return fallback; |
| 25 |
}; |
| 26 |
|
| 27 |
export const formatMessage = (m) => { |
| 28 |
if (m.title) { |
| 29 |
return m.message || ""; |
| 30 |
} |
| 31 |
const emojiList = toEmojis(m.tags); |
| 32 |
if (emojiList.length > 0) { |
| 33 |
return `${emojiList.join(" ")} ${m.message || ""}`; |
| 34 |
} |
| 35 |
return m.message || ""; |
| 36 |
}; |
| 37 |
|
| 38 |
export const imageRegex = /\.(png|jpe?g|gif|webp)$/i; |
| 39 |
export const isImage = (attachment) => { |
| 40 |
if (!attachment) return false; |
| 41 |
|
| 42 |
// if there's a type, only take that into account |
| 43 |
if (attachment.type) { |
| 44 |
return attachment.type.startsWith("image/"); |
| 45 |
} |
| 46 |
|
| 47 |
// otherwise, check the extension |
| 48 |
return attachment.name?.match(imageRegex) || attachment.url?.match(imageRegex); |
| 49 |
}; |
| 50 |
|
| 51 |
export const icon = "/static/images/ntfy.png"; |
| 52 |
export const badge = "/static/images/ntfy-mask.svg"; |
| 53 |
|
| 54 |
/** |
| 55 |
* Computes a unique notification tag scoped by baseUrl, topic, and sequence ID. |
| 56 |
* This ensures notifications from different topics with the same sequence ID don't collide. |
| 57 |
*/ |
| 58 |
export const notificationTag = (baseUrl, topic, sequenceId) => `${baseUrl}/${topic}/${sequenceId}`; |
| 59 |
|
| 60 |
export const toNotificationParams = ({ message, defaultTitle, topicRoute, baseUrl, topic }) => { |
| 61 |
const image = isImage(message.attachment) ? message.attachment.url : undefined; |
| 62 |
const sequenceId = message.sequence_id || message.id; |
| 63 |
const tag = notificationTag(baseUrl, topic, sequenceId); |
| 64 |
const subscriptionId = `${baseUrl}/${topic}`; |
| 65 |
|
| 66 |
// https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API |
| 67 |
return [ |
| 68 |
formatTitleWithDefault(message, defaultTitle), |
| 69 |
{ |
| 70 |
body: formatMessage(message), |
| 71 |
badge, |
| 72 |
icon, |
| 73 |
image, |
| 74 |
timestamp: message.time * 1000, |
| 75 |
tag, // Scoped by baseUrl/topic/sequenceId to avoid cross-topic collisions |
| 76 |
renotify: true, |
| 77 |
silent: false, |
| 78 |
// This is used by the notification onclick event |
| 79 |
data: { |
| 80 |
subscriptionId, |
| 81 |
message, |
| 82 |
topicRoute, |
| 83 |
}, |
| 84 |
actions: message.actions |
| 85 |
?.filter(({ action }) => action === ACTION_VIEW || action === ACTION_HTTP) |
| 86 |
.map(({ label }) => ({ |
| 87 |
action: label, |
| 88 |
title: label, |
| 89 |
})), |
| 90 |
}, |
| 91 |
]; |
| 92 |
}; |
| 93 |
|
| 94 |
export const messageWithSequenceId = (message) => { |
| 95 |
if (message.sequenceId) { |
| 96 |
return message; |
| 97 |
} |
| 98 |
return { ...message, sequenceId: message.sequence_id || message.id }; |
| 99 |
}; |