Skip to content

Commit 5123bc1

Browse files
committed
fix mastodon pagination
The pagination was not working as intended because I misunderstood how minId works
1 parent d74f58a commit 5123bc1

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

backend/src/MastodonFetcher.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,19 @@ protected function getLatestImportedStatusId(string $account): ?string
142142
*
143143
* @param string $instance The Mastodon instance URL
144144
* @param string $accountId The account ID
145-
* @param string|null $sinceId Only fetch statuses newer than this ID
145+
* @param string|null $minId Only fetch statuses newer than this ID
146146
* @return array The statuses
147147
*/
148-
protected function fetchAllStatuses(string $instance, string $accountId, ?string $sinceId = null): array
148+
protected function fetchAllStatuses(string $instance, string $accountId, ?string $minId = null): array
149149
{
150150
$allStatuses = [];
151151
$pageSize = 40;
152-
$maxId = null;
153152
$page = 1;
154-
$maxPages = 25;
153+
$maxPages = 50; // run-away protection, if no URL in the last 2000 posts it's a problem
155154

156-
$this->app->log()->info("Fetching posts in batches of $pageSize...");
157155

158156
while ($page <= $maxPages) {
159-
$url = $this->buildStatusesUrl($instance, $accountId, $pageSize, $maxId, $sinceId);
157+
$url = $this->buildStatusesUrl($instance, $accountId, $pageSize, $minId);
160158
$response = $this->makeHttpRequest($url);
161159

162160
if (!$response) {
@@ -171,14 +169,22 @@ protected function fetchAllStatuses(string $instance, string $accountId, ?string
171169
$allStatuses = array_merge($allStatuses, $statuses);
172170
$this->app->log()->info("Fetched page $page with " . count($statuses) . " posts (total: " . count($allStatuses) . ")");
173171

172+
$firstStatus = $statuses[0];
173+
$lastStatus = end($statuses);
174+
$this->app->log()->notice("Status {fid} {fdate} <- {lid} {ldate}", [
175+
'fid' => $firstStatus->id,
176+
'fdate' => date('Y-m-d H:i:s', strtotime($firstStatus->created_at)),
177+
'lid' => $lastStatus->id,
178+
'ldate' => date('Y-m-d H:i:s', strtotime($lastStatus->created_at))
179+
]);
180+
174181
// Check if we've reached the end
175182
if (count($statuses) < $pageSize) {
176183
break;
177184
}
178185

179186
// Get the ID of the last status for pagination
180-
$lastStatus = end($statuses);
181-
$maxId = $lastStatus->id;
187+
$minId = $firstStatus->id;
182188
$page++;
183189

184190
// Small delay to avoid rate limiting
@@ -194,20 +200,15 @@ protected function fetchAllStatuses(string $instance, string $accountId, ?string
194200
* @param string $instance The Mastodon instance URL
195201
* @param string $accountId The account ID
196202
* @param int $limit Number of statuses to fetch
197-
* @param string|null $maxId Only fetch statuses older than this ID
198-
* @param string|null $sinceId Only fetch statuses newer than this ID
203+
* @param string|null $minId Fetch statuses immediately newer than this ID
199204
* @return string The URL
200205
*/
201-
protected function buildStatusesUrl(string $instance, string $accountId, int $limit, ?string $maxId = null, ?string $sinceId = null): string
206+
protected function buildStatusesUrl(string $instance, string $accountId, int $limit, ?string $minId = null): string
202207
{
203208
$url = "$instance/api/v1/accounts/$accountId/statuses?limit=$limit&exclude_reblogs=true";
204209

205-
if ($maxId) {
206-
$url .= "&max_id=$maxId";
207-
}
208-
209-
if ($sinceId) {
210-
$url .= "&min_id=$sinceId";
210+
if ($minId) {
211+
$url .= "&min_id=$minId";
211212
}
212213

213214
return $url;

0 commit comments

Comments
 (0)