summaryrefslogtreecommitdiff
path: root/ui/service-worker.js
blob: 3a54d1e07d9d80e89ca1311f8fc49e40edc79c24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/// <reference types="@sveltejs/kit" />
/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference lib="webworker" />

self.addEventListener('install', (event) => {
  self.skipWaiting();
});

self.addEventListener('activate', (event) => {
  event.waitUntil(self.clients.claim());
});

// ###### Handle unread badge:
const conversationReadStatus = {
  // Format:
  // conversationId: { lastRead: Optional(Datetime), lastMessage: Datetime }
};

function countUnreadChannels() {
  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);
}

self.addEventListener('push', (event) => {
  // Let's show a notification right away so Safari doesn't tell Apple to be
  // mad at us:
  event.waitUntil(
    self.registration.showNotification('Test notification', {
      actions: [],
      body: event.data.text(),
    }),
  );

  const data = event.data?.json();
  // Now we can do slower things that might fail:
  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) {
        navigator.setAppBadge(countUnreadChannels());
      }
    })(),
  );
});

// The client has to tell us when it has read a conversation:
self.addEventListener('message', (event) => {
  const data = event.data;
  switch (data.type) {
    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) {
            navigator.setAppBadge(countUnreadChannels());
          }
        })(),
      );
      break;
    default:
      break;
  }
});