Conn state listener, click action button

pull/149/head
Philipp Heckel 2022-03-04 11:08:32 -05:00
parent 3bce0ad4ae
commit 5878d7e5a6
8 changed files with 120 additions and 27 deletions

View File

@ -1,9 +1,9 @@
import {basicAuth, encodeBase64Url, topicShortUrl, topicUrlWs} from "./utils"; import {basicAuth, encodeBase64Url, topicShortUrl, topicUrlWs} from "./utils";
const retryBackoffSeconds = [5, 10, 15, 20, 30, 45]; const retryBackoffSeconds = [5, 10, 15, 20, 30];
class Connection { class Connection {
constructor(connectionId, subscriptionId, baseUrl, topic, user, since, onNotification) { constructor(connectionId, subscriptionId, baseUrl, topic, user, since, onNotification, onStateChanged) {
this.connectionId = connectionId; this.connectionId = connectionId;
this.subscriptionId = subscriptionId; this.subscriptionId = subscriptionId;
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
@ -12,6 +12,7 @@ class Connection {
this.since = since; this.since = since;
this.shortUrl = topicShortUrl(baseUrl, topic); this.shortUrl = topicShortUrl(baseUrl, topic);
this.onNotification = onNotification; this.onNotification = onNotification;
this.onStateChanged = onStateChanged;
this.ws = null; this.ws = null;
this.retryCount = 0; this.retryCount = 0;
this.retryTimeout = null; this.retryTimeout = null;
@ -28,6 +29,7 @@ class Connection {
this.ws.onopen = (event) => { this.ws.onopen = (event) => {
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Connection established`, event); console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Connection established`, event);
this.retryCount = 0; this.retryCount = 0;
this.onStateChanged(this.subscriptionId, ConnectionState.Connected);
} }
this.ws.onmessage = (event) => { this.ws.onmessage = (event) => {
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Message received from server: ${event.data}`); console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Message received from server: ${event.data}`);
@ -60,6 +62,7 @@ class Connection {
this.retryCount++; this.retryCount++;
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Connection died, retrying in ${retrySeconds} seconds`); console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Connection died, retrying in ${retrySeconds} seconds`);
this.retryTimeout = setTimeout(() => this.start(), retrySeconds * 1000); this.retryTimeout = setTimeout(() => this.start(), retrySeconds * 1000);
this.onStateChanged(this.subscriptionId, ConnectionState.Connecting);
} }
}; };
this.ws.onerror = (event) => { this.ws.onerror = (event) => {
@ -95,4 +98,9 @@ class Connection {
} }
} }
export class ConnectionState {
static Connected = "connected";
static Connecting = "connecting";
}
export default Connection; export default Connection;

View File

@ -3,10 +3,36 @@ import {sha256} from "./utils";
class ConnectionManager { class ConnectionManager {
constructor() { constructor() {
console.log(`connection manager`)
this.connections = new Map(); // ConnectionId -> Connection (hash, see below) this.connections = new Map(); // ConnectionId -> Connection (hash, see below)
this.stateListener = null; // Fired when connection state changes
this.notificationListener = null; // Fired when new notifications arrive
} }
async refresh(subscriptions, users, onNotification) { registerStateListener(listener) {
this.stateListener = listener;
}
resetStateListener() {
this.stateListener = null;
}
registerNotificationListener(listener) {
this.notificationListener = listener;
}
resetNotificationListener() {
this.notificationListener = null;
}
/**
* 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) { if (!subscriptions || !users) {
return; return;
} }
@ -17,10 +43,9 @@ class ConnectionManager {
const connectionId = await makeConnectionId(s, user); const connectionId = await makeConnectionId(s, user);
return {...s, user, connectionId}; return {...s, user, connectionId};
})); }));
const activeIds = subscriptionsWithUsersAndConnectionId.map(s => s.connectionId); const targetIds = subscriptionsWithUsersAndConnectionId.map(s => s.connectionId);
const deletedIds = Array.from(this.connections.keys()).filter(id => !activeIds.includes(id)); const deletedIds = Array.from(this.connections.keys()).filter(id => !targetIds.includes(id));
console.log(subscriptionsWithUsersAndConnectionId);
// Create and add new connections // Create and add new connections
subscriptionsWithUsersAndConnectionId.forEach(subscription => { subscriptionsWithUsersAndConnectionId.forEach(subscription => {
const subscriptionId = subscription.id; const subscriptionId = subscription.id;
@ -31,7 +56,16 @@ class ConnectionManager {
const topic = subscription.topic; const topic = subscription.topic;
const user = subscription.user; const user = subscription.user;
const since = subscription.last; const since = subscription.last;
const connection = new Connection(connectionId, subscriptionId, baseUrl, topic, user, since, onNotification); const connection = new Connection(
connectionId,
subscriptionId,
baseUrl,
topic,
user,
since,
(subscriptionId, notification) => this.notificationReceived(subscriptionId, notification),
(subscriptionId, state) => this.stateChanged(subscriptionId, state)
);
this.connections.set(connectionId, connection); this.connections.set(connectionId, connection);
console.log(`[ConnectionManager] Starting new connection ${connectionId} (subscription ${subscriptionId} with user ${user ? user.username : "anonymous"})`); console.log(`[ConnectionManager] Starting new connection ${connectionId} (subscription ${subscriptionId} with user ${user ? user.username : "anonymous"})`);
connection.start(); connection.start();
@ -46,6 +80,18 @@ class ConnectionManager {
connection.close(); connection.close();
}); });
} }
stateChanged(subscriptionId, state) {
if (this.stateListener) {
this.stateListener(subscriptionId, state);
}
}
notificationReceived(subscriptionId, notification) {
if (this.notificationListener) {
this.notificationListener(subscriptionId, notification);
}
}
} }
const makeConnectionId = async (subscription, user) => { const makeConnectionId = async (subscription, user) => {

View File

@ -1,4 +1,4 @@
import {formatMessage, formatTitleWithFallback, topicShortUrl} from "./utils"; import {formatMessage, formatTitleWithFallback, openUrl, topicShortUrl} from "./utils";
import prefs from "./Prefs"; import prefs from "./Prefs";
import subscriptionManager from "./SubscriptionManager"; import subscriptionManager from "./SubscriptionManager";
@ -19,7 +19,7 @@ class NotificationManager {
icon: '/static/img/favicon.png' icon: '/static/img/favicon.png'
}); });
if (notification.click) { if (notification.click) {
n.onclick = (e) => window.open(notification.click); n.onclick = (e) => openUrl(notification.click);
} else { } else {
n.onclick = onClickFallback; n.onclick = onClickFallback;
} }

View File

@ -13,6 +13,11 @@ class SubscriptionManager {
await db.subscriptions.put(subscription); await db.subscriptions.put(subscription);
} }
async updateState(subscriptionId, state) {
console.log(`Update state: ${subscriptionId} ${state}`)
db.subscriptions.update(subscriptionId, { state: state });
}
async remove(subscriptionId) { async remove(subscriptionId) {
await db.subscriptions.delete(subscriptionId); await db.subscriptions.delete(subscriptionId);
await db.notifications await db.notifications

View File

@ -110,6 +110,10 @@ export const formatBytes = (bytes, decimals = 2) => {
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
} }
export const openUrl = (url) => {
window.open(url, "_blank", "noopener,noreferrer");
};
// From: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch // From: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
export async function* fetchLinesIterator(fileURL, headers) { export async function* fetchLinesIterator(fileURL, headers) {
const utf8Decoder = new TextDecoder('utf-8'); const utf8Decoder = new TextDecoder('utf-8');

View File

@ -23,11 +23,8 @@ import userManager from "../app/UserManager";
// TODO make default server functional // TODO make default server functional
// TODO routing // TODO routing
// TODO embed into ntfy server // TODO embed into ntfy server
// TODO connection indicator in subscription list
const App = () => { const App = () => {
console.log(`[App] Rendering main view`);
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false); const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
const [prefsOpen, setPrefsOpen] = useState(false); const [prefsOpen, setPrefsOpen] = useState(false);
const [selectedSubscription, setSelectedSubscription] = useState(null); const [selectedSubscription, setSelectedSubscription] = useState(null);
@ -75,18 +72,26 @@ const App = () => {
setTimeout(() => load(), 5000); setTimeout(() => load(), 5000);
}, [/* initial render */]); }, [/* initial render */]);
useEffect(() => { useEffect(() => {
const notificationClickFallback = (subscription) => setSelectedSubscription(subscription);
const handleNotification = async (subscriptionId, notification) => { const handleNotification = async (subscriptionId, notification) => {
try { try {
const added = await subscriptionManager.addNotification(subscriptionId, notification); const added = await subscriptionManager.addNotification(subscriptionId, notification);
if (added) { if (added) {
await notificationManager.notify(subscriptionId, notification, notificationClickFallback) const defaultClickAction = (subscription) => setSelectedSubscription(subscription);
await notificationManager.notify(subscriptionId, notification, defaultClickAction)
} }
} catch (e) { } catch (e) {
console.error(`[App] Error handling notification`, e); console.error(`[App] Error handling notification`, e);
} }
}; };
connectionManager.refresh(subscriptions, users, handleNotification); // Dangle connectionManager.registerStateListener(subscriptionManager.updateState);
connectionManager.registerNotificationListener(handleNotification);
return () => {
connectionManager.resetStateListener();
connectionManager.resetNotificationListener();
}
}, [/* initial render */]);
useEffect(() => {
connectionManager.refresh(subscriptions, users); // Dangle
}, [subscriptions, users]); }, [subscriptions, users]);
useEffect(() => { useEffect(() => {
const subscriptionId = (selectedSubscription) ? selectedSubscription.id : ""; const subscriptionId = (selectedSubscription) ? selectedSubscription.id : "";

View File

@ -11,10 +11,11 @@ import List from "@mui/material/List";
import SettingsIcon from "@mui/icons-material/Settings"; import SettingsIcon from "@mui/icons-material/Settings";
import AddIcon from "@mui/icons-material/Add"; import AddIcon from "@mui/icons-material/Add";
import SubscribeDialog from "./SubscribeDialog"; import SubscribeDialog from "./SubscribeDialog";
import {Alert, AlertTitle, ListSubheader} from "@mui/material"; import {Alert, AlertTitle, CircularProgress, ListSubheader} from "@mui/material";
import Button from "@mui/material/Button"; import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import {topicShortUrl} from "../app/utils"; import {topicShortUrl} from "../app/utils";
import {ConnectionState} from "../app/Connection";
const navWidth = 240; const navWidth = 240;
@ -117,19 +118,29 @@ const SubscriptionList = (props) => {
return ( return (
<> <>
{props.subscriptions.map(subscription => {props.subscriptions.map(subscription =>
<ListItemButton <SubscriptionItem
key={subscription.id} key={subscription.id}
onClick={() => props.onSubscriptionClick(subscription.id)} subscription={subscription}
selected={props.selectedSubscription && !props.prefsOpen && props.selectedSubscription.id === subscription.id} selected={props.selectedSubscription && !props.prefsOpen && props.selectedSubscription.id === subscription.id}
> onClick={() => props.onSubscriptionClick(subscription.id)}
<ListItemIcon><ChatBubbleOutlineIcon /></ListItemIcon> />)}
<ListItemText primary={topicShortUrl(subscription.baseUrl, subscription.topic)}/>
</ListItemButton>
)}
</> </>
); );
} }
const SubscriptionItem = (props) => {
const subscription = props.subscription;
const icon = (subscription.state === ConnectionState.Connecting)
? <CircularProgress size="24px"/>
: <ChatBubbleOutlineIcon/>;
return (
<ListItemButton onClick={props.onClick} selected={props.selected}>
<ListItemIcon>{icon}</ListItemIcon>
<ListItemText primary={topicShortUrl(subscription.baseUrl, subscription.topic)}/>
</ListItemButton>
);
};
const PermissionAlert = (props) => { const PermissionAlert = (props) => {
return ( return (
<> <>

View File

@ -4,7 +4,15 @@ import Card from "@mui/material/Card";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import * as React from "react"; import * as React from "react";
import {useState} from "react"; import {useState} from "react";
import {formatBytes, formatMessage, formatShortDateTime, formatTitle, topicShortUrl, unmatchedTags} from "../app/utils"; import {
formatBytes,
formatMessage,
formatShortDateTime,
formatTitle,
openUrl,
topicShortUrl,
unmatchedTags
} from "../app/utils";
import IconButton from "@mui/material/IconButton"; import IconButton from "@mui/material/IconButton";
import CloseIcon from '@mui/icons-material/Close'; import CloseIcon from '@mui/icons-material/Close';
import {LightboxBackdrop, Paragraph, VerticallyCenteredContainer} from "./styles"; import {LightboxBackdrop, Paragraph, VerticallyCenteredContainer} from "./styles";
@ -49,6 +57,9 @@ const NotificationItem = (props) => {
await subscriptionManager.deleteNotification(notification.id) await subscriptionManager.deleteNotification(notification.id)
} }
const expired = attachment && attachment.expires && attachment.expires < Date.now()/1000; const expired = attachment && attachment.expires && attachment.expires < Date.now()/1000;
const showAttachmentActions = attachment && !expired;
const showClickAction = notification.click;
const showActions = showAttachmentActions || showClickAction;
return ( return (
<Card sx={{ minWidth: 275, padding: 1 }}> <Card sx={{ minWidth: 275, padding: 1 }}>
<CardContent> <CardContent>
@ -69,10 +80,13 @@ const NotificationItem = (props) => {
{attachment && <Attachment attachment={attachment}/>} {attachment && <Attachment attachment={attachment}/>}
{tags && <Typography sx={{ fontSize: 14 }} color="text.secondary">Tags: {tags}</Typography>} {tags && <Typography sx={{ fontSize: 14 }} color="text.secondary">Tags: {tags}</Typography>}
</CardContent> </CardContent>
{attachment && !expired && {showActions &&
<CardActions sx={{paddingTop: 0}}> <CardActions sx={{paddingTop: 0}}>
<Button onClick={() => navigator.clipboard.writeText(attachment.url)}>Copy URL</Button> {showAttachmentActions && <>
<Button onClick={() => window.open(attachment.url)}>Open</Button> <Button onClick={() => navigator.clipboard.writeText(attachment.url)}>Copy URL</Button>
<Button onClick={() => openUrl(attachment.url)}>Open attachment</Button>
</>}
{showClickAction && <Button onClick={() => openUrl(notification.click)}>Open link</Button>}
</CardActions> </CardActions>
} }
</Card> </Card>