~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

A group defines labels of its own, for moderators to choose and apps to read

Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:host/label-definitions
crates/group-host/src/group_api.rs+7−0
@@ -241,6 +241,13 @@ pub async fn get_group(
merge(&mut view, profile_view(&profile));
}
view["rules"] = json!(rules(&host, group)?);
let labeler = record(&meta, "labeler", "self")?.unwrap_or_default();
let labels = &labeler["policies"]["labelValueDefinitions"];
view["labels"] = if labels.is_array() {
labels.clone()
} else {
json!([])
};
}
let member = !roles.is_empty();
let mut viewer = json!({
crates/group-host/src/lexicons.rs+2−0
@@ -53,6 +53,7 @@ pub const FILES: &[&str] = &[
lexicon!("fyi/opensocial/declineInvite"),
lexicon!("fyi/opensocial/defs"),
lexicon!("fyi/opensocial/deleteGroup"),
lexicon!("fyi/opensocial/deleteLabelDefinition"),
lexicon!("fyi/opensocial/deleteSite"),
lexicon!("fyi/opensocial/deleteSpace"),
lexicon!("fyi/opensocial/denyJoinRequest"),
@@ -102,6 +103,7 @@ pub const FILES: &[&str] = &[
lexicon!("fyi/opensocial/permissions"),
lexicon!("fyi/opensocial/profile"),
lexicon!("fyi/opensocial/provisionGroup"),
lexicon!("fyi/opensocial/putLabelDefinition"),
lexicon!("fyi/opensocial/putSite"),
lexicon!("fyi/opensocial/removeRecoveryKey"),
lexicon!("fyi/opensocial/requestJoin"),
crates/group-host/src/lib.rs+8−0
@@ -371,6 +371,14 @@ pub(crate) fn routes(host: Arc<Host>) -> Router {
"/xrpc/fyi.opensocial.negateLabel",
post(moderation::negate_label),
)
.route(
"/xrpc/fyi.opensocial.putLabelDefinition",
post(moderation::put_label_definition),
)
.route(
"/xrpc/fyi.opensocial.deleteLabelDefinition",
post(moderation::delete_label_definition),
)
.route(
"/xrpc/fyi.opensocial.listSubjects",
get(moderation::list_subjects),
crates/group-host/src/moderation.rs+111−0
@@ -645,3 +645,114 @@ pub async fn create_report(
let id = id.map_err(XrpcError::internal)?;
Ok(Json(json!({"id": id.to_string(), "createdAt": iso(now)})))
}
/// A label a group defines for itself: lowercase letters and hyphens, as
/// atproto has a labeler's own values; never one of the values atproto
/// defines, which start with `!`.
fn is_own_label(label: &str) -> bool {
!label.is_empty() && label.bytes().all(|b| b.is_ascii_lowercase() || b == b'-')
}
impl Host {
/// The group's labeler, as its meta space keeps it, and its policies'
/// values and definitions, made lists where it had none.
fn labeler_of(&self, group: &str) -> Result<Value, XrpcError> {
let meta = meta_space(group);
let kept = self.group_record(group, &meta, &format!("{NS}.labeler"), "self");
let mut labeler = kept.map_err(XrpcError::internal)?.unwrap_or_default();
for key in ["labelValues", "labelValueDefinitions"] {
if !labeler["policies"][key].is_array() {
labeler["policies"][key] = json!([]);
}
}
Ok(labeler)
}
/// `labeler` put back whole, as the group, after `change` to its
/// policies' values and definitions.
async fn relabel(
&self,
group: &str,
mut labeler: Value,
change: impl FnOnce(&mut Vec<Value>, &mut Vec<Value>),
) -> Result<(), XrpcError> {
let policies = labeler["policies"].as_object_mut().expect("made an object");
let mut values = policies["labelValues"].take();
let mut definitions = policies["labelValueDefinitions"].take();
change(
values.as_array_mut().expect("made a list"),
definitions.as_array_mut().expect("made a list"),
);
policies.insert("labelValues".into(), values);
policies.insert("labelValueDefinitions".into(), definitions);
let write = Write::Update {
collection: format!("{NS}.labeler"),
rkey: "self".into(),
value: labeler,
};
self.space_write(group, &meta_space(group), &[write])
.await?;
Ok(())
}
}
/// Defines one of the group's own labels, or changes one it defines, in
/// its labeler, the label among the values it applies.
pub async fn put_label_definition(
State(host): State<Arc<Host>>,
headers: HeaderMap,
Json(body): Json<Value>,
) -> Answer {
let group = body["group"].as_str().expect("its lexicon requires it");
host.moderator(&headers, "putLabelDefinition", group, "group.configure")
.await?;
let label = body["label"].as_str().expect("its lexicon requires it");
if !is_own_label(label) {
return Err(invalid("label is lowercase letters and hyphens"));
}
let or = |key: &str, unless: &str| body[key].as_str().unwrap_or(unless).to_string();
let mut definition = json!({
"identifier": label,
"severity": or("severity", "inform"),
"blurs": or("blurs", "none"),
"defaultSetting": or("defaultSetting", "warn"),
"locales": [{"lang": or("lang", "en"), "name": body["name"], "description": body["description"]}],
});
if body["adultOnly"] == true {
definition["adultOnly"] = json!(true);
}
let labeler = host.labeler_of(group)?;
let defined = definition.clone();
let change = |values: &mut Vec<Value>, definitions: &mut Vec<Value>| {
if !values.contains(&json!(label)) {
values.push(json!(label));
}
definitions.retain(|d| d["identifier"] != label);
definitions.push(defined);
};
host.relabel(group, labeler, change).await?;
Ok(Json(definition))
}
/// Stops defining one of the group's own labels: its definition, and the
/// value among those it applies. Labels already applied stay.
pub async fn delete_label_definition(
State(host): State<Arc<Host>>,
headers: HeaderMap,
Json(body): Json<Value>,
) -> Answer {
let group = body["group"].as_str().expect("its lexicon requires it");
host.moderator(&headers, "deleteLabelDefinition", group, "group.configure")
.await?;
let label = body["label"].as_str().expect("its lexicon requires it");
if !is_own_label(label) {
return Err(invalid("label is lowercase letters and hyphens"));
}
let labeler = host.labeler_of(group)?;
let change = |values: &mut Vec<Value>, definitions: &mut Vec<Value>| {
values.retain(|v| v != label);
definitions.retain(|d| d["identifier"] != label);
};
host.relabel(group, labeler, change).await?;
Ok(Json(json!({})))
}
crates/group-host/tests/moderation.rs+84−0
@@ -786,3 +786,87 @@ fn keeps_what_was_resolved_escalated_and_noted_as_a_status_set() {
);
assert_eq!(said("did:plc:c"), [status("open", Some("first"))]);
}
#[tokio::test]
async fn defines_the_groups_own_labels_for_one_who_configures_it() {
let h = start(&[], false).await;
let (did, _, _, [alice, bob, _]) = group(&h).await;
let labels = async || {
let (_, view) = query(&h, &alice, "getGroup", &format!("group={did}")).await;
view["group"]["labels"].clone()
};
// A new group defines none of its own.
assert_eq!(labels().await, json!([]));
let troll = json!({"group": did, "label": "troll", "name": "Troll", "description": "Picks fights on purpose."});
let (status, defined) = call(&h, &alice, "putLabelDefinition", troll.clone()).await;
assert_eq!(status, 200, "{defined}");
assert_eq!(defined["identifier"], "troll");
assert_eq!(defined["severity"], "inform");
assert_eq!(defined["blurs"], "none");
assert_eq!(defined["defaultSetting"], "warn");
assert_eq!(
defined["locales"][0],
json!({"lang": "en", "name": "Troll", "description": "Picks fights on purpose."})
);
assert!(defined.get("adultOnly").is_none());
assert_eq!(labels().await, json!([defined]));
// Defined again, it is changed in place, not listed twice; and it is
// among the values the labeler applies, beside atproto's.
let again = json!({"group": did, "label": "troll", "name": "Troll", "description": "Baits people.", "lang": "sl", "severity": "alert", "blurs": "content", "defaultSetting": "hide", "adultOnly": true});
let (_, changed) = call(&h, &alice, "putLabelDefinition", again).await;
assert_eq!(changed["adultOnly"], true);
assert_eq!(changed["locales"][0]["lang"], "sl");
assert_eq!(labels().await, json!([changed]));
let meta = meta_space(&did);
let labeler = h
.host
.group_record(&did, &meta, "fyi.opensocial.labeler", "self");
let labeler = labeler.unwrap().unwrap();
assert_eq!(
labeler["policies"]["labelValues"],
json!(["!hide", "!takedown", "troll"])
);
assert_eq!(labeler["subjectTypes"], json!(["account", "record"]));
// atproto's own values, and anything else, are not the group's to define.
for label in ["!hide", "Troll", "spam bot", ""] {
let body = json!({"group": did, "label": label, "name": "x", "description": "x"});
let (status, said) = call(&h, &alice, "putLabelDefinition", body).await;
assert_eq!(status, 400, "{label}: {said}");
let (status, _) = call(
&h,
&alice,
"deleteLabelDefinition",
json!({"group": did, "label": label}),
)
.await;
assert_eq!(status, 400, "{label}");
}
// Bob, a member, does not configure it.
let (status, said) = call(&h, &bob, "putLabelDefinition", troll).await;
assert_eq!((status, said["error"].as_str()), (403, Some("Forbidden")));
let (status, _) = call(
&h,
&bob,
"deleteLabelDefinition",
json!({"group": did, "label": "troll"}),
)
.await;
assert_eq!(status, 403);
// Taken away, it is neither defined nor applied, and atproto's stay.
let (status, said) = call(
&h,
&alice,
"deleteLabelDefinition",
json!({"group": did, "label": "troll"}),
)
.await;
assert_eq!((status, said), (200, json!({})));
assert_eq!(labels().await, json!([]));
let labeler = h
.host
.group_record(&did, &meta, "fyi.opensocial.labeler", "self");
assert_eq!(
labeler.unwrap().unwrap()["policies"]["labelValues"],
json!(["!hide", "!takedown"])
);
}
crates/group-host-mcp/src/lib.rs+15−0
@@ -51,6 +51,8 @@ const LEXICONS: &[(&str, &str)] = &[
lexicon!("getSubjectHistory"),
lexicon!("applyLabel"),
lexicon!("negateLabel"),
lexicon!("putLabelDefinition"),
lexicon!("deleteLabelDefinition"),
lexicon!("resolveSubject"),
lexicon!("putSite"),
lexicon!("deleteSite"),
@@ -340,6 +342,19 @@ const TOOLS: &[(&str, &[&str], &str, Made)] = &[
"Deletes a rule as the group.",
Made::Delete("rule", META, At::Named("rule")),
),
(
"label_define",
&["label"],
"Defines one of the group's own labels, or changes one it defines: what it is called and \
means, and how apps should show what wears it.",
Made::Method("putLabelDefinition", None),
),
(
"label_delete",
&["label"],
"Stops defining one of the group's own labels. Labels already applied stay.",
Made::Method("deleteLabelDefinition", None),
),
(
"role_set",
&["role"],
crates/group-host-mcp/tests/protocol.rs+16−0
@@ -35,6 +35,8 @@ fn names_each_tool_for_its_command() {
"profile_set",
"rule_set",
"rule_delete",
"label_define",
"label_delete",
"role_set",
"role_delete",
"role_grant",
@@ -206,6 +208,20 @@ fn plans_a_method_asked_with_its_arguments() {
panic!()
};
assert_eq!(input, json!({"limit": 5}));
// A label defined, its flags in snake case asked in the method's own names.
let args = json!({"label": "troll", "name": "Troll", "description": "Picks fights.", "default_setting": "hide", "adult_only": true});
let Ok(Plan::Call { nsid, input, .. }) = planned("label_define", args) else {
panic!()
};
assert_eq!(nsid, "fyi.opensocial.putLabelDefinition");
assert_eq!(
input,
json!({"label": "troll", "name": "Troll", "description": "Picks fights.", "defaultSetting": "hide", "adultOnly": true})
);
let Ok(Plan::Call { nsid, .. }) = planned("label_delete", json!({"label": "troll"})) else {
panic!()
};
assert_eq!(nsid, "fyi.opensocial.deleteLabelDefinition");
// An account, or a record at a version; the opposite asked instead.
let call = |args: Value| match planned("mod_label", args).unwrap() {
Plan::Call { nsid, input, .. } => (nsid, input),
crates/opensocial/src/lib.rs+5−0
@@ -42,6 +42,11 @@ rule
write a rule: the one --rule names, or a new one
rule delete <group> <rule>
delete a rule
label
label define <group> <label> --name <name> --description <words> [--severity <s>] …
define one of the group's own labels, or change one it defines
label delete <group> <label>
stop defining one of the group's own labels
role
role set <group> <role> --display-name <name> [--description <words>] [--default] [--bot]
write a role
docs/design/moderation.md+19−0
@@ -169,6 +169,25 @@ of its lists, when present, limits them: `reasonTypes` the reasons,
group's labeler takes reports of accounts and records, for the common
reasons, of any collection.
## The group's own labels
A label anyone can put on anything says nothing to an app that does not
know what it means. So the group's labeler also lists the labels it
applies, in `policies.labelValues`, and defines its own in
`policies.labelValueDefinitions`, each as atproto defines one: its
`identifier`, its name and what it means in `locales`, and how apps
should show what wears it (`severity`, `blurs`, `defaultSetting`,
`adultOnly`). A new group applies `!hide` and `!takedown`, which atproto
defines, and none of its own.
`putLabelDefinition` defines one, or changes the one with that value, and
adds it to the values the labeler applies. `deleteLabelDefinition` takes
both away; labels already applied stay. Each asks `group.configure`, and
writes the labeler as the group, as any change to its meta space is. A
group's own label is lowercase letters and hyphens, as atproto has a
labeler's own values, so atproto's are never redefined. `getGroup`
answers the definitions as `labels`, to whoever may read the meta space.
Details:
- A group not hosted here is `GroupNotFound`. A subject the reporter
docs/guides/shots/apps-activity-dark.png+0−0
A binary file, not shown.
docs/guides/shots/apps-activity.png+0−0
A binary file, not shown.
docs/guides/shots/apps-dialog-dark.png+0−0
A binary file, not shown.
docs/guides/shots/apps-dialog.png+0−0
A binary file, not shown.
docs/guides/shots/apps-empty-dark.png+0−0
A binary file, not shown.
docs/guides/shots/apps-empty.png+0−0
A binary file, not shown.
docs/guides/shots/apps-empty.txt+1−0
@@ -7,6 +7,7 @@
- navigation "Settings":
- link "Profile":
- link "Rules":
- link "Labels":
- link "Roles":
- link "Apps":
- link "Recovery keys":
docs/guides/shots/apps-made-dark.png+0−0
A binary file, not shown.
docs/guides/shots/apps-made.png+0−0
A binary file, not shown.
docs/guides/shots/apps-revoke-password-dark.png+0−0
A binary file, not shown.
docs/guides/shots/apps-revoke-password.png+0−0
A binary file, not shown.
docs/guides/shots/apps-signed-in-dark.png+0−0
A binary file, not shown.
docs/guides/shots/apps-signed-in.png+0−0
A binary file, not shown.
docs/guides/shots/moderating-define-dialog-dark.pngnew+0−0
A binary file, not shown.
docs/guides/shots/moderating-define-dialog.pngnew+0−0
A binary file, not shown.
docs/guides/shots/moderating-define-dialog.txtnew+27−0
docs/guides/shots/moderating-defined-dark.pngnew+0−0
A binary file, not shown.
docs/guides/shots/moderating-defined.pngnew+0−0
A binary file, not shown.
docs/guides/shots/moderating-defined.txtnew+8−0
docs/guides/shots/moderating-found-dark.png+0−0
A binary file, not shown.
docs/guides/shots/moderating-found.png+0−0
A binary file, not shown.
docs/guides/shots/moderating-found.txt+3−2
docs/guides/shots/moderating-label-dialog-dark.png+0−0
A binary file, not shown.
docs/guides/shots/moderating-label-dialog.png+0−0
A binary file, not shown.
docs/guides/shots/moderating-label-dialog.txt+2−0
docs/guides/shots/moderating-labelled-dark.png+0−0
A binary file, not shown.
docs/guides/shots/moderating-labelled.png+0−0
A binary file, not shown.
docs/guides/shots/moderating-labelled.txt+2−0
docs/guides/shots/moderating-resolve-dialog-dark.png+0−0
A binary file, not shown.
docs/guides/shots/moderating-resolve-dialog.png+0−0
A binary file, not shown.
docs/guides/shots/moderating-takedown-dark.png+0−0
A binary file, not shown.
docs/guides/shots/moderating-takedown.png+0−0
A binary file, not shown.
docs/guides/shots/moderating-takedown.txt+2−0
docs/guides/shots/recovery-added-dark.png+0−0
A binary file, not shown.
docs/guides/shots/recovery-added.png+0−0
A binary file, not shown.
docs/guides/shots/recovery-dialog-dark.png+0−0
A binary file, not shown.
docs/guides/shots/recovery-dialog.png+0−0
A binary file, not shown.
docs/guides/shots/recovery-refused-dark.png+0−0
A binary file, not shown.
docs/guides/shots/recovery-refused.png+0−0
A binary file, not shown.
docs/guides/shots/recovery-removed-dark.png+0−0
A binary file, not shown.
docs/guides/shots/recovery-removed.png+0−0
A binary file, not shown.
docs/guides/shots/recovery-tab-dark.png+0−0
A binary file, not shown.
docs/guides/shots/recovery-tab.png+0−0
A binary file, not shown.
docs/guides/shots/recovery-tab.txt+1−0
docs/guides/shots/starting-avatar-dark.png+0−0
A binary file, not shown.
docs/guides/shots/starting-avatar.png+0−0
A binary file, not shown.
docs/guides/shots/starting-words.txt+1−0
docs/guides/moderating.md+30−2
docs/reference/api.md+2−0
docs/reference/cli.md+7−0
docs/reference/mcp.md+7−0
harness/e2e/flows.spec.mjs+25−1
harness/look/seeds/reports.mjs+20−1
lexicons/fyi/opensocial/defs.json+8−0
lexicons/fyi/opensocial/deleteLabelDefinition.jsonnew+34−0
lexicons/fyi/opensocial/putLabelDefinition.jsonnew+73−0
web/src/lib/organisms/LabelsEditor.sveltenew+104−0
web/src/lib/organisms/ModerationQueue.svelte+13−3
web/src/lib/organisms/ReviewDialog.svelte+10−7
web/src/lib/server/settings.ts+21−0
web/src/routes/g/[handle]/moderation/+page.svelte+1−0
web/src/routes/g/[handle]/settings/labels/+page.server.tsnew+4−0
web/src/routes/g/[handle]/settings/labels/+page.sveltenew+18−0
web/src/routes/g/[handle]/settings/+layout.svelte+1−0