snort/src/feed/LoginFeed.js

62 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-01-01 10:44:38 +00:00
import { useEffect, useMemo } from "react";
2022-12-28 23:28:28 +00:00
import { useDispatch, useSelector } from "react-redux";
2022-12-29 22:23:41 +00:00
import EventKind from "../nostr/EventKind";
import { Subscriptions } from "../nostr/Subscriptions";
2022-12-30 23:35:02 +00:00
import { addNotifications, setFollows, setRelays } from "../state/Login";
2023-01-03 12:06:53 +00:00
import { setUserData } from "../state/Users";
2023-01-01 10:44:38 +00:00
import useSubscription from "./Subscription";
2023-01-03 12:06:53 +00:00
import { mapEventToProfile } from "./UsersFeed";
2022-12-28 23:28:28 +00:00
/**
* Managed loading data for the current logged in user
*/
export default function useLoginFeed() {
const dispatch = useDispatch();
2023-01-03 13:17:09 +00:00
const [pubKey, readNotifications] = useSelector(s => [s.login.publicKey, s.login.readNotifications]);
2022-12-28 23:28:28 +00:00
2023-01-01 10:44:38 +00:00
const sub = useMemo(() => {
2023-01-03 13:17:09 +00:00
if (pubKey === null) {
2023-01-01 10:44:38 +00:00
return null;
}
let sub = new Subscriptions();
sub.Id = `login:${sub.Id}`;
sub.Authors.add(pubKey);
sub.Kinds.add(EventKind.ContactList);
2023-01-03 12:06:53 +00:00
sub.Kinds.add(EventKind.SetMetadata);
2023-01-01 10:44:38 +00:00
let notifications = new Subscriptions();
notifications.Kinds.add(EventKind.TextNote);
notifications.PTags.add(pubKey);
notifications.Limit = 100;
sub.AddSubscription(notifications);
return sub;
}, [pubKey]);
const { notes } = useSubscription(sub, { leaveOpen: true });
2022-12-28 23:28:28 +00:00
useEffect(() => {
2023-01-03 12:06:53 +00:00
let contactList = notes.filter(a => a.kind === EventKind.ContactList);
let notifications = notes.filter(a => a.kind === EventKind.TextNote);
2023-01-03 13:17:09 +00:00
let metadata = notes.filter(a => a.kind === EventKind.SetMetadata).map(a => mapEventToProfile(a));
2023-01-01 10:44:38 +00:00
2023-01-03 13:17:09 +00:00
for (let cl of contactList) {
2023-01-03 12:06:53 +00:00
if (cl.content !== "") {
let relays = JSON.parse(cl.content);
2023-01-01 10:44:38 +00:00
dispatch(setRelays(relays));
2022-12-28 23:28:28 +00:00
}
2023-01-03 12:06:53 +00:00
let pTags = cl.tags.filter(a => a[0] === "p").map(a => a[1]);
2023-01-01 10:44:38 +00:00
dispatch(setFollows(pTags));
2022-12-28 23:28:28 +00:00
}
if ("Notification" in window && Notification.permission === "granted") {
2023-01-03 13:17:09 +00:00
for (let nx in notifications.filter(a => (a.created_at * 1000) > readNotifications)) {
//let n = new Notification(`New reply!`, { body: nx.content, icon: Nostrich });
//console.log(n);
2023-01-03 13:17:09 +00:00
}
}
2023-01-03 12:06:53 +00:00
dispatch(addNotifications(notifications));
dispatch(setUserData(metadata));
2023-01-01 10:44:38 +00:00
}, [notes]);
2022-12-28 23:28:28 +00:00
}