From 5bd74382f8ac51f9d823f99c4c49705182dffc70 Mon Sep 17 00:00:00 2001 From: ninjasoturi <52629375+Ninjasoturi@users.noreply.github.com> Date: Tue, 13 Feb 2024 20:27:28 +0200 Subject: [PATCH] Added thread support to overview management --- commands/overview.php | 1 + logic/config_chats.php | 7 +++ logic/insert_overview.php | 10 +++- mods/overview_delete.php | 54 ++++++++++---------- mods/overview_share.php | 102 ++++++++++++++++++++------------------ 5 files changed, 98 insertions(+), 76 deletions(-) diff --git a/commands/overview.php b/commands/overview.php index 4aca3d1c..cb60f82d 100644 --- a/commands/overview.php +++ b/commands/overview.php @@ -12,6 +12,7 @@ // Create keys array. $keys[][] = button(getTranslation('overview_share'), 'overview_share'); $keys[][] = button(getTranslation('overview_delete'), 'overview_delete'); +$keys[][] = button(getTranslation('abort'), ['exit', 'd' => '0']); // Set message. $msg = '' . getTranslation('raids_share_overview') . ':'; diff --git a/logic/config_chats.php b/logic/config_chats.php index 820a1ddf..7220c76d 100644 --- a/logic/config_chats.php +++ b/logic/config_chats.php @@ -60,6 +60,13 @@ function list_config_chats_by_short_id() { return $chats; } +function get_config_chat_by_chat_and_thread_id($chat_id, $thread_id) { + foreach(list_config_chats_by_short_id() as $chat) { + if($chat['id'] == $chat_id && ($thread_id == NULL && !isset($chat['thread']) || (isset($chat['thread']) && $chat['thread'] == $thread_id))) + return $chat; + } +} + function add_chat($chats, $chatToAdd) { foreach($chats as $chat) { if( diff --git a/logic/insert_overview.php b/logic/insert_overview.php index 60a6b553..425c4106 100644 --- a/logic/insert_overview.php +++ b/logic/insert_overview.php @@ -10,11 +10,19 @@ function insert_overview($chat_id, $message_id, $thread_id, $chat_title, $chat_username) { // Build query to check if overview details are already in database or not + $binds = [$chat_id]; + $threadQuery = ' = ?'; + if(!isset($thread_id)) { + $threadQuery = 'IS NULL'; + }else { + $binds[] = $thread_id; + } $rs = my_query(' SELECT COUNT(*) AS count FROM overview WHERE chat_id = ? - ', [$chat_id] + AND thread_id ' . $threadQuery . ' + ', $binds ); $row = $rs->fetch(); diff --git a/mods/overview_delete.php b/mods/overview_delete.php index a36b8573..ed20c4a8 100644 --- a/mods/overview_delete.php +++ b/mods/overview_delete.php @@ -1,13 +1,15 @@ accessCheck('overview'); @@ -16,63 +18,61 @@ $tg_json = array(); // Get all or specific overview -if ($chat_id == 0) { +if ($action == 0) { $request_overviews = my_query(' SELECT * FROM overview '); - // Count results. - $count = 0; - while ($rowOverviews = $request_overviews->fetch()) { - // Counter++ - $count = $count + 1; // Get info about chat for title. debug_log('Getting chat object for chat_id: ' . $rowOverviews['chat_id']); - $chat_obj = get_chat($rowOverviews['chat_id']); - $chat_title = ''; - - // Set title. - if ($chat_obj['ok'] == 'true') { - $chat_title = $chat_obj['result']['title']; - debug_log('Title of the chat: ' . $chat_obj['result']['title']); + $chat_obj = get_config_chat_by_chat_and_thread_id($rowOverviews['chat_id'], $rowOverviews['thread_id']); + if(!isset($chat_obj['title'])) { + $chat_info = get_chat($rowOverviews['chat_id']); + $chat_title = ''; + + // Set title. + if ($chat_info['ok'] == 'true') { + $chat_title = $chat_info['result']['title']; + debug_log('Title of the chat: ' . $chat_info['result']['title']); + } + }else { + $chat_title = $chat_obj['title']; } // Build message string. $msg = '' . getTranslation('delete_raid_overview_for_chat') . ' ' . $chat_title . '?'; // Set keys - Delete button. - $keys[0][0] = button(getTranslation('yes'), ['overview_delete', 'c' => $rowOverviews['chat_id']]); - $keys[0][1] = button(getTranslation('no'), ['overview_delete', 'c' => 1]); + $keys[0][0] = button(getTranslation('yes'), ['overview_delete', 'o' => $rowOverviews['id'], 'a' => 3]); + $keys[0][1] = button(getTranslation('no'), ['overview_delete', 'a' => 1]); // Send the message, but disable the web preview! - $tg_json[] = send_message($update['callback_query']['message']['chat']['id'], $msg, $keys, false, true); + $tg_json[] = send_message(create_chat_object([$update['callback_query']['message']['chat']['id']]), $msg, $keys, false, true); } // Set message. - if($count == 0) { + if($request_overviews->rowCount() == 0) { $callback_msg = '' . getTranslation('no_overviews_found') . ''; } else { $callback_msg = '' . getTranslation('list_all_overviews') . ':'; } -} else if ($chat_id == 1) { +} else if ($action == 1) { // Write to log. debug_log('Deletion of the raid overview was canceled!'); // Set message. $callback_msg = '' . getTranslation('overview_deletion_was_canceled') . ''; } else { - // Write to log. - debug_log('Triggering deletion of overview for Chat_ID ' . $chat_id); // Get chat and message ids for overview. $request_overviews = my_query(' SELECT * FROM overview - WHERE chat_id = ? - ', [$chat_id] + WHERE id = ? + ', [$overview_id] ); $overview = $request_overviews->fetch(); @@ -80,17 +80,19 @@ // Delete overview $chat_id = $overview['chat_id']; $message_id = $overview['message_id']; + // Write to log. + debug_log('Triggering deletion of overview for Chat_ID ' . $chat_id); // Delete telegram message. debug_log('Deleting overview telegram message ' . $message_id . ' from chat ' . $chat_id); delete_message($chat_id, $message_id); // Delete overview from database. - debug_log('Deleting overview information from database for Chat_ID: ' . $chat_id); + debug_log('Deleting overview information from database for Chat_ID: ' . $chat_id . ', thread_id: ' . $overview['thread_id']); $rs = my_query(' DELETE FROM overview - WHERE chat_id = ? - ', [$chat_id] + WHERE id = ? + ', [$overview_id] ); // Set message. diff --git a/mods/overview_share.php b/mods/overview_share.php index 9db31363..047db516 100644 --- a/mods/overview_share.php +++ b/mods/overview_share.php @@ -3,6 +3,7 @@ debug_log('overview_share()'); require_once(LOGIC_PATH . '/get_chat_title_username.php'); require_once(LOGIC_PATH . '/get_overview.php'); +require_once(LOGIC_PATH . '/config_chats.php'); // For debug. //debug_log($update); @@ -12,38 +13,41 @@ $botUser->accessCheck('overview'); // Get chat ID from data -$chat_id = $data['c'] ?? 0; - -// Get all or specific overview -$query_chat = ''; -if ($chat_id != 0) { - $query_chat = 'AND chat_id = \'' . $chat_id . '\''; -} -// Get active raids. -$request_active_raids = my_query(' - SELECT - cleanup.chat_id, cleanup.message_id, - raids.*, - gyms.lat, gyms.lon, gyms.address, gyms.gym_name, gyms.ex_gym, - TIME_FORMAT(TIMEDIFF(end_time, UTC_TIMESTAMP()) + INTERVAL 1 MINUTE, \'%k:%i\') AS t_left - FROM cleanup - LEFT JOIN raids - ON raids.id = cleanup.raid_id - LEFT JOIN gyms - ON raids.gym_id = gyms.id - WHERE raids.end_time>UTC_TIMESTAMP() - ' . $query_chat . ' - ORDER BY cleanup.chat_id, raids.end_time ASC, gyms.gym_name -'); -// Collect results in an array -$active_raids = $request_active_raids->fetchAll(PDO::FETCH_GROUP); +$chat_id = $data['c'] ?? NULL; $tg_json = []; // Share an overview -if($chat_id != 0) { - [$chat_title, $chat_username] = get_chat_title_username($chat_id); - $overview_message = get_overview($active_raids[$chat_id], $chat_title, $chat_username); +if($chat_id != NULL) { + $query_chat = ''; + $chatObj = get_config_chat_by_short_id($chat_id); + $query_chat = 'AND chat_id = ?'; + $binds[] = $chatObj['id']; + if(isset($chatObj['thread'])) { + $query_chat .= ' AND thread_id = ?'; + $binds[] = $chatObj['thread']; + } + // Get active raids. + $request_active_raids = my_query(' + SELECT + cleanup.chat_id, cleanup.thread_id, cleanup.message_id, + raids.*, + gyms.lat, gyms.lon, gyms.address, gyms.gym_name, gyms.ex_gym, + TIME_FORMAT(TIMEDIFF(end_time, UTC_TIMESTAMP()) + INTERVAL 1 MINUTE, \'%k:%i\') AS t_left + FROM cleanup + LEFT JOIN raids + ON raids.id = cleanup.raid_id + LEFT JOIN gyms + ON raids.gym_id = gyms.id + WHERE raids.end_time>UTC_TIMESTAMP() + ' . $query_chat . ' + ORDER BY cleanup.chat_id, raids.end_time ASC, gyms.gym_name + ', $binds); + // Collect results in an array + $active_raids = $request_active_raids->fetchAll(); + [$chat_title, $chat_username] = get_chat_title_username($chatObj['id']); + $title = $chatObj['title'] ?? $chat_title; + $overview_message = get_overview($active_raids, $title, $chat_username); // Shared overview $keys = []; @@ -57,47 +61,47 @@ $tg_json[] = edit_message($update, $msg_callback, $keys, ['disable_web_page_preview' => 'true'], true); // Send the message, but disable the web preview! - $tg_json[] = send_message($chat_id, $overview_message, $keys, ['disable_web_page_preview' => 'true'], true, 'overview'); + $tg_json[] = send_message($chatObj, $overview_message, $keys, ['disable_web_page_preview' => 'true'], true, 'overview'); // Telegram multicurl request. curl_json_multi_request($tg_json); exit; } +$keys = []; // List all overviews to user -foreach( array_keys($active_raids) as $chat_id ) { +foreach( list_config_chats_by_short_id() as $short_id => $chat ) { + $binds = [$chat['id']]; + $threadQuery = ' = ?'; + if(!isset($chat['thread'])) { + $threadQuery = 'IS NULL'; + }else { + $binds[] = $chat['thread']; + } // Make sure it's not already shared $rs = my_query(' - SELECT chat_id, message_id, chat_title, chat_username + SELECT chat_id, thread_id, message_id, chat_title, chat_username FROM overview WHERE chat_id = ? + AND thread_id ' . $threadQuery . ' LIMIT 1 - ', [$chat_id] + ', $binds ); - $keys = []; // Already shared - if($rs->rowCount() > 0 ) { - $keys[0][] = button(EMOJI_REFRESH, ['overview_refresh', 'c' => $chat_id]); - $keys[0][] = button(getTranslation('done'), ['exit', 'd' => '1']); - $res = $rs->fetch(); - $chat_title = $res['chat_title']; - $chat_username = $res['chat_username']; - }else { - [$chat_title, $chat_username] = get_chat_title_username($chat_id); - $keys[][] = button(getTranslation('share_with') . ' ' . $chat_title, ['overview_share', 'c' => $chat_id]); - } - $overview_message = get_overview($active_raids[$chat_id], $chat_title, $chat_username); - // Send the message, but disable the web preview! - $tg_json[] = send_message($update['callback_query']['message']['chat']['id'], $overview_message, $keys, ['disable_web_page_preview' => 'true'], true); + if($rs->rowCount() > 0 ) continue; + + [$chat_title, $chat_username] = get_chat_title_username($chat['id']); + $title = $chat['title'] ?? $chat_title; + $keys[][] = button(getTranslation('share_with') . ' ' . $title, ['overview_share', 'c' => $short_id]); } // Set the callback message and keys -$callback_keys = []; -$callback_msg = '' . getTranslation('list_all_overviews') . ':'; +$msg = '' . getTranslation('list_all_overviews') . ':'; +$keys[][] = button(getTranslation('abort'), ['exit', 'd' => '0']); // Answer the callback. $tg_json[] = answerCallbackQuery($update['callback_query']['id'], 'OK', true); // Edit the message. -$tg_json[] = edit_message($update, $callback_msg, $callback_keys, false, true); +$tg_json[] = edit_message($update, $msg, $keys, false, true); // Telegram multicurl request. curl_json_multi_request($tg_json);