Skip to content

Commit 97810c8

Browse files
committed
Add DB table for ChatJoinRequest
1 parent c96fd90 commit 97810c8

File tree

3 files changed

+105
-6
lines changed

3 files changed

+105
-6
lines changed

src/DB.php

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ protected static function insertTelegramUpdate(
348348
?string $poll_id = null,
349349
?string $poll_answer_poll_id = null,
350350
?string $my_chat_member_updated_id = null,
351-
?string $chat_member_updated_id = null
351+
?string $chat_member_updated_id = null,
352+
?string $chat_join_request_id = null
352353
): ?bool {
353354
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) {
354355
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');
@@ -361,9 +362,19 @@ protected static function insertTelegramUpdate(
361362
try {
362363
$sth = self::$pdo->prepare('
363364
INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '`
364-
(`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`)
365-
VALUES
366-
(: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)
365+
(
366+
`id`, `chat_id`, `message_id`, `edited_message_id`,
367+
`channel_post_id`, `edited_channel_post_id`, `inline_query_id`, `chosen_inline_result_id`,
368+
`callback_query_id`, `shipping_query_id`, `pre_checkout_query_id`,
369+
`poll_id`, `poll_answer_poll_id`, `my_chat_member_updated_id`, `chat_member_updated_id`,
370+
`chat_join_request_id`
371+
) VALUES (
372+
:id, :chat_id, :message_id, :edited_message_id,
373+
:channel_post_id, :edited_channel_post_id, :inline_query_id, :chosen_inline_result_id,
374+
:callback_query_id, :shipping_query_id, :pre_checkout_query_id,
375+
:poll_id, :poll_answer_poll_id, :my_chat_member_updated_id, :chat_member_updated_id,
376+
:chat_join_request_id
377+
)
367378
');
368379

369380
$sth->bindValue(':id', $update_id);
@@ -381,6 +392,7 @@ protected static function insertTelegramUpdate(
381392
$sth->bindValue(':poll_answer_poll_id', $poll_answer_poll_id);
382393
$sth->bindValue(':my_chat_member_updated_id', $my_chat_member_updated_id);
383394
$sth->bindValue(':chat_member_updated_id', $chat_member_updated_id);
395+
$sth->bindValue(':chat_join_request_id', $chat_join_request_id);
384396

385397
return $sth->execute();
386398
} catch (PDOException $e) {
@@ -547,6 +559,7 @@ public static function insertRequest(Update $update): bool
547559
$poll_answer_poll_id = null;
548560
$my_chat_member_updated_id = null;
549561
$chat_member_updated_id = null;
562+
$chat_join_request_id = null;
550563

551564
if (($message = $update->getMessage()) && self::insertMessageRequest($message)) {
552565
$chat_id = $message->getChat()->getId();
@@ -578,6 +591,8 @@ public static function insertRequest(Update $update): bool
578591
$my_chat_member_updated_id = self::$pdo->lastInsertId();
579592
} elseif (($chat_member = $update->getChatMember()) && self::insertChatMemberUpdatedRequest($chat_member)) {
580593
$chat_member_updated_id = self::$pdo->lastInsertId();
594+
} elseif (($chat_join_request = $update->getChatJoinRequest()) && self::insertChatJoinRequestRequest($chat_join_request)) {
595+
$chat_join_request_id = self::$pdo->lastInsertId();
581596
} else {
582597
return false;
583598
}
@@ -597,7 +612,8 @@ public static function insertRequest(Update $update): bool
597612
$poll_id,
598613
$poll_answer_poll_id,
599614
$my_chat_member_updated_id,
600-
$chat_member_updated_id
615+
$chat_member_updated_id,
616+
$chat_join_request_id
601617
);
602618
}
603619

@@ -989,6 +1005,54 @@ public static function insertChatMemberUpdatedRequest(ChatMemberUpdated $chat_me
9891005
}
9901006
}
9911007

1008+
/**
1009+
* Insert chat join request into database
1010+
*
1011+
* @param ChatJoinRequest $chat_join_request
1012+
*
1013+
* @return bool If the insert was successful
1014+
* @throws TelegramException
1015+
*/
1016+
public static function insertChatJoinRequestRequest(ChatJoinRequest $chat_join_request): bool
1017+
{
1018+
if (!self::isDbConnected()) {
1019+
return false;
1020+
}
1021+
1022+
try {
1023+
$sth = self::$pdo->prepare('
1024+
INSERT INTO `' . TB_CHAT_MEMBER_UPDATED . '`
1025+
(`chat_id`, `user_id`, `date`, `bio`, `invite_link`, `created_at`)
1026+
VALUES
1027+
(:chat_id, :user_id, :date, :bio, :invite_link, :created_at)
1028+
');
1029+
1030+
$date = self::getTimestamp();
1031+
$chat_id = null;
1032+
$user_id = null;
1033+
1034+
if ($chat = $chat_join_request->getChat()) {
1035+
$chat_id = $chat->getId();
1036+
self::insertChat($chat, $date);
1037+
}
1038+
if ($user = $chat_join_request->getFrom()) {
1039+
$user_id = $user->getId();
1040+
self::insertUser($user, $date);
1041+
}
1042+
1043+
$sth->bindValue(':chat_id', $chat_id);
1044+
$sth->bindValue(':user_id', $user_id);
1045+
$sth->bindValue(':date', self::getTimestamp($chat_join_request->getDate()));
1046+
$sth->bindValue(':bio', $chat_join_request->getBio());
1047+
$sth->bindValue(':invite_link', $chat_join_request->getInviteLink());
1048+
$sth->bindValue(':created_at', $date);
1049+
1050+
return $sth->execute();
1051+
} catch (PDOException $e) {
1052+
throw new TelegramException($e->getMessage());
1053+
}
1054+
}
1055+
9921056
/**
9931057
* Insert Message request in db
9941058
*

structure.sql

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,21 @@ CREATE TABLE IF NOT EXISTS `chat_member_updated` (
260260
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
261261
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
262262

263+
CREATE TABLE IF NOT EXISTS `chat_join_request` (
264+
`id` BIGINT UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry',
265+
`chat_id` BIGINT NOT NULL COMMENT 'Chat to which the request was sent',
266+
`user_id` BIGINT NOT NULL COMMENT 'User that sent the join request',
267+
`date` TIMESTAMP NOT NULL COMMENT 'Date the request was sent in Unix time',
268+
`bio` TEXT NOT NULL COMMENT 'Optional. Bio of the user',
269+
`invite_link` TEXT NULL COMMENT 'Optional. Chat invite link that was used by the user to send the join request',
270+
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
271+
272+
PRIMARY KEY (`id`),
273+
274+
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`),
275+
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
276+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
277+
263278
CREATE TABLE IF NOT EXISTS `telegram_update` (
264279
`id` bigint UNSIGNED COMMENT 'Update''s unique identifier',
265280
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Unique chat identifier',
@@ -276,6 +291,7 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
276291
`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.',
277292
`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.',
278293
`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.',
294+
`chat_join_request_id` BIGINT UNSIGNED NULL COMMENT 'A request to join the chat has been sent',
279295

280296
PRIMARY KEY (`id`),
281297
KEY `message_id` (`message_id`),
@@ -292,6 +308,7 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
292308
KEY `poll_answer_poll_id` (`poll_answer_poll_id`),
293309
KEY `my_chat_member_updated_id` (`my_chat_member_updated_id`),
294310
KEY `chat_member_updated_id` (`chat_member_updated_id`),
311+
KEY `chat_join_request_id` (`chat_join_request_id`),
295312

296313
FOREIGN KEY (`chat_id`, `message_id`) REFERENCES `message` (`chat_id`, `id`),
297314
FOREIGN KEY (`edited_message_id`) REFERENCES `edited_message` (`id`),
@@ -305,7 +322,8 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
305322
FOREIGN KEY (`poll_id`) REFERENCES `poll` (`id`),
306323
FOREIGN KEY (`poll_answer_poll_id`) REFERENCES `poll_answer` (`poll_id`),
307324
FOREIGN KEY (`my_chat_member_updated_id`) REFERENCES `chat_member_updated` (`id`),
308-
FOREIGN KEY (`chat_member_updated_id`) REFERENCES `chat_member_updated` (`id`)
325+
FOREIGN KEY (`chat_member_updated_id`) REFERENCES `chat_member_updated` (`id`),
326+
FOREIGN KEY (`chat_join_request_id`) REFERENCES `chat_join_request` (`id`)
309327
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
310328

311329
CREATE TABLE IF NOT EXISTS `conversation` (
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CREATE TABLE IF NOT EXISTS `chat_join_request` (
2+
`id` BIGINT UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry',
3+
`chat_id` BIGINT NOT NULL COMMENT 'Chat to which the request was sent',
4+
`user_id` BIGINT NOT NULL COMMENT 'User that sent the join request',
5+
`date` TIMESTAMP NOT NULL COMMENT 'Date the request was sent in Unix time',
6+
`bio` TEXT NOT NULL COMMENT 'Optional. Bio of the user',
7+
`invite_link` TEXT NULL COMMENT 'Optional. Chat invite link that was used by the user to send the join request',
8+
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',
9+
10+
PRIMARY KEY (`id`),
11+
12+
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`),
13+
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
14+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
15+
16+
ALTER TABLE `telegram_update` ADD COLUMN `chat_join_request_id` BIGINT UNSIGNED NULL COMMENT 'A request to join the chat has been sent';
17+
ALTER TABLE `telegram_update` ADD FOREIGN KEY (`chat_join_request_id`) REFERENCES `chat_join_request` (`id`);

0 commit comments

Comments
 (0)