2023-01-25 13:54:45 +00:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
2023-01-20 17:07:14 +00:00
|
|
|
import { useSelector } from "react-redux";
|
2023-03-28 14:34:01 +00:00
|
|
|
import { EventKind, u256 } from "@snort/nostr";
|
|
|
|
|
|
|
|
import { unixNow, unwrap, tagFilterOfTextRepost } from "Util";
|
2023-01-20 17:07:14 +00:00
|
|
|
import { RootState } from "State/Store";
|
|
|
|
import { UserPreferences } from "State/Login";
|
2023-03-28 14:34:01 +00:00
|
|
|
import { FlatNoteStore, RequestBuilder } from "System";
|
|
|
|
import useRequestBuilder from "Hooks/useRequestBuilder";
|
|
|
|
import useTimelineWindow from "Hooks/useTimelineWindow";
|
2022-12-18 14:51:47 +00:00
|
|
|
|
2023-01-17 21:55:53 +00:00
|
|
|
export interface TimelineFeedOptions {
|
2023-02-07 20:04:50 +00:00
|
|
|
method: "TIME_RANGE" | "LIMIT_UNTIL";
|
|
|
|
window?: number;
|
2023-02-10 18:25:17 +00:00
|
|
|
relay?: string;
|
2023-03-28 14:34:01 +00:00
|
|
|
now?: number;
|
2023-01-17 21:55:53 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 18:00:56 +00:00
|
|
|
export interface TimelineSubject {
|
2023-02-07 20:04:50 +00:00
|
|
|
type: "pubkey" | "hashtag" | "global" | "ptag" | "keyword";
|
|
|
|
discriminator: string;
|
|
|
|
items: string[];
|
2023-01-19 18:00:56 +00:00
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
});
|
2023-02-07 20:04:50 +00:00
|
|
|
const [trackingEvents, setTrackingEvent] = useState<u256[]>([]);
|
|
|
|
const [trackingParentEvents, setTrackingParentEvents] = useState<u256[]>([]);
|
2023-02-09 12:26:54 +00:00
|
|
|
const pref = useSelector<RootState, UserPreferences>(s => s.login.preferences);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
const createBuilder = useCallback(() => {
|
2023-02-07 20:04:50 +00:00
|
|
|
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}`);
|
|
|
|
const f = b.withFilter().kinds([EventKind.TextNote, EventKind.Repost]);
|
|
|
|
|
|
|
|
if (options.relay) {
|
|
|
|
b.withOptions({
|
|
|
|
leaveOpen: false,
|
|
|
|
relays: [options.relay],
|
|
|
|
});
|
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
switch (subject.type) {
|
|
|
|
case "pubkey": {
|
2023-03-28 14:34:01 +00:00
|
|
|
f.authors(subject.items);
|
2023-02-07 20:04:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "hashtag": {
|
2023-03-28 14:34:01 +00:00
|
|
|
f.tag("t", subject.items);
|
2023-02-07 20:04:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ptag": {
|
2023-03-28 14:34:01 +00:00
|
|
|
f.tag("p", subject.items);
|
2023-02-07 20:04:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "keyword": {
|
2023-03-28 14:34:01 +00:00
|
|
|
f.search(subject.items[0]);
|
2023-02-07 20:04:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 14:34:01 +00:00
|
|
|
return {
|
|
|
|
builder: b,
|
|
|
|
filter: f,
|
|
|
|
};
|
|
|
|
}, [subject.type, subject.items, subject.discriminator]);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
|
|
|
const sub = useMemo(() => {
|
2023-03-28 14:34:01 +00:00
|
|
|
const rb = createBuilder();
|
|
|
|
if (rb) {
|
2023-02-07 20:04:50 +00:00
|
|
|
if (options.method === "LIMIT_UNTIL") {
|
2023-03-28 14:34:01 +00:00
|
|
|
rb.filter.until(until).limit(10);
|
2023-02-07 20:04:50 +00:00
|
|
|
} else {
|
2023-03-28 14:34:01 +00:00
|
|
|
rb.filter.since(since).until(until);
|
2023-02-07 20:04:50 +00:00
|
|
|
if (since === undefined) {
|
2023-03-28 14:34:01 +00:00
|
|
|
rb.filter.limit(50);
|
2022-12-31 20:11:43 +00:00
|
|
|
}
|
2023-02-07 20:04:50 +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-02-07 20:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-28 14:34:01 +00:00
|
|
|
return rb?.builder ?? null;
|
|
|
|
}, [until, since, options.method, pref, createBuilder]);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
const main = useRequestBuilder<FlatNoteStore>(FlatNoteStore, sub);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
|
|
|
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-02-07 20:04:50 +00:00
|
|
|
}
|
2023-03-28 14:34:01 +00:00
|
|
|
return rb?.builder ?? null;
|
|
|
|
}, [pref.autoShowLatest, createBuilder]);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
const latest = useRequestBuilder<FlatNoteStore>(FlatNoteStore, subRealtime);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
2023-02-10 18:25:17 +00:00
|
|
|
useEffect(() => {
|
2023-03-28 14:34:01 +00:00
|
|
|
// clear store if changing relays
|
|
|
|
main.store.clear();
|
|
|
|
latest.store.clear();
|
2023-02-10 18:25:17 +00:00
|
|
|
}, [options.relay]);
|
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
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(
|
|
|
|
pref.enableReactions ? [EventKind.Reaction, EventKind.Repost, EventKind.ZapReceipt] : [EventKind.ZapReceipt]
|
|
|
|
)
|
|
|
|
.tag("e", trackingEvents);
|
2023-02-07 20:04:50 +00:00
|
|
|
}
|
|
|
|
if (trackingParentEvents.length > 0) {
|
2023-03-28 14:34:01 +00:00
|
|
|
rb.withFilter().ids(trackingParentEvents);
|
2023-02-07 20:04:50 +00:00
|
|
|
}
|
2023-03-28 14:34:01 +00:00
|
|
|
return rb.numFilters > 0 ? rb : null;
|
|
|
|
}, [trackingEvents, pref, subject.type]);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
const related = useRequestBuilder<FlatNoteStore>(FlatNoteStore, subNext);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
|
|
|
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))) {
|
2023-02-07 20:04:50 +00:00
|
|
|
return Array.from(new Set([...s, ...ids]));
|
2023-01-17 21:55:53 +00:00
|
|
|
}
|
2023-02-07 20:04:50 +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))
|
|
|
|
)
|
2023-02-16 14:11:29 +00:00
|
|
|
.map(a => a.tags.find(tagFilterOfTextRepost(a)))
|
2023-02-16 08:49:03 +00:00
|
|
|
.filter(a => a)
|
|
|
|
.map(a => unwrap(a)[1]);
|
|
|
|
const reposts = [...repostsByKind6, ...repostsByKind1];
|
2023-02-07 20:04:50 +00:00
|
|
|
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]);
|
2023-02-07 20:04:50 +00:00
|
|
|
return Array.from(temp);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 14:34:01 +00:00
|
|
|
}, [main]);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
|
|
|
return {
|
2023-03-28 14:34:01 +00:00
|
|
|
main: main.data,
|
|
|
|
related: related.data,
|
|
|
|
latest: latest.data,
|
|
|
|
loading: main.store.loading,
|
2023-02-07 20:04:50 +00:00
|
|
|
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();
|
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
showLatest: () => {
|
2023-03-28 14:34:01 +00:00
|
|
|
if (latest.data) {
|
|
|
|
main.store.add(latest.data);
|
|
|
|
latest.store.clear();
|
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
},
|
|
|
|
};
|
2023-01-24 12:33:18 +00:00
|
|
|
}
|