snort/packages/app/src/Feed/TimelineFeed.ts

203 lines
5.9 KiB
TypeScript
Raw Normal View History

2023-01-25 13:54:45 +00:00
import { useCallback, useEffect, useMemo, useState } from "react";
2023-03-28 14:34:01 +00:00
import { EventKind, u256 } from "@snort/nostr";
import { unixNow, unwrap, tagFilterOfTextRepost } from "Util";
import { FlatNoteStore, RequestBuilder } from "System";
import useRequestBuilder from "Hooks/useRequestBuilder";
import useTimelineWindow from "Hooks/useTimelineWindow";
2023-04-14 11:33:19 +00:00
import useLogin from "Hooks/useLogin";
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;
relay?: string;
2023-03-28 14:34:01 +00:00
now?: number;
2023-01-17 21:55:53 +00:00
}
export interface TimelineSubject {
type: "pubkey" | "hashtag" | "global" | "ptag" | "keyword";
discriminator: string;
items: string[];
}
2023-03-28 14:34:01 +00:00
export type TimelineFeed = ReturnType<typeof useTimelineFeed>;
2023-02-09 12:26:54 +00:00
export default function useTimelineFeed(subject: TimelineSubject, options: TimelineFeedOptions) {
2023-03-28 14:34:01 +00:00
const { now, since, until, older, setUntil } = useTimelineWindow({
window: options.window,
now: options.now ?? unixNow(),
});
const [trackingEvents, setTrackingEvent] = useState<u256[]>([]);
const [trackingParentEvents, setTrackingParentEvents] = useState<u256[]>([]);
2023-04-14 11:33:19 +00:00
const pref = useLogin().preferences;
2023-03-28 14:34:01 +00:00
const createBuilder = useCallback(() => {
if (subject.type !== "global" && subject.items.length === 0) {
return null;
}
2023-03-28 14:34:01 +00:00
const b = new RequestBuilder(`timeline:${subject.type}:${subject.discriminator}`);
2023-04-05 15:10:14 +00:00
const f = b.withFilter().kinds([EventKind.TextNote, EventKind.Repost, EventKind.Polls]);
2023-03-28 14:34:01 +00:00
if (options.relay) {
b.withOptions({
leaveOpen: false,
relays: [options.relay],
});
}
switch (subject.type) {
case "pubkey": {
2023-03-28 14:34:01 +00:00
f.authors(subject.items);
break;
}
case "hashtag": {
2023-03-28 14:34:01 +00:00
f.tag("t", subject.items);
break;
}
case "ptag": {
2023-03-28 14:34:01 +00:00
f.tag("p", subject.items);
break;
}
case "keyword": {
2023-03-28 14:34:01 +00:00
f.search(subject.items[0]);
break;
}
}
2023-03-28 14:34:01 +00:00
return {
builder: b,
filter: f,
};
}, [subject.type, subject.items, subject.discriminator]);
const sub = useMemo(() => {
2023-03-28 14:34:01 +00:00
const rb = createBuilder();
if (rb) {
if (options.method === "LIMIT_UNTIL") {
rb.filter.until(until).limit(200);
} else {
2023-03-28 14:34:01 +00:00
rb.filter.since(since).until(until);
if (since === undefined) {
2023-03-28 14:34:01 +00:00
rb.filter.limit(50);
2022-12-31 20:11:43 +00:00
}
}
if (pref.autoShowLatest) {
// copy properties of main sub but with limit 0
// this will put latest directly into main feed
2023-03-28 14:34:01 +00:00
rb.builder
.withOptions({
leaveOpen: true,
})
.withFilter()
.authors(rb.filter.filter.authors)
.kinds(rb.filter.filter.kinds)
.tag("p", rb.filter.filter["#p"])
.tag("t", rb.filter.filter["#t"])
.search(rb.filter.filter.search)
.limit(1)
.since(now);
}
}
2023-03-28 14:34:01 +00:00
return rb?.builder ?? null;
}, [until, since, options.method, pref, createBuilder]);
2023-03-28 14:34:01 +00:00
const main = useRequestBuilder<FlatNoteStore>(FlatNoteStore, sub);
const subRealtime = useMemo(() => {
2023-03-28 14:34:01 +00:00
const rb = createBuilder();
if (rb && !pref.autoShowLatest) {
rb.builder.withOptions({
leaveOpen: true,
});
rb.builder.id = `${rb.builder.id}:latest`;
rb.filter.limit(1).since(now);
}
2023-03-28 14:34:01 +00:00
return rb?.builder ?? null;
}, [pref.autoShowLatest, createBuilder]);
2023-03-28 14:34:01 +00:00
const latest = useRequestBuilder<FlatNoteStore>(FlatNoteStore, subRealtime);
useEffect(() => {
2023-03-28 14:34:01 +00:00
// clear store if changing relays
2023-03-28 15:41:57 +00:00
main.clear();
latest.clear();
}, [options.relay]);
const subNext = useMemo(() => {
2023-03-28 14:34:01 +00:00
const rb = new RequestBuilder(`timeline-related:${subject.type}`);
2023-02-28 19:47:04 +00:00
if (trackingEvents.length > 0) {
2023-03-28 14:34:01 +00:00
rb.withFilter()
.kinds(
2023-04-20 21:05:11 +00:00
pref.enableReactions
? [EventKind.Reaction, EventKind.Repost, EventKind.ZapReceipt]
: [EventKind.ZapReceipt, EventKind.Repost]
2023-03-28 14:34:01 +00:00
)
.tag("e", trackingEvents);
}
if (trackingParentEvents.length > 0) {
2023-03-28 14:34:01 +00:00
rb.withFilter().ids(trackingParentEvents);
}
2023-03-28 14:34:01 +00:00
return rb.numFilters > 0 ? rb : null;
}, [trackingEvents, pref, subject.type]);
2023-03-28 14:34:01 +00:00
const related = useRequestBuilder<FlatNoteStore>(FlatNoteStore, subNext);
useEffect(() => {
2023-03-28 14:34:01 +00:00
if (main.data && main.data.length > 0) {
2023-02-09 12:26:54 +00:00
setTrackingEvent(s => {
2023-03-28 14:34:01 +00:00
const ids = (main.data ?? []).map(a => a.id);
2023-02-09 12:26:54 +00:00
if (ids.some(a => !s.includes(a))) {
return Array.from(new Set([...s, ...ids]));
2023-01-17 21:55:53 +00:00
}
return s;
});
2023-03-28 14:34:01 +00:00
const repostsByKind6 = main.data
2023-02-09 12:26:54 +00:00
.filter(a => a.kind === EventKind.Repost && a.content === "")
.map(a => a.tags.find(b => b[0] === "e"))
.filter(a => a)
.map(a => unwrap(a)[1]);
2023-03-28 14:34:01 +00:00
const repostsByKind1 = main.data
2023-02-18 01:10:18 +00:00
.filter(
a => (a.kind === EventKind.Repost || a.kind === EventKind.TextNote) && a.tags.some(tagFilterOfTextRepost(a))
)
.map(a => a.tags.find(tagFilterOfTextRepost(a)))
.filter(a => a)
.map(a => unwrap(a)[1]);
const reposts = [...repostsByKind6, ...repostsByKind1];
if (reposts.length > 0) {
2023-02-09 12:26:54 +00:00
setTrackingParentEvents(s => {
if (reposts.some(a => !s.includes(a))) {
2023-02-07 19:47:57 +00:00
const temp = new Set([...s, ...reposts]);
return Array.from(temp);
}
return s;
});
}
}
2023-03-28 14:34:01 +00:00
}, [main]);
return {
2023-03-28 14:34:01 +00:00
main: main.data,
related: related.data,
latest: latest.data,
2023-03-28 15:41:57 +00:00
loading: main.loading(),
loadMore: () => {
2023-03-28 14:34:01 +00:00
if (main.data) {
console.debug("Timeline load more!");
if (options.method === "LIMIT_UNTIL") {
const oldest = main.data.reduce((acc, v) => (acc = v.created_at < acc ? v.created_at : acc), unixNow());
setUntil(oldest);
} else {
older();
}
}
},
showLatest: () => {
2023-03-28 14:34:01 +00:00
if (latest.data) {
2023-03-28 15:41:57 +00:00
main.add(latest.data);
latest.clear();
2023-03-28 14:34:01 +00:00
}
},
};
2023-01-24 12:33:18 +00:00
}