snort/src/Feed/TimelineFeed.ts

178 lines
6.2 KiB
TypeScript
Raw Normal View History

2023-01-25 13:54:45 +00:00
import { useCallback, useEffect, useMemo, useState } from "react";
2023-01-20 11:11:50 +00:00
import { u256 } from "Nostr";
import EventKind from "Nostr/EventKind";
import { Subscriptions } from "Nostr/Subscriptions";
import { unixNow } from "Util";
import useSubscription from "Feed/Subscription";
2023-01-20 17:07:14 +00:00
import { useSelector } from "react-redux";
import { RootState } from "State/Store";
import { UserPreferences } from "State/Login";
2022-12-18 14:51:47 +00:00
2023-01-17 21:55:53 +00:00
export interface TimelineFeedOptions {
method: "TIME_RANGE" | "LIMIT_UNTIL",
window?: number
2023-01-17 21:55:53 +00:00
}
export interface TimelineSubject {
2023-01-24 12:33:18 +00:00
type: "pubkey" | "hashtag" | "global" | "ptag" | "keyword",
2023-02-01 13:22:11 +00:00
discriminator: string,
items: string[]
}
export default function useTimelineFeed(subject: TimelineSubject, options: TimelineFeedOptions) {
2023-01-17 21:55:53 +00:00
const now = unixNow();
const [window] = useState<number>(options.window ?? 60 * 60);
2023-01-17 21:55:53 +00:00
const [until, setUntil] = useState<number>(now);
const [since, setSince] = useState<number>(now - window);
2023-01-17 13:03:15 +00:00
const [trackingEvents, setTrackingEvent] = useState<u256[]>([]);
2023-01-24 14:09:56 +00:00
const [trackingParentEvents, setTrackingParentEvents] = useState<u256[]>([]);
2023-01-20 17:07:14 +00:00
const pref = useSelector<RootState, UserPreferences>(s => s.login.preferences);
2023-01-17 13:03:15 +00:00
2023-01-25 13:54:45 +00:00
const createSub = useCallback(() => {
if (subject.type !== "global" && subject.items.length === 0) {
2022-12-31 20:11:43 +00:00
return null;
}
2022-12-30 23:35:02 +00:00
let sub = new Subscriptions();
2023-02-01 13:22:11 +00:00
sub.Id = `timeline:${subject.type}:${subject.discriminator}`;
2023-02-04 22:43:04 +00:00
sub.Kinds = new Set([EventKind.TextNote, EventKind.Repost]);
switch (subject.type) {
case "pubkey": {
sub.Authors = new Set(subject.items);
break;
}
case "hashtag": {
sub.HashTags = new Set(subject.items);
break;
}
2023-01-22 11:17:50 +00:00
case "ptag": {
sub.PTags = new Set(subject.items);
break;
}
2023-01-24 12:33:18 +00:00
case "keyword": {
2023-01-28 16:31:03 +00:00
sub.Kinds.add(EventKind.SetMetadata);
2023-01-28 15:40:19 +00:00
sub.Search = subject.items[0];
2023-01-24 12:33:18 +00:00
break;
}
}
2023-01-21 16:09:35 +00:00
return sub;
2023-02-01 13:22:11 +00:00
}, [subject.type, subject.items, subject.discriminator]);
2023-01-21 16:09:35 +00:00
const sub = useMemo(() => {
let sub = createSub();
if (sub) {
if (options.method === "LIMIT_UNTIL") {
sub.Until = until;
sub.Limit = 10;
} else {
sub.Since = since;
sub.Until = until;
if (since === undefined) {
sub.Limit = 50;
}
2023-01-17 21:55:53 +00:00
}
2022-12-18 14:51:47 +00:00
2023-01-21 16:09:35 +00:00
if (pref.autoShowLatest) {
// copy properties of main sub but with limit 0
// this will put latest directly into main feed
let latestSub = new Subscriptions();
2023-01-21 23:07:43 +00:00
latestSub.Authors = sub.Authors;
latestSub.HashTags = sub.HashTags;
latestSub.PTags = sub.PTags;
2023-01-21 16:09:35 +00:00
latestSub.Kinds = sub.Kinds;
2023-01-28 15:40:19 +00:00
latestSub.Search = sub.Search;
2023-01-21 22:46:57 +00:00
latestSub.Limit = 1;
2023-01-22 11:17:50 +00:00
latestSub.Since = Math.floor(new Date().getTime() / 1000);
2023-01-21 16:09:35 +00:00
sub.AddSubscription(latestSub);
}
}
2022-12-30 23:35:02 +00:00
return sub;
2023-01-25 13:54:45 +00:00
}, [until, since, options.method, pref, createSub]);
2022-12-18 14:51:47 +00:00
2023-02-01 13:22:11 +00:00
const main = useSubscription(sub, { leaveOpen: true, cache: true });
2023-01-21 16:09:35 +00:00
const subRealtime = useMemo(() => {
let subLatest = createSub();
if (subLatest && !pref.autoShowLatest) {
subLatest.Id = `${subLatest.Id}:latest`;
2023-01-21 22:46:57 +00:00
subLatest.Limit = 1;
2023-01-22 11:17:50 +00:00
subLatest.Since = Math.floor(new Date().getTime() / 1000);
2023-01-21 16:09:35 +00:00
}
return subLatest;
2023-01-25 13:54:45 +00:00
}, [pref, createSub]);
2023-01-21 16:09:35 +00:00
2023-02-01 13:22:11 +00:00
const latest = useSubscription(subRealtime, { leaveOpen: true, cache: false });
2023-01-21 16:09:35 +00:00
const subNext = useMemo(() => {
2023-01-24 14:09:56 +00:00
let sub: Subscriptions | undefined;
2023-01-20 17:07:14 +00:00
if (trackingEvents.length > 0 && pref.enableReactions) {
2023-01-24 14:09:56 +00:00
sub = new Subscriptions();
sub.Id = `timeline-related:${subject.type}`;
2023-02-03 21:38:14 +00:00
sub.Kinds = new Set([EventKind.Reaction, EventKind.Deletion, EventKind.ZapReceipt]);
2023-01-17 13:03:15 +00:00
sub.ETags = new Set(trackingEvents);
}
2023-01-24 14:09:56 +00:00
return sub ?? null;
2023-01-25 13:54:45 +00:00
}, [trackingEvents, pref, subject.type]);
2023-02-01 13:22:11 +00:00
const others = useSubscription(subNext, { leaveOpen: true, cache: true });
2023-01-24 14:09:56 +00:00
const subParents = useMemo(() => {
if (trackingParentEvents.length > 0) {
let parents = new Subscriptions();
parents.Id = `timeline-parent:${subject.type}`;
parents.Ids = new Set(trackingParentEvents);
return parents;
}
return null;
2023-01-25 13:54:45 +00:00
}, [trackingParentEvents, subject.type]);
2023-01-24 14:09:56 +00:00
const parent = useSubscription(subParents);
2023-01-17 13:03:15 +00:00
useEffect(() => {
2023-01-21 16:09:35 +00:00
if (main.store.notes.length > 0) {
2023-01-17 21:55:53 +00:00
setTrackingEvent(s => {
2023-01-21 16:09:35 +00:00
let ids = main.store.notes.map(a => a.id);
2023-01-28 15:40:19 +00:00
if (ids.some(a => !s.includes(a))) {
2023-01-25 13:54:45 +00:00
return Array.from(new Set([...s, ...ids]));
}
return s;
2023-01-17 21:55:53 +00:00
});
2023-01-24 14:09:56 +00:00
let reposts = main.store.notes
.filter(a => a.kind === EventKind.Repost && a.content === "")
.map(a => a.tags.find(b => b[0] === "e"))
.filter(a => a)
.map(a => a![1]);
if (reposts.length > 0) {
setTrackingParentEvents(s => {
if (reposts.some(a => !s.includes(a))) {
let temp = new Set([...s, ...reposts]);
return Array.from(temp);
}
return s;
})
}
2023-01-17 13:03:15 +00:00
}
2023-01-21 16:09:35 +00:00
}, [main.store]);
2023-01-17 17:13:22 +00:00
return {
2023-01-21 16:09:35 +00:00
main: main.store,
related: others.store,
latest: latest.store,
2023-01-24 14:09:56 +00:00
parent: parent.store,
2023-01-17 17:13:22 +00:00
loadMore: () => {
2023-01-21 16:09:35 +00:00
console.debug("Timeline load more!")
2023-01-17 21:55:53 +00:00
if (options.method === "LIMIT_UNTIL") {
2023-01-21 16:09:35 +00:00
let oldest = main.store.notes.reduce((acc, v) => acc = v.created_at < acc ? v.created_at : acc, unixNow());
2023-01-17 21:55:53 +00:00
setUntil(oldest);
} else {
setUntil(s => s - window);
setSince(s => s - window);
}
2023-01-21 16:09:35 +00:00
},
showLatest: () => {
main.append(latest.store.notes);
latest.clear();
2023-01-17 21:55:53 +00:00
}
2023-01-17 17:13:22 +00:00
};
2023-01-24 12:33:18 +00:00
}