mirror of
https://github.com/block/buzz.git
synced 2026-11-01 15:01:12 +00:00
Signed-off-by: Tyler Longwell <tlongwell@block.xyz> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Max <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Mari <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Quinn <96f056ad5f2305c8ddf637dc65d048aa4c12d7daeb8867690e34fca46b0ef64c@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Sami <f4a42a97e594b77bdbd8ee35191c8b28a94a4cb871d96f32921558275421fb68@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Perci <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
81 lines
2.6 KiB
Rust
81 lines
2.6 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(())
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|