Dedup without keeping deleted array

pull/149/head
Philipp Heckel 2022-02-24 14:53:45 -05:00
parent 48523a2269
commit fef46823eb
5 changed files with 10 additions and 12 deletions

View File

@ -8,7 +8,7 @@ class Api {
for await (let line of fetchLinesIterator(url)) { for await (let line of fetchLinesIterator(url)) {
messages.push(JSON.parse(line)); messages.push(JSON.parse(line));
} }
return messages.sort((a, b) => { return a.time < b.time ? 1 : -1; }); // Newest first return messages;
} }
async publish(baseUrl, topic, message) { async publish(baseUrl, topic, message) {

View File

@ -40,7 +40,7 @@ class Connection {
console.log(`[Connection, ${this.shortUrl}] Message irrelevant or invalid. Ignoring.`); console.log(`[Connection, ${this.shortUrl}] Message irrelevant or invalid. Ignoring.`);
return; return;
} }
this.since = data.time; this.since = data.time + 1; // Sigh. This works because on reconnect, we wait 5+ seconds anyway.
this.onNotification(this.subscriptionId, data); this.onNotification(this.subscriptionId, data);
} catch (e) { } catch (e) {
console.log(`[Connection, ${this.shortUrl}] Error handling message: ${e}`); console.log(`[Connection, ${this.shortUrl}] Error handling message: ${e}`);

View File

@ -6,14 +6,15 @@ export default class Subscription {
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
this.topic = topic; this.topic = topic;
this.notifications = new Map(); // notification ID -> notification object this.notifications = new Map(); // notification ID -> notification object
this.deleted = new Set(); // notification IDs this.last = 0;
} }
addNotification(notification) { addNotification(notification) {
if (this.notifications.has(notification.id) || this.deleted.has(notification.id)) { if (this.notifications.has(notification.id) || notification.time < this.last) {
return this; return this;
} }
this.notifications.set(notification.id, notification); this.notifications.set(notification.id, notification);
this.last = notification.time;
return this; return this;
} }
@ -24,14 +25,11 @@ export default class Subscription {
deleteNotification(notificationId) { deleteNotification(notificationId) {
this.notifications.delete(notificationId); this.notifications.delete(notificationId);
this.deleted.add(notificationId);
return this; return this;
} }
deleteAllNotifications() { deleteAllNotifications() {
console.log(this.notifications);
for (const [id] of this.notifications) { for (const [id] of this.notifications) {
console.log(`delete ${id}`);
this.deleteNotification(id); this.deleteNotification(id);
} }
return this; return this;

View File

@ -20,7 +20,7 @@ import SettingsIcon from "@mui/icons-material/Settings";
import AddIcon from "@mui/icons-material/Add"; import AddIcon from "@mui/icons-material/Add";
import AddDialog from "./AddDialog"; import AddDialog from "./AddDialog";
import NotificationList from "./NotificationList"; import NotificationList from "./NotificationList";
import DetailSettingsIcon from "./DetailSettingsIcon"; import SubscriptionSettings from "./SubscriptionSettings";
import theme from "./theme"; import theme from "./theme";
import api from "../app/Api"; import api from "../app/Api";
import repository from "../app/Repository"; import repository from "../app/Repository";
@ -184,7 +184,7 @@ const App = () => {
> >
{(selectedSubscription !== null) ? selectedSubscription.shortUrl() : "ntfy"} {(selectedSubscription !== null) ? selectedSubscription.shortUrl() : "ntfy"}
</Typography> </Typography>
{selectedSubscription !== null && <DetailSettingsIcon {selectedSubscription !== null && <SubscriptionSettings
subscription={selectedSubscription} subscription={selectedSubscription}
onClearAll={handleClearAll} onClearAll={handleClearAll}
onUnsubscribe={handleUnsubscribe} onUnsubscribe={handleUnsubscribe}

View File

@ -11,7 +11,7 @@ import MoreVertIcon from "@mui/icons-material/MoreVert";
import api from "../app/Api"; import api from "../app/Api";
// Originally from https://mui.com/components/menus/#MenuListComposition.js // Originally from https://mui.com/components/menus/#MenuListComposition.js
const DetailSettingsIcon = (props) => { const SubscriptionSettings = (props) => {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const anchorRef = useRef(null); const anchorRef = useRef(null);
@ -39,7 +39,7 @@ const DetailSettingsIcon = (props) => {
const handleSendTestMessage = () => { const handleSendTestMessage = () => {
const baseUrl = props.subscription.baseUrl; const baseUrl = props.subscription.baseUrl;
const topic = props.subscription.topic; const topic = props.subscription.topic;
api.publish(baseUrl, topic, `This is a test notification sent by the ntfy.sh Web UI at ${new Date().toString()}.`); // FIXME result ignored api.publish(baseUrl, topic, `This is a test notification sent by the ntfy Web UI at ${new Date().toString()}.`); // FIXME result ignored
setOpen(false); setOpen(false);
} }
@ -114,4 +114,4 @@ const DetailSettingsIcon = (props) => {
); );
} }
export default DetailSettingsIcon; export default SubscriptionSettings;