feat: worker-relay delete

fix: worker-relay insert replacable events duplicate
This commit is contained in:
2024-05-23 11:59:48 +01:00
parent bb5bf34fe9
commit b764cc1535
15 changed files with 271 additions and 138 deletions

View File

@ -0,0 +1,59 @@
import { NostrEvent, TaggedNostrEvent } from "@snort/system";
import { SnortContext } from "@snort/system-react";
import { useContext, useState } from "react";
import AsyncButton from "@/Components/Button/AsyncButton";
export function DebugPage() {
const system = useContext(SnortContext);
const [filter, setFilter] = useState("");
const [event, setEvent] = useState("");
const [results, setResult] = useState<Array<TaggedNostrEvent>>([]);
async function search() {
if (filter && system.cacheRelay) {
const r = await system.cacheRelay.query(["REQ", "test", JSON.parse(filter)]);
setResult(r.map(a => ({ ...a, relays: [] })));
}
}
async function insert() {
if (event && system.cacheRelay) {
const r = await system.cacheRelay.event(JSON.parse(event) as NostrEvent);
setResult([
{
content: JSON.stringify(r),
} as unknown as TaggedNostrEvent,
]);
}
}
async function removeEvents() {
if (filter && system.cacheRelay) {
const r = await system.cacheRelay.delete(["REQ", "delete-events", JSON.parse(filter)]);
setResult(r.map(a => ({ id: a }) as TaggedNostrEvent));
}
}
return (
<div className="flex flex-col gap-2">
<h3>Cache Query</h3>
<textarea value={filter} onChange={e => setFilter(e.target.value)} placeholder="nostr filter" />
<AsyncButton onClick={() => search()}>Query</AsyncButton>
<AsyncButton onClick={() => removeEvents()} className="!bg-red-500">
Delete
</AsyncButton>
<h3>Manual Insert</h3>
<textarea value={event} onChange={e => setEvent(e.target.value)} placeholder="nostr event" />
<AsyncButton onClick={() => insert()}>Insert</AsyncButton>
<div className="p-4 overflow-hidden">
<h4>Results: {results.length}</h4>
{results?.map(a => (
<pre key={a.id} className="text-mono text-xs text-pretty">
{JSON.stringify(a, undefined, 2)}
</pre>
))}
</div>
</div>
);
}

View File

@ -1,6 +1,7 @@
import { FeedCache } from "@snort/shared";
import { ReactNode, useEffect, useState, useSyncExternalStore } from "react";
import { FormattedMessage, FormattedNumber } from "react-intl";
import { useNavigate } from "react-router-dom";
import { GiftsCache, Relay, RelayMetrics } from "@/Cache";
import AsyncButton from "@/Components/Button/AsyncButton";
@ -32,7 +33,6 @@ function CacheDetails<T>({ cache, name }: { cache: FeedCache<T>; name: ReactNode
<small>
<FormattedMessage
defaultMessage="{count} ({count2} in memory)"
id="geppt8"
values={{
count: <FormattedNumber value={cache.keysOnTable().length} />,
count2: <FormattedNumber value={snapshot.length} />,
@ -53,6 +53,7 @@ function RelayCacheStats() {
const [counts, setCounts] = useState<Record<string, number>>({});
const [myEvents, setMyEvents] = useState<number>(0);
const login = useLogin();
const navigate = useNavigate();
useEffect(() => {
Relay.summary().then(setCounts);
@ -69,7 +70,6 @@ function RelayCacheStats() {
<p>
<FormattedMessage
defaultMessage="My events: {n}"
id="lEnclp"
values={{
n: <FormattedNumber value={myEvents} />,
}}
@ -124,6 +124,9 @@ function RelayCacheStats() {
}}>
<FormattedMessage defaultMessage="Dump" />
</AsyncButton>
<AsyncButton onClick={() => navigate("/cache-debug")}>
<FormattedMessage defaultMessage="Debug" />
</AsyncButton>
</div>
</div>
);

View File

@ -13,6 +13,7 @@ import { IntlProvider } from "@/Components/IntlProvider/IntlProvider";
import { db } from "@/Db";
import { addCachedMetadataToFuzzySearch } from "@/Db/FuzzySearch";
import { AboutPage } from "@/Pages/About";
import { DebugPage } from "@/Pages/CacheDebug";
import { SnortDeckLayout } from "@/Pages/Deck/DeckLayout";
import DonatePage from "@/Pages/Donate/DonatePage";
import ErrorPage from "@/Pages/ErrorPage";
@ -74,6 +75,10 @@ async function initSite() {
let didInit = false;
const mainRoutes = [
...RootRoutes,
{
path: "/cache-debug",
element: <DebugPage />,
},
{
path: "/help",
element: <HelpPage />,