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

Prevent serving duplicate updates, throw exception instead

parent e9e3588d
No related merge requests found
......@@ -173,29 +173,37 @@ class DB
* Fetch update(s) from DB
*
* @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
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function selectTelegramUpdate($limit = null)
public static function selectTelegramUpdate($limit = null, $id = null)
{
if (!self::isDbConnected()) {
return false;
}
try {
$sql = '
SELECT `id`
FROM `' . TB_TELEGRAM_UPDATE . '`
ORDER BY `id` DESC
';
$sql = 'SELECT `id` FROM `' . TB_TELEGRAM_UPDATE . '`';
if ($id !== null) {
$sql .= ' WHERE `id` = :id';
}
$sql.= ' ORDER BY `id` DESC';
if ($limit !== null) {
$sql .= 'LIMIT :limit';
$sql .= ' LIMIT :limit';
}
$sth = self::$pdo->prepare($sql);
$sth->bindParam(':limit', $limit, PDO::PARAM_INT);
if ($id !== null) {
$sth->bindParam(':id', $id, PDO::PARAM_INT);
}
$sth->execute();
return $sth->fetchAll(PDO::FETCH_ASSOC);
......@@ -474,6 +482,10 @@ class DB
$update_id = $update->getUpdateId();
$update_type = $update->getUpdateType();
if (count(self::selectTelegramUpdate(1, $update->getUpdateId())) == 1) {
throw new TelegramException('Duplicate update received!');
}
if ($update_type === 'message') {
$message = $update->getMessage();
......@@ -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
');
$date = self::getTimestamp(time());
$date = self::getTimestamp();
$date_minute = self::getTimestamp(strtotime('-1 minute'));
$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