No real changes, just renames

pull/751/head
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 { NavigationRoute, registerRoute } from "workbox-routing";
import { NetworkFirst } from "workbox-strategies"; import { NetworkFirst } from "workbox-strategies";
import { getDbAsync } from "../src/app/getDb"; import { dbAsync } from "../src/app/db";
import { formatMessage, formatTitleWithDefault } from "../src/app/notificationUtils"; import { formatMessage, formatTitleWithDefault } from "../src/app/notificationUtils";
// See WebPushWorker, this is to play a sound on supported browsers, // 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; const { subscription_id: subscriptionId, message } = data;
broadcastChannel.postMessage(message); 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 image = message.attachment?.name.match(/\.(png|jpe?g|gif|webp)$/i) ? message.attachment.url : undefined;
const actions = message.actions const actions = message.actions

View File

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

View File

@ -1,5 +1,9 @@
import sessionReplica from "./SessionReplica"; 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 { class Session {
constructor(replica) { constructor(replica) {
this.replica = replica; this.replica = replica;
@ -8,14 +12,12 @@ class Session {
store(username, token) { store(username, token) {
localStorage.setItem("user", username); localStorage.setItem("user", username);
localStorage.setItem("token", token); localStorage.setItem("token", token);
this.replica.store(username, token); this.replica.store(username, token);
} }
reset() { reset() {
localStorage.removeItem("user"); localStorage.removeItem("user");
localStorage.removeItem("token"); localStorage.removeItem("token");
this.replica.reset(); this.replica.reset();
} }

View File

@ -1,24 +1,21 @@
import Dexie from "dexie"; import Dexie from "dexie";
// Store to IndexedDB as well so that the /**
// service worker can access it * Replica of the session in IndexedDB. This is used by the service
// TODO: Probably make everything depend on this and not use localStorage, * worker to access the session. This is a bit of a hack.
// but that's a larger refactoring effort for another PR */
class SessionReplica { class SessionReplica {
constructor() { constructor() {
const db = new Dexie("session-replica"); const db = new Dexie("session-replica");
db.version(1).stores({ db.version(1).stores({
keyValueStore: "&key", kv: "&key",
}); });
this.db = db; this.db = db;
} }
async store(username, token) { async store(username, token) {
try { try {
await this.db.keyValueStore.bulkPut([ await this.db.kv.bulkPut([
{ key: "user", value: username }, { key: "user", value: username },
{ key: "token", value: token }, { key: "token", value: token },
]); ]);
@ -36,7 +33,7 @@ class SessionReplica {
} }
async username() { 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 api from "./Api";
import notifier from "./Notifier"; import notifier from "./Notifier";
import prefs from "./Prefs"; import prefs from "./Prefs";
import getDb from "./getDb"; import db from "./db";
import { topicUrl } from "./utils"; import { topicUrl } from "./utils";
class SubscriptionManager { 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"; import session from "./Session";
class UserManager { 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]); }, [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 const updateIntervalMillis = 60 * 60 * 1_000; // 1 hour
class WebPushRefreshWorker { class WebPushRefreshWorker {

View File

@ -8,12 +8,11 @@ import sessionReplica from "./SessionReplica";
// Notes: // Notes:
// - As per docs, we only declare the indexable columns, not all columns // - As per docs, we only declare the indexable columns, not all columns
const getDbBase = (username) => { const createDatabase = (username) => {
// The IndexedDB database name is based on the logged-in user const dbName = username ? `ntfy-${username}` : "ntfy"; // IndexedDB database is based on the logged-in user
const dbName = username ? `ntfy-${username}` : "ntfy";
const db = new Dexie(dbName); const db = new Dexie(dbName);
db.version(2).stores({ db.version(1).stores({
subscriptions: "&id,baseUrl,[baseUrl+mutedUntil]", subscriptions: "&id,baseUrl,[baseUrl+mutedUntil]",
notifications: "&id,subscriptionId,time,new,[subscriptionId+new]", // compound key for query performance notifications: "&id,subscriptionId,time,new,[subscriptionId+new]", // compound key for query performance
users: "&baseUrl,username", users: "&baseUrl,username",
@ -23,12 +22,11 @@ const getDbBase = (username) => {
return db; return db;
}; };
export const getDbAsync = async () => { export const dbAsync = async () => {
const username = await sessionReplica.username(); const username = await sessionReplica.username();
return createDatabase(username);
return getDbBase(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 { formatBytes, formatShortDate, formatShortDateTime, openUrl } from "../app/utils";
import accountApi, { LimitBasis, Role, SubscriptionInterval, SubscriptionStatus } from "../app/AccountApi"; import accountApi, { LimitBasis, Role, SubscriptionInterval, SubscriptionStatus } from "../app/AccountApi";
import { Pref, PrefGroup } from "./Pref"; import { Pref, PrefGroup } from "./Pref";
import getDb from "../app/getDb"; import db from "../app/db";
import UpgradeDialog from "./UpgradeDialog"; import UpgradeDialog from "./UpgradeDialog";
import { AccountContext } from "./App"; import { AccountContext } from "./App";
import DialogFooter from "./DialogFooter"; import DialogFooter from "./DialogFooter";
@ -1078,7 +1078,7 @@ const DeleteAccountDialog = (props) => {
const handleSubmit = async () => { const handleSubmit = async () => {
try { try {
await accountApi.delete(password); await accountApi.delete(password);
await getDb().delete(); await db().delete();
console.debug(`[Account] Account deleted`); console.debug(`[Account] Account deleted`);
session.resetAndRedirect(routes.app); session.resetAndRedirect(routes.app);
} catch (e) { } catch (e) {

View File

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