Skip to content

Commit

Permalink
Store chat's username and title in overview table
Browse files Browse the repository at this point in the history
Store the chat details in overview table instead of querying it at every update. Database is update once per day and upon sharing of the overview message.
  • Loading branch information
Ninjasoturi committed Jul 31, 2021
1 parent 6d96836 commit 2b505ec
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 50 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.211.1
2.1.212.2
2 changes: 1 addition & 1 deletion config/config.json.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"VERSION":"2.1.211.1",
"VERSION":"2.1.212.2",
"DB_HOST":"localhost",
"DB_NAME":"your_database_name",
"DB_USER":"your_database_user",
Expand Down
2 changes: 1 addition & 1 deletion logic.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
include('logic/delete_trainerinfo.php');
include('logic/disable_raid_level.php');
include('logic/edit_pokedex_keys.php');
include('logic/get_chat_title.php');
include('logic/get_chat_title_username.php');
include('logic/get_formatted_pokemon_cp.php');
include('logic/get_gym_by_telegram_id.php');
include('logic/get_gym_details.php');
Expand Down
41 changes: 22 additions & 19 deletions logic/curl_json_response.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ function curl_json_response($json_response, $json)
if ((isset($response['ok']) && $response['ok'] != true) || isset($response['update_id'])) {
info_log("{$json} -> {$json_response}", 'ERROR:');
} else {
// Result seems ok, get message_id and chat_id if supergroup or channel message
if (isset($response['result']['chat']['type']) && ($response['result']['chat']['type'] == "channel" || $response['result']['chat']['type'] == "supergroup")) {
// Result seems ok, get message_id and chat_id if supergroup or channel message
if (isset($response['result']['chat']['type']) && ($response['result']['chat']['type'] == "channel" || $response['result']['chat']['type'] == "supergroup")) {
// Init cleanup_id
$cleanup_id = 0;

// Set chat and message_id
// Set chat and message_id
$chat_id = $response['result']['chat']['id'];
$message_id = $response['result']['message_id'];

Expand All @@ -34,16 +34,16 @@ function curl_json_response($json_response, $json)
debug_log('Message was shared with ' . $response['result']['chat']['type'] . ' ' . $response['result']['chat']['title']);
debug_log('Checking input for cleanup info now...');

// Check if callback_data is present to get the cleanup id
// Check if callback_data is present to get the cleanup id
if (!empty($response['result']['reply_markup']['inline_keyboard']['0']['0']['callback_data'])) {
debug_log('Callback Data of this message likely contains cleanup info!');
$split_callback_data = explode(':', $response['result']['reply_markup']['inline_keyboard']['0']['0']['callback_data']);
// Get raid_id, but check for $config->BRIDGE_MODE first
if($config->BRIDGE_MODE) {
$cleanup_id = $split_callback_data[1];
} else {
$cleanup_id = $split_callback_data[0];
}
// Get raid_id, but check for $config->BRIDGE_MODE first
if($config->BRIDGE_MODE) {
$cleanup_id = $split_callback_data[1];
} else {
$cleanup_id = $split_callback_data[0];
}

// Check if it's a venue and get raid id
} else if (isset($response['result']['venue']['address']) && !empty($response['result']['venue']['address'])) {
Expand Down Expand Up @@ -82,13 +82,13 @@ function curl_json_response($json_response, $json)
debug_log('Chat_ID: ' . $chat_id);
debug_log('Message_ID: ' . $message_id);

// Trigger cleanup preparation process when necessary id's are not empty and numeric
if (!empty($chat_id) && !empty($message_id) && !empty($cleanup_id)) {
debug_log('Calling cleanup preparation now!');
insert_cleanup($chat_id, $message_id, $cleanup_id);
} else {
info_log($cleanup_id, 'Missing input! Cannot call cleanup preparation for raid:');
}
// Trigger cleanup preparation process when necessary id's are not empty and numeric
if (!empty($chat_id) && !empty($message_id) && !empty($cleanup_id)) {
debug_log('Calling cleanup preparation now!');
insert_cleanup($chat_id, $message_id, $cleanup_id);
} else {
info_log($cleanup_id, 'Missing input! Cannot call cleanup preparation for raid:');
}
} else if($cleanup_id != '0' && $cleanup_id == 'trainer') {
debug_log('Detected trainer info message from callback_data!');
debug_log('Chat_ID: ' . $chat_id);
Expand Down Expand Up @@ -119,9 +119,12 @@ function curl_json_response($json_response, $json)

// Write raid overview data to database
debug_log('Adding overview info to database now!');
insert_overview($chat_id, $message_id);
$chat_title = $response['result']['chat']['title'];
$chat_username = isset($response['result']['chat']['username']) ? $response['result']['chat']['username'] : '';

insert_overview($chat_id, $message_id, $chat_title, $chat_username);
}
}
}
}

