feat: mute list
This commit is contained in:
@ -1,3 +1,6 @@
|
||||
import { useMemo } from "react";
|
||||
import uniqBy from "lodash.uniqby";
|
||||
|
||||
import {
|
||||
RequestBuilder,
|
||||
ReplaceableNoteStore,
|
||||
@ -6,23 +9,9 @@ import {
|
||||
} from "@snort/system";
|
||||
import { useRequestBuilder } from "@snort/system-react";
|
||||
import { System } from "index";
|
||||
import { useMemo } from "react";
|
||||
import { findTag } from "utils";
|
||||
import { EMOJI_PACK, USER_EMOJIS } from "const";
|
||||
import type { EmojiTag } from "../element/emoji";
|
||||
import uniqBy from "lodash.uniqby";
|
||||
|
||||
export interface Emoji {
|
||||
native?: string;
|
||||
id?: string;
|
||||
}
|
||||
|
||||
export interface EmojiPack {
|
||||
address: string;
|
||||
name: string;
|
||||
author: string;
|
||||
emojis: EmojiTag[];
|
||||
}
|
||||
import { EmojiPack } from "types";
|
||||
|
||||
function cleanShortcode(shortcode?: string) {
|
||||
return shortcode?.replace(/\s+/g, "_").replace(/_$/, "");
|
||||
@ -44,22 +33,10 @@ export function packId(pack: EmojiPack): string {
|
||||
return `${pack.author}:${pack.name}`;
|
||||
}
|
||||
|
||||
export default function useEmoji(pubkey?: string) {
|
||||
const sub = useMemo(() => {
|
||||
if (!pubkey) return null;
|
||||
const rb = new RequestBuilder(`emoji:${pubkey}`);
|
||||
|
||||
rb.withFilter().authors([pubkey]).kinds([USER_EMOJIS]);
|
||||
|
||||
return rb;
|
||||
}, [pubkey]);
|
||||
|
||||
const { data: userEmoji } = useRequestBuilder<ReplaceableNoteStore>(
|
||||
System,
|
||||
ReplaceableNoteStore,
|
||||
sub,
|
||||
);
|
||||
|
||||
export function useUserEmojiPacks(
|
||||
pubkey?: string,
|
||||
userEmoji: { tags: string[][] },
|
||||
) {
|
||||
const related = useMemo(() => {
|
||||
if (userEmoji) {
|
||||
return userEmoji.tags.filter(
|
||||
@ -106,3 +83,23 @@ export default function useEmoji(pubkey?: string) {
|
||||
|
||||
return emojis;
|
||||
}
|
||||
|
||||
export default function useEmoji(pubkey?: string) {
|
||||
const sub = useMemo(() => {
|
||||
if (!pubkey) return null;
|
||||
const rb = new RequestBuilder(`emoji:${pubkey}`);
|
||||
|
||||
rb.withFilter().authors([pubkey]).kinds([USER_EMOJIS]);
|
||||
|
||||
return rb;
|
||||
}, [pubkey]);
|
||||
|
||||
const { data: userEmoji } = useRequestBuilder<ReplaceableNoteStore>(
|
||||
System,
|
||||
ReplaceableNoteStore,
|
||||
sub,
|
||||
);
|
||||
|
||||
const emojis = useUserEmojiPacks(pubkey, userEmoji ?? { tags: [] });
|
||||
return emojis;
|
||||
}
|
||||
|
@ -1,26 +0,0 @@
|
||||
import { useMemo } from "react";
|
||||
import { EventKind, ReplaceableNoteStore, RequestBuilder } from "@snort/system";
|
||||
import { useRequestBuilder } from "@snort/system-react";
|
||||
import { System } from "index";
|
||||
|
||||
export default function useFollows(pubkey: string, leaveOpen = false) {
|
||||
const sub = useMemo(() => {
|
||||
const b = new RequestBuilder(`follows:${pubkey.slice(0, 12)}`);
|
||||
b.withOptions({
|
||||
leaveOpen,
|
||||
})
|
||||
.withFilter()
|
||||
.authors([pubkey])
|
||||
.kinds([EventKind.ContactList]);
|
||||
return b;
|
||||
}, [pubkey, leaveOpen]);
|
||||
|
||||
const { data } = useRequestBuilder<ReplaceableNoteStore>(
|
||||
System,
|
||||
ReplaceableNoteStore,
|
||||
sub,
|
||||
);
|
||||
|
||||
const relays = JSON.parse(data?.content.length > 0 ? data?.content : "{}");
|
||||
return data ? { tags: data.tags, relays } : null;
|
||||
}
|
@ -1,17 +1,100 @@
|
||||
import { Login } from "index";
|
||||
import { getPublisher } from "login";
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { useSyncExternalStore, useMemo, useState, useEffect } from "react";
|
||||
|
||||
import { EventKind, NoteCollection, RequestBuilder } from "@snort/system";
|
||||
import { useRequestBuilder } from "@snort/system-react";
|
||||
|
||||
import { useUserEmojiPacks } from "hooks/emoji";
|
||||
import { USER_EMOJIS } from "const";
|
||||
import { System, Login } from "index";
|
||||
import {
|
||||
getPublisher,
|
||||
setMuted,
|
||||
setEmojis,
|
||||
setFollows,
|
||||
setRelays,
|
||||
} from "login";
|
||||
|
||||
export function useLogin() {
|
||||
const session = useSyncExternalStore(
|
||||
(c) => Login.hook(c),
|
||||
() => Login.snapshot()
|
||||
() => Login.snapshot(),
|
||||
);
|
||||
if (!session) return;
|
||||
return {
|
||||
...session,
|
||||
publisher: () => {
|
||||
return getPublisher(session);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function useLoginEvents(pubkey?: string, leaveOpen = false) {
|
||||
const [userEmojis, setUserEmojis] = useState([]);
|
||||
const session = useSyncExternalStore(
|
||||
(c) => Login.hook(c),
|
||||
() => Login.snapshot(),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (session) {
|
||||
Object.entries(session.relays).forEach((params) => {
|
||||
const [relay, settings] = params;
|
||||
System.ConnectToRelay(relay, settings);
|
||||
});
|
||||
}
|
||||
}, [session]);
|
||||
|
||||
const sub = useMemo(() => {
|
||||
if (!pubkey) return null;
|
||||
const b = new RequestBuilder(`login:${pubkey.slice(0, 12)}`);
|
||||
b.withOptions({
|
||||
leaveOpen,
|
||||
})
|
||||
.withFilter()
|
||||
.authors([pubkey])
|
||||
.kinds([
|
||||
EventKind.ContactList,
|
||||
EventKind.Relays,
|
||||
10_000 as EventKind,
|
||||
USER_EMOJIS,
|
||||
]);
|
||||
return b;
|
||||
}, [pubkey, leaveOpen]);
|
||||
|
||||
const { data } = useRequestBuilder<NoteCollection>(
|
||||
System,
|
||||
NoteCollection,
|
||||
sub,
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
if (!session) {
|
||||
return;
|
||||
}
|
||||
for (const ev of data) {
|
||||
if (ev?.kind === USER_EMOJIS) {
|
||||
setUserEmojis(ev.tags);
|
||||
}
|
||||
if (ev?.kind === 10_000) {
|
||||
// todo: decrypt ev.content tags
|
||||
setMuted(session, ev.tags, ev.created_at);
|
||||
}
|
||||
if (ev?.kind === EventKind.ContactList) {
|
||||
setFollows(session, ev.tags, ev.created_at);
|
||||
}
|
||||
if (ev?.kind === EventKind.Relays) {
|
||||
setRelays(session, ev.tags, ev.created_at);
|
||||
}
|
||||
}
|
||||
}, [session, data]);
|
||||
|
||||
const emojis = useUserEmojiPacks(pubkey, { tags: userEmojis });
|
||||
useEffect(() => {
|
||||
if (session) {
|
||||
setEmojis(session, emojis);
|
||||
}
|
||||
}, [session, emojis]);
|
||||
}
|
||||
|
Reference in New Issue
Block a user