Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(app): title push notifications with the instance name

Hampus4a2473f33155
diff --git a/fluxer_app/src/features/platform/service_worker/Worker.ts b/fluxer_app/src/features/platform/service_worker/Worker.ts
index 10ca68db8..89942eb54 100644
--- a/fluxer_app/src/features/platform/service_worker/Worker.ts
+++ b/fluxer_app/src/features/platform/service_worker/Worker.ts
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
-import {PRODUCT_NAME} from '@app/features/app/config/ProductConstants';
import {
getNotificationAlertOptions,
isMobileOrTabletUserAgent,
@@ -22,6 +21,7 @@ import {getWorkerFetchRoute} from '@app/features/platform/service_worker/WorkerF
import {
getBadgeCount,
getPushNotificationClientState,
+ type InstanceManifestSources,
isNotificationClearPayload,
isPushNotificationReadThrough,
matchesPushChannelNotification,
@@ -29,6 +29,7 @@ import {
resolvePushChannelId,
resolvePushMessageId,
resolvePushNotificationTag,
+ resolvePushTitle,
shouldRenotifyPushNotification,
shouldSilenceNonMobilePushNotification,
} from '@app/features/platform/service_worker/WorkerPushPayload';
@@ -63,14 +64,16 @@ try {
const log = async (level: SwLogLevel, message: string, data?: unknown): Promise<void> => {
try {
if (typeof console !== 'undefined') {
- const fn =
- level === 'error'
- ? console.error
- : level === 'warn'
- ? console.warn
- : level === 'info'
- ? console.info
- : console.debug;
+ let fn: typeof console.debug;
+ if (level === 'error') {
+ fn = console.error;
+ } else if (level === 'warn') {
+ fn = console.warn;
+ } else if (level === 'info') {
+ fn = console.info;
+ } else {
+ fn = console.debug;
+ }
if (data !== undefined) fn?.call(console, '[SW]', message, data);
else fn?.call(console, '[SW]', message);
}
@@ -129,6 +132,16 @@ const fetchNetworkFirst = async (request: Request): Promise<Response> => {
}
};
+const instanceManifestUrl = (): string => new URL('/manifest.json', self.location.origin).href;
+const instanceManifestSources: InstanceManifestSources = {
+ fetchManifest: async (signal) => {
+ const response = await fetch(instanceManifestUrl(), {signal});
+ await cacheRequest(PRECACHE_CACHE, instanceManifestUrl(), response);
+ return response;
+ },
+ readCachedManifest: async () => (await serviceWorkerCaches?.match(instanceManifestUrl())) ?? undefined,
+};
+
self.addEventListener('install', (event: ExtendableEvent) => {
event.waitUntil(
(async () => {
@@ -344,8 +357,7 @@ self.addEventListener('push', (event: PushEvent) => {
rawPreview: rawText?.slice(0, 512) ?? null,
});
}
- const payload = normalizePushPayload(raw ?? {title: PRODUCT_NAME});
- const title = payload.title ?? PRODUCT_NAME;
+ const payload = normalizePushPayload(raw);
const tag = resolvePushNotificationTag(payload);
const badgeCount = getBadgeCount(payload);
if (isNotificationClearPayload(payload)) {
@@ -361,6 +373,7 @@ self.addEventListener('push', (event: PushEvent) => {
await log('warn', 'push ignored in native desktop service worker', {badgeCount});
return;
}
+ const title = await resolvePushTitle(payload, instanceManifestSources);
const body = payload.body ?? '';
let clientState = getPushNotificationClientState([]);
try {
@@ -403,7 +416,7 @@ self.addEventListener('push', (event: PushEvent) => {
} catch (error) {
await log('error', 'push: showNotification failed, showing fallback', {error: describeError(error)});
try {
- await self.registration.showNotification(PRODUCT_NAME, {body: ''});
+ await self.registration.showNotification(title, {body: ''});
} catch (fallbackError) {
await log('error', 'push: fallback showNotification also failed', {
error: describeError(fallbackError),
diff --git a/fluxer_app/src/features/platform/service_worker/WorkerPushPayload.ts b/fluxer_app/src/features/platform/service_worker/WorkerPushPayload.ts
index 53a44694a..6df1160d7 100644
--- a/fluxer_app/src/features/platform/service_worker/WorkerPushPayload.ts
+++ b/fluxer_app/src/features/platform/service_worker/WorkerPushPayload.ts
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
+import {PRODUCT_NAME} from '@app/features/app/config/ProductConstants';
import {isObjectLike as isRecord} from '@fluxer/record_utils/src/RecordGuards';
export interface PushPayload {
@@ -31,6 +32,49 @@ export const normalizePushPayload = (raw: unknown): PushPayload => {
}
return payload;
};
+const PUSH_TITLE_MANIFEST_TIMEOUT_MS = 2000;
+export interface InstanceManifestSources {
+ fetchManifest: (signal: AbortSignal) => Promise<Response>;
+ readCachedManifest: () => Promise<Response | undefined>;
+}
+const readManifestName = async (response: Response | undefined): Promise<string | null> => {
+ if (!response?.ok) return null;
+ const manifest: unknown = await response.json();
+ const name = isRecord(manifest) && typeof manifest.name === 'string' ? manifest.name.trim() : '';
+ return name.length > 0 ? name : null;
+};
+const fetchManifestName = async (sources: InstanceManifestSources, timeoutMs: number): Promise<string | null> => {
+ const controller = new AbortController();
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const timedOut = new Promise<null>((resolve) => {
+ timer = setTimeout(() => {
+ controller.abort();
+ resolve(null);
+ }, timeoutMs);
+ });
+ try {
+ return await Promise.race([sources.fetchManifest(controller.signal).then(readManifestName), timedOut]);
+ } catch {
+ return null;
+ } finally {
+ clearTimeout(timer);
+ }
+};
+const readCachedManifestName = async (sources: InstanceManifestSources): Promise<string | null> => {
+ try {
+ return await readManifestName(await sources.readCachedManifest());
+ } catch {
+ return null;
+ }
+};
+export const resolvePushTitle = async (payload: PushPayload, sources: InstanceManifestSources): Promise<string> => {
+ if (typeof payload.title === 'string' && payload.title.trim()) return payload.title;
+ return (
+ (await fetchManifestName(sources, PUSH_TITLE_MANIFEST_TIMEOUT_MS)) ??
+ (await readCachedManifestName(sources)) ??
+ PRODUCT_NAME
+ );
+};
export const getBadgeCount = (payload: PushPayload): number | null => {
const badgeValue = payload.data?.badge_count;
if (typeof badgeValue === 'number' && Number.isFinite(badgeValue)) {