forked from Kieran/snort
feat: read global from specific (paid) relays
This commit is contained in:
parent
042726525c
commit
c35b144dba
@ -23,6 +23,7 @@ export interface TimelineProps {
|
|||||||
method: "TIME_RANGE" | "LIMIT_UNTIL";
|
method: "TIME_RANGE" | "LIMIT_UNTIL";
|
||||||
ignoreModeration?: boolean;
|
ignoreModeration?: boolean;
|
||||||
window?: number;
|
window?: number;
|
||||||
|
relay?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,11 +35,13 @@ export default function Timeline({
|
|||||||
method,
|
method,
|
||||||
ignoreModeration = false,
|
ignoreModeration = false,
|
||||||
window,
|
window,
|
||||||
|
relay,
|
||||||
}: TimelineProps) {
|
}: TimelineProps) {
|
||||||
const { muted, isMuted } = useModeration();
|
const { muted, isMuted } = useModeration();
|
||||||
const { main, related, latest, parent, loadMore, showLatest } = useTimelineFeed(subject, {
|
const { main, related, latest, parent, loadMore, showLatest } = useTimelineFeed(subject, {
|
||||||
method,
|
method,
|
||||||
window: window,
|
window: window,
|
||||||
|
relay,
|
||||||
});
|
});
|
||||||
|
|
||||||
const filterPosts = useCallback(
|
const filterPosts = useCallback(
|
||||||
|
@ -13,6 +13,7 @@ export type NoteStore = {
|
|||||||
export type UseSubscriptionOptions = {
|
export type UseSubscriptionOptions = {
|
||||||
leaveOpen: boolean;
|
leaveOpen: boolean;
|
||||||
cache: boolean;
|
cache: boolean;
|
||||||
|
relay?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
interface ReducerArg {
|
interface ReducerArg {
|
||||||
@ -130,10 +131,16 @@ export default function useSubscription(
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
console.debug("Adding sub: ", subDebounce.ToObject());
|
const subObj = subDebounce.ToObject();
|
||||||
|
console.debug("Adding sub: ", subObj);
|
||||||
|
if (options?.relay) {
|
||||||
|
System.AddSubscriptionToRelay(subDebounce, options.relay);
|
||||||
|
} else {
|
||||||
System.AddSubscription(subDebounce);
|
System.AddSubscription(subDebounce);
|
||||||
|
}
|
||||||
return () => {
|
return () => {
|
||||||
console.debug("Removing sub: ", subDebounce.ToObject());
|
console.debug("Removing sub: ", subObj);
|
||||||
|
subDebounce.OnEvent = () => undefined;
|
||||||
System.RemoveSubscription(subDebounce.Id);
|
System.RemoveSubscription(subDebounce.Id);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import { UserPreferences } from "State/Login";
|
|||||||
export interface TimelineFeedOptions {
|
export interface TimelineFeedOptions {
|
||||||
method: "TIME_RANGE" | "LIMIT_UNTIL";
|
method: "TIME_RANGE" | "LIMIT_UNTIL";
|
||||||
window?: number;
|
window?: number;
|
||||||
|
relay?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TimelineSubject {
|
export interface TimelineSubject {
|
||||||
@ -56,7 +57,7 @@ export default function useTimelineFeed(subject: TimelineSubject, options: Timel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sub;
|
return sub;
|
||||||
}, [subject.type, subject.items, subject.discriminator]);
|
}, [subject.type, subject.items, subject.discriminator, options.relay]);
|
||||||
|
|
||||||
const sub = useMemo(() => {
|
const sub = useMemo(() => {
|
||||||
const sub = createSub();
|
const sub = createSub();
|
||||||
@ -89,7 +90,7 @@ export default function useTimelineFeed(subject: TimelineSubject, options: Timel
|
|||||||
return sub;
|
return sub;
|
||||||
}, [until, since, options.method, pref, createSub]);
|
}, [until, since, options.method, pref, createSub]);
|
||||||
|
|
||||||
const main = useSubscription(sub, { leaveOpen: true, cache: true });
|
const main = useSubscription(sub, { leaveOpen: true, cache: subject.type !== "global", relay: options.relay });
|
||||||
|
|
||||||
const subRealtime = useMemo(() => {
|
const subRealtime = useMemo(() => {
|
||||||
const subLatest = createSub();
|
const subLatest = createSub();
|
||||||
@ -104,8 +105,15 @@ export default function useTimelineFeed(subject: TimelineSubject, options: Timel
|
|||||||
const latest = useSubscription(subRealtime, {
|
const latest = useSubscription(subRealtime, {
|
||||||
leaveOpen: true,
|
leaveOpen: true,
|
||||||
cache: false,
|
cache: false,
|
||||||
|
relay: options.relay,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// clear store if chaning relays
|
||||||
|
main.clear();
|
||||||
|
latest.clear();
|
||||||
|
}, [options.relay]);
|
||||||
|
|
||||||
const subNext = useMemo(() => {
|
const subNext = useMemo(() => {
|
||||||
let sub: Subscriptions | undefined;
|
let sub: Subscriptions | undefined;
|
||||||
if (trackingEvents.length > 0 && pref.enableReactions) {
|
if (trackingEvents.length > 0 && pref.enableReactions) {
|
||||||
@ -117,7 +125,7 @@ export default function useTimelineFeed(subject: TimelineSubject, options: Timel
|
|||||||
return sub ?? null;
|
return sub ?? null;
|
||||||
}, [trackingEvents, pref, subject.type]);
|
}, [trackingEvents, pref, subject.type]);
|
||||||
|
|
||||||
const others = useSubscription(subNext, { leaveOpen: true, cache: true });
|
const others = useSubscription(subNext, { leaveOpen: true, cache: subject.type !== "global", relay: options.relay });
|
||||||
|
|
||||||
const subParents = useMemo(() => {
|
const subParents = useMemo(() => {
|
||||||
if (trackingParentEvents.length > 0) {
|
if (trackingParentEvents.length > 0) {
|
||||||
@ -129,7 +137,7 @@ export default function useTimelineFeed(subject: TimelineSubject, options: Timel
|
|||||||
return null;
|
return null;
|
||||||
}, [trackingParentEvents, subject.type]);
|
}, [trackingParentEvents, subject.type]);
|
||||||
|
|
||||||
const parent = useSubscription(subParents);
|
const parent = useSubscription(subParents, { leaveOpen: false, cache: false, relay: options.relay });
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (main.store.notes.length > 0) {
|
if (main.store.notes.length > 0) {
|
||||||
|
@ -6,4 +6,7 @@ export interface RelayInfo {
|
|||||||
supported_nips?: number[];
|
supported_nips?: number[];
|
||||||
software?: string;
|
software?: string;
|
||||||
version?: string;
|
version?: string;
|
||||||
|
limitation?: {
|
||||||
|
payment_required: boolean;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -75,6 +75,10 @@ export class NostrSystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AddSubscriptionToRelay(sub: Subscriptions, relay: string) {
|
||||||
|
this.Sockets.get(relay)?.AddSubscription(sub);
|
||||||
|
}
|
||||||
|
|
||||||
AddSubscription(sub: Subscriptions) {
|
AddSubscription(sub: Subscriptions) {
|
||||||
for (const [, s] of this.Sockets) {
|
for (const [, s] of this.Sockets) {
|
||||||
s.AddSubscription(sub);
|
s.AddSubscription(sub);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import "./Root.css";
|
import "./Root.css";
|
||||||
import { useState } from "react";
|
import { useMemo, useState } from "react";
|
||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import { useIntl, FormattedMessage } from "react-intl";
|
import { useIntl, FormattedMessage } from "react-intl";
|
||||||
@ -10,6 +10,7 @@ import Timeline from "Element/Timeline";
|
|||||||
import { TimelineSubject } from "Feed/TimelineFeed";
|
import { TimelineSubject } from "Feed/TimelineFeed";
|
||||||
|
|
||||||
import messages from "./messages";
|
import messages from "./messages";
|
||||||
|
import { System } from "Nostr/System";
|
||||||
|
|
||||||
export default function RootPage() {
|
export default function RootPage() {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
@ -29,6 +30,7 @@ export default function RootPage() {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
const [tab, setTab] = useState<Tab>(RootTab.Posts);
|
const [tab, setTab] = useState<Tab>(RootTab.Posts);
|
||||||
|
const [relay, setRelay] = useState<string>();
|
||||||
const tagTabs = tags.map((t, idx) => {
|
const tagTabs = tags.map((t, idx) => {
|
||||||
return { text: `#${t}`, value: idx + 3 };
|
return { text: `#${t}`, value: idx + 3 };
|
||||||
});
|
});
|
||||||
@ -51,6 +53,20 @@ export default function RootPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const globalRelays = useMemo(() => {
|
||||||
|
const ret: string[] = [];
|
||||||
|
System.Sockets.forEach((v, k) => {
|
||||||
|
if (v.Info?.limitation?.payment_required === true) {
|
||||||
|
ret.push(k);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (ret.length > 0 && !relay) {
|
||||||
|
setRelay(ret[0]);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}, [relays, relay]);
|
||||||
|
|
||||||
const isGlobal = loggedOut || tab.value === RootTab.Global.value;
|
const isGlobal = loggedOut || tab.value === RootTab.Global.value;
|
||||||
const timelineSubect: TimelineSubject = (() => {
|
const timelineSubect: TimelineSubject = (() => {
|
||||||
if (isGlobal) {
|
if (isGlobal) {
|
||||||
@ -63,16 +79,38 @@ export default function RootPage() {
|
|||||||
|
|
||||||
return { type: "pubkey", items: follows, discriminator: "follows" };
|
return { type: "pubkey", items: follows, discriminator: "follows" };
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
if (isGlobal && globalRelays.length === 0) return null;
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="main-content">{pubKey && <Tabs tabs={tabs} tab={tab} setTab={setTab} />}</div>
|
<div className="main-content">{pubKey && <Tabs tabs={tabs} tab={tab} setTab={setTab} />}</div>
|
||||||
|
{isGlobal && (
|
||||||
|
<div className="flex mb10">
|
||||||
|
<div className="f-grow">
|
||||||
|
<FormattedMessage
|
||||||
|
defaultMessage="Read global from"
|
||||||
|
description="Label for reading global feed from specific relays"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<select onChange={e => setRelay(e.target.value)}>
|
||||||
|
{globalRelays.map(a => (
|
||||||
|
<option key={a} value={a}>
|
||||||
|
{new URL(a).host}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
{followHints()}
|
{followHints()}
|
||||||
<Timeline
|
<Timeline
|
||||||
key={tab.value}
|
key={tab.value}
|
||||||
subject={timelineSubect}
|
subject={timelineSubect}
|
||||||
postsOnly={tab.value === RootTab.Posts.value}
|
postsOnly={tab.value === RootTab.Posts.value}
|
||||||
method={"TIME_RANGE"}
|
method={"TIME_RANGE"}
|
||||||
window={tab.value === RootTab.Global.value ? 60 : undefined}
|
window={isGlobal ? 60 : undefined}
|
||||||
|
relay={isGlobal ? relay : undefined}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user