No real changes, just renames

This commit is contained in:
binwiederhier 2023-06-09 14:32:34 -04:00
parent 4704b2a0e4
commit 2e8292a65f
10 changed files with 31 additions and 35 deletions

View file

@ -3,7 +3,7 @@ import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute } from
import { NavigationRoute, registerRoute } from "workbox-routing";
import { NetworkFirst } from "workbox-strategies";
import { getDbAsync } from "../src/app/getDb";
import { dbAsync } from "../src/app/db";
import { formatMessage, formatTitleWithDefault } from "../src/app/notificationUtils";
// See WebPushWorker, this is to play a sound on supported browsers,
@ -44,8 +44,7 @@ self.addEventListener("push", (event) => {
const { subscription_id: subscriptionId, message } = data;
broadcastChannel.postMessage(message);
const db = await getDbAsync();
const db = await dbAsync();
const image = message.attachment?.name.match(/\.(png|jpe?g|gif|webp)$/i) ? message.attachment.url : undefined;
const actions = message.actions

View file

@ -1,4 +1,4 @@
import getDb from "./getDb";
import db from "./db";
class Prefs {
constructor(db) {
@ -42,5 +42,5 @@ class Prefs {
}
}
const prefs = new Prefs(getDb());
const prefs = new Prefs(db());
export default prefs;

View file

@ -1,5 +1,9 @@
import sessionReplica from "./SessionReplica";
/**
* Manages the logged-in user's session and access token.
* The session replica is stored in IndexedDB so that the service worker can access it.
*/
class Session {
constructor(replica) {
this.replica = replica;
@ -8,14 +12,12 @@ class Session {
store(username, token) {
localStorage.setItem("user", username);
localStorage.setItem("token", token);
this.replica.store(username, token);
}
reset() {
localStorage.removeItem("user");
localStorage.removeItem("token");
this.replica.reset();
}

View file

@ -1,24 +1,21 @@
import Dexie from "dexie";
// Store to IndexedDB as well so that the
// service worker can access it
// TODO: Probably make everything depend on this and not use localStorage,
// but that's a larger refactoring effort for another PR
/**
* Replica of the session in IndexedDB. This is used by the service
* worker to access the session. This is a bit of a hack.
*/
class SessionReplica {
constructor() {
const db = new Dexie("session-replica");
db.version(1).stores({
keyValueStore: "&key",
kv: "&key",
});
this.db = db;
}
async store(username, token) {
try {
await this.db.keyValueStore.bulkPut([
await this.db.kv.bulkPut([
{ key: "user", value: username },
{ key: "token", value: token },
]);
@ -36,7 +33,7 @@ class SessionReplica {
}
async username() {
return (await this.db.keyValueStore.get({ key: "user" }))?.value;
return (await this.db.kv.get({ key: "user" }))?.value;
}
}

View file

@ -1,7 +1,7 @@
import api from "./Api";
import notifier from "./Notifier";
import prefs from "./Prefs";
import getDb from "./getDb";
import db from "./db";
import { topicUrl } from "./utils";
class SubscriptionManager {
@ -244,4 +244,4 @@ class SubscriptionManager {
}
}
export default new SubscriptionManager(getDb());
export default new SubscriptionManager(db());

View file

@ -1,4 +1,4 @@
import getDb from "./getDb";
import db from "./db";
import session from "./Session";
class UserManager {
@ -47,4 +47,4 @@ class UserManager {
}
}
export default new UserManager(getDb());
export default new UserManager(db());

View file

@ -26,7 +26,7 @@ export const useWebPushUpdateWorker = () => {
}, [topics, lastTopics]);
};
const intervalMillis = 5 * 60 * 1_000; // 5 minutes
const intervalMillis = 13 * 60 * 1_000; // 13 minutes
const updateIntervalMillis = 60 * 60 * 1_000; // 1 hour
class WebPushRefreshWorker {

View file

@ -8,12 +8,11 @@ import sessionReplica from "./SessionReplica";
// Notes:
// - As per docs, we only declare the indexable columns, not all columns
const getDbBase = (username) => {
// The IndexedDB database name is based on the logged-in user
const dbName = username ? `ntfy-${username}` : "ntfy";
const createDatabase = (username) => {
const dbName = username ? `ntfy-${username}` : "ntfy"; // IndexedDB database is based on the logged-in user
const db = new Dexie(dbName);
db.version(2).stores({
db.version(1).stores({
subscriptions: "&id,baseUrl,[baseUrl+mutedUntil]",
notifications: "&id,subscriptionId,time,new,[subscriptionId+new]", // compound key for query performance
users: "&baseUrl,username",
@ -23,12 +22,11 @@ const getDbBase = (username) => {
return db;
};
export const getDbAsync = async () => {
export const dbAsync = async () => {
const username = await sessionReplica.username();
return getDbBase(username);
return createDatabase(username);
};
const getDb = () => getDbBase(session.username());
export const db = () => createDatabase(session.username());
export default getDb;
export default db;

View file

@ -48,7 +48,7 @@ import routes from "./routes";
import { formatBytes, formatShortDate, formatShortDateTime, openUrl } from "../app/utils";
import accountApi, { LimitBasis, Role, SubscriptionInterval, SubscriptionStatus } from "../app/AccountApi";
import { Pref, PrefGroup } from "./Pref";
import getDb from "../app/getDb";
import db from "../app/db";
import UpgradeDialog from "./UpgradeDialog";
import { AccountContext } from "./App";
import DialogFooter from "./DialogFooter";
@ -1078,7 +1078,7 @@ const DeleteAccountDialog = (props) => {
const handleSubmit = async () => {
try {
await accountApi.delete(password);
await getDb().delete();
await db().delete();
console.debug(`[Account] Account deleted`);
session.resetAndRedirect(routes.app);
} catch (e) {

View file

@ -13,7 +13,7 @@ import session from "../app/Session";
import logo from "../img/ntfy.svg";
import subscriptionManager from "../app/SubscriptionManager";
import routes from "./routes";
import getDb from "../app/getDb";
import db from "../app/db";
import { topicDisplayName } from "../app/utils";
import Navigation from "./Navigation";
import accountApi from "../app/AccountApi";
@ -121,7 +121,7 @@ const ProfileIcon = () => {
const handleLogout = async () => {
try {
await accountApi.logout();
await getDb().delete();
await db().delete();
} finally {
session.resetAndRedirect(routes.app);
}