2023-01-25 13:54:45 +00:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
2023-02-11 20:05:46 +00:00
|
|
|
import { u256 } from "@snort/nostr";
|
|
|
|
import { EventKind, Subscriptions } from "@snort/nostr";
|
2023-02-16 14:11:29 +00:00
|
|
|
import { unixNow, unwrap, tagFilterOfTextRepost } from "Util";
|
2023-01-20 11:11:50 +00:00
|
|
|
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 {
|
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-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-02-09 12:26:54 +00:00
|
|
|
export default function useTimelineFeed(subject: TimelineSubject, options: TimelineFeedOptions) {
|
2023-02-07 20:04:50 +00:00
|
|
|
const now = unixNow();
|
|
|
|
const [window] = useState<number>(options.window ?? 60 * 60);
|
|
|
|
const [until, setUntil] = useState<number>(now);
|
|
|
|
const [since, setSince] = useState<number>(now - window);
|
|
|
|
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
|
|
|
|
|
|
|
const createSub = useCallback(() => {
|
|
|
|
if (subject.type !== "global" && subject.items.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-02-07 19:47:57 +00:00
|
|
|
const sub = new Subscriptions();
|
2023-02-07 20:04:50 +00:00
|
|
|
sub.Id = `timeline:${subject.type}:${subject.discriminator}`;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
case "ptag": {
|
|
|
|
sub.PTags = new Set(subject.items);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "keyword": {
|
|
|
|
sub.Kinds.add(EventKind.SetMetadata);
|
|
|
|
sub.Search = subject.items[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sub;
|
2023-02-10 18:25:17 +00:00
|
|
|
}, [subject.type, subject.items, subject.discriminator, options.relay]);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
|
|
|
const sub = useMemo(() => {
|
2023-02-07 19:47:57 +00:00
|
|
|
const sub = createSub();
|
2023-02-07 20:04:50 +00:00
|
|
|
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;
|
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-02-07 19:47:57 +00:00
|
|
|
const latestSub = new Subscriptions();
|
2023-02-07 20:04:50 +00:00
|
|
|
latestSub.Authors = sub.Authors;
|
|
|
|
latestSub.HashTags = sub.HashTags;
|
|
|
|
latestSub.PTags = sub.PTags;
|
|
|
|
latestSub.Kinds = sub.Kinds;
|
|
|
|
latestSub.Search = sub.Search;
|
|
|
|
latestSub.Limit = 1;
|
|
|
|
latestSub.Since = Math.floor(new Date().getTime() / 1000);
|
|
|
|
sub.AddSubscription(latestSub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sub;
|
|
|
|
}, [until, since, options.method, pref, createSub]);
|
|
|
|
|
2023-02-10 18:25:17 +00:00
|
|
|
const main = useSubscription(sub, { leaveOpen: true, cache: subject.type !== "global", relay: options.relay });
|
2023-02-07 20:04:50 +00:00
|
|
|
|
|
|
|
const subRealtime = useMemo(() => {
|
2023-02-07 19:47:57 +00:00
|
|
|
const subLatest = createSub();
|
2023-02-07 20:04:50 +00:00
|
|
|
if (subLatest && !pref.autoShowLatest) {
|
|
|
|
subLatest.Id = `${subLatest.Id}:latest`;
|
|
|
|
subLatest.Limit = 1;
|
|
|
|
subLatest.Since = Math.floor(new Date().getTime() / 1000);
|
|
|
|
}
|
|
|
|
return subLatest;
|
|
|
|
}, [pref, createSub]);
|
|
|
|
|
|
|
|
const latest = useSubscription(subRealtime, {
|
|
|
|
leaveOpen: true,
|
|
|
|
cache: false,
|
2023-02-10 18:25:17 +00:00
|
|
|
relay: options.relay,
|
2023-02-07 20:04:50 +00:00
|
|
|
});
|
|
|
|
|
2023-02-10 18:25:17 +00:00
|
|
|
useEffect(() => {
|
|
|
|
// clear store if chaning relays
|
|
|
|
main.clear();
|
|
|
|
latest.clear();
|
|
|
|
}, [options.relay]);
|
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
const subNext = useMemo(() => {
|
|
|
|
let sub: Subscriptions | undefined;
|
2023-02-28 19:47:04 +00:00
|
|
|
if (trackingEvents.length > 0) {
|
2023-02-07 20:04:50 +00:00
|
|
|
sub = new Subscriptions();
|
|
|
|
sub.Id = `timeline-related:${subject.type}`;
|
2023-02-28 19:47:04 +00:00
|
|
|
sub.Kinds = new Set(
|
|
|
|
pref.enableReactions ? [EventKind.Reaction, EventKind.Repost, EventKind.ZapReceipt] : [EventKind.ZapReceipt]
|
|
|
|
);
|
2023-02-07 20:04:50 +00:00
|
|
|
sub.ETags = new Set(trackingEvents);
|
|
|
|
}
|
|
|
|
return sub ?? null;
|
|
|
|
}, [trackingEvents, pref, subject.type]);
|
|
|
|
|
2023-02-10 18:25:17 +00:00
|
|
|
const others = useSubscription(subNext, { leaveOpen: true, cache: subject.type !== "global", relay: options.relay });
|
2023-02-07 20:04:50 +00:00
|
|
|
|
|
|
|
const subParents = useMemo(() => {
|
|
|
|
if (trackingParentEvents.length > 0) {
|
2023-02-07 19:47:57 +00:00
|
|
|
const parents = new Subscriptions();
|
2023-02-07 20:04:50 +00:00
|
|
|
parents.Id = `timeline-parent:${subject.type}`;
|
|
|
|
parents.Ids = new Set(trackingParentEvents);
|
|
|
|
return parents;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}, [trackingParentEvents, subject.type]);
|
|
|
|
|
2023-02-10 18:25:17 +00:00
|
|
|
const parent = useSubscription(subParents, { leaveOpen: false, cache: false, relay: options.relay });
|
2023-02-07 20:04:50 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (main.store.notes.length > 0) {
|
2023-02-09 12:26:54 +00:00
|
|
|
setTrackingEvent(s => {
|
|
|
|
const ids = main.store.notes.map(a => a.id);
|
|
|
|
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-02-16 08:49:03 +00:00
|
|
|
const repostsByKind6 = main.store.notes
|
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-02-16 08:49:03 +00:00
|
|
|
const repostsByKind1 = main.store.notes
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [main.store]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
main: main.store,
|
|
|
|
related: others.store,
|
|
|
|
latest: latest.store,
|
|
|
|
parent: parent.store,
|
|
|
|
loadMore: () => {
|
|
|
|
console.debug("Timeline load more!");
|
|
|
|
if (options.method === "LIMIT_UNTIL") {
|
2023-02-09 12:26:54 +00:00
|
|
|
const oldest = main.store.notes.reduce((acc, v) => (acc = v.created_at < acc ? v.created_at : acc), unixNow());
|
2023-02-07 20:04:50 +00:00
|
|
|
setUntil(oldest);
|
|
|
|
} else {
|
2023-02-09 12:26:54 +00:00
|
|
|
setUntil(s => s - window);
|
|
|
|
setSince(s => s - window);
|
2023-02-07 20:04:50 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
showLatest: () => {
|
|
|
|
main.append(latest.store.notes);
|
|
|
|
latest.clear();
|
|
|
|
},
|
|
|
|
};
|
2023-01-24 12:33:18 +00:00
|
|
|
}
|