summaryrefslogtreecommitdiff
path: root/ui/service-worker.js
blob: eee33979915cf1d79779c5e8346744ace8af6334 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/// <reference types="@sveltejs/kit" />
/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference lib="webworker" />

// Because of this line, this service worker won't run in dev mode in Firefox.
// Only Safari, Edge, Chrome can run it at the moment, because only they
// support modules in service workers.
//
// That's okay! If you run `tools/run` with PILCROW_DEV unset, you will get the
// bundled version, and can work on it. Or just use Safari.
import { build, files, version } from '$service-worker';

// Create a unique cache name for this deployment
const CACHE = `cache-${version}`;

const ASSETS = [
  ...build, // the app itself
  ...files, // everything in `static`
];

self.addEventListener('install', (event) => {
  // Create a new cache and add all files to it
  async function addFilesToCache() {
    const cache = await caches.open(CACHE);
    await cache.addAll(ASSETS);
  }

  event.waitUntil(addFilesToCache());
});

self.addEventListener('activate', (event) => {
  // Remove previous cached data from disk
  async function deleteOldCaches() {
    for (const key of await caches.keys()) {
      if (key !== CACHE) await caches.delete(key);
    }
  }

  event.waitUntil(deleteOldCaches());
});

// The simplest possible use of the caches above:
async function cacheFirst(request) {
  const responseFromCache = await caches.match(request);
  if (responseFromCache) {
    return responseFromCache;
  }
  return fetch(request);
}

self.addEventListener('fetch', (event) => {
  event.respondWith(cacheFirst(event.request));
});

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

function countUnreadChannels() {
  return Object.values(conversationReadStatus)
    .map(({ lastRead, lastMessage }) => {
      return !lastRead || lastRead < lastMessage ? 1 : 0;
    })
    .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(),
    }),
  );
  // Now we can do slower things that might fail:
  conversationReadStatus[event.conversationId] ||= { lastRead: null, lastMessage: null };
  conversationReadStatus[event.conversationId].lastMessage = new Date();
  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) => {
  switch (event.data?.type) {
    case 'CONVERSATION_READ':
      conversationReadStatus[event.data.conversationId] ||= { lastRead: null, lastMessage: null };
      conversationReadStatus[event.data.conversationId].lastMessage = event.data.at || new Date();
      event.waitUntil(
        (async () => {
          if (navigator.setAppBadge) {
            navigator.setAppBadge(countUnreadChannels());
          }
        })(),
      );
      break;
    default:
      break;
  }
});