summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKit La Touche <kit@transneptune.net>2026-02-16 22:44:06 -0500
committerKit La Touche <kit@transneptune.net>2026-02-16 22:44:06 -0500
commitd82e6849992a22bb6a4f41c3144ffa0cd249b2a2 (patch)
treee8112ef84edaa84f2e614d8582dc5ef261b020c2
parenteae9c15a7cd76d886337b8034d9802ac9d9d7c2d (diff)
Track unread conversation count in service workernotifs-behaviour
-rw-r--r--ui/routes/(app)/c/[conversation]/+page.svelte4
-rw-r--r--ui/service-worker.js23
2 files changed, 21 insertions, 6 deletions
diff --git a/ui/routes/(app)/c/[conversation]/+page.svelte b/ui/routes/(app)/c/[conversation]/+page.svelte
index 5a832ee..a487e85 100644
--- a/ui/routes/(app)/c/[conversation]/+page.svelte
+++ b/ui/routes/(app)/c/[conversation]/+page.svelte
@@ -61,7 +61,9 @@
registration.active.postMessage({
type: 'CONVERSATION_READ',
conversationId,
- at,
+ // The service worker's idea of "has been read yet" is more
+ // constrained, so we just tell it "we read it now, at this moment".
+ at: new Date(),
});
});
}
diff --git a/ui/service-worker.js b/ui/service-worker.js
index 67391f7..3a54d1e 100644
--- a/ui/service-worker.js
+++ b/ui/service-worker.js
@@ -18,9 +18,12 @@ const conversationReadStatus = {
};
function countUnreadChannels() {
- return Object.values(conversationReadStatus)
- .map(({ lastRead, lastMessage }) => {
- return !lastRead || lastRead < lastMessage ? 1 : 0;
+ console.debug(conversationReadStatus);
+ return Object.entries(conversationReadStatus)
+ .map(([conversationId, { lastRead, lastMessage }]) => {
+ const unread = (!lastRead || (lastRead < lastMessage)) ? 1 : 0;
+ if (unread) { console.debug(`Found unread in ${conversationId}`, lastRead, lastMessage); }
+ return unread;
})
.reduce((total, current) => total + current, 0);
}
@@ -37,8 +40,12 @@ self.addEventListener('push', (event) => {
const data = event.data?.json();
// Now we can do slower things that might fail:
- conversationReadStatus[data.conversationId] ||= { lastRead: null, lastMessage: null };
- conversationReadStatus[data.conversationId].lastMessage = new Date();
+ conversationReadStatus[data.conversation] ||= { lastRead: null, lastMessage: null };
+ conversationReadStatus[data.conversation].lastMessage = new Date();
+ console.debug(
+ `Last message in ${data.conversation}`,
+ conversationReadStatus[data.conversation].lastMessage,
+ );
event.waitUntil(
(async () => {
if (navigator.setAppBadge) {
@@ -55,10 +62,16 @@ self.addEventListener('message', (event) => {
case 'CONVERSATION_READ':
conversationReadStatus[data.conversationId] ||= { lastRead: null, lastMessage: null };
if (data.at) {
+ console.debug('Found data.at', new Date(data.at.ts));
conversationReadStatus[data.conversationId].lastRead = new Date(data.at.ts);
} else {
+ console.debug('Found no data.at');
conversationReadStatus[data.conversationId].lastRead = new Date();
}
+ console.debug(
+ `Last read in ${data.conversationId}`,
+ conversationReadStatus[data.conversationId].lastRead,
+ );
event.waitUntil(
(async () => {
if (navigator.setAppBadge) {