mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
fix(cli): report presence subject (#2104)
Signed-off-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co>
This commit is contained in:
co-authored by
npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf
parent
d011c3f747
commit
5c359deb9b
@@ -265,7 +265,7 @@ pub async fn cmd_get_presence(client: &BuzzClient, pubkeys_csv: &str) -> Result<
|
||||
.iter()
|
||||
.map(|e| {
|
||||
serde_json::json!({
|
||||
"pubkey": e.get("pubkey").and_then(|v| v.as_str()).unwrap_or(""),
|
||||
"pubkey": presence_subject(e),
|
||||
"status": e.get("content").and_then(|v| v.as_str()).unwrap_or(""),
|
||||
"updated_at": e.get("created_at").and_then(|v| v.as_u64()).unwrap_or(0),
|
||||
})
|
||||
@@ -276,6 +276,20 @@ pub async fn cmd_get_presence(client: &BuzzClient, pubkeys_csv: &str) -> Result<
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn presence_subject(event: &serde_json::Value) -> &str {
|
||||
event
|
||||
.get("tags")
|
||||
.and_then(|tags| tags.as_array())
|
||||
.and_then(|tags| {
|
||||
tags.iter()
|
||||
.find_map(|tag| match tag.as_array()?.as_slice() {
|
||||
[name, subject, ..] if name == "p" => subject.as_str(),
|
||||
_ => None,
|
||||
})
|
||||
})
|
||||
.unwrap_or_else(|| event.get("pubkey").and_then(|v| v.as_str()).unwrap_or(""))
|
||||
}
|
||||
|
||||
/// Set presence status — sign and submit a kind:20001 presence update event via WebSocket.
|
||||
///
|
||||
/// Kind 20001 is ephemeral and only accepted via WebSocket connections. This
|
||||
@@ -319,3 +333,27 @@ pub async fn dispatch(
|
||||
UsersCmd::SetPresence { status } => cmd_set_presence(client, &status.to_string()).await,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::presence_subject;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn presence_subject_uses_p_tag() {
|
||||
let event = json!({"pubkey": "relay", "tags": [["p", "user"]]});
|
||||
assert_eq!(presence_subject(&event), "user");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn presence_subject_falls_back_to_author_without_p_tag() {
|
||||
let event = json!({"pubkey": "user", "tags": [["status", "online"]]});
|
||||
assert_eq!(presence_subject(&event), "user");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn presence_subject_falls_back_to_author_for_malformed_p_tag() {
|
||||
let event = json!({"pubkey": "user", "tags": [["p"]]});
|
||||
assert_eq!(presence_subject(&event), "user");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user