Skip to content

Commit 0657221

Browse files
npub1cc3ha7z055mu0rwwu7806t2wt8mj3pvu0uv5mfp2c50dahaqhczshdalg6Tyler Longwell
andcommitted
feat(desktop): moderation data layer — shared/api + feature hooks
Foundation for the Phase-2 community-moderation UI (U1/U2 both consume it). Adds the client-side wire contract with no new relay surface: - shared/api/moderation.ts: signed command events (report 1984, ban/unban/ timeout/untimeout 9040-9043, resolve 9044) published over the existing WS path (mirrors relayMembers.ts); NIP-98-authed HTTP GET reader for the three /moderation/* reads (reports, audit, restricted). Command events carry no h tag — the relay binds the tenant from the connection host. The NIP-98 u tag is signed over the fully-finalized URL (query included) so the signed u and the fetched URL are always identical. - features/moderation/hooks.ts: React Query hooks over that API, mutations invalidate the affected read caches on success (mirrors relay-members). - constants/kinds.ts: KIND_REPORT + KIND_MODERATION_* kinds. Tag shapes are pinned by buzz-sdk builders, report.rs, and api/bridge.rs. Validated: tsc --noEmit clean, biome check clean. Co-authored-by: Tyler Longwell <[email protected]> Signed-off-by: Tyler Longwell <[email protected]>
1 parent c893a10 commit 0657221

3 files changed

Lines changed: 537 additions & 0 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
2+
3+
import {
4+
banMember,
5+
type CommunityRestriction,
6+
listAuditActions,
7+
listReports,
8+
listRestrictions,
9+
type ModerationAction,
10+
type ModerationReport,
11+
type ReportType,
12+
type ResolutionAction,
13+
type ResolutionStatus,
14+
resolveReport,
15+
submitReport,
16+
timeoutMember,
17+
unbanMember,
18+
untimeoutMember,
19+
} from "@/shared/api/moderation";
20+
21+
export const moderationReportsQueryKey = ["moderationReports"] as const;
22+
export const moderationAuditQueryKey = ["moderationAudit"] as const;
23+
export const moderationRestrictionsQueryKey = [
24+
"moderationRestrictions",
25+
] as const;
26+
27+
// --- Reads (mod-authz gated; consumed by the U2 queue/audit surfaces) ---
28+
29+
export function useModerationReportsQuery(
30+
options?: { status?: string; limit?: number },
31+
enabled = true,
32+
) {
33+
return useQuery({
34+
enabled,
35+
queryKey: [
36+
...moderationReportsQueryKey,
37+
options?.status ?? null,
38+
options?.limit ?? null,
39+
],
40+
queryFn: () => listReports(options),
41+
staleTime: 15_000,
42+
});
43+
}
44+
45+
export function useModerationAuditQuery(limit?: number, enabled = true) {
46+
return useQuery({
47+
enabled,
48+
queryKey: [...moderationAuditQueryKey, limit ?? null],
49+
queryFn: () => listAuditActions(limit),
50+
staleTime: 15_000,
51+
});
52+
}
53+
54+
export function useModerationRestrictionsQuery(enabled = true) {
55+
return useQuery({
56+
enabled,
57+
queryKey: moderationRestrictionsQueryKey,
58+
queryFn: listRestrictions,
59+
staleTime: 15_000,
60+
});
61+
}
62+
63+
// --- Writes ---
64+
//
65+
// Moderation writes are relay-validated command events whose effects surface in
66+
// the queue/audit/restricted reads after processing, so mutations invalidate the
67+
// affected read queries on success rather than fabricating optimistic rows.
68+
69+
function useInvalidateModerationReads() {
70+
const queryClient = useQueryClient();
71+
return () =>
72+
Promise.all([
73+
queryClient.invalidateQueries({ queryKey: moderationReportsQueryKey }),
74+
queryClient.invalidateQueries({ queryKey: moderationAuditQueryKey }),
75+
queryClient.invalidateQueries({
76+
queryKey: moderationRestrictionsQueryKey,
77+
}),
78+
]);
79+
}
80+
81+
/** Submit a NIP-56 report. Does not touch the mod-gated read caches. */
82+
export function useSubmitReportMutation() {
83+
return useMutation({
84+
mutationFn: (input: {
85+
authorPubkey: string;
86+
eventId: string;
87+
reportType: ReportType;
88+
note?: string;
89+
}) => submitReport(input),
90+
});
91+
}
92+
93+
export function useBanMemberMutation() {
94+
const invalidate = useInvalidateModerationReads();
95+
return useMutation({
96+
mutationFn: (input: {
97+
pubkey: string;
98+
expiresAt?: number;
99+
reason?: string;
100+
}) => banMember(input),
101+
onSuccess: invalidate,
102+
});
103+
}
104+
105+
export function useUnbanMemberMutation() {
106+
const invalidate = useInvalidateModerationReads();
107+
return useMutation({
108+
mutationFn: (pubkey: string) => unbanMember(pubkey),
109+
onSuccess: invalidate,
110+
});
111+
}
112+
113+
export function useTimeoutMemberMutation() {
114+
const invalidate = useInvalidateModerationReads();
115+
return useMutation({
116+
mutationFn: (input: {
117+
pubkey: string;
118+
expiresAt: number;
119+
reason?: string;
120+
}) => timeoutMember(input),
121+
onSuccess: invalidate,
122+
});
123+
}
124+
125+
export function useUntimeoutMemberMutation() {
126+
const invalidate = useInvalidateModerationReads();
127+
return useMutation({
128+
mutationFn: (pubkey: string) => untimeoutMember(pubkey),
129+
onSuccess: invalidate,
130+
});
131+
}
132+
133+
export function useResolveReportMutation() {
134+
const invalidate = useInvalidateModerationReads();
135+
return useMutation({
136+
mutationFn: (input: {
137+
reportEventId: string;
138+
status: ResolutionStatus;
139+
action: ResolutionAction;
140+
reason?: string;
141+
}) => resolveReport(input),
142+
onSuccess: invalidate,
143+
});
144+
}
145+
146+
export type {
147+
CommunityRestriction,
148+
ModerationAction,
149+
ModerationReport,
150+
ReportType,
151+
ResolutionAction,
152+
ResolutionStatus,
153+
};

0 commit comments

Comments
 (0)