Skip to content

Commit

Permalink
Feat: allow to get single message by id
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Jan 26, 2024
1 parent dcf40fb commit a4902c0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion app/Controller/BaseFeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion app/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion app/Controller/RSSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
2 changes: 2 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ <h3>JSON</h3>
<br />Maximum posts - 100 posts
</li>
<li>Pagination: <a href="/json/breakingmash?page=2" target="_blank" rel="noreferrer">https://tg.i-c-a.su/json/breakingmash?page=2</a></li>
<li>One post: <a href="/json/breakingmash/?id=10738&limit=1" target="_blank" rel="noreferrer">https://tg.i-c-a.su/json/breakingmash/?id=10738&limit=1</a></li>
</ul>
<h3>RSS</h3>
<ul>
Expand All @@ -83,6 +84,7 @@ <h3>RSS</h3>
<br />Maximum posts - 100 posts
</li>
<li>Pagination: <a href="/rss/breakingmash/2" target="_blank" rel="noreferrer">https://tg.i-c-a.su/rss/breakingmash/2</a></li>
<li>One post: <a href="/rss/breakingmash/?id=10738&limit=1" target="_blank" rel="noreferrer">https://tg.i-c-a.su/rss/breakingmash/?id=10738&limit=1</a></li>
</ul>
<h3>Media</h3>
<ul>
Expand Down

0 comments on commit a4902c0

Please sign in to comment.