Commit e2f62f2b authored by Jack'lul's avatar Jack'lul

Prevent serving duplicate updates, throw exception instead

parent e9e3588d
...@@ -173,29 +173,37 @@ class DB ...@@ -173,29 +173,37 @@ class DB
* Fetch update(s) from DB * Fetch update(s) from DB
* *
* @param int $limit Limit the number of updates to fetch * @param int $limit Limit the number of updates to fetch
* @param int $id Check for unique update id
* *
* @return array|bool Fetched data or false if not connected * @return array|bool Fetched data or false if not connected
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public static function selectTelegramUpdate($limit = null) public static function selectTelegramUpdate($limit = null, $id = null)
{ {
if (!self::isDbConnected()) { if (!self::isDbConnected()) {
return false; return false;
} }
try { try {
$sql = ' $sql = 'SELECT `id` FROM `' . TB_TELEGRAM_UPDATE . '`';
SELECT `id`
FROM `' . TB_TELEGRAM_UPDATE . '` if ($id !== null) {
ORDER BY `id` DESC $sql .= ' WHERE `id` = :id';
'; }
$sql.= ' ORDER BY `id` DESC';
if ($limit !== null) { if ($limit !== null) {
$sql .= 'LIMIT :limit'; $sql .= ' LIMIT :limit';
} }
$sth = self::$pdo->prepare($sql); $sth = self::$pdo->prepare($sql);
$sth->bindParam(':limit', $limit, PDO::PARAM_INT); $sth->bindParam(':limit', $limit, PDO::PARAM_INT);
if ($id !== null) {
$sth->bindParam(':id', $id, PDO::PARAM_INT);
}
$sth->execute(); $sth->execute();
return $sth->fetchAll(PDO::FETCH_ASSOC); return $sth->fetchAll(PDO::FETCH_ASSOC);
...@@ -474,6 +482,10 @@ class DB ...@@ -474,6 +482,10 @@ class DB
$update_id = $update->getUpdateId(); $update_id = $update->getUpdateId();
$update_type = $update->getUpdateType(); $update_type = $update->getUpdateType();
if (count(self::selectTelegramUpdate(1, $update->getUpdateId())) == 1) {
throw new TelegramException('Duplicate update received!');
}
if ($update_type === 'message') { if ($update_type === 'message') {
$message = $update->getMessage(); $message = $update->getMessage();
...@@ -1089,7 +1101,7 @@ class DB ...@@ -1089,7 +1101,7 @@ class DB
(SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `chat_id` = :chat_id AND `created_at` >= :date_minute) as LIMIT_PER_MINUTE (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `chat_id` = :chat_id AND `created_at` >= :date_minute) as LIMIT_PER_MINUTE
'); ');
$date = self::getTimestamp(time()); $date = self::getTimestamp();
$date_minute = self::getTimestamp(strtotime('-1 minute')); $date_minute = self::getTimestamp(strtotime('-1 minute'));
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR); $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment