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

72 lines
2.0 KiB
TypeScript
Raw Normal View History

2023-06-08 10:45:23 +00:00
import { HexKey } from "@snort/system";
2023-01-26 11:34:18 +00:00
import useEventPublisher from "Feed/EventPublisher";
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();
const { muted, blocked } = 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
}
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-01-26 11:34:18 +00:00
}