federator/php/federator/api/fedusers/outbox.php
Sascha Nitsch 4cc9cfdc8c converted from date field to timestamp
fixed reloading logic on paging
2025-06-11 03:21:19 +02:00

101 lines
2.9 KiB
PHP

<?php
/**
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
* SPDX-License-Identifier: GPL-3.0-or-later
*
* @author Sascha Nitsch (grumpydeveloper)
**/
namespace Federator\Api\FedUsers;
/**
* handle activitypub outbox requests
*/
class Outbox implements \Federator\Api\FedUsers\FedUsersInterface
{
/**
* main instance
*
* @var \Federator\Api $main
*/
private $main;
/**
* constructor
* @param \Federator\Api $main main instance
*/
public function __construct($main)
{
$this->main = $main;
}
/**
* handle get call
*
* @param string|null $_user user to fetch outbox for
* @return string|false response
*/
public function get($_user)
{
if (!isset($_user)) {
return false;
}
$dbh = $this->main->getDatabase();
$cache = $this->main->getCache();
$connector = $this->main->getConnector();
// get user
$user = \Federator\DIO\User::getUserByName(
$dbh,
$_user,
$connector,
$cache
);
if ($user->id === null) {
return false;
}
// get posts from user
$outbox = new \Federator\Data\ActivityPub\Common\Outbox();
$min = intval($this->main->extractFromURI('min', '0'), 10);
$max = intval($this->main->extractFromURI('max', '0'), 10);
$page = $this->main->extractFromURI("page", "");
if ($page !== "") {
$items = \Federator\DIO\Posts::getPostsByUser($dbh, $user->id, $connector, $cache, $min, $max, 20);
$outbox->setItems($items);
} else {
$tmpitems = \Federator\DIO\Posts::getPostsByUser($dbh, $user->id, $connector, $cache, $min, $max, 99999);
$outbox->setTotalItems(sizeof($tmpitems));
$items = [];
}
$config = $this->main->getConfig();
$domain = $config['generic']['externaldomain'];
$id = 'https://' . $domain . '/' . $_user . '/outbox';
$outbox->setPartOf($id);
$outbox->setID($id);
if ($page === '') {
$outbox->setType('OrderedCollection');
}
if ($page === '' || $outbox->getCount() == 0) {
$outbox->setFirst($id . '?page=true');
}
if (sizeof($items) > 0) {
$oldestTS = $items[0]->getPublished();
$newestTS = $items[sizeof($items) - 1]->getPublished();
$outbox->setNext($id . '?page=true&max=' . $newestTS);
$outbox->setPrev($id . '?page=true&min=' . $oldestTS);
}
$obj = $outbox->toObject();
return json_encode($obj, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}
/**
* handle post call
*
* @param string|null $_user user to add data to outbox @unused-param
* @return string|false response
*/
public function post($_user)
{
return false;
}
}