snort/packages/app/src/Hooks/useModeration.tsx

82 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-09-24 12:33:12 +00:00
import { HexKey, TaggedNostrEvent } from "@snort/system";
2023-09-21 20:02:59 +00:00
import useEventPublisher from "Hooks/useEventPublisher";
2023-04-14 11:33:19 +00:00
import useLogin from "Hooks/useLogin";
import { setBlocked, setMuted } from "Login";
2023-05-24 10:12:23 +00:00
import { appendDedupe } from "SnortUtils";
2023-06-15 11:03:05 +00:00
import { System } from "index";
2023-01-26 11:34:18 +00:00
export default function useModeration() {
2023-04-14 11:33:19 +00:00
const login = useLogin();
2023-09-24 12:33:12 +00:00
const { muted, blocked, appData } = login;
const publisher = useEventPublisher();
2023-01-26 11:34:18 +00:00
async function setMutedList(pub: HexKey[], priv: HexKey[]) {
2023-04-14 15:02:15 +00:00
if (publisher) {
const ev = await publisher.muted(pub, priv);
2023-06-15 11:03:05 +00:00
System.BroadcastEvent(ev);
2023-04-14 15:02:15 +00:00
return ev.created_at * 1000;
2023-01-26 11:34:18 +00:00
}
2023-04-14 11:33:19 +00:00
return 0;
}
2023-01-26 11:34:18 +00:00
function isMuted(id: HexKey) {
2023-04-14 11:33:19 +00:00
return muted.item.includes(id) || blocked.item.includes(id);
}
function isBlocked(id: HexKey) {
2023-04-14 11:33:19 +00:00
return blocked.item.includes(id);
2023-01-26 11:34:18 +00:00
}
2023-04-14 11:33:19 +00:00
async function unmute(id: HexKey) {
const newMuted = muted.item.filter(p => p !== id);
const ts = await setMutedList(newMuted, blocked.item);
setMuted(login, newMuted, ts);
}
2023-04-14 11:33:19 +00:00
async function unblock(id: HexKey) {
const newBlocked = blocked.item.filter(p => p !== id);
const ts = await setMutedList(muted.item, newBlocked);
setBlocked(login, newBlocked, ts);
2023-01-26 11:34:18 +00:00
}
2023-04-14 11:33:19 +00:00
async function mute(id: HexKey) {
const newMuted = muted.item.includes(id) ? muted.item : muted.item.concat([id]);
const ts = await setMutedList(newMuted, blocked.item);
setMuted(login, newMuted, ts);
2023-01-26 11:34:18 +00:00
}
2023-04-14 11:33:19 +00:00
async function block(id: HexKey) {
const newBlocked = blocked.item.includes(id) ? blocked.item : blocked.item.concat([id]);
const ts = await setMutedList(muted.item, newBlocked);
setBlocked(login, newBlocked, ts);
}
2023-04-14 11:33:19 +00:00
async function muteAll(ids: HexKey[]) {
const newMuted = appendDedupe(muted.item, ids);
const ts = await setMutedList(newMuted, blocked.item);
setMuted(login, newMuted, ts);
2023-01-26 11:34:18 +00:00
}
2023-09-24 12:33:12 +00:00
function isMutedWord(word: string) {
return appData.item.mutedWords.includes(word.toLowerCase());
}
function isEventMuted(ev: TaggedNostrEvent) {
return isMuted(ev.pubkey) || appData.item.mutedWords.some(w => ev.content.toLowerCase().includes(w));
}
return {
2023-04-14 11:33:19 +00:00
muted: muted.item,
mute,
muteAll,
unmute,
isMuted,
2023-04-14 11:33:19 +00:00
blocked: blocked.item,
block,
unblock,
isBlocked,
2023-09-24 12:33:12 +00:00
isMutedWord,
2023-09-24 12:36:51 +00:00
isEventMuted,
};
2023-01-26 11:34:18 +00:00
}