2023-06-02 13:22:54 +02:00
|
|
|
import api from "./Api";
|
2023-05-24 21:36:01 +02:00
|
|
|
import notifier from "./Notifier";
|
|
|
|
import prefs from "./Prefs";
|
2023-06-09 20:32:34 +02:00
|
|
|
import db from "./db";
|
2022-03-08 21:19:15 +01:00
|
|
|
import { topicUrl } from "./utils";
|
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
|
|
|
|
2023-06-11 02:42:02 +02:00
|
|
|
/** List of topics for which Web Push is enabled, excludes internal topics; returns empty list if Web Push is disabled */
|
2023-06-02 13:22:54 +02:00
|
|
|
async webPushTopics() {
|
2023-06-08 09:22:56 +02:00
|
|
|
// the Promise.resolve wrapper is not superfluous, without it the live query breaks:
|
|
|
|
// https://dexie.org/docs/dexie-react-hooks/useLiveQuery()#calling-non-dexie-apis-from-querier
|
2023-06-11 02:42:02 +02:00
|
|
|
const pushEnabled = await Promise.resolve(notifier.pushEnabled());
|
|
|
|
if (!pushEnabled) {
|
2023-06-08 09:22:56 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const subscriptions = await this.db.subscriptions.where({ mutedUntil: 0, baseUrl: config.base_url }).toArray();
|
|
|
|
return subscriptions.filter(({ internal }) => !internal).map(({ topic }) => topic);
|
2023-06-02 13:22:54 +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;
|
|
|
|
}
|
|
|
|
|
2023-06-02 13:22:54 +02:00
|
|
|
await Promise.all([notifier.playSound(), 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
|
|
|
|
* @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
|
|
|
|
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 = {
|
2023-06-02 13:22:54 +02:00
|
|
|
...opts,
|
2022-03-08 21:19:15 +01:00
|
|
|
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,
|
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
|
|
|
|
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) => {
|
|
|
|
const reservation = remoteReservations?.find((r) => remote.base_url === config.base_url && remote.topic === r.topic) || null;
|
|
|
|
|
2023-06-02 13:22:54 +02:00
|
|
|
const local = await this.add(remote.base_url, remote.topic, {
|
2023-05-24 17:48:39 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-06-11 02:42:02 +02:00
|
|
|
async updateWebPushSubscriptions(presetTopics) {
|
2023-06-02 13:22:54 +02:00
|
|
|
const topics = presetTopics ?? (await this.webPushTopics());
|
2023-06-08 10:55:11 +02:00
|
|
|
const browserSubscription = await notifier.getBrowserSubscription();
|
|
|
|
|
|
|
|
if (!browserSubscription) {
|
|
|
|
console.log("[SubscriptionManager] No browser subscription currently exists, so web push was never enabled. Skipping.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-11 03:09:01 +02:00
|
|
|
if (topics.length > 0) {
|
|
|
|
await api.updateWebPush(browserSubscription, topics);
|
|
|
|
} else {
|
|
|
|
await api.deleteWebPush(browserSubscription);
|
|
|
|
}
|
|
|
|
|
2023-06-02 13:22:54 +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();
|
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
|
|
|
}
|
|
|
|
|
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-06-09 20:32:34 +02:00
|
|
|
export default new SubscriptionManager(db());
|