mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-06-02 11:30:35 +02:00
Make connections react on changes of users; this works wonderfully
This commit is contained in:
parent
08846e4cc2
commit
695e029147
4 changed files with 49 additions and 31 deletions
web/src/app
|
@ -1,30 +1,39 @@
|
|||
import Connection from "./Connection";
|
||||
import {sha256} from "./utils";
|
||||
|
||||
class ConnectionManager {
|
||||
constructor() {
|
||||
this.connections = new Map();
|
||||
this.connections = new Map(); // ConnectionId -> Connection (hash, see below)
|
||||
}
|
||||
|
||||
refresh(subscriptions, users, onNotification) {
|
||||
async refresh(subscriptions, users, onNotification) {
|
||||
if (!subscriptions || !users) {
|
||||
return;
|
||||
}
|
||||
console.log(`[ConnectionManager] Refreshing connections`);
|
||||
const subscriptionIds = subscriptions.map(s => s.id);
|
||||
const deletedIds = Array.from(this.connections.keys()).filter(id => !subscriptionIds.includes(id));
|
||||
const subscriptionsWithUsersAndConnectionId = await Promise.all(subscriptions
|
||||
.map(async s => {
|
||||
const [user] = users.filter(u => u.baseUrl === s.baseUrl);
|
||||
const connectionId = await makeConnectionId(s, user);
|
||||
return {...s, user, connectionId};
|
||||
}));
|
||||
const activeIds = subscriptionsWithUsersAndConnectionId.map(s => s.connectionId);
|
||||
const deletedIds = Array.from(this.connections.keys()).filter(id => !activeIds.includes(id));
|
||||
|
||||
console.log(subscriptionsWithUsersAndConnectionId);
|
||||
// Create and add new connections
|
||||
subscriptions.forEach(subscription => {
|
||||
const id = subscription.id;
|
||||
const added = !this.connections.get(id)
|
||||
subscriptionsWithUsersAndConnectionId.forEach(subscription => {
|
||||
const subscriptionId = subscription.id;
|
||||
const connectionId = subscription.connectionId;
|
||||
const added = !this.connections.get(connectionId)
|
||||
if (added) {
|
||||
const baseUrl = subscription.baseUrl;
|
||||
const topic = subscription.topic;
|
||||
const [user] = users.filter(user => user.baseUrl === baseUrl);
|
||||
const user = subscription.user;
|
||||
const since = subscription.last;
|
||||
const connection = new Connection(id, baseUrl, topic, user, since, onNotification);
|
||||
this.connections.set(id, connection);
|
||||
console.log(`[ConnectionManager] Starting new connection ${id}`);
|
||||
const connection = new Connection(connectionId, subscriptionId, baseUrl, topic, user, since, onNotification);
|
||||
this.connections.set(connectionId, connection);
|
||||
console.log(`[ConnectionManager] Starting new connection ${connectionId} (subscription ${subscriptionId} with user ${user ? user.username : "anonymous"})`);
|
||||
connection.start();
|
||||
}
|
||||
});
|
||||
|
@ -39,5 +48,12 @@ class ConnectionManager {
|
|||
}
|
||||
}
|
||||
|
||||
const makeConnectionId = async (subscription, user) => {
|
||||
const hash = (user)
|
||||
? await sha256(`${subscription.id}|${user.username}|${user.password}`)
|
||||
: await sha256(`${subscription.id}`);
|
||||
return hash.substring(0, 10);
|
||||
}
|
||||
|
||||
const connectionManager = new ConnectionManager();
|
||||
export default connectionManager;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue