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;
|
2023-02-07 20:04:50 +00:00
|
|
|
const publisher = useEventPublisher();
|
2023-01-26 11:34:18 +00:00
|
|
|
|
2023-01-27 21:10:14 +00:00
|
|
|
async function setMutedList(pub: HexKey[], priv: HexKey[]) {
|
2023-04-14 15:02:15 +00:00
|
|
|
if (publisher) {
|
2023-02-07 20:04:50 +00:00
|
|
|
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-27 21:10:14 +00:00
|
|
|
}
|
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);
|
2023-01-27 21:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-01-27 21:10:14 +00:00
|
|
|
}
|
|
|
|
|
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-01-27 21:10:14 +00:00
|
|
|
}
|
|
|
|
|
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-02-07 20:04:50 +00:00
|
|
|
return {
|
2023-04-14 11:33:19 +00:00
|
|
|
muted: muted.item,
|
2023-02-07 20:04:50 +00:00
|
|
|
mute,
|
|
|
|
muteAll,
|
|
|
|
unmute,
|
|
|
|
isMuted,
|
2023-04-14 11:33:19 +00:00
|
|
|
blocked: blocked.item,
|
2023-02-07 20:04:50 +00:00
|
|
|
block,
|
|
|
|
unblock,
|
|
|
|
isBlocked,
|
|
|
|
};
|
2023-01-26 11:34:18 +00:00
|
|
|
}
|