2023-05-24 21:36:01 +02:00
|
|
|
import notifier from "./Notifier";
|
|
|
|
import prefs from "./Prefs";
|
|
|
|
import getDb from "./getDb";
|
2022-03-08 21:19:15 +01:00
|
|
|
import { topicUrl } from "./utils";
|
2022-03-03 22:52:07 +01:00
|
|
|
|
2023-05-24 21:36:01 +02:00
|
|
|
/** @typedef {string} NotificationTypeEnum */
|
|
|
|
|
|
|
|
/** @enum {NotificationTypeEnum} */
|
|
|
|
export const NotificationType = {
|
|
|
|
/** sound-only */
|
|
|
|
SOUND: "sound",
|
|
|
|
/** browser notifications when there is an active tab, via websockets */
|
|
|
|
BROWSER: "browser",
|
|
|
|
/** web push notifications, regardless of whether the window is open */
|
|
|
|
BACKGROUND: "background",
|
|
|
|
};
|
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
class SubscriptionManager {
|
2023-05-24 21:36:01 +02:00
|
|
|
constructor(db) {
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
2022-03-08 21:19:15 +01:00
|
|
|
/** All subscriptions, including "new count"; this is a JOIN, see https://dexie.org/docs/API-Reference#joining */
|
2022-03-03 22:52:07 +01:00
|
|
|
async all() {
|
2023-05-24 21:36:01 +02:00
|
|
|
const subscriptions = await this.db.subscriptions.toArray();
|
2023-05-24 17:48:39 +02:00
|
|
|
return Promise.all(
|
|
|
|
subscriptions.map(async (s) => ({
|
|
|
|
...s,
|
2023-05-24 21:36:01 +02:00
|
|
|
new: await this.db.notifications.where({ subscriptionId: s.id, new: 1 }).count(),
|
2023-05-24 17:48:39 +02:00
|
|
|
}))
|
2022-03-07 04:37:13 +01:00
|
|
|
);
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
async get(subscriptionId) {
|
2023-05-24 21:36:01 +02:00
|
|
|
return this.db.subscriptions.get(subscriptionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async notify(subscriptionId, notification, defaultClickAction) {
|
|
|
|
const subscription = await this.get(subscriptionId);
|
|
|
|
|
|
|
|
if (subscription.mutedUntil === 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const priority = notification.priority ?? 3;
|
|
|
|
if (priority < (await prefs.minPriority())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await notifier.playSound();
|
|
|
|
|
|
|
|
// sound only
|
|
|
|
if (subscription.notificationType === "sound") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await notifier.notify(subscription, notification, defaultClickAction);
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2023-05-24 21:36:01 +02:00
|
|
|
/**
|
|
|
|
* @param {string} baseUrl
|
|
|
|
* @param {string} topic
|
|
|
|
* @param {object} opts
|
|
|
|
* @param {boolean} opts.internal
|
|
|
|
* @param {NotificationTypeEnum} opts.notificationType
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
async add(baseUrl, topic, opts = {}) {
|
2022-12-09 02:50:48 +01:00
|
|
|
const id = topicUrl(baseUrl, topic);
|
2023-05-24 21:36:01 +02:00
|
|
|
|
2023-05-31 21:08:46 +02:00
|
|
|
if (opts.notificationType === "background") {
|
|
|
|
await notifier.subscribeWebPush(baseUrl, topic);
|
|
|
|
}
|
2023-05-24 21:36:01 +02:00
|
|
|
|
2022-12-09 02:50:48 +01:00
|
|
|
const existingSubscription = await this.get(id);
|
|
|
|
if (existingSubscription) {
|
|
|
|
return existingSubscription;
|
|
|
|
}
|
2023-05-24 21:36:01 +02:00
|
|
|
|
2022-03-08 21:19:15 +01:00
|
|
|
const subscription = {
|
|
|
|
id: topicUrl(baseUrl, topic),
|
|
|
|
baseUrl,
|
|
|
|
topic,
|
2022-03-08 22:56:41 +01:00
|
|
|
mutedUntil: 0,
|
2022-12-09 02:50:48 +01:00
|
|
|
last: null,
|
2023-05-24 21:36:01 +02:00
|
|
|
...opts,
|
2022-03-08 21:19:15 +01:00
|
|
|
};
|
2023-05-24 21:36:01 +02:00
|
|
|
|
|
|
|
await this.db.subscriptions.put(subscription);
|
|
|
|
|
2022-03-08 21:19:15 +01:00
|
|
|
return subscription;
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2023-01-03 17:28:04 +01:00
|
|
|
async syncFromRemote(remoteSubscriptions, remoteReservations) {
|
2022-12-26 04:29:55 +01:00
|
|
|
console.log(`[SubscriptionManager] Syncing subscriptions from remote`, remoteSubscriptions);
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2023-05-24 21:36:01 +02:00
|
|
|
const notificationType = (await prefs.webPushDefaultEnabled()) === "enabled" ? "background" : "browser";
|
|
|
|
|
2022-12-09 02:50:48 +01:00
|
|
|
// Add remote subscriptions
|
2023-05-24 17:48:39 +02:00
|
|
|
const remoteIds = await Promise.all(
|
|
|
|
remoteSubscriptions.map(async (remote) => {
|
2023-05-24 21:36:01 +02:00
|
|
|
const local = await this.add(remote.base_url, remote.topic, {
|
|
|
|
notificationType,
|
|
|
|
});
|
2023-05-24 17:48:39 +02:00
|
|
|
const reservation = remoteReservations?.find((r) => remote.base_url === config.base_url && remote.topic === r.topic) || null;
|
|
|
|
|
|
|
|
await this.update(local.id, {
|
|
|
|
displayName: remote.display_name, // May be undefined
|
|
|
|
reservation, // May be null!
|
|
|
|
});
|
|
|
|
|
|
|
|
return local.id;
|
|
|
|
})
|
|
|
|
);
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-12-09 02:50:48 +01:00
|
|
|
// Remove local subscriptions that do not exist remotely
|
2023-05-24 21:36:01 +02:00
|
|
|
const localSubscriptions = await this.db.subscriptions.toArray();
|
2023-05-24 17:48:39 +02:00
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
localSubscriptions.map(async (local) => {
|
|
|
|
const remoteExists = remoteIds.includes(local.id);
|
|
|
|
if (!local.internal && !remoteExists) {
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.remove(local);
|
2023-05-24 17:48:39 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2023-05-23 21:13:01 +02:00
|
|
|
}
|
|
|
|
|
2022-03-04 17:08:32 +01:00
|
|
|
async updateState(subscriptionId, state) {
|
2023-05-24 21:36:01 +02:00
|
|
|
this.db.subscriptions.update(subscriptionId, { state });
|
2022-03-04 17:08:32 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2023-05-24 21:36:01 +02:00
|
|
|
async remove(subscription) {
|
|
|
|
await this.db.subscriptions.delete(subscription.id);
|
|
|
|
await this.db.notifications.where({ subscriptionId: subscription.id }).delete();
|
|
|
|
|
2023-05-31 21:08:46 +02:00
|
|
|
if (subscription.notificationType === NotificationType.BACKGROUND) {
|
2023-05-24 21:36:01 +02:00
|
|
|
await notifier.unsubscribeWebPush(subscription);
|
|
|
|
}
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
async first() {
|
2023-05-24 21:36:01 +02:00
|
|
|
return this.db.subscriptions.toCollection().first(); // May be undefined
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-03-08 17:21:11 +01:00
|
|
|
async getNotifications(subscriptionId) {
|
2022-03-08 02:11:58 +01:00
|
|
|
// This is quite awkward, but it is the recommended approach as per the Dexie docs.
|
|
|
|
// It's actually fine, because the reading and filtering is quite fast. The rendering is what's
|
|
|
|
// killing performance. See https://dexie.org/docs/Collection/Collection.offset()#a-better-paging-approach
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2023-05-24 21:36:01 +02:00
|
|
|
return this.db.notifications
|
2022-03-08 02:11:58 +01:00
|
|
|
.orderBy("time") // Sort by time first
|
|
|
|
.filter((n) => n.subscriptionId === subscriptionId)
|
2022-03-07 22:36:49 +01:00
|
|
|
.reverse()
|
2022-03-08 02:11:58 +01:00
|
|
|
.toArray();
|
2022-03-07 22:36:49 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-03-07 22:36:49 +01:00
|
|
|
async getAllNotifications() {
|
2023-05-24 21:36:01 +02:00
|
|
|
return this.db.notifications
|
2022-03-07 22:36:49 +01:00
|
|
|
.orderBy("time") // Efficient, see docs
|
|
|
|
.reverse()
|
2022-03-03 22:52:07 +01:00
|
|
|
.toArray();
|
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
/** Adds notification, or returns false if it already exists */
|
|
|
|
async addNotification(subscriptionId, notification) {
|
2023-05-24 21:36:01 +02:00
|
|
|
const exists = await this.db.notifications.get(notification.id);
|
2022-03-03 22:52:07 +01:00
|
|
|
if (exists) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-07 04:37:13 +01:00
|
|
|
try {
|
2023-05-24 21:36:01 +02:00
|
|
|
// sw.js duplicates this logic, so if you change it here, change it there too
|
|
|
|
await this.db.notifications.add({
|
2023-05-24 18:08:59 +02:00
|
|
|
...notification,
|
|
|
|
subscriptionId,
|
|
|
|
// New marker (used for bubble indicator); cannot be boolean; Dexie index limitation
|
|
|
|
new: 1,
|
|
|
|
}); // FIXME consider put() for double tab
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.subscriptions.update(subscriptionId, {
|
2022-03-07 04:37:13 +01:00
|
|
|
last: notification.id,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`[SubscriptionManager] Error adding notification`, e);
|
|
|
|
}
|
2022-03-03 22:52:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
/** Adds/replaces notifications, will not throw if they exist */
|
|
|
|
async addNotifications(subscriptionId, notifications) {
|
|
|
|
const notificationsWithSubscriptionId = notifications.map((notification) => ({ ...notification, subscriptionId }));
|
|
|
|
const lastNotificationId = notifications.at(-1).id;
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.notifications.bulkPut(notificationsWithSubscriptionId);
|
|
|
|
await this.db.subscriptions.update(subscriptionId, {
|
2022-03-03 22:52:07 +01:00
|
|
|
last: lastNotificationId,
|
|
|
|
});
|
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-04-21 22:33:49 +02:00
|
|
|
async updateNotification(notification) {
|
2023-05-24 21:36:01 +02:00
|
|
|
const exists = await this.db.notifications.get(notification.id);
|
2022-04-21 22:33:49 +02:00
|
|
|
if (!exists) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.notifications.put({ ...notification });
|
2022-04-21 22:33:49 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`[SubscriptionManager] Error updating notification`, e);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
async deleteNotification(notificationId) {
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.notifications.delete(notificationId);
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
async deleteNotifications(subscriptionId) {
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.notifications.where({ subscriptionId }).delete();
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-05-08 00:10:48 +02:00
|
|
|
async markNotificationRead(notificationId) {
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.notifications.where({ id: notificationId }).modify({ new: 0 });
|
2022-05-08 00:10:48 +02:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-03-07 04:37:13 +01:00
|
|
|
async markNotificationsRead(subscriptionId) {
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.notifications.where({ subscriptionId, new: 1 }).modify({ new: 0 });
|
2022-03-07 04:37:13 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-03-08 22:56:41 +01:00
|
|
|
async setMutedUntil(subscriptionId, mutedUntil) {
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.subscriptions.update(subscriptionId, {
|
2022-03-08 22:56:41 +01:00
|
|
|
mutedUntil,
|
|
|
|
});
|
2023-05-24 21:36:01 +02:00
|
|
|
|
|
|
|
const subscription = await this.get(subscriptionId);
|
|
|
|
|
|
|
|
if (subscription.notificationType === "background") {
|
|
|
|
if (mutedUntil === 1) {
|
|
|
|
await notifier.unsubscribeWebPush(subscription);
|
|
|
|
} else {
|
2023-05-31 21:08:46 +02:00
|
|
|
await notifier.subscribeWebPush(subscription.baseUrl, subscription.topic);
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {object} subscription
|
|
|
|
* @param {NotificationTypeEnum} newNotificationType
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
async setNotificationType(subscription, newNotificationType) {
|
|
|
|
const oldNotificationType = subscription.notificationType ?? "browser";
|
|
|
|
|
|
|
|
if (oldNotificationType === newNotificationType) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldNotificationType === "background") {
|
|
|
|
await notifier.unsubscribeWebPush(subscription);
|
|
|
|
} else if (newNotificationType === "background") {
|
2023-05-31 21:08:46 +02:00
|
|
|
await notifier.subscribeWebPush(subscription.baseUrl, subscription.topic);
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.db.subscriptions.update(subscription.id, {
|
|
|
|
notificationType: newNotificationType,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// for logout/delete, unsubscribe first to prevent receiving dangling notifications
|
|
|
|
async unsubscribeAllWebPush() {
|
|
|
|
const subscriptions = await this.db.subscriptions.where({ notificationType: "background" }).toArray();
|
|
|
|
await Promise.all(subscriptions.map((subscription) => notifier.unsubscribeWebPush(subscription)));
|
|
|
|
}
|
|
|
|
|
|
|
|
async refreshWebPushSubscriptions() {
|
|
|
|
const subscriptions = await this.db.subscriptions.where({ notificationType: "background" }).toArray();
|
|
|
|
const browserSubscription = await (await navigator.serviceWorker.getRegistration())?.pushManager?.getSubscription();
|
|
|
|
|
|
|
|
if (browserSubscription) {
|
|
|
|
await Promise.all(subscriptions.map((subscription) => notifier.subscribeWebPush(subscription.baseUrl, subscription.topic)));
|
|
|
|
} else {
|
|
|
|
await Promise.all(subscriptions.map((subscription) => this.setNotificationType(subscription, "sound")));
|
|
|
|
}
|
2022-03-08 22:56:41 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-06-29 21:57:56 +02:00
|
|
|
async setDisplayName(subscriptionId, displayName) {
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.subscriptions.update(subscriptionId, {
|
2022-06-29 21:57:56 +02:00
|
|
|
displayName,
|
|
|
|
});
|
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2023-01-03 17:28:04 +01:00
|
|
|
async setReservation(subscriptionId, reservation) {
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.subscriptions.update(subscriptionId, {
|
2023-01-03 17:28:04 +01:00
|
|
|
reservation,
|
|
|
|
});
|
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2023-01-12 03:38:10 +01:00
|
|
|
async update(subscriptionId, params) {
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.subscriptions.update(subscriptionId, params);
|
2023-01-12 03:38:10 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
async pruneNotifications(thresholdTimestamp) {
|
2023-05-24 21:36:01 +02:00
|
|
|
await this.db.notifications.where("time").below(thresholdTimestamp).delete();
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 21:36:01 +02:00
|
|
|
export default new SubscriptionManager(getDb());
|