// Return response.
Expand Down
18 changes: 12 additions & 6 deletions logic/get_chat_title.php → logic/get_chat_title_username.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
/**
* Return the title of the given chat_id.
* @param $chat_id
* @return string
* @return array [chat_title, chat_username]
*/
function get_chat_title($chat_id, $chat_obj = false){
function get_chat_title_username($chat_id){
$chat_obj = get_chat($chat_id);
$chat_username = '';

// Set chat username if available.
if ($chat_obj['ok'] == 'true' && isset($chat_obj['result']['username'])) {
$chat_username = $chat_obj['result']['username'];
debug_log('Username of the chat: ' . $chat_obj['result']['username']);
}

// Get info about chat for title.
debug_log('Getting chat object for chat_id: ' . $chat_id);
if($chat_obj == false) {
$chat_obj = get_chat($chat_id);
}
$chat_title = '<unknown chat>';

// Set title.
Expand All @@ -19,7 +25,7 @@ function get_chat_title($chat_id, $chat_obj = false){
} else {
debug_log($chat_obj, 'Unable to find title for ' . $chat_id . ' from:');
}
return $chat_title;
return [$chat_title, $chat_username];
}

?>
16 changes: 3 additions & 13 deletions logic/get_overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@
/**
* Return the overview message for a specific chat.
* @param $active_raids - Custom array of gym and raid info
* @param $chat_id - String
* @param $chat_title - String
* @param $chat_username - String
* @return string
*/
function get_overview( $active_raids, $chat_id )
function get_overview( $active_raids, $chat_title, $chat_username )
{
global $config;

// Get info about chat for username.
debug_log('Getting chat object for chat_id: ' . $chat_id);
$chat_obj = get_chat($chat_id);
$chat_username = '';

// Set chat username if available.
if ($chat_obj['ok'] == 'true' && isset($chat_obj['result']['username'])) {
$chat_username = $chat_obj['result']['username'];
debug_log('Username of the chat: ' . $chat_obj['result']['username']);
}
$chat_title = get_chat_title(false, $chat_obj);
$msg = '<b>' . getPublicTranslation('raid_overview_for_chat') . ' ' . $chat_title . ' ' . getPublicTranslation('from') . ' '. dt2time('now') . '</b>' . CR . CR;

$now = utcnow();
Expand Down
9 changes: 7 additions & 2 deletions logic/insert_overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
* Insert overview.
* @param $chat_id
* @param $message_id
* @param $chat_title
* @param $chat_username
*/
function insert_overview($chat_id, $message_id)
function insert_overview($chat_id, $message_id, $chat_title, $chat_username)
{
// Build query to check if overview details are already in database or not
$rs = my_query(
Expand All @@ -25,7 +27,10 @@ function insert_overview($chat_id, $message_id)
"
INSERT INTO overview
SET chat_id = '{$chat_id}',
message_id = '{$message_id}'
message_id = '{$message_id}',
chat_title = '{$chat_title}',
chat_username = '{$chat_username}',
updated = DATE(NOW())
"
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion mods/overview_delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
];

// Send the message, but disable the web preview!
$tg_json[] = send_message($update['callback_query']['message']['chat']['id'], $msg, $keys, true);
$tg_json[] = send_message($update['callback_query']['message']['chat']['id'], $msg, $keys, false, true);
}

// Set message.
Expand Down
19 changes: 17 additions & 2 deletions mods/overview_refresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

$request_overviews = my_query(
"
SELECT chat_id, message_id
SELECT chat_id, message_id, chat_title, chat_username, (IF(updated < DATE(NOW()) or updated IS NULL, 1, 0)) as update_needed
FROM overview
{$query_chat}
"
Expand Down Expand Up @@ -83,7 +83,22 @@
debug_log('Active raids:');
debug_log($active_raids);

$overview_message = get_overview($active_raids, $overview_row['chat_id']);
if($overview_row['update_needed'] == 1) {
$chat_title_username = get_chat_title_username($overview_row['chat_id']);
$chat_title = $chat_title_username[0];
$chat_username = $chat_title_username[1];
my_query('
UPDATE overview
SET chat_title = \''.$chat_title.'\',
chat_username = \''.$chat_username.'\',
updated = DATE(NOW())
WHERE chat_id = \''.$overview_row['chat_id'].'\'
');
}else {
$chat_title = $overview_row['chat_title'];
$chat_username = $overview_row['chat_username'];
}
$overview_message = get_overview($active_raids, $chat_title, $chat_username);
// Triggered from user or cronjob?
if (!empty($update['callback_query']['id'])) {
// Answer the callback.
Expand Down
15 changes: 11 additions & 4 deletions mods/overview_share.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@

// Share an overview
if($chat_id != 0) {
$overview_message = get_overview($active_raids[$chat_id], $chat_id);
$chat_title_username = get_chat_title_username($chat_id);
$overview_message = get_overview($active_raids[$chat_id], $chat_title_username[1], $chat_title_username[0]);
// Shared overview
$keys = [];

Expand All @@ -64,12 +65,12 @@
// Make sure it's not already shared
$rs = my_query(
"
SELECT chat_id, message_id
SELECT chat_id, message_id, chat_title, chat_username
FROM overview
WHERE chat_id = '{$chat_id}'
LIMIT 1
"
);
$overview_message = get_overview($active_raids[$chat_id], $chat_id);
// Already shared
if($rs->rowCount() > 0 ) {
$keys = [
Expand All @@ -84,8 +85,13 @@
]
]
];
$res = $rs->fetch();
$chat_title = $res['chat_title'];
$chat_username = $res['chat_username'];
}else {
$chat_title = get_chat_title($chat_id);
$chat_title_username = get_chat_title_username($chat_id);
$chat_title = $chat_title_username[0];
$chat_username = $chat_title_username[1];
$keys = [
[
[
Expand All @@ -95,6 +101,7 @@
]
];
}
$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);
}
Expand Down
3 changes: 3 additions & 0 deletions sql/pokemon-raid-bot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ CREATE TABLE `overview` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`chat_id` bigint(20) NOT NULL,
`message_id` bigint(20) unsigned NOT NULL,
`chat_title` varchar(128) DEFAULT NULL,
`chat_username` varchar(32) DEFAULT NULL,
`updated` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
Expand Down
4 changes: 4 additions & 0 deletions sql/upgrade/2.1.212.1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE `overview`
ADD COLUMN IF NOT EXISTS `chat_title` VARCHAR(128) NULL AFTER `message_id`,
ADD COLUMN IF NOT EXISTS `chat_username` VARCHAR(32) NULL AFTER `chat_title`,
ADD COLUMN IF NOT EXISTS `updated` DATE NULL AFTER `chat_username`;

0 comments on commit 2b505ec

Please sign in to comment.