Use the same notification pipeline everywhere

This means less duplication and `actions` support for all
notifications.
This commit is contained in:
nimbleghost
2023-06-14 23:20:48 +02:00
parent fa418eef16
commit b197ea3ab6
5 changed files with 102 additions and 73 deletions

View File

@@ -16,7 +16,7 @@ export const formatTitle = (m) => {
return m.title;
};
export const formatTitleWithDefault = (m, fallback) => {
const formatTitleWithDefault = (m, fallback) => {
if (m.title) {
return formatTitle(m);
}
@@ -33,3 +33,38 @@ export const formatMessage = (m) => {
}
return m.message;
};
const isImage = (filenameOrUrl) => filenameOrUrl?.match(/\.(png|jpe?g|gif|webp)$/i) ?? false;
export const icon = "/static/images/ntfy.png";
export const badge = "/static/images/mask-icon.svg";
export const getNotificationParams = ({ subscriptionId, message, defaultTitle, topicRoute }) => {
const image = isImage(message.attachment?.name) ? message.attachment.url : undefined;
// https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API
return [
formatTitleWithDefault(message, defaultTitle),
{
body: formatMessage(message),
badge,
icon,
image,
timestamp: message.time * 1_000,
tag: subscriptionId,
renotify: true,
silent: false,
// This is used by the notification onclick event
data: {
message,
topicRoute,
},
actions: message.actions
?.filter(({ action }) => action === "view" || action === "http")
.map(({ label }) => ({
action: label,
title: label,
})),
},
];
};