event.rs (1563B)
1 use anyhow::{Context, Result}; 2 use serde::{Deserialize, Serialize}; 3 4 use crate::{ 5 domain::indexer::kind::{IndexerEventKind, IndexerEventKindParseError}, 6 RelayEventRecord, 7 }; 8 9 #[derive(Debug, Clone, Deserialize, Serialize)] 10 pub struct RelayRawEvent { 11 pub id: String, 12 pub pubkey: String, 13 pub created_at: u32, 14 pub kind: u32, 15 pub tags: Vec<Vec<String>>, 16 pub content: String, 17 pub sig: String, 18 } 19 #[derive(Debug, Clone, Serialize, Deserialize)] 20 pub struct RelayIndexerEvent { 21 pub id: String, 22 pub author: String, 23 pub created_at: u32, 24 pub pubkey: String, 25 pub kind: IndexerEventKind, 26 pub tags: Vec<Vec<String>>, 27 pub content: String, 28 pub hash: String, 29 pub sig: String, 30 } 31 32 impl TryFrom<RelayEventRecord> for RelayIndexerEvent { 33 type Error = anyhow::Error; 34 35 fn try_from(rec: RelayEventRecord) -> Result<Self> { 36 let raw: RelayRawEvent = serde_json::from_str(&rec.content) 37 .with_context(|| format!("Failed to parse relay JSON for event {}", rec.event_hash))?; 38 39 let kind = IndexerEventKind::try_from(raw.kind as u64) 40 .map_err(|e: IndexerEventKindParseError| anyhow::anyhow!(e))?; 41 42 Ok(RelayIndexerEvent { 43 id: raw.id.to_lowercase(), 44 author: rec.author.to_lowercase(), 45 created_at: raw.created_at, 46 pubkey: raw.pubkey.to_lowercase(), 47 kind, 48 tags: raw.tags, 49 content: raw.content, 50 hash: rec.event_hash, 51 sig: raw.sig.to_lowercase(), 52 }) 53 } 54 }