Skip to content

Commit

Permalink
Added thread support to overview management
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjasoturi committed Feb 13, 2024
1 parent abeb5c2 commit 5bd7438
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 76 deletions.
1 change: 1 addition & 0 deletions commands/overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<b>' . getTranslation('raids_share_overview') . ':</b>';
Expand Down
7 changes: 7 additions & 0 deletions logic/config_chats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 9 additions & 1 deletion logic/insert_overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
54 changes: 28 additions & 26 deletions mods/overview_delete.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
// Write to log.
debug_log('overview_delete()');
require_once(LOGIC_PATH . '/config_chats.php');

// For debug.
//debug_log($update);
//debug_log($data);

// Delete or list to deletion?
$chat_id = $data['c'] ?? 0;
$action = $data['a'] ?? 0;
$overview_id = $data['o'] ?? null;

// Check access.
$botUser->accessCheck('overview');
Expand All @@ -16,81 +18,81 @@
$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 = '<b>' . getTranslation('delete_raid_overview_for_chat') . ' ' . $chat_title . '?</b>';

// 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 = '<b>' . getTranslation('no_overviews_found') . '</b>';
} else {
$callback_msg = '<b>' . getTranslation('list_all_overviews') . ':</b>';
}
} 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 = '<b>' . getTranslation('overview_deletion_was_canceled') . '</b>';
} 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();

// 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.
Expand Down
102 changes: 53 additions & 49 deletions mods/overview_share.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = [];

Expand All @@ -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 = '<b>' . getTranslation('list_all_overviews') . ':</b>';
$msg = '<b>' . getTranslation('list_all_overviews') . ':</b>';
$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);

0 comments on commit 5bd7438

Please sign in to comment.