commit 184a472cd2c4532c86f63acdac17779458d8796e
parent 29c84cbaf982645f154a40a0592c241e5a729d10
Author: triesap <triesap@radroots.dev>
Date: Sun, 4 Jan 2026 03:39:15 +0000
events: flatten listing event response
- Replace flattened event view with explicit row fields
- Add helper to parse listing from tags and content
- Sort listings by created_at on flattened row
- Update tests to assert against flattened row fields
Diffstat:
1 file changed, 31 insertions(+), 15 deletions(-)
diff --git a/src/api/jsonrpc/methods/events/listing/list.rs b/src/api/jsonrpc/methods/events/listing/list.rs
@@ -3,7 +3,7 @@ use jsonrpsee::server::RpcModule;
use serde::Serialize;
use std::time::Duration;
-use crate::api::jsonrpc::nostr::{event_tags, event_view_with_tags, NostrEventView};
+use crate::api::jsonrpc::nostr::{event_tags, event_view_with_tags};
use crate::api::jsonrpc::params::{
apply_time_bounds,
limit_or,
@@ -22,18 +22,23 @@ use radroots_nostr::prelude::{
use radroots_trade::listing::codec::listing_from_event_parts;
#[derive(Clone, Debug, Serialize)]
-struct ListingEventFlat {
- #[serde(flatten)]
- event: NostrEventView,
+struct ListingRow {
+ id: String,
+ author: String,
+ created_at: u64,
+ kind: u32,
+ tags: Vec<Vec<String>>,
+ content: String,
+ sig: String,
listing: Option<RadrootsListing>,
}
#[derive(Clone, Debug, Serialize)]
struct ListingListResponse {
- listings: Vec<ListingEventFlat>,
+ listings: Vec<ListingRow>,
}
-fn build_listing_rows<I>(events: I) -> Vec<ListingEventFlat>
+fn build_listing_rows<I>(events: I) -> Vec<ListingRow>
where
I: IntoIterator<Item = RadrootsNostrEvent>,
{
@@ -41,17 +46,28 @@ where
.into_iter()
.map(|ev| {
let tags = event_tags(&ev);
- let listing = listing_from_event_parts(&tags, &ev.content).ok();
- ListingEventFlat {
- event: event_view_with_tags(&ev, tags),
+ let listing = parse_listing_event(&ev, &tags);
+ let event = event_view_with_tags(&ev, tags);
+ ListingRow {
+ id: event.id,
+ author: event.author,
+ created_at: event.created_at,
+ kind: event.kind,
+ tags: event.tags,
+ content: event.content,
+ sig: event.sig,
listing,
}
})
.collect::<Vec<_>>();
- items.sort_by(|a, b| b.event.created_at.cmp(&a.event.created_at));
+ items.sort_by(|a, b| b.created_at.cmp(&a.created_at));
items
}
+fn parse_listing_event(event: &RadrootsNostrEvent, tags: &[Vec<String>]) -> Option<RadrootsListing> {
+ listing_from_event_parts(tags, &event.content).ok()
+}
+
pub fn register(m: &mut RpcModule<RpcContext>, registry: &MethodRegistry) -> Result<()> {
registry.track("events.listing.list");
m.register_async_method("events.listing.list", |params, ctx, _| async move {
@@ -196,10 +212,10 @@ mod tests {
let listings = build_listing_rows(vec![older, newer]);
assert_eq!(listings.len(), 2);
- assert_eq!(listings[0].event.id, new_id);
- assert_eq!(listings[0].event.created_at, 200);
- assert_eq!(listings[1].event.id, old_id);
- assert_eq!(listings[1].event.created_at, 100);
+ assert_eq!(listings[0].id, new_id);
+ assert_eq!(listings[0].created_at, 200);
+ assert_eq!(listings[1].id, old_id);
+ assert_eq!(listings[1].created_at, 100);
}
#[test]
@@ -213,7 +229,7 @@ mod tests {
let listings = build_listing_rows(vec![event]);
assert_eq!(listings.len(), 1);
- assert_eq!(listings[0].event.tags, tags);
+ assert_eq!(listings[0].tags, tags);
let parsed = listings[0].listing.as_ref().expect("listing");
assert_eq!(parsed.d_tag, "listing-1");
assert_eq!(parsed.farm.pubkey, pubkey);