Skip to content

Commit

Permalink
More thread_id support
Browse files Browse the repository at this point in the history
Added thread_id columns to cleanup, overview and trainderinfo tables. Almost everything but overview sharing should now fully support threads. Working on that
  • Loading branch information
Ninjasoturi committed Feb 12, 2024
1 parent 4638050 commit 28fb58c
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 82 deletions.
8 changes: 5 additions & 3 deletions logic/collectCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ function collectCleanup($response, $request, $identifier = false)
// Set chat and message_id
$chat_id = $response['result']['chat']['id'];
$message_id = $response['result']['message_id'];
$thread_id = $response['result']['message_thread_id'] ?? null;

debug_log('Return data: Chat id: '.$chat_id.', message_id: '.$message_id.', type: '.(is_array($identifier) ? print_r($identifier,true) : $identifier));
if($identifier == 'trainer') {
debug_log('Adding trainermessage info to database now!');
insert_trainerinfo($chat_id, $message_id);
insert_trainerinfo($chat_id, $message_id, $thread_id);
return $response;
}
if ($identifier == 'overview') {
debug_log('Adding overview info to database now!');
$chat_title = $response['result']['chat']['title'];
$chat_username = $response['result']['chat']['username'] ?? '';

insert_overview($chat_id, $message_id, $chat_title, $chat_username);
insert_overview($chat_id, $message_id, $thread_id, $chat_title, $chat_username);
return $response;
}

Expand Down Expand Up @@ -73,7 +75,7 @@ function collectCleanup($response, $request, $identifier = false)
);
}
$raid_id = is_array($identifier) ? $identifier['id'] : $identifier;
insert_cleanup($chat_id, $message_id, $raid_id, $type, $unique_id);
insert_cleanup($chat_id, $message_id, $thread_id, $raid_id, $type, $unique_id);
// Return response.
return $response;
}
77 changes: 77 additions & 0 deletions logic/config_chats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

function list_config_chats_by_short_id() {
global $config;
if(!isset($config->CHATS_SHARE)) {
$chat_vars = ['TRAINER_CHATS','SHARE_CHATS','WEBHOOK_CHATS'];
$chatsTemp = [];
foreach(array_keys($config) as $var) {
foreach($chat_vars as $start) {
if(!strpos($var, $start)) continue;
if(is_string($config->{$var})) {
array_merge($chatsTemp, explode(',', $config->{$var}));
continue;
}
array_merge($chatsTemp, $config->{$var});
}
}
$chats = [];
foreach($chatsTemp as $chat) {
$chats[] = create_chat_object($chat);
}
}else {
$chats = [];
if(isset($config->CHATS_SHARE['manual_share'])) {
foreach($config->CHATS_SHARE['manual_share'] as $chatGroup) {
foreach($chatGroup as $chat) {
$chats = add_chat($chats, $chat);
}
}
}
if(isset($config->CHATS_SHARE['after_attendance'])) {
foreach($config->CHATS_SHARE['after_attendance'] as $chat) {
$chats = add_chat($chats, $chat);
}
}
if(isset($config->CHATS_SHARE['webhook'])) {
if(isset($config->CHATS_SHARE['webhook']['all'])) {
foreach($config->CHATS_SHARE['webhook']['all'] as $chat) {
$chats = add_chat($chats, $chat);
}
}
if(isset($config->CHATS_SHARE['webhook']['by_pokemon'])) {
foreach($config->CHATS_SHARE['webhook']['by_pokemon'] as $chatGroup) {
foreach($chatGroup['chats'] as $chat) {
$chats = add_chat($chats, $chat);
}
}
}
if(isset($config->CHATS_SHARE['webhook']['geofences'])) {
foreach($config->CHATS_SHARE['webhook']['geofences'] as $geofence) {
foreach($geofence as $chatGroup) {
foreach($chatGroup as $chat) {
$chats = add_chat($chats, $chat);
}
}
}
}
}
}
return $chats;
}

function add_chat($chats, $chatToAdd) {
foreach($chats as $chat) {
if(
$chat['id'] == $chatToAdd['id'] && !isset($chat['thread']) ||
$chat['id'] == $chatToAdd['id'] && isset($chat['thread']) && isset($chatToAdd['thread']) && $chat['thread'] == $chatToAdd['thread']
) return $chats;
}
$chats[] = $chatToAdd;
return $chats;
}

