From 42117885f499907163f38c4b7b2ab14825a0b8f9 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Mon, 4 May 2020 15:49:26 +0200
Subject: [PATCH] WIP

---
 inc/lektor/database.h | 11 ++---------
 src/commands.c        | 32 ++++++++++++++++++--------------
 src/database/find.c   | 14 --------------
 3 files changed, 20 insertions(+), 37 deletions(-)

diff --git a/inc/lektor/database.h b/inc/lektor/database.h
index 24cff874..fbfbba6f 100644
--- a/inc/lektor/database.h
+++ b/inc/lektor/database.h
@@ -95,15 +95,8 @@ struct lkt_search {
     long continuation;      /* The continuation state of the client         */
     int msg_count;          /* How much messages we can send                */
     const char *name;       /* Stickers and playlists                       */
-    union {
-        int qu_priority;    /* Can be used... where the priority is needed! */
-        int st_value;       /* The value of a sticker                       */
-    };
-    union {
-        int st_uri;         /* URI of a sticker                             */
-    };
-    int must_init;          /* Skip the init phase ?                        */
-    struct lkt_uri *qu_uri; /* Kara uri for a queue init function           */
+    int st_value;           /* The value of a sticker                       */
+    int st_uri;             /* URI of a sticker                             */
 };
 
 typedef bool (*lkt_search_init_add_func)(volatile sqlite3 *, struct lkt_uri *, int);
diff --git a/src/commands.c b/src/commands.c
index f38599fb..d282b858 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -397,18 +397,17 @@ command_find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MA
              enum lkt_find_action action, bool(*init)(volatile sqlite3 *, char *, char *, struct lkt_search *))
 {
     char rgx[PATH_MAX], *col_name, *mpd_tag;
-    int count;
+    int count, must_init = (continuation == 0), priority;
     struct lkt_uri uri;
     struct lkt_queue_state queue;
     struct lkt_search search = {
         .srv = srv,
         .c = c,
-        .qu_uri = &uri,
         .continuation = continuation,
-        .must_init = (continuation == 0),
         .msg_count = lkt_remaining_msg(srv, c) - 3, /* Reserve slots for OK/ACK and continue: */
         .init = NULL,
     };
+    search.msg_count = MIN(search.msg_count, 10);
 
     /* Check args */
     RETURN_UNLESS(cmd_args && cmd_args[0], "Invalid argument", false);
@@ -422,24 +421,15 @@ command_find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MA
         search.call = NULL;
         break;
     case LKT_FND_ACT_ENQUEUE:
-        search.qu_priority = 5;
+        priority = 5;
         break;
     case LKT_FND_ACT_ADD:
-        search.qu_priority = 1;
+        priority = 1;
         break;
     default:
         return false;
     }
 
-    /* Begin, when we will prints the queue after a modification  */
-    if (action & (LKT_FND_ACT_ADD | LKT_FND_ACT_ENQUEUE) && ! continuation) {
-        search.call = (void(*)(void)) lkt_callback_send_row_v2;
-        search.init = (void(*)(void)) database_queue_add_uri;
-        search.continuation = queue.length + 1;
-        RETURN_UNLESS(database_queue_state(srv->db, &queue), "Failed to get the status of the queue", false);
-        srv->mpd_idle_events |= MPD_IDLE_PLAYLIST;
-    }
-
     /* Select the right column */
     mpd_tag = cmd_args[0];
     if (!strcasecmp("any", mpd_tag) || !strcasecmp("all", mpd_tag) || !strcasecmp("query", mpd_tag) ||
@@ -475,6 +465,20 @@ command_find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MA
     }
     uri.value = rgx;
 
+    /* Begin, when we will prints the queue after a modification  */
+    if (action & (LKT_FND_ACT_ADD | LKT_FND_ACT_ENQUEUE) && ! continuation) {
+        search.call = (void(*)(void)) lkt_callback_send_row_v2;
+        search.init = (void(*)(void)) database_queue_add_uri;
+        search.continuation = queue.length;
+        RETURN_UNLESS(database_queue_state(srv->db, &queue), "Failed to get the status of the queue", false);
+        srv->mpd_idle_events |= MPD_IDLE_PLAYLIST;
+
+        if (must_init && ! database_queue_add_uri(srv->db, &uri, priority)) {
+            LOG_ERROR_SCT("DB", "%s", "Failed to init search + (add | insert) command");
+            return false;
+        }
+    }
+
     /* Make the search langand do the right action */
     RETURN_UNLESS(init(srv->db, col_name, rgx, &search), "Failed to init search", false);
 
diff --git a/src/database/find.c b/src/database/find.c
index 5daba500..c95a2374 100644
--- a/src/database/find.c
+++ b/src/database/find.c
@@ -21,13 +21,6 @@ database_search_database_init(volatile sqlite3 *db, char *col_name, char *rgx, s
     char SQL_STMT[LKT_MAX_SQLITE_STATEMENT];
     ret->type = lkt_search_database;
 
-    /* Init */
-    if (ret->must_init && ret->init &&
-        ! ((lkt_search_init_add_func) ret->init)(db, ret->qu_uri, ret->qu_priority)) {
-        LOG_ERROR_SCT("DB", "%s", "Failed to init search query, init func failed");
-        return false;
-    }
-
     /* Search part */
     snprintf(SQL_STMT, LKT_MAX_SQLITE_STATEMENT - 1, SQL_STMT_TEMPLATE, col_name,
              ret->msg_count, ret->continuation);
@@ -90,13 +83,6 @@ database_search_queue_init(volatile sqlite3 *db, char *col_name, char *rgx, stru
     char SQL_STMT[LKT_MAX_SQLITE_STATEMENT];
     ret->type = lkt_search_queue;
 
-    /* Init */
-    if (ret->must_init && ret->init &&
-        ! ((lkt_search_init_add_func) ret->init)(db, ret->qu_uri, ret->qu_priority)) {
-        LOG_ERROR_SCT("DB", "%s", "Failed to init search query, called to init func failed");
-        return false;
-    }
-
     /* Search part */
     snprintf(SQL_STMT, LKT_MAX_SQLITE_STATEMENT - 1, SQL_STMT_TEMPLATE, col_name,
              ret->msg_count, ret->continuation);
-- 
GitLab