Add API to retrieve posts (with hardcoded max limit 10k)

This commit is contained in:
Bofh 2022-12-24 02:41:32 +01:00
parent 26871ff539
commit 52f99b064b
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,3 @@
<?php chdir('../../../../../') ?>
<?php require 'base.php' ?>
<?php mod_php() ?>

View File

@ -0,0 +1,41 @@
<?php
instance_config();
$_ = function() {
$MAX_LIMIT = 10000;
$start = arg('*get.start', null, 'valid_mastodon_account_id');
$limit = arg('i:get.limit', $MAX_LIMIT);
if ($limit > $MAX_LIMIT)
$limit = $MAX_LIMIT;
$fields = "id, TRIM(CONCAT(spoiler_text, ' ', text)) AS post, created_at, updated_at";
$sql_all = '
SELECT '.$fields.', reblog_of_id
FROM statuses
WHERE id >= '.$start.'
ORDER BY id ASC LIMIT '.$limit.'
';
$pg = new PgDatabase();
if (!$pg->is_ok())
return apiresult(['error' => 'Unknown database error ocurred'], 500);
$objects = $pg->fetch_all($sql_all);
foreach ($objects as &$object) {
if ($object['reblog_of_id'] !== null)
$object['original'] = $pg->fetch("SELECT $fields FROM statuses WHERE id = {$object['reblog_of_id']} LIMIT 1");
unset($object['reblog_of_id']);
}
$delay = $pg->fetch('SELECT COUNT(1) FROM statuses WHERE id > '.$objects[count($objects)-1]['id']);
$delay = intval($delay['count']);
if ($delay > $limit)
$delay = ['total' => $delay, 'ticks' => intval($delay / $limit)];
$pg->close();
return apiresult([
'items' => $objects,
'limit' => $limit,
'delay' => $delay,
]);
};
$_();