function get_config_chat_by_short_id($id) {
$chats = list_config_chats_by_short_id();
return $chats[$id];
}
16 changes: 10 additions & 6 deletions logic/insert_cleanup.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?php
/**
* Insert raid cleanup info to database.
* @param $chat_id
* @param $message_id
* @param $raid_id
* @param $type
* @param $photo_id
* @param int $chat_id
* @param int $message_id
* @param int $thread_id
* @param int $raid_id
* @param string $type
* @param string|null $photo_id
*/
function insert_cleanup($chat_id, $message_id, $raid_id, $type, $photo_id = NULL)
function insert_cleanup($chat_id, $message_id, $thread_id, $raid_id, $type, $photo_id = NULL)
{
// Log ID's of raid, chat and message
debug_log('Raid_ID: ' . $raid_id);
debug_log('Chat_ID: ' . $chat_id);
debug_log('Message_ID: ' . $message_id);
debug_log('Thread_ID: ' . $thread_id);
debug_log('Type: ' . $type);

if (!is_numeric($chat_id) || !is_numeric($message_id) || !is_numeric($raid_id) || $raid_id < 1) {
Expand All @@ -27,12 +29,14 @@ function insert_cleanup($chat_id, $message_id, $raid_id, $type, $photo_id = NULL
SET raid_id = :raid_id,
chat_id = :chat_id,
message_id = :message_id,
thread_id = :thread_id,
type = :type,
media_unique_id = :media_unique_id
', [
':raid_id' => $raid_id,
':chat_id' => $chat_id,
':message_id' => $message_id,
':thread_id' => $thread_id,
':type' => $type,
':media_unique_id' => $photo_id,
]
Expand Down
13 changes: 8 additions & 5 deletions logic/insert_overview.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
/**
* Insert overview.
* @param $chat_id
* @param $message_id
* @param $chat_title
* @param $chat_username
* @param int $chat_id
* @param int $message_id
* @param int|null $thread_id
* @param string $chat_title
* @param string $chat_username
*/
function insert_overview($chat_id, $message_id, $chat_title, $chat_username)
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
$rs = my_query('
Expand All @@ -31,12 +32,14 @@ function insert_overview($chat_id, $message_id, $chat_title, $chat_username)
INSERT INTO overview
SET chat_id = :chat_id,
message_id = :message_id,
thread_id = :thread_id,
chat_title = :chat_title,
chat_username = :chat_username,
updated = DATE(NOW())
', [
'chat_id' => $chat_id,
'message_id' => $message_id,
'thread_id' => $thread_id,
'chat_title' => $chat_title,
'chat_username' => $chat_username,
]
Expand Down
12 changes: 7 additions & 5 deletions logic/insert_trainerinfo.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
/**
* Insert trainer info.
* @param $chat_id
* @param $message_id
* @param int $chat_id
* @param int $message_id
* @param int|null $thread_id
*/
function insert_trainerinfo($chat_id, $message_id)
function insert_trainerinfo($chat_id, $message_id, $thread_id)
{
// Build query to check if trainer info details are already in database or not
$rs = my_query('
Expand All @@ -27,7 +28,8 @@ function insert_trainerinfo($chat_id, $message_id)
my_query('
INSERT INTO trainerinfo
SET chat_id = ?,
message_id = ?
', [$chat_id, $message_id]
message_id = ?,
thread_id = ?
', [$chat_id, $message_id, $thread_id]
);
}
7 changes: 4 additions & 3 deletions logic/update_raid_poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function update_raid_poll($raid_id, $raid = false, $update = false, $tg_json = f
if(empty($chat_and_message) or isset($update['callback_query']['inline_message_id'])) {
if($update_photo) $photo_query = 'AND (type = \'photo\' OR type = \'poll_photo\')'; else $photo_query = '';
$rs_chann = my_query('
SELECT chat_id, message_id, type, media_unique_id
SELECT chat_id, message_id, thread_id, type, media_unique_id
FROM cleanup
WHERE raid_id = ?
' . $photo_query,[$raid_id]
Expand Down Expand Up @@ -95,6 +95,7 @@ function update_raid_poll($raid_id, $raid = false, $update = false, $tg_json = f
foreach($chat_and_message as $chat_id_msg_id) {
$chat = $chat_id_msg_id['chat_id'];
$message = $chat_id_msg_id['message_id'];
$thread_id = $chat_id_msg_id['thread_id'];
$type = $chat_id_msg_id['type'];
if ($type == 'poll_text') {
$raid_picture_hide_level = explode(",",$config->RAID_PICTURE_HIDE_LEVEL);
Expand All @@ -120,8 +121,8 @@ function update_raid_poll($raid_id, $raid = false, $update = false, $tg_json = f
$media_content = get_raid_picture($raid, true);
$raid['standalone_photo'] = true; // Inject this into raid array so we can pass it all the way to photo cache
// Resend raid poll as text message.
send_photo($chat, $media_content[1], $media_content[0], '', [], [], false, $raid);
send_message($chat, $text['full'], $keys, ['disable_web_page_preview' => 'true'], false, $raid_id);
send_photo(create_chat_object([$chat, $thread_id]), $media_content[1], $media_content[0], '', [], [], false, $raid);
send_message(create_chat_object([$chat, $thread_id]), $text['full'], $keys, ['disable_web_page_preview' => 'true'], false, $raid_id);
continue;
}
$media_content = get_raid_picture($raid);
Expand Down
2 changes: 1 addition & 1 deletion mods/refresh_polls.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
if(strlen($data['id']) > 5) $where_chat = 'chat_id = '.$data['id']; else $where_chat = 'chat_id != 0';
if(!empty($config->RAID_POLL_HIDE_BUTTONS_RAID_LEVEL)) $level_exclude = 'AND raids.level NOT IN ('.$config->RAID_POLL_HIDE_BUTTONS_RAID_LEVEL.')'; else $level_exclude = '';
$query_messages = my_query('
SELECT cleanup.raid_id, cleanup.chat_id, cleanup.message_id, cleanup.type, cleanup.media_unique_id
SELECT cleanup.raid_id, cleanup.chat_id, cleanup.thread_id, cleanup.message_id, cleanup.type, cleanup.media_unique_id
FROM cleanup
LEFT JOIN raids
ON cleanup.raid_id = raids.id
Expand Down
84 changes: 27 additions & 57 deletions mods/trainer_add.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
// Write to log.
debug_log('TRAINER()');
require_once(LOGIC_PATH . '/config_chats.php');

// For debug.
//debug_log($update);
Expand All @@ -11,74 +12,43 @@

// Init keys and chat list.
$keys = [];
$chat_list = '';

// $config->TRAINER_CHATS ?
if(!empty($config->TRAINER_CHATS)) {
$chat_list = $config->TRAINER_CHATS;
debug_log($chat_list, 'Added trainer chats to the chat list:');
}

// $config->SHARE_CHATS ?
if(!empty($config->SHARE_CHATS) && !empty($chat_list)) {
$chat_list .= ',' . $config->SHARE_CHATS;
debug_log($chat_list, 'Added share chats to the chat list:');
} else if(!empty($config->SHARE_CHATS) && empty($chat_list)) {
$chat_list = $config->SHARE_CHATS;
debug_log($chat_list, 'Added share chats to the chat list:');
}

// Get chats from config and add to keys.
for($i = 1; $i <= 10; $i++) {
// Raid level adjustment
if($i == 10) {
$raid_level = 'X';
} else {
$raid_level = $i;
}
$const = 'SHARE_CHATS_LEVEL_' . $raid_level;
$const_chats = $config->{$const};

// Sharing keys for this raid level?
if(!empty($const_chats)) {
debug_log('Found chats by level, adding them');
// Add chats.
if(!empty($chat_list)) {
$chat_list .= ',' . $const_chats;
debug_log($chat_list, 'Added ' . $const . ' chats to the chat list:');
} else {
$chat_list = $const_chats;
debug_log($chat_list, 'Added ' . $const . ' chats to the chat list:');
}
}
}

// Delete duplicate chats.
debug_log($chat_list, 'Searching and removing duplicates from chat list:');
$chat_list = explode(',', $chat_list);
$chats = array_unique($chat_list);
$chats = list_config_chats_by_short_id();

// Get chats already in the database.
debug_log('Searching and removing chats already having the trainer message');
$rs = my_query('
SELECT chat_id
SELECT chat_id, thread_id
FROM trainerinfo
');

$chats_db = [];
while ($row = $rs->fetch()) {
$chats_db[] = $row['chat_id'];
$chats_db = $rs->fetchAll();
for($i=0;$i<count($chats);$i++) {
foreach($chats_db as $chat_db) {
if(
$chats[$i]['id'] == $chat_db['chat_id'] && !isset($chats[$i]['thread']) ||
($chats[$i]['id'] == $chat_db['chat_id'] && isset($chats[$i]['thread']) && $chats[$i]['thread'] == $chat_db['thread_id'])
) {
unset($chats[$i]);
}
}
}
$log_chats_db = implode(',', $chats_db);

debug_log($log_chats_db, 'Chats already having the trainer message:');

$chats = array_diff($chats, $chats_db);
debug_log(implode(',', $chats), 'Chat list without duplicates:');

// Create keys.
if(!empty($chats)) {
$keys = share_keys(false, 'trainer_share', $update, '', $chats, true);
foreach($chats as $chatShortId => $chat) {
// Get chat object
debug_log("Getting chat object for '" . $chat['id'] . "'");
$chat_obj = get_chat($chat['id']);

// Check chat object for proper response.
if ($chat_obj['ok'] != true) {
info_log($chat, 'Invalid chat id in your configuration:');
continue;
}
$chatTitle = $chat['title'] ?? $chat_obj['result']['title'];
debug_log('Proper chat object received, continuing to add key for this chat: ' . $chatTitle);
$shareData = [0 => 'trainer_share', 'c' => $chatShortId];
$keys[][] = button(getTranslation('share_with') . ' ' . $chatTitle, $shareData);
}

// Add abort key.
Expand Down
5 changes: 3 additions & 2 deletions mods/trainer_share.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
debug_log('trainer_share()');
require_once(LOGIC_PATH . '/keys_trainerinfo.php');
require_once(LOGIC_PATH . '/show_trainerinfo.php');
require_once(LOGIC_PATH . '/config_chats.php');

// For debug.
//debug_log($update);
Expand All @@ -11,8 +12,8 @@
// Access check.
$botUser->accessCheck('trainer-share');

// Get chat id.
$chat = $data['c'];
// Get chat object by chat short id
$chat = get_config_chat_by_short_id($data['c']);

// Get text and keys.
$text = show_trainerinfo($update);
Expand Down
Loading

0 comments on commit 28fb58c

Please sign in to comment.