1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-27 05:35:07 +01:00
ntfy/web/src/app/ConnectionManager.js

125 lines
4.4 KiB
JavaScript
Raw Normal View History

import Connection from "./Connection";
import { NotificationType } from "./SubscriptionManager";
2023-05-23 21:13:01 +02:00
import { hashCode } from "./utils";
const makeConnectionId = (subscription, user) =>
user ? hashCode(`${subscription.id}|${user.username}|${user.password ?? ""}|${user.token ?? ""}`) : hashCode(`${subscription.id}`);
2022-03-11 21:17:12 +01:00
/**
* The connection manager keeps track of active connections (WebSocket connections, see Connection).
*
* Its refresh() method reconciles state changes with the target state by closing/opening connections
* as required. This is done pretty much exactly the same way as in the Android app.
*/
class ConnectionManager {
2023-05-23 21:13:01 +02:00
constructor() {
this.connections = new Map(); // ConnectionId -> Connection (hash, see below)
this.stateListener = null; // Fired when connection state changes
this.messageListener = null; // Fired when new notifications arrive
}
2023-05-23 21:13:01 +02:00
registerStateListener(listener) {
this.stateListener = listener;
}
2023-05-23 21:13:01 +02:00
resetStateListener() {
this.stateListener = null;
}
2023-05-23 21:13:01 +02:00
registerMessageListener(listener) {
this.messageListener = listener;
}
2023-05-23 21:13:01 +02:00
resetMessageListener() {
this.messageListener = null;
}
2023-05-23 21:13:01 +02:00
/**
* This function figures out which websocket connections should be running by comparing the
* current state of the world (connections) with the target state (targetIds).
*
* It uses a "connectionId", which is sha256($subscriptionId|$username|$password) to identify
* connections. If any of them change, the connection is closed/replaced.
*/
async refresh(subscriptions, users) {
if (!subscriptions || !users) {
return;
}
console.log(`[ConnectionManager] Refreshing connections`);
const subscriptionsWithUsersAndConnectionId = subscriptions
.map((s) => {
2023-05-23 21:13:01 +02:00
const [user] = users.filter((u) => u.baseUrl === s.baseUrl);
const connectionId = makeConnectionId(s, user);
2023-05-23 21:13:01 +02:00
return { ...s, user, connectionId };
})
// we want to create a ws for both sound-only and active browser notifications,
// only background notifications don't need this as they come over web push.
// however, if background notifications are muted, we again need the ws while
// the page is active
.filter((s) => s.notificationType !== NotificationType.BACKGROUND && s.mutedUntil !== 1);
console.log();
2023-05-24 01:29:47 +02:00
const targetIds = subscriptionsWithUsersAndConnectionId.map((s) => s.connectionId);
const deletedIds = Array.from(this.connections.keys()).filter((id) => !targetIds.includes(id));
2023-05-23 21:13:01 +02:00
// Create and add new connections
subscriptionsWithUsersAndConnectionId.forEach((subscription) => {
const subscriptionId = subscription.id;
2023-05-24 09:03:28 +02:00
const { connectionId } = subscription;
2023-05-23 21:13:01 +02:00
const added = !this.connections.get(connectionId);
if (added) {
2023-05-24 20:47:19 +02:00
const { baseUrl, topic, user } = subscription;
2023-05-23 21:13:01 +02:00
const since = subscription.last;
const connection = new Connection(
connectionId,
subscriptionId,
baseUrl,
topic,
user,
since,
(subId, notification) => this.notificationReceived(subId, notification),
(subId, state) => this.stateChanged(subId, state)
2023-05-23 21:13:01 +02:00
);
this.connections.set(connectionId, connection);
console.log(
2023-05-24 02:16:29 +02:00
`[ConnectionManager] Starting new connection ${connectionId} (subscription ${subscriptionId} with user ${
user ? user.username : "anonymous"
})`
2023-05-23 21:13:01 +02:00
);
connection.start();
}
});
2023-05-23 21:13:01 +02:00
// Delete old connections
deletedIds.forEach((id) => {
console.log(`[ConnectionManager] Closing connection ${id}`);
const connection = this.connections.get(id);
this.connections.delete(id);
connection.close();
});
}
2023-05-23 21:13:01 +02:00
stateChanged(subscriptionId, state) {
if (this.stateListener) {
try {
this.stateListener(subscriptionId, state);
} catch (e) {
2023-05-24 01:29:47 +02:00
console.error(`[ConnectionManager] Error updating state of ${subscriptionId} to ${state}`, e);
2023-05-23 21:13:01 +02:00
}
}
2023-05-23 21:13:01 +02:00
}
2023-05-23 21:13:01 +02:00
notificationReceived(subscriptionId, notification) {
if (this.messageListener) {
try {
this.messageListener(subscriptionId, notification);
} catch (e) {
2023-05-24 01:29:47 +02:00
console.error(`[ConnectionManager] Error handling notification for ${subscriptionId}`, e);
2023-05-23 21:13:01 +02:00
}
}
2023-05-23 21:13:01 +02:00
}
}
const connectionManager = new ConnectionManager();
export default connectionManager;