use std::collections::BTreeSet;
use std::str::FromStr;
use crate::{GroupId, Secret};
use nostr::{EventId, PublicKey, RelayUrl, Timestamp, UnsignedEvent};
use serde::{Deserialize, Serialize};
use super::error::WelcomeError;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ProcessedWelcome {
pub wrapper_event_id: EventId,
pub welcome_event_id: Option<EventId>,
pub processed_at: Timestamp,
pub state: ProcessedWelcomeState,
pub failure_reason: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Welcome {
pub id: EventId,
pub event: UnsignedEvent,
pub mls_group_id: GroupId,
pub nostr_group_id: [u8; 32],
pub group_name: String,
pub group_description: String,
pub group_image_hash: Option<[u8; 32]>,
pub group_image_key: Option<Secret<[u8; 32]>>,
pub group_image_nonce: Option<Secret<[u8; 12]>>,
pub group_admin_pubkeys: BTreeSet<PublicKey>,
pub group_relays: BTreeSet<RelayUrl>,
pub welcomer: PublicKey,
pub member_count: u32,
pub state: WelcomeState,
pub wrapper_event_id: EventId,
}
string_enum! {
pub enum ProcessedWelcomeState => WelcomeError, "Invalid processed welcome state: {}" {
/// The welcome was successfully processed and stored in the database
Processed => "processed",
/// The welcome failed to be processed and stored in the database
Failed => "failed",
}
}
string_enum! {
/// The state of a welcome
pub enum WelcomeState => WelcomeError, "Invalid welcome state: {}" {
/// The welcome is pending
Pending => "pending",
/// The welcome was accepted
Accepted => "accepted",
/// The welcome was declined
Declined => "declined",
/// The welcome was ignored
Ignored => "ignored",
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
#[test]
fn test_processed_welcome_serialization() {
// Create a processed welcome to test serialization
let processed_welcome = ProcessedWelcome {
wrapper_event_id: EventId::all_zeros(), // Using all_zeros for testing
welcome_event_id: None,
processed_at: Timestamp::now(),
state: ProcessedWelcomeState::Processed,
failure_reason: None,
};
let serialized = serde_json::to_value(&processed_welcome).unwrap();
assert_eq!(serialized["state"], json!("processed"));
assert_eq!(serialized["failure_reason"], json!(null));
}
}