mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-06-02 19:35:53 +02:00
Refactor the db; move to *Manager classes
This commit is contained in:
parent
f9219d2d96
commit
08846e4cc2
12 changed files with 162 additions and 64 deletions
web/src/app
75
web/src/app/SubscriptionManager.js
Normal file
75
web/src/app/SubscriptionManager.js
Normal file
|
@ -0,0 +1,75 @@
|
|||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
await db.notifications.add({ ...notification, subscriptionId });
|
||||
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;
|
Loading…
Add table
Add a link
Reference in a new issue