communitas 0.2.6

A diagnostic chat application for the P2P Foundation network
Documentation
//! Message types and structures

use crate::Identity;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Unique message identifier
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct MessageId(pub String);

/// Chat message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    /// Unique identifier
    pub id: MessageId,
    /// Sender identity
    pub sender: Identity,
    /// Message content
    pub content: MessageContent,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
    /// Digital signatures
    pub signatures: Vec<Vec<u8>>,
}

/// Message content types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MessageContent {
    /// Text message
    Text(String),
    /// File attachment
    File(FileMetadata),
    /// Voice call invitation
    VoiceCall(CallInfo),
    /// Video call invitation
    VideoCall(CallInfo),
}

/// File metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileMetadata {
    /// File name
    pub name: String,
    /// File size in bytes
    pub size: u64,
    /// BLAKE3 hash
    pub hash: String,
    /// MIME type
    pub mime_type: String,
}

/// Call information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallInfo {
    /// Call ID
    pub call_id: String,
    /// SDP offer/answer
    pub sdp: String,
}

impl Message {
    /// Create a new message
    pub fn new(sender: Identity, content: MessageContent) -> Self {
        Self {
            id: MessageId(format!("msg-{}", chrono::Utc::now().timestamp())),
            sender,
            content,
            timestamp: Utc::now(),
            signatures: Vec::new(),
        }
    }
}