SkateSpots/src/hooks/useAdminModeration.ts
shakespeare.diy 82a74e01e0 Extend admin takedown to Lab profiles and gear/marketplace listings
Completes the admin moderation coverage from the previous commit.

Lab profiles (coaches, filmers, editors, photographers, builders, artists,
graffiti, tattoo, trainers, musicians):
- useLabProfiles / useLabProfile and useCoaches / useCoach now filter out
  admin-hidden entries (list) and 404 hidden ones (detail) via the shared
  moderation layer. Added filterAdminHiddenMixed() for the mixed-kind Lab
  union (kind read from each item's event).
- CoachDetailPage (the shared Lab detail page) now shows the delete/takedown
  card to admins too, not just the owner, with admin-specific copy.
  DeleteEntityDialog already handles the owner-or-admin logic.

Gear / marketplace listings (NIP-99, kind 30402):
- useMarketplaceItems / useMarketplaceAtSpot filter out admin-hidden listings.
- GearSwapItemCard: admins get a "Remove (admin)" action in the card menu with
  a confirmation dialog that routes through useDeleteEntity (NIP-09 delete +
  hidden label). Report stays for non-owners.

SECURITY.md: updated the admin-moderation coverage list and auditor file map.
Build + lint pass, no console errors.

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
2026-08-18 19:17:16 +07:00

130 lines
4.1 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { useSkateRelays } from '@/hooks/useSkateRelays';
import { ADMIN_PUBKEYS } from '@/lib/admins';
import {
MODERATION_LABEL_NAMESPACE,
MODERATION_LABEL_HIDDEN,
} from '@/lib/types';
/**
* App-wide admin moderation layer.
*
* SkateSpots admins (see src/lib/admins.ts) can take down ANY listing (spot,
* crew, tour, event, shop, Lab profile, …). Because a NIP-09 deletion request
* signed by a non-author is not honored by most relays, admin takedowns also
* publish a NIP-32 kind:1985 `hidden` label referencing the target. This hook
* fetches those admin labels so the client can hide the content everywhere,
* reliably, regardless of relay NIP-09 behavior.
*
* Security: only labels authored by a hardcoded ADMIN pubkey are honored, so a
* random user cannot hide someone else's listing. The label authorship is the
* trust boundary (see .agents/skills/nostr-security §3).
*/
export interface AdminHiddenSet {
/** Hidden addressable coordinates `kind:pubkey:d`. */
coords: Set<string>;
/** Hidden concrete event ids. */
ids: Set<string>;
/** True if either set contains anything (cheap guard for callers). */
has: (opts: { coord?: string; id?: string }) => boolean;
}
const EMPTY: AdminHiddenSet = {
coords: new Set(),
ids: new Set(),
has: () => false,
};
/**
* Drop admin-hidden entities from a parsed list. Works for any entity that
* carries `{ id, pubkey, identifier }` (spots, crews, tours, shops, events, …).
* `kind` is the entity's Nostr kind, used to build the `a` coordinate.
*/
export function filterAdminHidden<
T extends { id: string; pubkey: string; identifier: string },
>(items: T[], kind: number, hidden: AdminHiddenSet): T[] {
if (hidden.coords.size === 0 && hidden.ids.size === 0) return items;
return items.filter(
(it) =>
!hidden.has({ coord: `${kind}:${it.pubkey}:${it.identifier}`, id: it.id }),
);
}
/**
* Like `filterAdminHidden`, but for a mixed-kind list where each item carries
* its own source `event` (e.g. Lab profiles, which span 10 different kinds).
* The kind is read from `it.event.kind`.
*/
export function filterAdminHiddenMixed<
T extends {
id: string;
pubkey: string;
identifier: string;
event: { kind: number };
},
>(items: T[], hidden: AdminHiddenSet): T[] {
if (hidden.coords.size === 0 && hidden.ids.size === 0) return items;
return items.filter(
(it) =>
!hidden.has({
coord: `${it.event.kind}:${it.pubkey}:${it.identifier}`,
id: it.id,
}),
);
}
export function useAdminModeration() {
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
const query = useQuery({
queryKey: ['admin-moderation', getQueryKeySuffix()],
queryFn: async (): Promise<AdminHiddenSet> => {
const signal = AbortSignal.timeout(8000);
const events = await querySkateContent(
[
{
kinds: [1985],
authors: ADMIN_PUBKEYS,
'#l': [MODERATION_LABEL_HIDDEN],
limit: 1000,
},
],
{ signal },
);
const adminSet = new Set(ADMIN_PUBKEYS.map((p) => p.toLowerCase()));
const coords = new Set<string>();
const ids = new Set<string>();
for (const ev of events) {
// Defense in depth: re-verify author + namespace + label client-side.
if (!adminSet.has(ev.pubkey.toLowerCase())) continue;
const hasNamespace = ev.tags.some(
([n, v]) => n === 'L' && v === MODERATION_LABEL_NAMESPACE,
);
const hasLabel = ev.tags.some(
([n, v]) => n === 'l' && v === MODERATION_LABEL_HIDDEN,
);
if (!hasNamespace || !hasLabel) continue;
for (const [n, v] of ev.tags) {
if (n === 'a' && v) coords.add(v);
if (n === 'e' && v) ids.add(v);
}
}
return {
coords,
ids,
has: ({ coord, id }) =>
(!!coord && coords.has(coord)) || (!!id && ids.has(id)),
};
},
staleTime: 60 * 1000,
gcTime: 10 * 60 * 1000,
refetchOnMount: true,
});
return query.data ?? EMPTY;
}