Skip to content

Commit f3baa96

Browse files
matt2eclaude
andcommitted
fix(cli): report the presence subject, not the relay signer
buzz users presence misidentified who is online: the relay's /query bridge intercepts presence filters and synthesizes relay-signed kind:20001 events (bridge.rs synthesize_presence) with the subject in the p tag, but cmd_get_presence mapped event.pubkey — the relay keypair — so every row reported the relay signer's pubkey. Extract the row mapping into map_presence_events, which reports the p-tag subject when present and falls back to the event author otherwise. The fallback keeps real user-signed kind:20001 updates correct (build_presence_update emits no p tag — the author is the subject there) and guards against a malformed bare ["p"] tag blanking the pubkey. Regression tests cover all three shapes: synthesized (p-tag subject wins over the relay signer), user-signed (author reported), and malformed p tag (author fallback). Tested: cargo test -p buzz-cli --lib (149 passed, incl. the 3 new tests), cargo clippy -p buzz-cli --all-targets -D warnings, cargo fmt --check. Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent 0b5525f commit f3baa96

1 file changed

Lines changed: 88 additions & 6 deletions

File tree

‎crates/buzz-cli/src/commands/users.rs‎

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,19 +261,45 @@ pub async fn cmd_get_presence(client: &BuzzClient, pubkeys_csv: &str) -> Result<
261261
});
262262
let resp = client.query(&filter).await?;
263263
let events: Vec<serde_json::Value> = serde_json::from_str(&resp).unwrap_or_default();
264-
let presence: Vec<serde_json::Value> = events
264+
let presence = map_presence_events(&events);
265+
let output = serde_json::to_string(&presence).unwrap_or_default();
266+
println!("{output}");
267+
Ok(())
268+
}
269+
270+
/// Map raw presence events to the CLI's `{pubkey, status, updated_at}` rows.
271+
///
272+
/// Presence from the relay's `/query` bridge is synthesized: the event is
273+
/// signed with the *relay* keypair and names the online user in a `p` tag, so
274+
/// the subject must be read from that tag — `event.pubkey` there is the relay
275+
/// signer, not who is online. Real user-signed kind:20001 updates carry no
276+
/// `p` tag; for those the author is the subject.
277+
fn map_presence_events(events: &[serde_json::Value]) -> Vec<serde_json::Value> {
278+
events
265279
.iter()
266280
.map(|e| {
281+
let author = e.get("pubkey").and_then(|v| v.as_str()).unwrap_or("");
282+
let subject = e
283+
.get("tags")
284+
.and_then(|t| t.as_array())
285+
.and_then(|tags| {
286+
tags.iter().find_map(|tag| {
287+
let tag = tag.as_array()?;
288+
if tag.first()?.as_str()? == "p" {
289+
tag.get(1)?.as_str()
290+
} else {
291+
None
292+
}
293+
})
294+
})
295+
.unwrap_or(author);
267296
serde_json::json!({
268-
"pubkey": e.get("pubkey").and_then(|v| v.as_str()).unwrap_or(""),
297+
"pubkey": subject,
269298
"status": e.get("content").and_then(|v| v.as_str()).unwrap_or(""),
270299
"updated_at": e.get("created_at").and_then(|v| v.as_u64()).unwrap_or(0),
271300
})
272301
})
273-
.collect();
274-
let output = serde_json::to_string(&presence).unwrap_or_default();
275-
println!("{output}");
276-
Ok(())
302+
.collect()
277303
}
278304

279305
/// Set presence status — sign and submit a kind:20001 presence update event via WebSocket.
@@ -319,3 +345,59 @@ pub async fn dispatch(
319345
UsersCmd::SetPresence { status } => cmd_set_presence(client, &status.to_string()).await,
320346
}
321347
}
348+
349+
#[cfg(test)]
350+
mod tests {
351+
use super::*;
352+
353+
const RELAY_PK: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
354+
const SUBJECT_PK: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
355+
356+
#[test]
357+
fn synthesized_presence_reports_p_tag_subject_not_relay_signer() {
358+
// Shape produced by the relay's /query bridge (synthesize_presence):
359+
// relay-signed, subject in the `p` tag.
360+
let events = vec![serde_json::json!({
361+
"pubkey": RELAY_PK,
362+
"kind": 20001,
363+
"content": "online",
364+
"created_at": 1700000000,
365+
"tags": [["p", SUBJECT_PK]],
366+
})];
367+
let rows = map_presence_events(&events);
368+
assert_eq!(rows.len(), 1);
369+
assert_eq!(rows[0]["pubkey"], SUBJECT_PK);
370+
assert_eq!(rows[0]["status"], "online");
371+
assert_eq!(rows[0]["updated_at"], 1700000000u64);
372+
}
373+
374+
#[test]
375+
fn user_signed_presence_without_p_tag_reports_author() {
376+
// Shape published by `buzz users set-presence` (build_presence_update):
377+
// user-signed, no `p` tag, only a `status` tag.
378+
let events = vec![serde_json::json!({
379+
"pubkey": SUBJECT_PK,
380+
"kind": 20001,
381+
"content": "away",
382+
"created_at": 1700000001,
383+
"tags": [["status", "away"]],
384+
})];
385+
let rows = map_presence_events(&events);
386+
assert_eq!(rows.len(), 1);
387+
assert_eq!(rows[0]["pubkey"], SUBJECT_PK);
388+
assert_eq!(rows[0]["status"], "away");
389+
}
390+
391+
#[test]
392+
fn malformed_p_tag_falls_back_to_author() {
393+
// A bare ["p"] tag with no value must not blank the pubkey.
394+
let events = vec![serde_json::json!({
395+
"pubkey": SUBJECT_PK,
396+
"content": "online",
397+
"created_at": 1700000002,
398+
"tags": [["p"]],
399+
})];
400+
let rows = map_presence_events(&events);
401+
assert_eq!(rows[0]["pubkey"], SUBJECT_PK);
402+
}
403+
}

0 commit comments

Comments
 (0)