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";
|
2023-01-15 19:40:47 +00:00
|
|
|
import { HexKey } from "../nostr";
|
2022-12-29 22:23:41 +00:00
|
|
|
import EventKind from "../nostr/EventKind";
|
|
|
|
import { Subscriptions } from "../nostr/Subscriptions";
|
2023-01-12 09:48:39 +00:00
|
|
|
import { addDirectMessage, addNotifications, setFollows, setRelays } from "../state/Login";
|
2023-01-15 19:40:47 +00:00
|
|
|
import { RootState } from "../state/Store";
|
2023-01-15 01:46:13 +00:00
|
|
|
import { db } from "../db";
|
2023-01-01 10:44:38 +00:00
|
|
|
import useSubscription from "./Subscription";
|
2023-01-16 13:17:29 +00:00
|
|
|
import { mapEventToProfile } from "../db/User";
|
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-15 19:40:47 +00:00
|
|
|
const [pubKey, readNotifications] = useSelector<RootState, [HexKey | undefined, number]>(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-12 12:00:44 +00:00
|
|
|
if (!pubKey) {
|
2023-01-01 10:44:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let sub = new Subscriptions();
|
|
|
|
sub.Id = `login:${sub.Id}`;
|
2023-01-15 19:40:47 +00:00
|
|
|
sub.Authors = new Set([pubKey]);
|
|
|
|
sub.Kinds = new Set([EventKind.ContactList, EventKind.SetMetadata, EventKind.DirectMessage]);
|
2023-01-01 10:44:38 +00:00
|
|
|
|
|
|
|
let notifications = new Subscriptions();
|
2023-01-18 18:53:34 +00:00
|
|
|
notifications.Kinds = new Set([EventKind.TextNote]);
|
2023-01-15 19:40:47 +00:00
|
|
|
notifications.PTags = new Set([pubKey]);
|
2023-01-01 10:44:38 +00:00
|
|
|
notifications.Limit = 100;
|
|
|
|
sub.AddSubscription(notifications);
|
|
|
|
|
2023-01-18 18:53:34 +00:00
|
|
|
let dms = new Subscriptions();
|
|
|
|
dms.Kinds = new Set([EventKind.DirectMessage]);
|
|
|
|
dms.PTags = new Set([pubKey]);
|
|
|
|
sub.AddSubscription(dms);
|
|
|
|
|
2023-01-01 10:44:38 +00:00
|
|
|
return sub;
|
|
|
|
}, [pubKey]);
|
|
|
|
|
2023-01-15 19:40:47 +00:00
|
|
|
const main = useSubscription(sub, { leaveOpen: true });
|
2023-01-01 10:44:38 +00:00
|
|
|
|
2022-12-28 23:28:28 +00:00
|
|
|
useEffect(() => {
|
2023-01-15 19:40:47 +00:00
|
|
|
let contactList = main.notes.filter(a => a.kind === EventKind.ContactList);
|
|
|
|
let notifications = main.notes.filter(a => a.kind === EventKind.TextNote);
|
2023-01-15 22:59:05 +00:00
|
|
|
let metadata = main.notes.filter(a => a.kind === EventKind.SetMetadata);
|
|
|
|
let profiles = metadata.map(a => mapEventToProfile(a))
|
2023-01-15 19:40:47 +00:00
|
|
|
.filter(a => a !== undefined)
|
|
|
|
.map(a => a!);
|
|
|
|
let dms = main.notes.filter(a => a.kind === EventKind.DirectMessage);
|
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-14 18:31:42 +00:00
|
|
|
dispatch(setRelays({ relays, createdAt: cl.created_at }));
|
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
|
|
|
}
|
|
|
|
|
2023-01-06 15:10:41 +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)) {
|
2023-01-03 14:18:08 +00:00
|
|
|
//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));
|
2023-01-15 22:59:05 +00:00
|
|
|
db.users.bulkPut(profiles);
|
2023-01-12 09:48:39 +00:00
|
|
|
dispatch(addDirectMessage(dms));
|
2023-01-15 19:40:47 +00:00
|
|
|
}, [main]);
|
2022-12-28 23:28:28 +00:00
|
|
|
}
|