Repository navigation
Expand file tree
/
Copy pathadmin-protocol.ts
More file actions
108 lines (102 loc) · 3.54 KB
/
Copy pathadmin-protocol.ts
File metadata and controls
108 lines (102 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Owner/admin community commands shared by the development broker and the
// native adapter. Dependency-free so the Node broker can import it. The relay
// decides authority; this only bounds shape.
const MIN_TTL = 60;
const MAX_TTL = 30 * 24 * 60 * 60;
const MAX_USES = 10_000;
const ROLES = ["admin", "member"];
const KINDS: Record<string, number> = { add: 9030, remove: 9031, role: 9032 };
/** Normalized `POST /api/invites` body; unknown fields are dropped. */
export function inviteRequest(value: unknown) {
const { ttl_secs: ttl, max_uses: uses = null } = (value ?? {}) as {
ttl_secs?: unknown;
max_uses?: unknown;
};
if (
typeof ttl !== "number" ||
!Number.isSafeInteger(ttl) ||
ttl < MIN_TTL ||
ttl > MAX_TTL
)
throw new Error("Invalid invite expiry");
if (
uses !== null &&
(typeof uses !== "number" ||
!Number.isSafeInteger(uses) ||
uses < 1 ||
uses > MAX_USES)
)
throw new Error("Invalid invite use limit");
return { ttl_secs: ttl, max_uses: uses };
}
/** Unsigned NIP-43 add/remove/role command for one member. */
export function memberCommand(
value: unknown,
now = Math.floor(Date.now() / 1000),
) {
const { action, pubkey, role } = (value ?? {}) as {
action?: unknown;
pubkey?: unknown;
role?: unknown;
};
const kind =
typeof action === "string" && Object.hasOwn(KINDS, action)
? KINDS[action]
: undefined;
if (!kind || typeof pubkey !== "string" || !/^[0-9a-f]{64}$/.test(pubkey))
throw new Error("Invalid member change");
if (
action === "remove"
? role !== undefined
: typeof role !== "string" || !ROLES.includes(role)
)
throw new Error("Invalid member role");
return {
kind,
content: "",
created_at: now,
tags: [
["p", pubkey],
...(typeof role === "string" ? [["role", role]] : []),
],
};
}
// Exact relay refusals (buzz-relay relay_admin.rs, ingest.rs, api/invites.rs).
// Anything else, including database/internal text, stays a generic summary.
const REASONS = new Set([
"invalid: actor not authorized: must be admin or owner",
"invalid: actor not authorized: must be owner",
"invalid: actor not authorized: only owner can grant admin role",
"invalid: actor not authorized: admins can only remove members",
"invalid: cannot remove yourself",
"invalid: cannot remove the relay owner",
"invalid: cannot change your own role",
"invalid: cannot change the relay owner's role",
"invalid: cannot set role to owner",
"blocked: you are banned from this community",
"only relay owners and admins can create invites",
"community writes are temporarily unavailable",
]);
/** Relay-authored refusal worth showing verbatim; anything else stays generic. */
export function adminReason(body: unknown): string | undefined {
const reason = (body as { error?: unknown } | null)?.error;
if (typeof reason !== "string") return undefined;
return REASONS.has(reason) ||
/^invalid: member not found: [0-9a-f]{64}$/.test(reason) ||
/^(?:ttl_secs|max_uses) must be between \d{1,8} and \d{1,8}$/.test(reason)
? reason
: undefined;
}
// Exact invite-claim refusal codes (buzz-relay api/invites.rs claim_invite).
const CLAIM_REASONS = new Set([
"invite_expired",
"invite_exhausted",
"invite_invalid",
]);
/** Exact relay claim refusal code; anything else stays generic. */
export function claimReason(body: unknown): string | undefined {
const reason = (body as { error?: unknown } | null)?.error;
return typeof reason === "string" && CLAIM_REASONS.has(reason)
? reason
: undefined;
}