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
|
@ -1,17 +1,17 @@
|
|||
import {
|
||||
topicUrlJsonPoll,
|
||||
fetchLinesIterator,
|
||||
topicUrl,
|
||||
topicUrlAuth,
|
||||
maybeWithBasicAuth,
|
||||
topicShortUrl,
|
||||
topicUrl,
|
||||
topicUrlAuth,
|
||||
topicUrlJsonPoll,
|
||||
topicUrlJsonPollWithSince
|
||||
} from "./utils";
|
||||
import db from "./db";
|
||||
import userManager from "./UserManager";
|
||||
|
||||
class Api {
|
||||
async poll(baseUrl, topic, since) {
|
||||
const user = await db.users.get(baseUrl);
|
||||
const user = await userManager.get(baseUrl);
|
||||
const shortUrl = topicShortUrl(baseUrl, topic);
|
||||
const url = (since)
|
||||
? topicUrlJsonPollWithSince(baseUrl, topic, since)
|
||||
|
@ -27,7 +27,7 @@ class Api {
|
|||
}
|
||||
|
||||
async publish(baseUrl, topic, message) {
|
||||
const user = await db.users.get(baseUrl);
|
||||
const user = await userManager.get(baseUrl);
|
||||
const url = topicUrl(baseUrl, topic);
|
||||
console.log(`[Api] Publishing message to ${url}`);
|
||||
await fetch(url, {
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
import {formatMessage, formatTitleWithFallback, topicShortUrl} from "./utils";
|
||||
import prefs from "./Prefs";
|
||||
import subscriptionManager from "./SubscriptionManager";
|
||||
|
||||
class NotificationManager {
|
||||
async notify(subscription, notification, onClickFallback) {
|
||||
async notify(subscriptionId, notification, onClickFallback) {
|
||||
const subscription = await subscriptionManager.get(subscriptionId);
|
||||
const shouldNotify = await this.shouldNotify(subscription, notification);
|
||||
if (!shouldNotify) {
|
||||
return;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import db from "./db";
|
||||
import api from "./Api";
|
||||
import subscriptionManager from "./SubscriptionManager";
|
||||
|
||||
const delayMillis = 3000; // 3 seconds
|
||||
const intervalMillis = 300000; // 5 minutes
|
||||
|
@ -19,7 +20,7 @@ class Poller {
|
|||
|
||||
async pollAll() {
|
||||
console.log(`[Poller] Polling all subscriptions`);
|
||||
const subscriptions = await db.subscriptions.toArray();
|
||||
const subscriptions = await subscriptionManager.all();
|
||||
for (const s of subscriptions) {
|
||||
try {
|
||||
await this.poll(s);
|
||||
|
@ -38,11 +39,20 @@ class Poller {
|
|||
console.log(`[Poller] No new notifications found for ${subscription.id}`);
|
||||
return;
|
||||
}
|
||||
const notificationsWithSubscriptionId = notifications
|
||||
.map(notification => ({ ...notification, subscriptionId: subscription.id }));
|
||||
await db.notifications.bulkPut(notificationsWithSubscriptionId); // FIXME
|
||||
await db.subscriptions.update(subscription.id, {last: notifications.at(-1).id}); // FIXME
|
||||
};
|
||||
console.log(`[Poller] Adding ${notifications.length} notification(s) for ${subscription.id}`);
|
||||
await subscriptionManager.addNotifications(subscription.id, notifications);
|
||||
}
|
||||
|
||||
pollInBackground(subscription) {
|
||||
const fn = async () => {
|
||||
try {
|
||||
await this.poll(subscription);
|
||||
} catch (e) {
|
||||
console.error(`[App] Error polling subscription ${subscription.id}`, e);
|
||||
}
|
||||
};
|
||||
setTimeout(() => fn(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
const poller = new Poller();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import db from "./db";
|
||||
import prefs from "./Prefs";
|
||||
import subscriptionManager from "./SubscriptionManager";
|
||||
|
||||
const delayMillis = 15000; // 15 seconds
|
||||
const intervalMillis = 1800000; // 30 minutes
|
||||
|
@ -26,9 +26,7 @@ class Pruner {
|
|||
}
|
||||
console.log(`[Pruner] Pruning notifications older than ${deleteAfterSeconds}s (timestamp ${pruneThresholdTimestamp})`);
|
||||
try {
|
||||
await db.notifications
|
||||
.where("time").below(pruneThresholdTimestamp)
|
||||
.delete();
|
||||
await subscriptionManager.pruneNotifications(pruneThresholdTimestamp);
|
||||
} catch (e) {
|
||||
console.log(`[Pruner] Error pruning old subscriptions`, e);
|
||||
}
|
||||
|
|
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;
|
22
web/src/app/UserManager.js
Normal file
22
web/src/app/UserManager.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
import db from "./db";
|
||||
|
||||
class UserManager {
|
||||
async all() {
|
||||
return db.users.toArray();
|
||||
}
|
||||
|
||||
async get(baseUrl) {
|
||||
return db.users.get(baseUrl);
|
||||
}
|
||||
|
||||
async save(user) {
|
||||
await db.users.put(user);
|
||||
}
|
||||
|
||||
async delete(baseUrl) {
|
||||
await db.users.delete(baseUrl);
|
||||
}
|
||||
}
|
||||
|
||||
const userManager = new UserManager();
|
||||
export default userManager;
|
Loading…
Add table
Add a link
Reference in a new issue