forked from mirrors/ntfy
1
0
Fork 0
ntfy/web/src/app/SubscriptionManager.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

191 lines
5.7 KiB
JavaScript
Raw Normal View History

import db from "./db";
2022-03-08 20:19:15 +00:00
import { topicUrl } from "./utils";
class SubscriptionManager {
2022-03-08 20:19:15 +00:00
/** All subscriptions, including "new count"; this is a JOIN, see https://dexie.org/docs/API-Reference#joining */
async all() {
2022-03-07 03:37:13 +00:00
const subscriptions = await db.subscriptions.toArray();
return Promise.all(
subscriptions.map(async (s) => ({
...s,
new: await db.notifications.where({ subscriptionId: s.id, new: 1 }).count(),
}))
2022-03-07 03:37:13 +00:00
);
}
2023-05-23 19:13:01 +00:00
async get(subscriptionId) {
return db.subscriptions.get(subscriptionId);
}
2023-05-23 19:13:01 +00:00
2023-01-24 20:31:39 +00:00
async add(baseUrl, topic, internal) {
2022-12-09 01:50:48 +00:00
const id = topicUrl(baseUrl, topic);
const existingSubscription = await this.get(id);
if (existingSubscription) {
return existingSubscription;
}
2022-03-08 20:19:15 +00:00
const subscription = {
id: topicUrl(baseUrl, topic),
baseUrl,
topic,
2022-03-08 21:56:41 +00:00
mutedUntil: 0,
2022-12-09 01:50:48 +00:00
last: null,
2023-01-24 20:31:39 +00:00
internal: internal || false,
2022-03-08 20:19:15 +00:00
};
await db.subscriptions.put(subscription);
2022-03-08 20:19:15 +00:00
return subscription;
}
2023-05-23 19:13:01 +00:00
2023-01-03 16:28:04 +00:00
async syncFromRemote(remoteSubscriptions, remoteReservations) {
2022-12-26 03:29:55 +00:00
console.log(`[SubscriptionManager] Syncing subscriptions from remote`, remoteSubscriptions);
2023-05-23 19:13:01 +00:00
2022-12-09 01:50:48 +00:00
// Add remote subscriptions
const remoteIds = await Promise.all(
remoteSubscriptions.map(async (remote) => {
const local = await this.add(remote.base_url, remote.topic, false);
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 19:13:01 +00:00
2022-12-09 01:50:48 +00:00
// Remove local subscriptions that do not exist remotely
const localSubscriptions = await db.subscriptions.toArray();
await Promise.all(
localSubscriptions.map(async (local) => {
const remoteExists = remoteIds.includes(local.id);
if (!local.internal && !remoteExists) {
await this.remove(local.id);
}
})
);
2023-05-23 19:13:01 +00:00
}
async updateState(subscriptionId, state) {
db.subscriptions.update(subscriptionId, { state });
}
2023-05-23 19:13:01 +00:00
async remove(subscriptionId) {
await db.subscriptions.delete(subscriptionId);
await db.notifications.where({ subscriptionId }).delete();
}
2023-05-23 19:13:01 +00:00
async first() {
return db.subscriptions.toCollection().first(); // May be undefined
}
2023-05-23 19:13:01 +00:00
2022-03-08 16:21:11 +00:00
async getNotifications(subscriptionId) {
2022-03-08 01:11:58 +00: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 19:13:01 +00:00
return db.notifications
2022-03-08 01:11:58 +00:00
.orderBy("time") // Sort by time first
.filter((n) => n.subscriptionId === subscriptionId)
2022-03-07 21:36:49 +00:00
.reverse()
2022-03-08 01:11:58 +00:00
.toArray();
2022-03-07 21:36:49 +00:00
}
2023-05-23 19:13:01 +00:00
2022-03-07 21:36:49 +00:00
async getAllNotifications() {
return db.notifications
.orderBy("time") // Efficient, see docs
.reverse()
.toArray();
}
2023-05-23 19:13:01 +00:00
/** Adds notification, or returns false if it already exists */
async addNotification(subscriptionId, notification) {
const exists = await db.notifications.get(notification.id);
if (exists) {
return false;
}
2022-03-07 03:37:13 +00:00
try {
2023-05-24 16:08:59 +00:00
await db.notifications.add({
...notification,
subscriptionId,
// New marker (used for bubble indicator); cannot be boolean; Dexie index limitation
new: 1,
}); // FIXME consider put() for double tab
2022-03-07 03:37:13 +00:00
await db.subscriptions.update(subscriptionId, {
last: notification.id,
});
} catch (e) {
console.error(`[SubscriptionManager] Error adding notification`, e);
}
return true;
}
2023-05-23 19:13:01 +00: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;
await db.notifications.bulkPut(notificationsWithSubscriptionId);
await db.subscriptions.update(subscriptionId, {
last: lastNotificationId,
});
}
2023-05-23 19:13:01 +00:00
2022-04-21 20:33:49 +00:00
async updateNotification(notification) {
const exists = await db.notifications.get(notification.id);
if (!exists) {
return false;
}
try {
await db.notifications.put({ ...notification });
} catch (e) {
console.error(`[SubscriptionManager] Error updating notification`, e);
}
return true;
}
2023-05-23 19:13:01 +00:00
async deleteNotification(notificationId) {
await db.notifications.delete(notificationId);
}
2023-05-23 19:13:01 +00:00
async deleteNotifications(subscriptionId) {
await db.notifications.where({ subscriptionId }).delete();
}
2023-05-23 19:13:01 +00:00
async markNotificationRead(notificationId) {
2022-05-07 23:16:08 +00:00
await db.notifications.where({ id: notificationId }).modify({ new: 0 });
}
2023-05-23 19:13:01 +00:00
2022-03-07 03:37:13 +00:00
async markNotificationsRead(subscriptionId) {
await db.notifications.where({ subscriptionId, new: 1 }).modify({ new: 0 });
}
2023-05-23 19:13:01 +00:00
2022-03-08 21:56:41 +00:00
async setMutedUntil(subscriptionId, mutedUntil) {
await db.subscriptions.update(subscriptionId, {
mutedUntil,
});
}
2023-05-23 19:13:01 +00:00
2022-06-29 19:57:56 +00:00
async setDisplayName(subscriptionId, displayName) {
await db.subscriptions.update(subscriptionId, {
displayName,
});
}
2023-05-23 19:13:01 +00:00
2023-01-03 16:28:04 +00:00
async setReservation(subscriptionId, reservation) {
await db.subscriptions.update(subscriptionId, {
reservation,
});
}
2023-05-23 19:13:01 +00:00
2023-01-12 02:38:10 +00:00
async update(subscriptionId, params) {
await db.subscriptions.update(subscriptionId, params);
}
2023-05-23 19:13:01 +00:00
async pruneNotifications(thresholdTimestamp) {
await db.notifications.where("time").below(thresholdTimestamp).delete();
}
}
const subscriptionManager = new SubscriptionManager();
export default subscriptionManager;