Skip to content

Commit 6223d60

Browse files
committed
Remove chat_invite_link table and add code to insert chat_member_updated entries to the database
1 parent 63c0ee3 commit 6223d60

File tree

3 files changed

+112
-75
lines changed

3 files changed

+112
-75
lines changed

src/DB.php

Lines changed: 94 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Longman\TelegramBot\Entities\CallbackQuery;
1616
use Longman\TelegramBot\Entities\Chat;
17+
use Longman\TelegramBot\Entities\ChatMemberUpdated;
1718
use Longman\TelegramBot\Entities\ChosenInlineResult;
1819
use Longman\TelegramBot\Entities\InlineQuery;
1920
use Longman\TelegramBot\Entities\Message;
@@ -140,13 +141,14 @@ protected static function defineTables(): void
140141
$tables = [
141142
'callback_query',
142143
'chat',
144+
'chat_member_updated',
143145
'chosen_inline_result',
144146
'edited_message',
145147
'inline_query',
146148
'message',
147-
'pre_checkout_query',
148149
'poll',
149150
'poll_answer',
151+
'pre_checkout_query',
150152
'request_limiter',
151153
'shipping_query',
152154
'telegram_update',
@@ -320,6 +322,8 @@ public static function entitiesArrayToJson(array $entities, $default = null)
320322
* @param string|null $pre_checkout_query_id
321323
* @param string|null $poll_id
322324
* @param string|null $poll_answer_poll_id
325+
* @param string|null $my_chat_member_updated_id
326+
* @param string|null $chat_member_updated_id
323327
*
324328
* @return bool If the insert was successful
325329
* @throws TelegramException
@@ -337,10 +341,12 @@ protected static function insertTelegramUpdate(
337341
?string $shipping_query_id = null,
338342
?string $pre_checkout_query_id = null,
339343
?string $poll_id = null,
340-
?string $poll_answer_poll_id = null
344+
?string $poll_answer_poll_id = null,
345+
?string $my_chat_member_updated_id = null,
346+
?string $chat_member_updated_id = null
341347
): ?bool {
342-
if ($message_id === null && $edited_message_id === null && $channel_post_id === null && $edited_channel_post_id === null && $inline_query_id === null && $chosen_inline_result_id === null && $callback_query_id === null && $shipping_query_id === null && $pre_checkout_query_id === null && $poll_id === null && $poll_answer_poll_id === null) {
343-
throw new TelegramException('message_id, edited_message_id, channel_post_id, edited_channel_post_id, inline_query_id, chosen_inline_result_id, callback_query_id, shipping_query_id, pre_checkout_query_id, poll_id, poll_answer_poll_id are all null');
348+
if ($message_id === null && $edited_message_id === null && $channel_post_id === null && $edited_channel_post_id === null && $inline_query_id === null && $chosen_inline_result_id === null && $callback_query_id === null && $shipping_query_id === null && $pre_checkout_query_id === null && $poll_id === null && $poll_answer_poll_id === null && $my_chat_member_updated_id === null && $chat_member_updated_id === null) {
349+
throw new TelegramException('message_id, edited_message_id, channel_post_id, edited_channel_post_id, inline_query_id, chosen_inline_result_id, callback_query_id, shipping_query_id, pre_checkout_query_id, poll_id, poll_answer_poll_id, my_chat_member_updated_id, chat_member_updated_id are all null');
344350
}
345351

346352
if (!self::isDbConnected()) {
@@ -350,9 +356,9 @@ protected static function insertTelegramUpdate(
350356
try {
351357
$sth = self::$pdo->prepare('
352358
INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '`
353-
(`id`, `chat_id`, `message_id`, `edited_message_id`, `channel_post_id`, `edited_channel_post_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `shipping_query_id`, `pre_checkout_query_id`, `poll_id`, `poll_answer_poll_id`)
359+
(`id`, `chat_id`, `message_id`, `edited_message_id`, `channel_post_id`, `edited_channel_post_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `shipping_query_id`, `pre_checkout_query_id`, `poll_id`, `poll_answer_poll_id`, `my_chat_member_updated_id`, `chat_member_updated_id`)
354360
VALUES
355-
(:id, :chat_id, :message_id, :edited_message_id, :channel_post_id, :edited_channel_post_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :shipping_query_id, :pre_checkout_query_id, :poll_id, :poll_answer_poll_id)
361+
(:id, :chat_id, :message_id, :edited_message_id, :channel_post_id, :edited_channel_post_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :shipping_query_id, :pre_checkout_query_id, :poll_id, :poll_answer_poll_id, :my_chat_member_updated_id, :chat_member_updated_id)
356362
');
357363

358364
$sth->bindValue(':id', $update_id);
@@ -368,6 +374,8 @@ protected static function insertTelegramUpdate(
368374
$sth->bindValue(':pre_checkout_query_id', $pre_checkout_query_id);
369375
$sth->bindValue(':poll_id', $poll_id);
370376
$sth->bindValue(':poll_answer_poll_id', $poll_answer_poll_id);
377+
$sth->bindValue(':my_chat_member_updated_id', $my_chat_member_updated_id);
378+
$sth->bindValue(':chat_member_updated_id', $chat_member_updated_id);
371379

372380
return $sth->execute();
373381
} catch (PDOException $e) {
@@ -520,18 +528,20 @@ public static function insertRequest(Update $update): bool
520528
return false;
521529
}
522530

523-
$chat_id = null;
524-
$message_id = null;
525-
$edited_message_id = null;
526-
$channel_post_id = null;
527-
$edited_channel_post_id = null;
528-
$inline_query_id = null;
529-
$chosen_inline_result_id = null;
530-
$callback_query_id = null;
531-
$shipping_query_id = null;
532-
$pre_checkout_query_id = null;
533-
$poll_id = null;
534-
$poll_answer_poll_id = null;
531+
$chat_id = null;
532+
$message_id = null;
533+
$edited_message_id = null;
534+
$channel_post_id = null;
535+
$edited_channel_post_id = null;
536+
$inline_query_id = null;
537+
$chosen_inline_result_id = null;
538+
$callback_query_id = null;
539+
$shipping_query_id = null;
540+
$pre_checkout_query_id = null;
541+
$poll_id = null;
542+
$poll_answer_poll_id = null;
543+
$my_chat_member_updated_id = null;
544+
$chat_member_updated_id = null;
535545

536546
if (($message = $update->getMessage()) && self::insertMessageRequest($message)) {
537547
$chat_id = $message->getChat()->getId();
@@ -559,6 +569,10 @@ public static function insertRequest(Update $update): bool
559569
$poll_id = $poll->getId();
560570
} elseif (($poll_answer = $update->getPollAnswer()) && self::insertPollAnswerRequest($poll_answer)) {
561571
$poll_answer_poll_id = $poll_answer->getPollId();
572+
} elseif (($my_chat_member = $update->getMyChatMember()) && self::insertChatMemberUpdatedRequest($my_chat_member)) {
573+
$my_chat_member_updated_id = self::$pdo->lastInsertId();
574+
} elseif (($chat_member = $update->getChatMember()) && self::insertChatMemberUpdatedRequest($chat_member)) {
575+
$chat_member_updated_id = self::$pdo->lastInsertId();
562576
} else {
563577
return false;
564578
}
@@ -576,7 +590,9 @@ public static function insertRequest(Update $update): bool
576590
$shipping_query_id,
577591
$pre_checkout_query_id,
578592
$poll_id,
579-
$poll_answer_poll_id
593+
$poll_answer_poll_id,
594+
$my_chat_member_updated_id,
595+
$chat_member_updated_id
580596
);
581597
}
582598

@@ -918,6 +934,55 @@ public static function insertPollAnswerRequest(PollAnswer $poll_answer): bool
918934
}
919935
}
920936

937+
/**
938+
* Insert chat member updated request into database
939+
*
940+
* @param ChatMemberUpdated $chat_member_updated
941+
*
942+
* @return bool If the insert was successful
943+
* @throws TelegramException
944+
*/
945+
public static function insertChatMemberUpdatedRequest(ChatMemberUpdated $chat_member_updated): bool
946+
{
947+
if (!self::isDbConnected()) {
948+
return false;
949+
}
950+
951+
try {
952+
$sth = self::$pdo->prepare('
953+
INSERT INTO `' . TB_CHAT_MEMBER_UPDATED . '`
954+
(`chat_id`, `user_id`, `date`, `old_chat_member`, `new_chat_member`, `invite_link`, `created_at`)
955+
VALUES
956+
(:chat_id, :user_id, :date, :old_chat_member, :new_chat_member, :invite_link, :created_at)
957+
');
958+
959+
$date = self::getTimestamp();
960+
$chat_id = null;
961+
$user_id = null;
962+
963+
if ($chat = $chat_member_updated->getChat()) {
964+
$chat_id = $chat->getId();
965+
self::insertChat($chat, $date);
966+
}
967+
if ($user = $chat_member_updated->getFrom()) {
968+
$user_id = $user->getId();
969+
self::insertUser($user, $date);
970+
}
971+
972+
$sth->bindValue(':chat_id', $chat_id);
973+
$sth->bindValue(':user_id', $user_id);
974+
$sth->bindValue(':date', self::getTimestamp($chat_member_updated->getDate()));
975+
$sth->bindValue(':old_chat_member', $chat_member_updated->getOldChatMember());
976+
$sth->bindValue(':new_chat_member', $chat_member_updated->getNewChatMember());
977+
$sth->bindValue(':invite_link', $chat_member_updated->getInviteLink());
978+
$sth->bindValue(':created_at', $date);
979+
980+
return $sth->execute();
981+
} catch (PDOException $e) {
982+
throw new TelegramException($e->getMessage());
983+
}
984+
}
985+
921986
/**
922987
* Insert Message request in db
923988
*
@@ -998,17 +1063,19 @@ public static function insertMessageRequest(Message $message): bool
9981063
`audio`, `document`, `animation`, `game`, `photo`, `sticker`, `video`, `voice`, `video_note`, `caption`, `contact`,
9991064
`location`, `venue`, `poll`, `dice`, `new_chat_members`, `left_chat_member`,
10001065
`new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`,
1001-
`supergroup_chat_created`, `channel_chat_created`, `migrate_to_chat_id`, `migrate_from_chat_id`,
1002-
`pinned_message`, `invoice`, `successful_payment`, `connected_website`, `passport_data`, `proximity_alert_triggered`, `reply_markup`
1066+
`supergroup_chat_created`, `channel_chat_created`, `message_auto_delete_timer_changed`, `migrate_to_chat_id`, `migrate_from_chat_id`,
1067+
`pinned_message`, `invoice`, `successful_payment`, `connected_website`, `passport_data`, `proximity_alert_triggered`,
1068+
`voice_chat_started`, `voice_chat_ended`, `voice_chat_participants_invited`, `reply_markup`
10031069
) VALUES (
10041070
:message_id, :user_id, :chat_id, :sender_chat_id, :date, :forward_from, :forward_from_chat, :forward_from_message_id,
10051071
:forward_signature, :forward_sender_name, :forward_date,
10061072
:reply_to_chat, :reply_to_message, :via_bot, :edit_date, :media_group_id, :author_signature, :text, :entities, :caption_entities,
10071073
:audio, :document, :animation, :game, :photo, :sticker, :video, :voice, :video_note, :caption, :contact,
10081074
:location, :venue, :poll, :dice, :new_chat_members, :left_chat_member,
10091075
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created,
1010-
:supergroup_chat_created, :channel_chat_created, :migrate_to_chat_id, :migrate_from_chat_id,
1011-
:pinned_message, :invoice, :successful_payment, :connected_website, :passport_data, :proximity_alert_triggered, :reply_markup
1076+
:supergroup_chat_created, :channel_chat_created, :message_auto_delete_timer_changed, :migrate_to_chat_id, :migrate_from_chat_id,
1077+
:pinned_message, :invoice, :successful_payment, :connected_website, :passport_data, :proximity_alert_triggered,
1078+
:voice_chat_started, :voice_chat_ended, :voice_chat_participants_invited, :reply_markup
10121079
)
10131080
');
10141081

@@ -1072,6 +1139,7 @@ public static function insertMessageRequest(Message $message): bool
10721139
$sth->bindValue(':group_chat_created', $message->getGroupChatCreated());
10731140
$sth->bindValue(':supergroup_chat_created', $message->getSupergroupChatCreated());
10741141
$sth->bindValue(':channel_chat_created', $message->getChannelChatCreated());
1142+
$sth->bindValue(':message_auto_delete_timer_changed', $message->getMessageAutoDeleteTimerChanged());
10751143
$sth->bindValue(':migrate_to_chat_id', $message->getMigrateToChatId());
10761144
$sth->bindValue(':migrate_from_chat_id', $message->getMigrateFromChatId());
10771145
$sth->bindValue(':pinned_message', $message->getPinnedMessage());
@@ -1080,6 +1148,9 @@ public static function insertMessageRequest(Message $message): bool
10801148
$sth->bindValue(':connected_website', $message->getConnectedWebsite());
10811149
$sth->bindValue(':passport_data', $message->getPassportData());
10821150
$sth->bindValue(':proximity_alert_triggered', $message->getProximityAlertTriggered());
1151+
$sth->bindValue(':voice_chat_started', $message->getVoiceChatStarted());
1152+
$sth->bindValue(':voice_chat_ended', $message->getVoiceChatEnded());
1153+
$sth->bindValue(':voice_chat_participants_invited', $message->getVoiceChatParticipantsInvited());
10831154
$sth->bindValue(':reply_markup', $message->getReplyMarkup());
10841155

10851156
return $sth->execute();

structure.sql

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,6 @@ CREATE TABLE IF NOT EXISTS `user_chat` (
3838
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
3939
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
4040

41-
CREATE TABLE IF NOT EXISTS `chat_invite_link` (
42-
`id` BIGINT UNSIGNED COMMENT 'Unique identifier for this entry',
43-
`invite_link` VARCHAR(2083) CHARACTER SET 'ascii' COLLATE 'ascii_general_ci' NOT NULL COMMENT 'The invite link. If the link was created by another chat administrator, then the second part of the link will be replaced with “…”',
44-
# IE7 has a 2083 character limit for HTTP GET operations: http://support.microsoft.com/kb/208427
45-
`creator_id` BIGINT NOT NULL COMMENT 'Creator of the link',
46-
`is_primary` BOOLEAN NOT NULL COMMENT 'True, if the link is primary',
47-
`is_revoked` BOOLEAN NOT NULL COMMENT 'True, if the link is revoked',
48-
`expire_date` TIMESTAMP NULL COMMENT 'Point in time (Unix TIMESTAMP) when the link will expire or has been expired',
49-
`member_limit` MEDIUMINT UNSIGNED NULL COMMENT 'Maximum number of users that can be members of the chat simultaneously after joining the chat via this invite link; 1-99999',
50-
51-
PRIMARY KEY (`id`),
52-
53-
FOREIGN KEY (`creator_id`) REFERENCES `user` (`id`)
54-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
55-
5641
CREATE TABLE IF NOT EXISTS `inline_query` (
5742
`id` bigint UNSIGNED COMMENT 'Unique identifier for this query',
5843
`user_id` bigint NULL COMMENT 'Unique user identifier',
@@ -259,18 +244,18 @@ CREATE TABLE IF NOT EXISTS `poll_answer` (
259244
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
260245

261246
CREATE TABLE IF NOT EXISTS `chat_member_updated` (
262-
`id` BIGINT UNSIGNED COMMENT 'Unique identifier for this entry',
247+
`id` BIGINT UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry',
263248
`chat_id` BIGINT NOT NULL COMMENT 'Chat the user belongs to',
264249
`user_id` BIGINT NOT NULL COMMENT 'Performer of the action, which resulted in the change',
265250
`date` TIMESTAMP NOT NULL COMMENT 'Date the change was done in Unix time',
266251
`old_chat_member` TEXT NOT NULL COMMENT 'Previous information about the chat member',
267252
`new_chat_member` TEXT NOT NULL COMMENT 'New information about the chat member',
268-
`chat_invite_link_id` BIGINT UNSIGNED NULL COMMENT 'Chat invite link, which was used by the user to join the chat; for joining by invite link events only',
253+
`invite_link` TEXT NULL COMMENT 'Chat invite link, which was used by the user to join the chat; for joining by invite link events only',
254+
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
269255

270256
PRIMARY KEY (`id`),
271257
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`),
272-
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
273-
FOREIGN KEY (`chat_invite_link_id`) REFERENCES `chat_invite_link` (`id`)
258+
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
274259
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
275260

276261
CREATE TABLE IF NOT EXISTS `telegram_update` (
@@ -287,8 +272,8 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
287272
`pre_checkout_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'New incoming pre-checkout query. Contains full information about checkout',
288273
`poll_id` bigint UNSIGNED DEFAULT NULL COMMENT 'New poll state. Bots receive only updates about polls, which are sent or stopped by the bot',
289274
`poll_answer_poll_id` bigint UNSIGNED DEFAULT NULL COMMENT 'A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself.',
290-
`my_chat_member_update_id` BIGINT UNSIGNED NULL COMMENT 'The bot''s chat member status was updated in a chat. For private chats, this update is received only when the bot is blocked or unblocked by the user.',
291-
`chat_member_update_id` BIGINT UNSIGNED NULL COMMENT 'A chat member''s status was updated in a chat. The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowed_updates to receive these updates.',
275+
`my_chat_member_updated_id` BIGINT UNSIGNED NULL COMMENT 'The bot''s chat member status was updated in a chat. For private chats, this update is received only when the bot is blocked or unblocked by the user.',
276+
`chat_member_updated_id` BIGINT UNSIGNED NULL COMMENT 'A chat member''s status was updated in a chat. The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowed_updates to receive these updates.',
292277

293278
PRIMARY KEY (`id`),
294279
KEY `message_id` (`message_id`),
@@ -303,8 +288,8 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
303288
KEY `pre_checkout_query_id` (`pre_checkout_query_id`),
304289
KEY `poll_id` (`poll_id`),
305290
KEY `poll_answer_poll_id` (`poll_answer_poll_id`),
306-
KEY `my_chat_member_update_id` (`my_chat_member_update_id`),
307-
KEY `chat_member_update_id` (`chat_member_update_id`),
291+
KEY `my_chat_member_updated_id` (`my_chat_member_updated_id`),
292+
KEY `chat_member_updated_id` (`chat_member_updated_id`),
308293

309294
FOREIGN KEY (`chat_id`, `message_id`) REFERENCES `message` (`chat_id`, `id`),
310295
FOREIGN KEY (`edited_message_id`) REFERENCES `edited_message` (`id`),
@@ -317,8 +302,8 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
317302
FOREIGN KEY (`pre_checkout_query_id`) REFERENCES `pre_checkout_query` (`id`),
318303
FOREIGN KEY (`poll_id`) REFERENCES `poll` (`id`),
319304
FOREIGN KEY (`poll_answer_poll_id`) REFERENCES `poll_answer` (`poll_id`),
320-
FOREIGN KEY (`my_chat_member_update_id`) REFERENCES `chat_member_updated` (`id`),
321-
FOREIGN KEY (`chat_member_update_id`) REFERENCES `chat_member_updated` (`id`)
305+
FOREIGN KEY (`my_chat_member_updated_id`) REFERENCES `chat_member_updated` (`id`),
306+
FOREIGN KEY (`chat_member_updated_id`) REFERENCES `chat_member_updated` (`id`)
322307
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
323308

324309
CREATE TABLE IF NOT EXISTS `conversation` (

0 commit comments

Comments
 (0)