p2p_chat/
types.rs

1//! This module defines common data structures and types used throughout the p2p-chat application.
2use libp2p::PeerId;
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6/// Represents the delivery status of a message.
7#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
8pub enum DeliveryStatus {
9    /// The message is currently being sent.
10    Sending,
11    /// The message has been sent (e.g., to the network or outbox).
12    Sent,
13    /// The message has been delivered to the recipient or a mailbox.
14    Delivered,
15    /// The message has been read by the recipient (future feature).
16    Read,
17}
18
19impl Default for DeliveryStatus {
20    /// Returns the default delivery status, which is `Sending`.
21    fn default() -> Self {
22        DeliveryStatus::Sending
23    }
24}
25
26/// Represents a chat message.
27#[derive(Serialize, Deserialize, Clone, Debug)]
28pub struct Message {
29    /// The unique identifier for the message.
30    pub id: Uuid,
31    /// The Peer ID of the sender.
32    pub sender: PeerId,
33    /// The Peer ID of the recipient.
34    pub recipient: PeerId,
35    /// The timestamp when the message was created (milliseconds since epoch).
36    pub timestamp: i64,
37    /// The encrypted content of the message.
38    pub content: Vec<u8>,
39    /// A random nonce used for ordering or cryptographic purposes.
40    pub nonce: u64,
41    /// The current delivery status of the message.
42    #[serde(default)]
43    pub delivery_status: DeliveryStatus,
44}
45
46/// Represents a friend in the application.
47#[derive(Serialize, Deserialize, Clone, Debug)]
48pub struct Friend {
49    /// The Peer ID of the friend.
50    pub peer_id: PeerId,
51    /// The E2E public key of the friend.
52    pub e2e_public_key: Vec<u8>,
53    /// An optional nickname for the friend.
54    pub nickname: Option<String>,
55}
56
57/// Represents an encrypted message stored in a mailbox.
58#[derive(Serialize, Deserialize, Clone, Debug)]
59pub struct EncryptedMessage {
60    /// The unique identifier for the message.
61    pub id: Uuid,
62    /// The Peer ID of the original sender.
63    pub sender: PeerId,
64    /// The cryptographic hash of the recipient's public key, used for mailbox lookup.
65    pub recipient_hash: [u8; 32],
66    /// The encrypted content of the message.
67    pub encrypted_content: Vec<u8>,
68    /// The timestamp when the message was created (milliseconds since epoch).
69    pub timestamp: i64,
70    /// A random nonce used for ordering or cryptographic purposes.
71    #[serde(default)]
72    pub nonce: u64,
73    /// The sender's E2E public key.
74    pub sender_pub_key: Vec<u8>,
75}
76
77/// Represents a delivery confirmation for a message.
78#[derive(Serialize, Deserialize, Clone, Debug)]
79pub struct DeliveryConfirmation {
80    /// The ID of the original message being confirmed.
81    pub original_message_id: Uuid,
82    /// The timestamp when the confirmation was generated.
83    pub timestamp: i64,
84}
85
86/// Represents a read receipt for a message.
87#[derive(Serialize, Deserialize, Clone, Debug)]
88pub struct ReadReceipt {
89    /// The ID of the message that was read.
90    pub message_id: Uuid,
91    /// The timestamp when the message was read.
92    pub timestamp: i64,
93}
94
95/// Represents a request in the chat protocol.
96#[derive(Serialize, Deserialize, Clone, Debug)]
97pub enum ChatRequest {
98    /// Request to send a chat message.
99    SendMessage {
100        /// The message to send.
101        message: Message,
102    },
103    /// Request to send a delivery confirmation.
104    DeliveryConfirmation {
105        /// The delivery confirmation details.
106        confirmation: DeliveryConfirmation,
107    },
108    /// Request to send a read receipt.
109    ReadReceipt {
110        /// The read receipt details.
111        receipt: ReadReceipt,
112    },
113}
114
115/// Represents a response in the chat protocol.
116#[derive(Serialize, Deserialize, Clone, Debug)]
117pub enum ChatResponse {
118    /// Response to a message sending request.
119    MessageResult {
120        /// Whether the message operation was successful.
121        success: bool,
122        /// The ID of the message, if successful.
123        message_id: Option<Uuid>,
124    },
125}
126
127/// Represents a request to a mailbox node.
128#[derive(Serialize, Deserialize, Clone, Debug)]
129pub enum MailboxRequest {
130    /// Request to put an encrypted message into the mailbox.
131    Put {
132        /// The cryptographic hash of the recipient's public key.
133        recipient: [u8; 32],
134        /// The encrypted message to store.
135        message: EncryptedMessage,
136    },
137    /// Request to fetch encrypted messages for a recipient.
138    Fetch {
139        /// The cryptographic hash of the recipient's public key.
140        recipient: [u8; 32],
141        /// The maximum number of messages to fetch.
142        limit: usize,
143    },
144    /// Request to acknowledge and delete messages from the mailbox.
145    Ack {
146        /// The cryptographic hash of the recipient's public key.
147        recipient: [u8; 32],
148        /// The IDs of the messages to acknowledge and delete.
149        msg_ids: Vec<Uuid>,
150    },
151}
152
153/// Represents a response from a mailbox node.
154#[derive(Serialize, Deserialize, Clone, Debug)]
155pub enum MailboxResponse {
156    /// Response to a `Put` request.
157    PutResult {
158        /// Whether the put operation was successful.
159        success: bool,
160    },
161    /// Response containing fetched messages.
162    Messages {
163        /// A vector of encrypted messages.
164        items: Vec<EncryptedMessage>,
165    },
166    /// Response to an `Ack` request.
167    AckResult {
168        /// The number of messages successfully deleted.
169        deleted: usize,
170    },
171}