nexus-tui-common 0.1.0

Common types and utilities for the Nexus TUI client and server.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// common/src/lib.rs

use serde::{Deserialize, Serialize};
use uuid::Uuid;

pub mod config;
pub use config::{ServerConfig, ClientConfig};

// Simple color representation that works for both client and server
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct UserColor(pub String);

impl UserColor {
    pub fn new(color: impl Into<String>) -> Self {
        Self(color.into())
    }
    
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<String> for UserColor {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for UserColor {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

// For client compatibility with ratatui
#[cfg(feature = "ratatui")]
impl From<ratatui::style::Color> for UserColor {
    fn from(color: ratatui::style::Color) -> Self {
        match color {
            ratatui::style::Color::Rgb(r, g, b) => Self(format!("#{:02X}{:02X}{:02X}", r, g, b)),
            ratatui::style::Color::Red => Self("Red".to_string()),
            ratatui::style::Color::Green => Self("Green".to_string()),
            ratatui::style::Color::Blue => Self("Blue".to_string()),
            ratatui::style::Color::Yellow => Self("Yellow".to_string()),
            ratatui::style::Color::Cyan => Self("Cyan".to_string()),
            ratatui::style::Color::Magenta => Self("Magenta".to_string()),
            ratatui::style::Color::White => Self("White".to_string()),
            ratatui::style::Color::Black => Self("Black".to_string()),
            _ => Self("Cyan".to_string()), // Default
        }
    }
}

#[cfg(feature = "ratatui")]
impl From<UserColor> for ratatui::style::Color {
    fn from(user_color: UserColor) -> ratatui::style::Color {
        if user_color.0.starts_with('#') && user_color.0.len() == 7 {
            if let (Ok(r), Ok(g), Ok(b)) = (
                u8::from_str_radix(&user_color.0[1..3], 16),
                u8::from_str_radix(&user_color.0[3..5], 16),
                u8::from_str_radix(&user_color.0[5..7], 16),
            ) {
                return ratatui::style::Color::Rgb(r, g, b);
            }
        }
        
        match user_color.0.as_str() {
            "Red" => ratatui::style::Color::Red,
            "Green" => ratatui::style::Color::Green,
            "Blue" => ratatui::style::Color::Blue,
            "Yellow" => ratatui::style::Color::Yellow,
            "Cyan" => ratatui::style::Color::Cyan,
            "Magenta" => ratatui::style::Color::Magenta,
            "White" => ratatui::style::Color::White,
            "Black" => ratatui::style::Color::Black,
            _ => ratatui::style::Color::Cyan, // Default
        }
    }
}

// --- User & Role Management ---

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum UserRole {
    User,
    Moderator,
    Admin,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum UserStatus {
    Connected,
    Away,
    Busy,
    Offline,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct User {
    pub id: Uuid,
    pub username: String,
    pub color: UserColor,
    pub role: UserRole,
    pub profile_pic: Option<String>,
    pub cover_banner: Option<String>,
    pub status: UserStatus,
}

/// Complete user profile with all fields (for profile editing/viewing)
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserProfile {
    pub id: Uuid,
    pub username: String,
    pub hash: String, // Password hash (server-side only)
    pub color: UserColor,
    pub role: UserRole,
    pub bio: Option<String>,
    pub url1: Option<String>,
    pub url2: Option<String>,
    pub url3: Option<String>,
    pub location: Option<String>,
    pub profile_pic: Option<String>,
    pub cover_banner: Option<String>,
}

/// Lightweight user info without profile images - use this for lists, mentions, etc.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserInfo {
    pub id: Uuid,
    pub username: String,
    pub color: UserColor,
    pub role: UserRole,
    pub status: UserStatus,
}

impl From<User> for UserInfo {
    fn from(user: User) -> Self {
        Self {
            id: user.id,
            username: user.username,
            color: user.color,
            role: user.role,
            status: user.status,
        }
    }
}

impl From<&User> for UserInfo {
    fn from(user: &User) -> Self {
        Self {
            id: user.id,
            username: user.username.clone(),
            color: user.color.clone(),
            role: user.role,
            status: user.status, // Now UserStatus implements Copy, so no clone needed
        }
    }
}

// --- Data Structures ---

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Forum {
    pub id: Uuid,
    pub name: String,
    pub description: String,
    pub threads: Vec<Thread>,
}

/// Lightweight forum structure for initial loading (no profile images)
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ForumLightweight {
    pub id: Uuid,
    pub name: String,
    pub description: String,
    pub threads: Vec<ThreadLightweight>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Thread {
    pub id: Uuid,
    pub title: String,
    pub author: User,
    pub posts: Vec<Post>,
    pub timestamp: i64,
}

/// Lightweight thread structure for initial loading (no profile images)
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ThreadLightweight {
    pub id: Uuid,
    pub title: String,
    pub author: UserInfo,
    pub posts: Vec<PostLightweight>,
    pub timestamp: i64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Post {
    pub id: Uuid,
    pub author: User,
    pub content: String,
    pub timestamp: i64,
    pub reply_to: Option<Uuid>,
}

/// Lightweight post structure for initial loading (no profile images)
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PostLightweight {
    pub id: Uuid,
    pub author: UserInfo,
    pub content: String,
    pub timestamp: i64,
    pub reply_to: Option<Uuid>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ChatMessage {
    pub author: String,
    pub content: String,
    pub color: UserColor,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Server {
    pub id: Uuid,
    pub name: String,
    pub description: String,
    pub public: bool,
    pub invite_code: Option<String>,
    pub icon: Option<String>, // base64
    pub banner: Option<String>, // base64
    pub owner: Uuid,
    pub mods: Vec<Uuid>,
    pub userlist: Vec<Uuid>,
    pub channels: Vec<Channel>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Channel {
    pub id: Uuid,
    pub server_id: Uuid,
    pub name: String,
    pub description: String,
    pub permissions: ChannelPermissions,
    pub userlist: Vec<Uuid>,
    pub messages: Vec<ChannelMessage>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ChannelPermissions {
    pub can_read: Vec<Uuid>,
    pub can_write: Vec<Uuid>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ChannelMessage {
    pub id: Uuid,
    pub channel_id: Uuid,
    pub sent_by: Uuid, // This is the author ID - frontend will look up user info
    pub timestamp: i64,
    pub content: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DirectMessage {
    pub id: Uuid,
    pub from: Uuid, // This is the author ID - frontend will look up user info
    pub to: Uuid,
    pub timestamp: i64,
    pub content: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum NotificationType {
    ThreadReply,
    DM,
    Announcement,
    Mention,
    Other(String),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Notification {
    pub id: Uuid,
    pub user_id: Uuid,
    pub notif_type: NotificationType,
    pub related_id: Uuid,
    pub created_at: i64,
    pub read: bool,
    pub extra: Option<String>,
}

// --- Server Invites ---

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ServerInvite {
    pub id: Uuid,
    pub from_user: User,
    pub to_user_id: Uuid,
    pub server: Server,
    pub timestamp: i64,
    pub status: ServerInviteStatus,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum ServerInviteStatus {
    Pending,
    Accepted,
    Declined,
    Expired,
}


// --- Network Protocol Definitions ---

#[derive(Serialize, Deserialize, Debug)]
pub enum ClientMessage {
    // Auth
    Register { username: String, password: String },
    Login { username: String, password: String },
    Logout,
    // User
    UpdatePassword(String),
    UpdateColor(UserColor), // Changed from SerializableColor to UserColor
    UpdateProfile {
        bio: Option<String>,
        url1: Option<String>,
        url2: Option<String>,
        url3: Option<String>,
        location: Option<String>,
        profile_pic: Option<String>,
        cover_banner: Option<String>,
    },
    // Forums
    GetForums,
    CreateForum { name: String, description: String },
    DeleteForum { forum_id: Uuid },
    CreateThread { forum_id: Uuid, title: String, content: String },
    CreatePost { thread_id: Uuid, content: String },
    CreatePostReply { thread_id: Uuid, content: String, reply_to: Uuid }, // New: Reply to specific post
    // Chat
    SendDirectMessage { to: Uuid, content: String },
    SendChannelMessage { channel_id: Uuid, content: String },
    // Server invites
    SendServerInvite { to_user_id: Uuid, server_id: Uuid },
    RespondToServerInvite { invite_id: Uuid, accept: bool },
    // New: Accept/decline invite from a specific user (for DM commands)
    AcceptServerInviteFromUser { from_user_id: Uuid },
    DeclineServerInviteFromUser { from_user_id: Uuid },
    // Moderation
    DeletePost(Uuid),
    DeleteThread(Uuid),
    // User management
    GetUserList, // Request the list of connected users
    GetProfile { user_id: Uuid },
    GetServers, // Request all servers the user is a member of
    // --- ENHANCED PAGINATION SUPPORT ---
    GetChannelMessagesPaginated { 
        channel_id: Uuid, 
        cursor: PaginationCursor,
        limit: Option<usize>,
        direction: PaginationDirection,
    },
    GetDirectMessagesPaginated { 
        user_id: Uuid, 
        cursor: PaginationCursor,
        limit: Option<usize>,
        direction: PaginationDirection,
    },
    // --- LEGACY COMPATIBILITY ---
    GetChannelMessages { channel_id: Uuid, before: Option<i64> },
    GetChannelUserList { channel_id: Uuid },
    GetDMUserList, // Request list of users you have DMs with
    GetDirectMessages { user_id: Uuid, before: Option<i64> }, // Fetch DMs with a user, paginated by timestamp
    // --- NOTIFICATIONS ---
    GetNotifications { before: Option<i64> },
    MarkNotificationRead { notification_id: Uuid },
    // --- CACHE MANAGEMENT ---
    InvalidateImageCache { keys: Vec<String> },
    GetCacheStats,
    // Profile picture requests (for efficient loading)
    GetUserAvatars { user_ids: Vec<Uuid> },
}

/// Pagination cursor for network protocol
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum PaginationCursor {
    Timestamp(i64),
    Offset(usize),
    Start,
}

/// Pagination direction for network protocol
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum PaginationDirection {
    Forward,
    Backward,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ServerMessage {
    // Auth
    AuthSuccess(User),
    AuthFailure(String),
    // General
    Forums(Vec<Forum>),
    ForumsLightweight(Vec<ForumLightweight>), // Lightweight version without profile images
    NewChatMessage(ChatMessage),
    DirectMessage(DirectMessage),
    MentionNotification { from: User, content: String },
    ForumReplyNotification { thread_id: Uuid, from_username: String, message: String, from_user_profile_pic: Option<String> },
    Notification(String, bool), // Message, is_error
    // Server invites
    ServerInviteReceived(ServerInvite),
    ServerInviteResponse { invite_id: Uuid, accepted: bool, user: User },
    // User management
    UserList(Vec<User>), // List of connected users
    UserJoined(User),    // A user joined
    UserLeft(Uuid),      // A user left (by id)
    Profile(UserProfile),
    UserUpdated(User), // Broadcast when a user updates their profile
    Servers(Vec<Server>), // List of servers and their channels
    NewChannelMessage(ChannelMessage),
    // --- ENHANCED PAGINATION RESPONSES ---
    ChannelMessagesPaginated { 
        channel_id: Uuid, 
        messages: Vec<ChannelMessage>, 
        has_more: bool,
        next_cursor: Option<PaginationCursor>,
        prev_cursor: Option<PaginationCursor>,
        total_count: Option<usize>,
    },
    DirectMessagesPaginated { 
        user_id: Uuid, 
        messages: Vec<DirectMessage>, 
        has_more: bool,
        next_cursor: Option<PaginationCursor>,
        prev_cursor: Option<PaginationCursor>,
        total_count: Option<usize>,
    },
    // --- LEGACY COMPATIBILITY ---
    ChannelMessages { channel_id: Uuid, messages: Vec<ChannelMessage>, history_complete: bool },
    ChannelUserList { channel_id: Uuid, users: Vec<User> },
    DMUserList(Vec<User>), // List of users you have DMs with
    DirectMessages { user_id: Uuid, messages: Vec<DirectMessage>, history_complete: bool },
    // --- NOTIFICATIONS ---
    Notifications { notifications: Vec<Notification>, history_complete: bool },
    // --- CACHE MANAGEMENT ---
    CacheStats { 
        total_entries: usize, 
        total_size_mb: f64, 
        hit_ratio: f64,
        expired_entries: usize,
    },
    ImageCacheInvalidated { keys: Vec<String> },
    UserAvatars { avatars: Vec<(Uuid, Option<String>)> }, // user_id, profile_pic
    // --- PERFORMANCE METRICS ---
    PerformanceMetrics {
        query_time_ms: u64,
        cache_hit_rate: f64,
        message_count: usize,
    },
}


// Initial data creation
pub fn create_initial_forums() -> Vec<Forum> {
    let system_user = User {
        id: Uuid::new_v4(),
        username: "system".to_string(),
        color: UserColor::new("Red"),
        role: UserRole::Admin,
        profile_pic: Some("system.png".to_string()),
        cover_banner: Some("system_banner.png".to_string()),
        status: UserStatus::Connected, // Default to connected
    };
    vec![
        Forum {
            id: Uuid::new_v4(),
            name: "Decompiling Corporate ICE".to_string(),
            description: "Tips and tricks for getting past the big boys' security.".to_string(),
            threads: vec![
                Thread {
                    id: Uuid::new_v4(),
                    title: "Militech's 'Aegis' Firewall - Any exploits?".to_string(),
                    author: system_user.clone(),
                    timestamp: 1633072800,
                    posts: vec![ Post {
                        id: Uuid::new_v4(),
                        author: system_user.clone(),
                        content: "I've been probing their new Aegis system. It's tough.".to_string(),
                        timestamp: 1633072800,
                        reply_to: None, // No parent post
                    }],
                },
            ],
        },
    ]
}