From a4902c069fa0cd629e8ef384d52c5292512fa383 Mon Sep 17 00:00:00 2001 From: Alexander Pankratov Date: Fri, 26 Jan 2024 19:39:12 +0100 Subject: [PATCH] Feat: allow to get single message by id --- README.md | 2 ++ app/Controller/BaseFeedController.php | 8 +++++++- app/Controller/JsonController.php | 3 ++- app/Controller/RSSController.php | 2 +- public/index.html | 2 ++ 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ce28ca9..29b6375 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ docker compose up -d Maximum: 100 posts * Pagination: https://tg.i-c-a.su/json/breakingmash?page=2 +* Get one message: https://tg.i-c-a.su/json/breakingmash?id=10738&limit=1 ### RSS * URL: https://tg.i-c-a.su/rss/breakingmash @@ -90,6 +91,7 @@ docker compose up -d Maximum: 100 posts * Pagination: https://tg.i-c-a.su/rss/breakingmash/2 +* Get one message: https://tg.i-c-a.su/rss/breakingmash?id=10738&limit=1 ### Media * https://tg.i-c-a.su/media/breakingmash/10738/preview diff --git a/app/Controller/BaseFeedController.php b/app/Controller/BaseFeedController.php index 3367a6b..dfeb3d7 100644 --- a/app/Controller/BaseFeedController.php +++ b/app/Controller/BaseFeedController.php @@ -33,9 +33,14 @@ protected function getLimit(Request $request): int return max(1, min($limit, self::LIMIT_MAX)); } + protected function getId(Request $request): int + { + return (int)($request->getQueryParameter('id') ?? 0); + } + abstract protected function getHeaders(): array; - protected function getMessages(string $channel, int $page, int $limit): array + protected function getMessages(string $channel, int $page, int $limit, int $message_id = 0): array { $isChannel = in_array($this->client->getInfo($channel)['type'], ['channel', 'supergroup']); @@ -44,6 +49,7 @@ protected function getMessages(string $channel, int $page, int $limit): array 'peer' => $channel, 'limit' => $limit, 'add_offset' => ($page - 1) * $limit, + 'offset_id' => $message_id ? ($message_id + 1) : 0, ] ); if ($isChannel) { diff --git a/app/Controller/JsonController.php b/app/Controller/JsonController.php index 6efe4b3..d30bb29 100644 --- a/app/Controller/JsonController.php +++ b/app/Controller/JsonController.php @@ -26,7 +26,8 @@ public function handleRequest(Request $request): Response $this->getMessages( $request->getAttribute('channel'), $this->getPage($request), - $this->getLimit($request) + $this->getLimit($request), + $this->getId($request) ), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE ) diff --git a/app/Controller/RSSController.php b/app/Controller/RSSController.php index 4ba7f31..ef0ba76 100644 --- a/app/Controller/RSSController.php +++ b/app/Controller/RSSController.php @@ -31,7 +31,7 @@ protected function getContent(Request $request): string { $channel = $request->getAttribute('channel'); $messages = new Messages( - $this->getMessages($channel, $this->getPage($request), $this->getLimit($request)), + $this->getMessages($channel, $this->getPage($request), $this->getLimit($request), $this->getId($request)), $this->client, $channel ); diff --git a/public/index.html b/public/index.html index 6a6af14..83cf6d0 100644 --- a/public/index.html +++ b/public/index.html @@ -75,6 +75,7 @@

JSON


Maximum posts - 100 posts
  • Pagination: https://tg.i-c-a.su/json/breakingmash?page=2
  • +
  • One post: https://tg.i-c-a.su/json/breakingmash/?id=10738&limit=1
  • RSS

    Media