Add Dexie for persistence; user management with dexie; this is the way
This commit is contained in:
parent
8036aa2942
commit
23d275acec
16 changed files with 285 additions and 494 deletions
|
@ -7,9 +7,11 @@ import {
|
|||
topicShortUrl,
|
||||
topicUrlJsonPollWithSince
|
||||
} from "./utils";
|
||||
import db from "./db";
|
||||
|
||||
class Api {
|
||||
async poll(baseUrl, topic, since, user) {
|
||||
async poll(baseUrl, topic, since) {
|
||||
const user = await db.users.get(baseUrl);
|
||||
const shortUrl = topicShortUrl(baseUrl, topic);
|
||||
const url = (since)
|
||||
? topicUrlJsonPollWithSince(baseUrl, topic, since)
|
||||
|
@ -24,7 +26,8 @@ class Api {
|
|||
return messages;
|
||||
}
|
||||
|
||||
async publish(baseUrl, topic, user, message) {
|
||||
async publish(baseUrl, topic, message) {
|
||||
const user = await db.users.get(baseUrl);
|
||||
const url = topicUrl(baseUrl, topic);
|
||||
console.log(`[Api] Publishing message to ${url}`);
|
||||
await fetch(url, {
|
||||
|
|
|
@ -85,7 +85,7 @@ class Connection {
|
|||
if (this.since) {
|
||||
params.push(`since=${this.since}`);
|
||||
}
|
||||
if (this.user !== null) {
|
||||
if (this.user) {
|
||||
const auth = encodeBase64Url(basicAuth(this.user.username, this.user.password));
|
||||
params.push(`auth=${auth}`);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,11 @@ class ConnectionManager {
|
|||
}
|
||||
|
||||
refresh(subscriptions, users, onNotification) {
|
||||
if (!subscriptions || !users) {
|
||||
return;
|
||||
}
|
||||
console.log(`[ConnectionManager] Refreshing connections`);
|
||||
console.log(users);
|
||||
const subscriptionIds = subscriptions.ids();
|
||||
const deletedIds = Array.from(this.connections.keys()).filter(id => !subscriptionIds.includes(id));
|
||||
|
||||
|
@ -16,7 +20,7 @@ class ConnectionManager {
|
|||
if (added) {
|
||||
const baseUrl = subscription.baseUrl;
|
||||
const topic = subscription.topic;
|
||||
const user = users.get(baseUrl);
|
||||
const [user] = users.filter(user => user.baseUrl === baseUrl);
|
||||
const since = subscription.last;
|
||||
const connection = new Connection(id, baseUrl, topic, user, since, onNotification);
|
||||
this.connections.set(id, connection);
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import Subscription from "./Subscription";
|
||||
import Subscriptions from "./Subscriptions";
|
||||
import Users from "./Users";
|
||||
import User from "./User";
|
||||
|
||||
class Repository {
|
||||
loadSubscriptions() {
|
||||
|
@ -43,40 +41,6 @@ class Repository {
|
|||
localStorage.setItem('subscriptions', serialized);
|
||||
}
|
||||
|
||||
loadUsers() {
|
||||
console.log(`[Repository] Loading users from localStorage`);
|
||||
const users = new Users();
|
||||
users.loaded = true;
|
||||
const serialized = localStorage.getItem('users');
|
||||
if (serialized === null) {
|
||||
return users;
|
||||
}
|
||||
try {
|
||||
JSON.parse(serialized).forEach(u => {
|
||||
users.add(new User(u.baseUrl, u.username, u.password));
|
||||
});
|
||||
return users;
|
||||
} catch (e) {
|
||||
console.log(`[Repository] Unable to deserialize users: ${e.message}`);
|
||||
return users;
|
||||
}
|
||||
}
|
||||
|
||||
saveUsers(users) {
|
||||
if (!users.loaded) {
|
||||
return; // Avoid saving invalid state, triggered by initial useEffect hook
|
||||
}
|
||||
console.log(`[Repository] Saving users to localStorage`);
|
||||
const serialized = JSON.stringify(users.map(user => {
|
||||
return {
|
||||
baseUrl: user.baseUrl,
|
||||
username: user.username,
|
||||
password: user.password
|
||||
}
|
||||
}));
|
||||
localStorage.setItem('users', serialized);
|
||||
}
|
||||
|
||||
loadSelectedSubscriptionId() {
|
||||
console.log(`[Repository] Loading selected subscription ID from localStorage`);
|
||||
const selectedSubscriptionId = localStorage.getItem('selectedSubscriptionId');
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
class User {
|
||||
constructor(baseUrl, username, password) {
|
||||
this.baseUrl = baseUrl;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
||||
export default User;
|
|
@ -1,38 +0,0 @@
|
|||
class Users {
|
||||
constructor() {
|
||||
this.loaded = false; // FIXME I hate this
|
||||
this.users = new Map();
|
||||
}
|
||||
|
||||
add(user) {
|
||||
this.users.set(user.baseUrl, user);
|
||||
return this;
|
||||
}
|
||||
|
||||
get(baseUrl) {
|
||||
const user = this.users.get(baseUrl);
|
||||
return (user) ? user : null;
|
||||
}
|
||||
|
||||
update(user) {
|
||||
return this.add(user);
|
||||
}
|
||||
|
||||
remove(baseUrl) {
|
||||
this.users.delete(baseUrl);
|
||||
return this;
|
||||
}
|
||||
|
||||
map(cb) {
|
||||
return Array.from(this.users.values()).map(cb);
|
||||
}
|
||||
|
||||
clone() {
|
||||
const c = new Users();
|
||||
c.loaded = this.loaded;
|
||||
c.users = new Map(this.users);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
export default Users;
|
15
web/src/app/db.js
Normal file
15
web/src/app/db.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
import Dexie from 'dexie';
|
||||
|
||||
// Uses Dexie.js
|
||||
// https://dexie.org/docs/API-Reference#quick-reference
|
||||
//
|
||||
// Notes:
|
||||
// - As per docs, we only declare the indexable columns, not all columns
|
||||
|
||||
const db = new Dexie('ntfy');
|
||||
|
||||
db.version(1).stores({
|
||||
users: '&baseUrl, username',
|
||||
});
|
||||
|
||||
export default db;
|
Loading…
Add table
Add a link
Reference in a new issue