mirror of
https://github.com/block/buzz.git
synced 2026-10-30 14:01:37 +00:00
Signed-off-by: Will Pfleger <pfleger.will@gmail.com> Signed-off-by: Will Pfleger <wpfleger@block.xyz> Signed-off-by: Will Pfleger <wpfleger@squareup.com> Signed-off-by: Will Pfleger <wpfleger96@gmail.com> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1fgdl5qqnh3k3f2xkqrvt7cujalhm623x4s7fdjdj5yrtp5fzjl9qrjpucw <4a1bfa0013bc6d14a8d600d8bf6392efefbd2a26ac3c96c9b2a106b0d12297ca@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub16v54tttfqacx9ycvc3k0ut0npj564ahcuajzy6qjvh57ntmsf4uq4806j2 <d32955ad69077062930cc46cfe2df30ca9aaf6f8e76422681265e9e9af704d78@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Will Pfleger <wpfleger96@gmail.com>
85 lines
2.8 KiB
Rust
85 lines
2.8 KiB
Rust
use std::cmp::Reverse;
|
|
|
|
use crate::client::{normalize_events, BuzzClient};
|
|
use crate::error::CliError;
|
|
|
|
const VALID_FEED_TYPES: &[&str] = &["mentions", "needs_action", "activity", "agent_activity"];
|
|
|
|
/// Get activity feed — query events mentioning our pubkey (via p-tag).
|
|
pub async fn cmd_get_feed(
|
|
client: &BuzzClient,
|
|
since: Option<i64>,
|
|
limit: Option<u32>,
|
|
types: Option<&str>,
|
|
format: &crate::OutputFormat,
|
|
) -> Result<(), CliError> {
|
|
let my_pk = client.keys().public_key().to_hex();
|
|
let limit = limit.unwrap_or(20).min(50);
|
|
|
|
let mut filter = serde_json::json!({
|
|
"#p": [my_pk],
|
|
"limit": limit
|
|
});
|
|
|
|
if let Some(s) = since {
|
|
filter["since"] = serde_json::json!(s);
|
|
}
|
|
|
|
if let Some(types_str) = types {
|
|
let type_list: Vec<&str> = types_str.split(',').map(str::trim).collect();
|
|
for t in &type_list {
|
|
if !VALID_FEED_TYPES.contains(t) {
|
|
return Err(crate::error::CliError::Usage(format!(
|
|
"invalid feed type {t:?} — must be one of: {}",
|
|
VALID_FEED_TYPES.join(", ")
|
|
)));
|
|
}
|
|
}
|
|
filter["feed_types"] = serde_json::json!(type_list);
|
|
}
|
|
|
|
let resp = client.query(&filter).await?;
|
|
let mut events: Vec<serde_json::Value> = serde_json::from_str(&resp).unwrap_or_default();
|
|
events.sort_by_key(|e| Reverse(e.get("created_at").and_then(|v| v.as_u64()).unwrap_or(0)));
|
|
let normalized = normalize_events(&events);
|
|
let output = match format {
|
|
crate::OutputFormat::Compact => {
|
|
let evts: Vec<serde_json::Value> =
|
|
serde_json::from_str(&normalized).unwrap_or_default();
|
|
let compact: Vec<serde_json::Value> = evts
|
|
.iter()
|
|
.map(|e| {
|
|
serde_json::json!({
|
|
"id": e.get("id").cloned().unwrap_or_default(),
|
|
"content": e.get("content").cloned().unwrap_or_default(),
|
|
"created_at": e.get("created_at").cloned().unwrap_or_default(),
|
|
})
|
|
})
|
|
.collect();
|
|
serde_json::to_string(&compact).unwrap_or_default()
|
|
}
|
|
crate::OutputFormat::Json => normalized,
|
|
};
|
|
println!("{output}");
|
|
Ok(())
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Dispatch
|
|
// ---------------------------------------------------------------------------
|
|
|
|
pub async fn dispatch(
|
|
cmd: crate::FeedCmd,
|
|
client: &BuzzClient,
|
|
format: &crate::OutputFormat,
|
|
) -> Result<(), CliError> {
|
|
use crate::FeedCmd;
|
|
match cmd {
|
|
FeedCmd::Get {
|
|
since,
|
|
limit,
|
|
types,
|
|
} => cmd_get_feed(client, since, limit, types.as_deref(), format).await,
|
|
}
|
|
}
|