2022-03-03 22:52:07 +01:00
|
|
|
import db from "./db";
|
|
|
|
|
|
|
|
class SubscriptionManager {
|
|
|
|
async all() {
|
|
|
|
return db.subscriptions.toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(subscriptionId) {
|
|
|
|
return await db.subscriptions.get(subscriptionId)
|
|
|
|
}
|
|
|
|
|
|
|
|
async save(subscription) {
|
|
|
|
await db.subscriptions.put(subscription);
|
|
|
|
}
|
|
|
|
|
2022-03-04 17:08:32 +01:00
|
|
|
async updateState(subscriptionId, state) {
|
|
|
|
console.log(`Update state: ${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
|
|
|
|
}
|
|
|
|
|
|
|
|
async getNotifications(subscriptionId) {
|
|
|
|
return db.notifications
|
|
|
|
.where({ subscriptionId: subscriptionId })
|
|
|
|
.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-06 06:02:27 +01:00
|
|
|
await db.notifications.add({ ...notification, subscriptionId }); // FIXME consider put() for double tab
|
2022-03-03 22:52:07 +01:00
|
|
|
await db.subscriptions.update(subscriptionId, {
|
|
|
|
last: notification.id
|
|
|
|
});
|
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteNotification(notificationId) {
|
|
|
|
await db.notifications.delete(notificationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteNotifications(subscriptionId) {
|
|
|
|
await db.notifications
|
|
|
|
.where({subscriptionId: subscriptionId})
|
|
|
|
.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
async pruneNotifications(thresholdTimestamp) {
|
|
|
|
await db.notifications
|
|
|
|
.where("time").below(thresholdTimestamp)
|
|
|
|
.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const subscriptionManager = new SubscriptionManager();
|
|
|
|
export default subscriptionManager;
|