bhcli

A TUI for chatting on LE PHP Chats
git clone https://git.dasho.dev/bhcli.git
Log | Files | Refs | README

notes.rs (5931B)


      1 //! Note management commands for BHCLI
      2 //!
      3 //! Provides functionality to view and edit notes in le-chat-php systems.
      4 //! Supports personal, public, staff, and admin notes based on user permissions.
      5 
      6 use crate::chatops::{ChatCommand, ChatOpError, ChatOpResult, CommandContext, UserRole};
      7 
      8 /// Command for viewing notes
      9 pub struct ViewNotesCommand;
     10 
     11 impl ChatCommand for ViewNotesCommand {
     12    fn name(&self) -> &'static str {
     13        "viewnotes"
     14    }
     15 
     16    fn description(&self) -> &'static str {
     17        "View notes (personal, public, staff, admin)"
     18    }
     19 
     20    fn usage(&self) -> &'static str {
     21        "/viewnotes [personal|public|staff|admin|viewpublic]"
     22    }
     23 
     24    fn execute(
     25        &self,
     26        args: Vec<String>,
     27        context: &CommandContext,
     28    ) -> Result<ChatOpResult, ChatOpError> {
     29        let note_type = args.get(0).map(|s| s.as_str()).unwrap_or("personal");
     30 
     31        // Validate permissions
     32        match note_type {
     33            "personal" | "" => {
     34                if context.role == UserRole::Guest || context.role == UserRole::Member {
     35                    return Err(ChatOpError::PermissionDenied("Insufficient privileges for personal notes".to_string()));
     36                }
     37            }
     38            "public" => {
     39                if context.role == UserRole::Guest || context.role == UserRole::Member {
     40                    return Err(ChatOpError::PermissionDenied("Insufficient privileges for public notes".to_string()));
     41                }
     42            }
     43            "staff" => {
     44                if context.role != UserRole::Staff && context.role != UserRole::Admin {
     45                    return Err(ChatOpError::PermissionDenied("Staff or Admin role required for staff notes".to_string()));
     46                }
     47            }
     48            "admin" => {
     49                if context.role != UserRole::Admin {
     50                    return Err(ChatOpError::PermissionDenied("Admin role required for admin notes".to_string()));
     51                }
     52            }
     53            "viewpublic" => {} // No special permissions needed
     54            _ => {
     55                return Err(ChatOpError::InvalidSyntax(
     56                    "Invalid note type. Use: personal, public, staff, admin, or viewpublic".to_string(),
     57                ))
     58            }
     59        }
     60 
     61        // For now, return a message indicating that the server integration is needed
     62        Ok(ChatOpResult::Message(format!(
     63            "Note viewing feature requires server integration.\nType: {}\nRequested by: {}\n\nThis feature will use the active chat session to fetch notes from the server.",
     64            note_type,
     65            context.username
     66        )))
     67    }
     68 
     69    fn required_role(&self) -> UserRole {
     70        UserRole::Member
     71    }
     72 }
     73 
     74 /// Command for editing notes
     75 pub struct EditNotesCommand;
     76 
     77 impl ChatCommand for EditNotesCommand {
     78    fn name(&self) -> &'static str {
     79        "editnotes"
     80    }
     81 
     82    fn description(&self) -> &'static str {
     83        "Edit notes (personal, public, staff, admin)"
     84    }
     85 
     86    fn usage(&self) -> &'static str {
     87        "/editnotes [personal|public|staff|admin] <text>"
     88    }
     89 
     90    fn execute(
     91        &self,
     92        args: Vec<String>,
     93        context: &CommandContext,
     94    ) -> Result<ChatOpResult, ChatOpError> {
     95        if args.is_empty() {
     96            return Err(ChatOpError::MissingArguments(
     97                "Usage: /editnotes [type] <text>".to_string(),
     98            ));
     99        }
    100 
    101        let (note_type, text) = if args.len() == 1 {
    102            // Default to personal notes if only text is provided
    103            ("personal".to_string(), args[0].clone())
    104        } else {
    105            let note_type = args[0].as_str();
    106            let text = args[1..].join(" ");
    107            
    108            match note_type {
    109                "personal" => {
    110                    if context.role == UserRole::Guest || context.role == UserRole::Member {
    111                        return Err(ChatOpError::PermissionDenied("Insufficient privileges for personal notes".to_string()));
    112                    }
    113                    ("personal".to_string(), text)
    114                }
    115                "public" => {
    116                    if context.role == UserRole::Guest || context.role == UserRole::Member {
    117                        return Err(ChatOpError::PermissionDenied("Insufficient privileges for public notes".to_string()));
    118                    }
    119                    ("public".to_string(), text)
    120                }
    121                "staff" => {
    122                    if context.role != UserRole::Staff && context.role != UserRole::Admin {
    123                        return Err(ChatOpError::PermissionDenied("Staff or Admin role required for staff notes".to_string()));
    124                    }
    125                    ("staff".to_string(), text)
    126                }
    127                "admin" => {
    128                    if context.role != UserRole::Admin {
    129                        return Err(ChatOpError::PermissionDenied("Admin role required for admin notes".to_string()));
    130                    }
    131                    ("admin".to_string(), text)
    132                }
    133                _ => {
    134                    // Treat first arg as part of text for personal notes
    135                    if context.role == UserRole::Guest || context.role == UserRole::Member {
    136                        return Err(ChatOpError::PermissionDenied("Insufficient privileges for personal notes".to_string()));
    137                    }
    138                    ("personal".to_string(), args.join(" "))
    139                }
    140            }
    141        };
    142 
    143        // For now, return a message indicating that the server integration is needed
    144        Ok(ChatOpResult::Message(format!(
    145            "Note editing feature requires server integration.\nType: {}\nText: {}\nRequested by: {}\n\nThis feature will use the active chat session to save notes to the server.",
    146            note_type,
    147            text,
    148            context.username
    149        )))
    150    }
    151 
    152    fn required_role(&self) -> UserRole {
    153        UserRole::Member
    154    }
    155 }