From 6b4557d7ad6c879e4aa0dd013a99b0a2ae39bda4 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 25 Jan 2023 10:36:50 +0100
Subject: [PATCH] LKT: Add a way to identifiate some commands.

---
 src/rust/amadeus-next/amalib/src/query.rs    | 62 +++++++++++---------
 src/rust/amadeus-next/amalib/src/response.rs |  6 ++
 src/rust/amadeus-next/lkt-rs/src/main.rs     | 18 ++++--
 3 files changed, 53 insertions(+), 33 deletions(-)

diff --git a/src/rust/amadeus-next/amalib/src/query.rs b/src/rust/amadeus-next/amalib/src/query.rs
index 5bccb15f..ad0bc165 100644
--- a/src/rust/amadeus-next/amalib/src/query.rs
+++ b/src/rust/amadeus-next/amalib/src/query.rs
@@ -23,6 +23,7 @@ pub enum LektorQuery {
     Populate,
     DryPopulate,
     ConnectAsUser(String, Box<LektorQuery>),
+    WithUserName(String, Box<LektorQuery>),
 
     CurrentKara,
     PlaybackStatus,
@@ -140,7 +141,9 @@ impl LektorQuery {
             PlaybackStatus => LektorPlaybackStatusResponse::from_formated,
             CurrentKara => LektorCurrentKaraResponse::from_formated,
 
-            ConnectAsUser(_, cmd) | Continuation(_, cmd) => cmd.get_response_type(),
+            WithUserName(_, cmd) | ConnectAsUser(_, cmd) | Continuation(_, cmd) => {
+                cmd.get_response_type()
+            }
 
             CountKaraDatabase(_) => LektorIntegerResponse::from_formated,
 
@@ -166,10 +169,10 @@ impl LektorQuery {
     /// Verify that a query is Ok.
     pub(crate) fn verify(&self) -> Result<(), String> {
         use LektorQuery::*;
-        macro_rules! admin_commands {
-            () => {
-                Populate | DryPopulate | DryUpdate | Update | KillServer | RestartServer | Close
-            };
+        #[rustfmt::skip]
+        macro_rules! commands {
+            (admin) => { Populate | DryPopulate | DryUpdate | Update | KillServer | RestartServer | Close };
+            (user)  => { CreatePlaylist(_) | DestroyPlaylist(_) | AddToPlaylist(_, _) | RemoveFromPlaylist(_, _) };
         }
         match self {
             // User commands
@@ -197,25 +200,31 @@ impl LektorQuery {
             | GetKaraInfo(_)
             | Ping => Ok(()),
 
-            CreatePlaylist(str)
-            | DestroyPlaylist(str)
-            | AddToPlaylist(str, _)
-            | RemoveFromPlaylist(str, _) => {
-                either!(!str.is_empty() => Ok(()); Err("need to pass a non-empty string as playlist name".to_string()))
-            }
-
-            // Should be admin commands
-            admin_commands!() => Err("admin command".to_string()),
+            // Should be admin or user commands
+            commands!(admin) => Err("admin command".to_string()),
+            commands!(user) => Err("user command".to_string()),
 
             // Admin commands
             ConnectAsUser(_, cmd) => match cmd.as_ref() {
-                admin_commands!() => Ok(()),
+                commands!(admin) => Ok(()),
                 _ => Err(format!("not an admin command: {cmd:?}")),
             },
 
+            // Commands that needs a user name, just for identification, not
+            // authentification
+            WithUserName(_, cmd) => match cmd.as_ref() {
+                commands!(user) => Ok(()),
+                _ => Err(format!("not a user command: {cmd:?}")),
+            },
+
             // Continuation commands
             Continuation(_, cmd) => match cmd.as_ref() {
-                ListAllPlaylists | SearchKaraDatabase(_) | ListPlaylist(_) => Ok(()),
+                SearchKaraQueue(_)
+                | SearchKaraPlaylist(_, _)
+                | ListAllPlaylists
+                | SearchKaraDatabase(_)
+                | GetKaraInfo(_)
+                | ListPlaylist(_) => Ok(()),
                 _ => Err(format!("not a continuable command: {cmd:?}")),
             },
         }
@@ -232,22 +241,16 @@ impl LektorQuery {
         }
         use LektorQuery::*;
         match self {
-            Populate => lkt_str!("rescan"),
-            DryPopulate => lkt_str!("__dry_rescan"),
             Update => lkt_str!("update"),
+            Populate => lkt_str!("rescan"),
             DryUpdate => lkt_str!("__dry_update"),
+            DryPopulate => lkt_str!("__dry_rescan"),
             Ping => lkt_str!("ping"),
             Close => lkt_str!("close"),
             KillServer => lkt_str!("kill"),
             RestartServer => lkt_str!("__restart"),
-            ConnectAsUser(password, cmd) => format!(
-                concat!(
-                    "command_list_ok_begin\n",
-                    "password {}\n",
-                    "{}",
-                    "command_list_end\n",
-                ),
-                password,
+            ConnectAsUser(password, cmd) => lkt_str!(
+                "command_list_ok_begin\npassword {password}\n{}command_list_end",
                 cmd.format_query()
             ),
 
@@ -282,6 +285,10 @@ impl LektorQuery {
             DestroyPlaylist(name) => lkt_str!("playlistdelete {name}"),
             AddToPlaylist(name, uri) => lkt_str!("playlistadd {name} {uri}"),
             RemoveFromPlaylist(name, uri) => lkt_str!("playlistdelete {name} {uri}"),
+            WithUserName(name, cmd) => lkt_str!(
+                "command_list_ok_begin\nuser {name}\n{}command_list_end",
+                cmd.format_query()
+            ),
 
             ListQueue(Range { start, end }) => lkt_str!("playlist {start}:{end}"),
             Continuation(start, query) if matches!(&**query, ListQueue(_)) => {
@@ -290,8 +297,7 @@ impl LektorQuery {
             }
 
             Continuation(cont, query) => {
-                let query = query.format_query();
-                lkt_str!("{cont} {query}")
+                lkt_str!("{cont} {}", query.format_query())
             }
         }
     }
diff --git a/src/rust/amadeus-next/amalib/src/response.rs b/src/rust/amadeus-next/amalib/src/response.rs
index e7325716..460cd0ce 100644
--- a/src/rust/amadeus-next/amalib/src/response.rs
+++ b/src/rust/amadeus-next/amalib/src/response.rs
@@ -232,6 +232,12 @@ impl LektorIntegerResponse {
     }
 }
 
+impl LektorKaraInfoResponse {
+    pub fn into_inner(self) -> Vec<(String, String)> {
+        self.infos
+    }
+}
+
 impl LektorKaraSetResponse {
     pub fn iter(&self) -> &[String] {
         &self.karas[..]
diff --git a/src/rust/amadeus-next/lkt-rs/src/main.rs b/src/rust/amadeus-next/lkt-rs/src/main.rs
index df0b55c5..0644a1ef 100644
--- a/src/rust/amadeus-next/lkt-rs/src/main.rs
+++ b/src/rust/amadeus-next/lkt-rs/src/main.rs
@@ -179,20 +179,28 @@ async fn handle_cmd_search(_: LktConfig, mut conn: LektorConnexion, cmd: LktSear
     }
 }
 
-async fn handle_cmd_playlist(_: LktConfig, mut conn: LektorConnexion, cmd: LktPlaylistCommand) {
+async fn handle_cmd_playlist(
+    config: LktConfig,
+    mut conn: LektorConnexion,
+    cmd: LktPlaylistCommand,
+) {
+    let user = config.user.user;
     match cmd {
+        // Do actions on playlists
         LktPlaylistCommand::Create { name } => {
-            send!(conn => LektorQuery::CreatePlaylist(name); ok)
+            send!(conn => LektorQuery::WithUserName(user, Box::new(LektorQuery::CreatePlaylist(name))); ok)
         }
         LktPlaylistCommand::Destroy { name } => {
-            send!(conn => LektorQuery::DestroyPlaylist(name); ok)
+            send!(conn => LektorQuery::WithUserName(user, Box::new(LektorQuery::DestroyPlaylist(name))); ok)
         }
         LktPlaylistCommand::Add { name, query } => {
-            send!(conn => LektorQuery::AddToPlaylist(name, query); ok)
+            send!(conn => LektorQuery::WithUserName(user, Box::new(LektorQuery::AddToPlaylist(name, query))); ok)
         }
         LktPlaylistCommand::Remove { name, query } => {
-            send!(conn => LektorQuery::RemoveFromPlaylist(name, query); ok)
+            send!(conn => LektorQuery::WithUserName(user, Box::new(LektorQuery::RemoveFromPlaylist(name, query))); ok)
         }
+
+        // List playlists
         LktPlaylistCommand::List => {
             send!(conn => LektorQuery::ListAllPlaylists; LektorResponse::PlaylistSet(playlists) => {
                 playlists.into_iter().for_each(|plt| println!("{plt}"))
-- 
GitLab