MetadataCache -> CachedMetadata, addCachedMetadataToFuzzySearch

This commit is contained in:
Martti Malmi
2024-01-08 16:12:05 +02:00
parent 88924941a5
commit c80eb25d29
20 changed files with 92 additions and 71 deletions

View File

@ -1,4 +1,4 @@
import { MetadataCache } from "@snort/system";
import { CachedMetadata } from "@snort/system";
import { FormattedMessage } from "react-intl";
import { Link } from "react-router-dom";
@ -10,7 +10,7 @@ export class BackupKeyTask extends BaseUITask {
id = "backup-key";
noBaseStyle = true;
check(_: MetadataCache, session: LoginSession): boolean {
check(_: CachedMetadata, session: LoginSession): boolean {
return !this.state.muted && session.type == "private_key";
}

View File

@ -1,4 +1,4 @@
import { MetadataCache } from "@snort/system";
import { CachedMetadata } from "@snort/system";
import { FormattedMessage } from "react-intl";
import { Link } from "react-router-dom";
@ -7,7 +7,7 @@ import { BaseUITask } from "@/Components/Tasks/index";
export class Nip5Task extends BaseUITask {
id = "buy-nip5";
check(user: MetadataCache): boolean {
check(user: CachedMetadata): boolean {
return !this.state.muted && !user.nip05;
}

View File

@ -1,4 +1,4 @@
import { MetadataCache } from "@snort/system";
import { CachedMetadata } from "@snort/system";
import { FormattedMessage } from "react-intl";
import { BaseUITask } from "@/Components/Tasks/index";
@ -9,7 +9,7 @@ import { getCurrentSubscription } from "@/Utils/Subscription";
export class RenewSubTask extends BaseUITask {
id = "renew-sub";
check(user: MetadataCache, session: LoginSession): boolean {
check(user: CachedMetadata, session: LoginSession): boolean {
const sub = getCurrentSubscription(session.subscriptions);
return !sub && session.subscriptions.length > 0;
}

View File

@ -1,4 +1,4 @@
import { MetadataCache } from "@snort/system";
import { CachedMetadata } from "@snort/system";
import { LoginSession } from "@/Utils/Login";
@ -8,7 +8,7 @@ export interface UITask {
/**
* Run checks to determine if this Task should be triggered for this user
*/
check(user: MetadataCache, session: LoginSession): boolean;
check(user: CachedMetadata, session: LoginSession): boolean;
mute(): void;
load(cb: () => void): void;
render(): JSX.Element;
@ -26,7 +26,7 @@ export abstract class BaseUITask implements UITask {
abstract id: string;
noBaseStyle = false;
abstract check(user: MetadataCache, session: LoginSession): boolean;
abstract check(user: CachedMetadata, session: LoginSession): boolean;
abstract render(): JSX.Element;
constructor() {

View File

@ -1,7 +1,7 @@
import "@webscopeio/react-textarea-autocomplete/style.css";
import "./Textarea.css";
import { MetadataCache, NostrPrefix } from "@snort/system";
import { CachedMetadata, NostrPrefix } from "@snort/system";
import ReactTextareaAutocomplete from "@webscopeio/react-textarea-autocomplete";
import { useIntl } from "react-intl";
import TextareaAutosize from "react-textarea-autosize";
@ -28,7 +28,7 @@ const EmojiItem = ({ entity: { name, char } }: { entity: EmojiItemProps }) => {
);
};
const UserItem = (metadata: MetadataCache) => {
const UserItem = (metadata: CachedMetadata) => {
const { pubkey, display_name, nip05, ...rest } = metadata;
return (
<div key={pubkey} className="user-item">
@ -84,7 +84,7 @@ const Textarea = (props: TextareaProps) => {
"@": {
afterWhitespace: true,
dataProvider: userDataProvider,
component: (props: { entity: MetadataCache }) => <UserItem {...props.entity} />,
component: (props: { entity: CachedMetadata }) => <UserItem {...props.entity} />,
output: (item: { pubkey: string }) => `@${hexToBech32(NostrPrefix.PublicKey, item.pubkey)}`,
},
}}

View File

@ -1,4 +1,4 @@
import { MetadataCache, NostrLink, NostrPrefix, UserMetadata } from "@snort/system";
import { CachedMetadata, NostrLink, NostrPrefix, UserMetadata } from "@snort/system";
import { SnortContext } from "@snort/system-react";
import { ReactNode, useContext } from "react";
import { Link, LinkProps } from "react-router-dom";
@ -13,7 +13,7 @@ export function ProfileLink({
...others
}: {
pubkey: string;
user?: UserMetadata | MetadataCache;
user?: UserMetadata | CachedMetadata;
explicitLink?: string;
children?: ReactNode;
} & Omit<LinkProps, "to">) {

View File

@ -1,10 +1,10 @@
import "./UserWebsiteLink.css";
import { MetadataCache, UserMetadata } from "@snort/system";
import { CachedMetadata, UserMetadata } from "@snort/system";
import Icon from "@/Components/Icons/Icon";
export function UserWebsiteLink({ user }: { user?: MetadataCache | UserMetadata }) {
export function UserWebsiteLink({ user }: { user?: CachedMetadata | UserMetadata }) {
const website_url =
user?.website && !user.website.startsWith("http") ? "https://" + user.website : user?.website || "";

View File

@ -1,4 +1,5 @@
import Fuse from "fuse.js";
import {CachedMetadata} from "@snort/system";
export type FuzzySearchResult = {
pubkey: string;
@ -19,23 +20,43 @@ export const addEventToFuzzySearch = ev => {
if (ev.kind !== 0) {
return;
}
const existing = profileTimestamps.get(ev.pubkey);
if (existing) {
if (existing > ev.created_at) {
return;
requestAnimationFrame(() => {
const existing = profileTimestamps.get(ev.pubkey);
if (existing) {
if (existing > ev.created_at) {
return;
}
fuzzySearch.remove(doc => doc.pubkey === ev.pubkey);
}
fuzzySearch.remove(doc => doc.pubkey === ev.pubkey);
}
profileTimestamps.set(ev.pubkey, ev.created_at);
try {
const data = JSON.parse(ev.content);
if (ev.pubkey && (data.name || data.display_name || data.nip05)) {
data.pubkey = ev.pubkey;
fuzzySearch.add(data);
profileTimestamps.set(ev.pubkey, ev.created_at);
try {
const data = JSON.parse(ev.content);
if (ev.pubkey && (data.name || data.display_name || data.nip05)) {
data.pubkey = ev.pubkey;
fuzzySearch.add(data);
}
} catch (e) {
console.error(e);
}
} catch (e) {
console.error(e);
}
});
};
export const addCachedMetadataToFuzzySearch = (profile: CachedMetadata) => {
// TODO add profiles from Cache
requestAnimationFrame(() => {
const existing = profileTimestamps.get(profile.pubkey);
if (existing) {
if (existing > profile.created) {
return;
}
fuzzySearch.remove(doc => doc.pubkey === profile.pubkey);
}
profileTimestamps.set(profile.pubkey, profile.created);
if (profile.pubkey && (profile.name || profile.display_name || profile.nip05)) {
fuzzySearch.add(profile);
console.log('added profile to fuzzy search', profile);
}
});
}
export default fuzzySearch;

View File

@ -1,4 +1,4 @@
import { MetadataCache } from "@snort/system";
import { CachedMetadata } from "@snort/system";
import { ChatParticipant } from "@/chat";
import NoteToSelf from "@/Components/User/NoteToSelf";
@ -10,5 +10,5 @@ export function ChatParticipantProfile({ participant }: { participant: ChatParti
if (participant.id === publicKey) {
return <NoteToSelf className="grow" />;
}
return <ProfileImage pubkey={participant.id} className="grow" profile={participant.profile as MetadataCache} />;
return <ProfileImage pubkey={participant.id} className="grow" profile={participant.profile as CachedMetadata} />;
}

View File

@ -1,4 +1,4 @@
import { MetadataCache, socialGraphInstance, STR, UID } from "@snort/system";
import { CachedMetadata, socialGraphInstance, STR, UID } from "@snort/system";
import { SnortContext } from "@snort/system-react";
import { useContext, useEffect, useState } from "react";
import { NodeObject } from "react-force-graph-3d";
@ -12,7 +12,7 @@ import { defaultAvatar } from "../Utils";
interface GraphNode {
id: UID;
profile?: MetadataCache;
profile?: CachedMetadata;
distance: number;
val: number;
inboundCount: number;

View File

@ -4,7 +4,7 @@ import { fetchNip05Pubkey, LNURL } from "@snort/shared";
import {
encodeTLVEntries,
EventKind,
MetadataCache,
CachedMetadata,
NostrPrefix,
TLVEntryType,
tryParseNostrLink,
@ -59,13 +59,13 @@ import { ZapTarget } from "@/Utils/Zapper";
interface ProfilePageProps {
id?: string;
state?: MetadataCache;
state?: CachedMetadata;
}
export default function ProfilePage({ id: propId, state }: ProfilePageProps) {
const params = useParams();
const location = useLocation();
const profileState = (location.state as MetadataCache | undefined) || state;
const profileState = (location.state as CachedMetadata | undefined) || state;
const navigate = useNavigate();
const [id, setId] = useState<string | undefined>(profileState?.pubkey);
const [relays, setRelays] = useState<Array<string>>();

View File

@ -1,6 +1,6 @@
import { base64 } from "@scure/base";
import { removeUndefined, unwrap } from "@snort/shared";
import { EventKind, EventPublisher, MetadataCache, TaggedNostrEvent } from "@snort/system";
import { EventKind, EventPublisher, CachedMetadata, TaggedNostrEvent } from "@snort/system";
import { UserCache } from "@/Cache";
import SnortApi from "@/External/SnortApi";
@ -38,7 +38,7 @@ export async function makeNotification(ev: TaggedNostrEvent): Promise<Notificati
return null;
}
function replaceTagsWithUser(ev: TaggedNostrEvent, users: MetadataCache[]) {
function replaceTagsWithUser(ev: TaggedNostrEvent, users: CachedMetadata[]) {
return ev.content
.split(MentionRegex)
.map(match => {

View File

@ -9,7 +9,7 @@ import {
encodeTLV,
EventKind,
HexKey,
MetadataCache,
CachedMetadata,
NostrEvent,
NostrLink,
NostrPrefix,
@ -214,8 +214,8 @@ export function getLatestByPubkey(events: TaggedNostrEvent[]): Map<HexKey, Tagge
return deduped;
}
export function getLatestProfileByPubkey(profiles: MetadataCache[]): Map<HexKey, MetadataCache> {
const deduped = profiles.reduce((results: Map<HexKey, MetadataCache>, ev) => {
export function getLatestProfileByPubkey(profiles: CachedMetadata[]): Map<HexKey, CachedMetadata> {
const deduped = profiles.reduce((results: Map<HexKey, CachedMetadata>, ev) => {
if (!results.has(ev.pubkey)) {
const latest = getNewestProfile(profiles.filter(a => a.pubkey === ev.pubkey));
if (latest) {
@ -223,7 +223,7 @@ export function getLatestProfileByPubkey(profiles: MetadataCache[]): Map<HexKey,
}
}
return results;
}, new Map<HexKey, MetadataCache>());
}, new Map<HexKey, CachedMetadata>());
return deduped;
}
@ -255,7 +255,7 @@ export function getNewest(rawNotes: readonly TaggedNostrEvent[]) {
}
}
export function getNewestProfile(rawNotes: MetadataCache[]) {
export function getNewestProfile(rawNotes: CachedMetadata[]) {
const notes = [...rawNotes];
notes.sort((a, b) => b.created - a.created);
if (notes.length > 0) {
@ -279,7 +279,7 @@ export function tagFilterOfTextRepost(note: TaggedNostrEvent, id?: u256): (tag:
tag[0] === "e" && tag[3] === "mention" && note.content === `#[${i}]` && (id ? tag[1] === id : true);
}
export function groupByPubkey(acc: Record<HexKey, MetadataCache>, user: MetadataCache) {
export function groupByPubkey(acc: Record<HexKey, CachedMetadata>, user: CachedMetadata) {
return { ...acc, [user.pubkey]: user };
}

View File

@ -2,7 +2,7 @@ import { removeUndefined, throwIfOffline } from "@snort/shared";
import { mapEventToProfile, NostrEvent, NostrSystem, ProfileLoaderService, socialGraphInstance } from "@snort/system";
import { RelayMetrics, SystemDb, UserCache, UserRelays } from "@/Cache";
import { addEventToFuzzySearch } from "@/Db/FuzzySearch";
import { addEventToFuzzySearch, addProfileToFuzzySearch } from "@/Db/FuzzySearch";
import { LoginStore } from "@/Utils/Login";
import { hasWasm, WasmOptimizer } from "@/Utils/wasm";