2022-03-03 22:52:07 +01: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 {
|
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() {
|
2022-03-07 04:37:13 +01:00
|
|
|
const subscriptions = await db.subscriptions.toArray();
|
|
|
|
await Promise.all(subscriptions.map(async s => {
|
|
|
|
s.new = await db.notifications
|
|
|
|
.where({ subscriptionId: s.id, new: 1 })
|
|
|
|
.count();
|
|
|
|
}));
|
|
|
|
return subscriptions;
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async get(subscriptionId) {
|
|
|
|
return await db.subscriptions.get(subscriptionId)
|
|
|
|
}
|
|
|
|
|
2022-03-10 05:28:55 +01:00
|
|
|
async add(baseUrl, topic) {
|
2022-03-08 21:19:15 +01:00
|
|
|
const subscription = {
|
|
|
|
id: topicUrl(baseUrl, topic),
|
|
|
|
baseUrl: baseUrl,
|
|
|
|
topic: topic,
|
2022-03-08 22:56:41 +01:00
|
|
|
mutedUntil: 0,
|
2022-03-08 21:19:15 +01:00
|
|
|
last: null
|
|
|
|
};
|
2022-03-03 22:52:07 +01:00
|
|
|
await db.subscriptions.put(subscription);
|
2022-03-08 21:19:15 +01:00
|
|
|
return subscription;
|
2022-03-03 22:52:07 +01:00
|
|
|
}
|
|
|
|
|
2022-03-04 17:08:32 +01:00
|
|
|
async updateState(subscriptionId, state) {
|
|
|
|
db.subscriptions.update(subscriptionId, { state: state });
|
|
|
|
}
|
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
async remove(subscriptionId) {
|
|
|
|
await db.subscriptions.delete(subscriptionId);
|
|
|
|
await db.notifications
|
|
|
|
.where({subscriptionId: subscriptionId})
|
|
|
|
.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
async first() {
|
|
|
|
return db.subscriptions.toCollection().first(); // May be undefined
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
return 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
|
|
|
}
|
|
|
|
|
|
|
|
async getAllNotifications() {
|
|
|
|
return db.notifications
|
|
|
|
.orderBy("time") // Efficient, see docs
|
|
|
|
.reverse()
|
2022-03-03 22:52:07 +01:00
|
|
|
.toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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 04:37:13 +01:00
|
|
|
try {
|
|
|
|
notification.new = 1; // New marker (used for bubble indicator); cannot be boolean; Dexie index limitation
|
|
|
|
await db.notifications.add({ ...notification, subscriptionId }); // FIXME consider put() for double tab
|
|
|
|
await db.subscriptions.update(subscriptionId, {
|
|
|
|
last: notification.id
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`[SubscriptionManager] Error adding notification`, e);
|
|
|
|
}
|
2022-03-03 22:52:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-21 22:33:49 +02: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;
|
|
|
|
}
|
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
async deleteNotification(notificationId) {
|
|
|
|
await db.notifications.delete(notificationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteNotifications(subscriptionId) {
|
|
|
|
await db.notifications
|
|
|
|
.where({subscriptionId: subscriptionId})
|
|
|
|
.delete();
|
|
|
|
}
|
|
|
|
|
2022-03-07 04:37:13 +01:00
|
|
|
async markNotificationsRead(subscriptionId) {
|
|
|
|
await db.notifications
|
|
|
|
.where({subscriptionId: subscriptionId, new: 1})
|
|
|
|
.modify({new: 0});
|
|
|
|
}
|
|
|
|
|
2022-03-08 22:56:41 +01:00
|
|
|
async setMutedUntil(subscriptionId, mutedUntil) {
|
|
|
|
await db.subscriptions.update(subscriptionId, {
|
|
|
|
mutedUntil: mutedUntil
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
async pruneNotifications(thresholdTimestamp) {
|
|
|
|
await db.notifications
|
|
|
|
.where("time").below(thresholdTimestamp)
|
|
|
|
.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const subscriptionManager = new SubscriptionManager();
|
|
|
|
export default subscriptionManager;
|