forked from grumpydevelop/federator
Compare commits
5 commits
207d876254
...
097f871ed6
Author | SHA1 | Date | |
---|---|---|---|
![]() |
097f871ed6 | ||
![]() |
c1cf2a6be8 | ||
![]() |
d99479c188 | ||
![]() |
4cc9cfdc8c | ||
![]() |
474631dff2 |
19 changed files with 381 additions and 97 deletions
|
@ -87,7 +87,7 @@ class Followers implements \Federator\Api\FedUsers\FedUsersInterface
|
|||
// Pagination navigation
|
||||
$lastPage = max(0, ceil($totalItems / $pageSize) - 1);
|
||||
|
||||
if ($page === "" || $followers->count() == 0) {
|
||||
if ($page === "" || $followers->getCount() == 0) {
|
||||
$followers->setFirst($baseUrl . '?page=0');
|
||||
$followers->setLast($baseUrl . '?page=' . $lastPage);
|
||||
}
|
||||
|
|
|
@ -87,7 +87,7 @@ class Following implements \Federator\Api\FedUsers\FedUsersInterface
|
|||
// Pagination navigation
|
||||
$lastPage = max(0, ceil($totalItems / $pageSize) - 1);
|
||||
|
||||
if ($page === "" || $following->count() == 0) {
|
||||
if ($page === "" || $following->getCount() == 0) {
|
||||
$following->setFirst($baseUrl . '?page=0');
|
||||
$following->setLast($baseUrl . '?page=' . $lastPage);
|
||||
}
|
||||
|
|
|
@ -56,34 +56,33 @@ class Outbox implements \Federator\Api\FedUsers\FedUsersInterface
|
|||
|
||||
// get posts from user
|
||||
$outbox = new \Federator\Data\ActivityPub\Common\Outbox();
|
||||
$min = $this->main->extractFromURI("min", "");
|
||||
$max = $this->main->extractFromURI("max", "");
|
||||
$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);
|
||||
$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 . '/users/' . $_user . '/outbox';
|
||||
$id = 'https://' . $domain . '/' . $_user . '/outbox';
|
||||
$outbox->setPartOf($id);
|
||||
$outbox->setID($id);
|
||||
if ($page !== '') {
|
||||
$id .= '?page=' . urlencode($page);
|
||||
} else {
|
||||
if ($page === '') {
|
||||
$outbox->setType('OrderedCollection');
|
||||
}
|
||||
if ($page === '' || $outbox->count() == 0) {
|
||||
$outbox->setFirst($id . '?page=0');
|
||||
$outbox->setLast($id . '&min=0');
|
||||
if ($page === '' || $outbox->getCount() == 0) {
|
||||
$outbox->setFirst($id . '?page=true');
|
||||
}
|
||||
if (sizeof($items) > 0) {
|
||||
$newestId = $items[0]->getPublished();
|
||||
$oldestId = $items[sizeof($items) - 1]->getPublished();
|
||||
$outbox->setNext($id . '&max=' . $newestId);
|
||||
$outbox->setPrev($id . '&min=' . $oldestId);
|
||||
$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);
|
||||
|
|
5
php/federator/cache/cache.php
vendored
5
php/federator/cache/cache.php
vendored
|
@ -35,10 +35,13 @@ interface Cache extends \Federator\Connector\Connector
|
|||
* save remote posts by user
|
||||
*
|
||||
* @param string $user user name
|
||||
* @param int $min min timestamp
|
||||
* @param int $max max timestamp
|
||||
* @param int $limit limit results
|
||||
* @param \Federator\Data\ActivityPub\Common\APObject[]|false $posts user posts
|
||||
* @return void
|
||||
*/
|
||||
public function saveRemotePostsByUser($user, $posts);
|
||||
public function saveRemotePostsByUser($user, $min, $max, $limit, $posts);
|
||||
|
||||
/**
|
||||
* save remote stats
|
||||
|
|
|
@ -35,12 +35,13 @@ interface Connector
|
|||
* get posts by given user
|
||||
*
|
||||
* @param string $id user id
|
||||
* @param string $min min date
|
||||
* @param string $max max date
|
||||
* @param int $min min value
|
||||
* @param int $max max value
|
||||
* @param int $limit maximum number of results
|
||||
|
||||
* @return \Federator\Data\ActivityPub\Common\Activity[]|false
|
||||
*/
|
||||
public function getRemotePostsByUser($id, $min, $max);
|
||||
public function getRemotePostsByUser($id, $min, $max, $limit);
|
||||
|
||||
/**
|
||||
* get remote user by given name
|
||||
|
|
|
@ -48,7 +48,20 @@ class Collection extends APObject
|
|||
*/
|
||||
public function fromJson($json)
|
||||
{
|
||||
return parent::fromJson($json);
|
||||
$success = parent::fromJson($json);
|
||||
if (!$success) {
|
||||
return false;
|
||||
}
|
||||
if (array_key_exists('totalItems', $json)) {
|
||||
$this->totalItems = $json['totalItems'];
|
||||
}
|
||||
if (array_key_exists('first', $json)) {
|
||||
$this->first = $json['first'];
|
||||
}
|
||||
if (array_key_exists('last', $json)) {
|
||||
$this->last = $json['last'];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -61,11 +74,16 @@ class Collection extends APObject
|
|||
$this->totalItems = $totalItems;
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
public function getTotalItems(): int
|
||||
{
|
||||
return $this->totalItems;
|
||||
}
|
||||
|
||||
public function getFirst(): string
|
||||
{
|
||||
return $this->first;
|
||||
}
|
||||
|
||||
public function setFirst(string $url): void
|
||||
{
|
||||
$this->first = $url;
|
||||
|
|
|
@ -25,7 +25,7 @@ class Followers extends OrderedCollectionPage
|
|||
public function setItems(&$items)
|
||||
{
|
||||
// Optionally: type check that all $items are Activity objects
|
||||
$this->items = $items;
|
||||
$this->orderedItems = $items;
|
||||
$this->totalItems = sizeof($items);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ class Following extends OrderedCollectionPage
|
|||
public function setItems(&$items)
|
||||
{
|
||||
// Optionally: type check that all $items are Activity objects
|
||||
$this->items = $items;
|
||||
$this->orderedItems = $items;
|
||||
$this->totalItems = sizeof($items);
|
||||
}
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ class OrderedCollection extends Collection
|
|||
*
|
||||
* @var APObject[]|string[]
|
||||
*/
|
||||
protected $items = [];
|
||||
protected $orderedItems = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
@ -32,8 +32,8 @@ class OrderedCollection extends Collection
|
|||
{
|
||||
$return = parent::toObject();
|
||||
$return['type'] = 'OrderedCollection';
|
||||
if ($this->totalItems > 0) {
|
||||
foreach ($this->items as $item) {
|
||||
if (sizeof($this->orderedItems) > 0) {
|
||||
foreach ($this->orderedItems as $item) {
|
||||
if (is_string($item)) {
|
||||
$return['orderedItems'][] = $item;
|
||||
} elseif (is_object($item)) {
|
||||
|
@ -52,7 +52,20 @@ class OrderedCollection extends Collection
|
|||
*/
|
||||
public function fromJson($json)
|
||||
{
|
||||
return parent::fromJson($json);
|
||||
$success = parent::fromJson($json);
|
||||
if (!$success) {
|
||||
return false;
|
||||
}
|
||||
if (array_key_exists('orderedItems', $json)) {
|
||||
foreach ($json['orderedItems'] as $item) {
|
||||
$obj = \Federator\Data\ActivityPub\Factory::newActivityFromJson($item);
|
||||
if ($obj !== false) {
|
||||
$this->orderedItems[] = $obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -61,8 +74,7 @@ class OrderedCollection extends Collection
|
|||
*/
|
||||
public function append(&$item): void
|
||||
{
|
||||
$this->items[] = $item;
|
||||
$this->totalItems = sizeof($this->items);
|
||||
$this->orderedItems[] = $item;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,18 +85,22 @@ class OrderedCollection extends Collection
|
|||
public function get(int $index)
|
||||
{
|
||||
if ($index >= 0) {
|
||||
if ($index >= $this->totalItems) {
|
||||
if ($index >= sizeof($this->orderedItems)) {
|
||||
return false;
|
||||
}
|
||||
return $this->items[$index];
|
||||
return $this->orderedItems[$index];
|
||||
} else {
|
||||
if ($this->totalItems + $index < 0) {
|
||||
if (sizeof($this->orderedItems) + $index < 0) {
|
||||
return false;
|
||||
}
|
||||
return $this->items[$this->totalItems + $index];
|
||||
return $this->orderedItems[sizeof($this->orderedItems) + $index];
|
||||
}
|
||||
}
|
||||
|
||||
public function getCount(): int
|
||||
{
|
||||
return sizeof($this->orderedItems);
|
||||
}
|
||||
/**
|
||||
* set items
|
||||
*
|
||||
|
@ -93,7 +109,6 @@ class OrderedCollection extends Collection
|
|||
*/
|
||||
public function setItems(&$items)
|
||||
{
|
||||
$this->items = $items;
|
||||
$this->totalItems = sizeof($items);
|
||||
$this->orderedItems = $items;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,7 +51,29 @@ class OrderedCollectionPage extends OrderedCollection
|
|||
*/
|
||||
public function fromJson($json)
|
||||
{
|
||||
return parent::fromJson($json);
|
||||
$success = parent::fromJson($json);
|
||||
if (!$success) {
|
||||
return false;
|
||||
}
|
||||
if (array_key_exists('next', $json)) {
|
||||
$this->next = $json['next'];
|
||||
}
|
||||
if (array_key_exists('prev', $json)) {
|
||||
$this->prev = $json['prev'];
|
||||
}
|
||||
if (array_key_exists('partOf', $json)) {
|
||||
$this->partOf = $json['partOf'];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* get next url
|
||||
* @return string next URL
|
||||
*/
|
||||
public function getNext()
|
||||
{
|
||||
return $this->next;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -35,8 +35,7 @@ class Outbox extends OrderedCollectionPage
|
|||
public function setItems(&$items)
|
||||
{
|
||||
// Optionally: type check that all $items are Activity objects
|
||||
$this->items = $items;
|
||||
$this->totalItems = sizeof($items);
|
||||
$this->orderedItems = $items;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
16
php/federator/data/activitypub/common/unsupported.txt
Normal file
16
php/federator/data/activitypub/common/unsupported.txt
Normal file
|
@ -0,0 +1,16 @@
|
|||
Array
|
||||
(
|
||||
[type] => Document
|
||||
[mediaType] => image/jpeg
|
||||
[url] => https://noc.social/system/media_attachments/files/112/350/286/131/419/396/original/26ab9c8a4ab13f16.jpg
|
||||
[name] => Screenshot Zeitleiste aus kdenlive.
|
||||
[blurhash] => UC8|^tSwI-bu-taeRiaeu5e.aJjGsBWnR*jH
|
||||
[focalPoint] => Array
|
||||
(
|
||||
[0] => -0.01
|
||||
[1] => -0.79
|
||||
)
|
||||
|
||||
[width] => 1333
|
||||
[height] => 651
|
||||
)
|
|
@ -16,7 +16,7 @@ class Factory
|
|||
|
||||
/**
|
||||
* create object tree from json
|
||||
* @param array<string, mixed> $json input json
|
||||
* @param array<string, mixed>|mixed $json input json
|
||||
* @return Common\APObject|null object or false on error
|
||||
*/
|
||||
public static function newFromJson($json, string $jsonstring)
|
||||
|
@ -67,6 +67,12 @@ class Factory
|
|||
case 'Inbox':
|
||||
$return = new Common\Inbox();
|
||||
break;
|
||||
case 'OrderedCollection':
|
||||
$return = new Common\OrderedCollection();
|
||||
break;
|
||||
case 'OrderedCollectionPage':
|
||||
$return = new Common\OrderedCollectionPage();
|
||||
break;
|
||||
case 'Tombstone':
|
||||
$return = new Common\APObject("Tombstone");
|
||||
break;
|
||||
|
|
|
@ -24,22 +24,24 @@ class Posts
|
|||
* connector to fetch use with
|
||||
* @param \Federator\Cache\Cache|null $cache
|
||||
* optional caching service
|
||||
* @param string $min
|
||||
* minimum date
|
||||
* @param string $max
|
||||
* maximum date
|
||||
* @param int $min
|
||||
* minimum timestamp
|
||||
* @param int $max
|
||||
* maximum timestamp
|
||||
* @param int $limit
|
||||
* maximum number of results
|
||||
* @return \Federator\Data\ActivityPub\Common\Activity[]
|
||||
*/
|
||||
public static function getPostsByUser($dbh, $userid, $connector, $cache, $min, $max)
|
||||
public static function getPostsByUser($dbh, $userid, $connector, $cache, $min, $max, $limit)
|
||||
{
|
||||
// ask cache
|
||||
if ($cache !== null) {
|
||||
$posts = $cache->getRemotePostsByUser($userid, $min, $max);
|
||||
$posts = $cache->getRemotePostsByUser($userid, $min, $max, $limit);
|
||||
if ($posts !== false) {
|
||||
return $posts;
|
||||
}
|
||||
}
|
||||
$posts = self::getPostsFromDb($dbh, $userid, $min, $max);
|
||||
$posts = self::getPostsFromDb($dbh, $userid, $min, $max, $limit);
|
||||
if ($posts === false) {
|
||||
$posts = [];
|
||||
}
|
||||
|
@ -52,9 +54,8 @@ class Posts
|
|||
foreach ($posts as $post) {
|
||||
$published = $post->getPublished();
|
||||
if ($published != null) {
|
||||
$publishedStr = gmdate('Y-m-d H:i:s', $published);
|
||||
if ($latestPublished === null || $publishedStr > $latestPublished) {
|
||||
$latestPublished = $publishedStr;
|
||||
if ($latestPublished === null || $published > $latestPublished) {
|
||||
$latestPublished = $published;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -63,8 +64,9 @@ class Posts
|
|||
}
|
||||
}
|
||||
|
||||
// Always fetch newer posts from connector (if any)
|
||||
$newPosts = $connector->getRemotePostsByUser($userid, $remoteMin, $max);
|
||||
// Fetch newer posts from connector (if any) if max is not set and limit not reached
|
||||
if ($max == 0 && sizeof($posts) < $limit) {
|
||||
$newPosts = $connector->getRemotePostsByUser($userid, $remoteMin, $max, $limit);
|
||||
if ($newPosts !== false && is_array($newPosts)) {
|
||||
// Merge new posts with DB posts, avoiding duplicates by ID
|
||||
$existingIds = [];
|
||||
|
@ -73,10 +75,16 @@ class Posts
|
|||
}
|
||||
foreach ($newPosts as $newPost) {
|
||||
if (!isset($existingIds[$newPost->getID()])) {
|
||||
if ($newPost->getID() !== "") {
|
||||
self::savePost($dbh, $userid, $newPost);
|
||||
}
|
||||
if (sizeof($posts) < $limit) {
|
||||
$posts[] = $newPost;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$originUrl = 'localhost';
|
||||
if (isset($_SERVER['HTTP_HOST'])) {
|
||||
|
@ -95,11 +103,8 @@ class Posts
|
|||
$originUrl = 'localhost'; // Fallback to localhost if no origin is set
|
||||
}
|
||||
|
||||
// save posts to DB
|
||||
// optionally convert from article to note
|
||||
foreach ($posts as $post) {
|
||||
if ($post->getID() !== "") {
|
||||
self::savePost($dbh, $userid, $post);
|
||||
}
|
||||
switch (strtolower($post->getType())) {
|
||||
case 'undo':
|
||||
$object = $post->getObject();
|
||||
|
@ -134,7 +139,7 @@ class Posts
|
|||
}
|
||||
|
||||
if ($cache !== null) {
|
||||
$cache->saveRemotePostsByUser($userid, $posts);
|
||||
$cache->saveRemotePostsByUser($userid, $min, $max, $limit, $posts);
|
||||
}
|
||||
return $posts;
|
||||
}
|
||||
|
@ -144,26 +149,27 @@ class Posts
|
|||
*
|
||||
* @param \mysqli $dbh
|
||||
* @param string $userId
|
||||
* @param string|null $min
|
||||
* @param string|null $max
|
||||
* @param int $min min timestamp
|
||||
* @param int $max max timestamp
|
||||
* @param int $limit
|
||||
* @return \Federator\Data\ActivityPub\Common\Activity[]|false
|
||||
*/
|
||||
public static function getPostsFromDb($dbh, $userId, $min = null, $max = null)
|
||||
public static function getPostsFromDb($dbh, $userId, $min, $max, $limit = 20)
|
||||
{
|
||||
$sql = 'SELECT `id`, `url`, `user_id`, `actor`, `type`, `object`, `to`, `cc`, `published` FROM posts WHERE user_id = ?';
|
||||
$sql = 'SELECT `id`, `url`, `user_id`, `actor`, `type`, `object`, `to`, `cc`, unix_timestamp(`published`) as published FROM posts WHERE user_id = ?';
|
||||
$params = [$userId];
|
||||
$types = 's';
|
||||
if ($min !== null && $min !== "") {
|
||||
$sql .= ' AND published >= ?';
|
||||
if ($min > 0) {
|
||||
$sql .= ' AND published >= from_unixtime(?)';
|
||||
$params[] = $min;
|
||||
$types .= 's';
|
||||
}
|
||||
if ($max !== null && $max !== "") {
|
||||
$sql .= ' AND published <= ?';
|
||||
if ($max > 0) {
|
||||
$sql .= ' AND published <= from_unixtime(?)';
|
||||
$params[] = $max;
|
||||
$types .= 's';
|
||||
}
|
||||
$sql .= ' ORDER BY published DESC';
|
||||
$sql .= ' ORDER BY published DESC LIMIT ' . $limit;
|
||||
|
||||
$stmt = $dbh->prepare($sql);
|
||||
if ($stmt === false) {
|
||||
|
@ -193,9 +199,7 @@ class Posts
|
|||
}
|
||||
if (isset($row['published']) && $row['published'] !== null) {
|
||||
// If it's numeric, keep as int. If it's a string, try to parse as ISO 8601.
|
||||
if (is_numeric($row['published'])) {
|
||||
$row['published'] = intval($row['published'], 10);
|
||||
} else {
|
||||
if (!is_numeric($row['published'])) {
|
||||
// Try to parse as datetime string
|
||||
$timestamp = strtotime($row['published']);
|
||||
$row['published'] = $timestamp !== false ? $timestamp : null;
|
||||
|
|
193
php/federator/test.php
Normal file
193
php/federator/test.php
Normal file
|
@ -0,0 +1,193 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* test functions
|
||||
*/
|
||||
class Test
|
||||
{
|
||||
/**
|
||||
* fetch outbox
|
||||
* @param string $_name user handle to fetch
|
||||
* @return void
|
||||
*/
|
||||
public static function fetchOutbox($_name)
|
||||
{
|
||||
// make webfinger request
|
||||
$r = self::webfinger($_name);
|
||||
$outbox = $r['outbox'];
|
||||
$headers = ['Accept: application/activity+json'];
|
||||
[$response, $info] = \Federator\Main::getFromRemote($outbox, $headers);
|
||||
if ($info['http_code'] != 200) {
|
||||
return;
|
||||
}
|
||||
$json = json_decode($response, true);
|
||||
if ($json === false) {
|
||||
return;
|
||||
}
|
||||
$ap = \Federator\Data\ActivityPub\Factory::newFromJson($json, $response);
|
||||
if (!($ap instanceof \Federator\Data\ActivityPub\Common\OrderedCollection)) {
|
||||
echo "unsupport reply from $outbox\n";
|
||||
return;
|
||||
}
|
||||
// first and total items should be set
|
||||
$total = $ap->getTotalItems();
|
||||
echo "total items: " . $total;
|
||||
echo " first: " . $ap->getFirst() . "\n";
|
||||
|
||||
$page = $ap->getFirst();
|
||||
$count = 0;
|
||||
|
||||
while ($count < $total) {
|
||||
echo "query $page\n";
|
||||
// query pages
|
||||
[$response, $info] = \Federator\Main::getFromRemote($page, $headers);
|
||||
if ($info['http_code'] != 200) {
|
||||
return;
|
||||
}
|
||||
// echo "'$response'\n";
|
||||
$json = json_decode($response, true);
|
||||
$ap = \Federator\Data\ActivityPub\Factory::newFromJson($json, $response);
|
||||
if (!($ap instanceof \Federator\Data\ActivityPub\Common\OrderedCollectionPage)) {
|
||||
echo "unsupport reply from $page\n";
|
||||
return;
|
||||
}
|
||||
$thisCount = $ap->getCount();
|
||||
echo "count: " . $thisCount . "\n";
|
||||
for ($i = 0; $i < $thisCount; ++$i) {
|
||||
$entry = $ap->get($i);
|
||||
if ($entry instanceof \Federator\Data\ActivityPub\Common\APObject) {
|
||||
echo $entry->getID() . " " . $entry->getPublished() . "\n";
|
||||
}
|
||||
}
|
||||
$count += $thisCount;
|
||||
$page = $ap->getNext();
|
||||
}
|
||||
//print_r($ap);
|
||||
}
|
||||
/**
|
||||
* run test
|
||||
*
|
||||
* @param int $argc number of arguments
|
||||
* @param string[] $argv arguments
|
||||
* @return void
|
||||
*/
|
||||
public static function run($argc, $argv)
|
||||
{
|
||||
date_default_timezone_set("Europe/Berlin");
|
||||
spl_autoload_register(static function (string $className) {
|
||||
include PROJECT_ROOT . '/php/' . str_replace("\\", "/", strtolower($className)) . '.php';
|
||||
});
|
||||
if ($argc < 2) {
|
||||
self::printUsage();
|
||||
}
|
||||
// pretend that we are running from web directory
|
||||
define('PROJECT_ROOT', dirname(__DIR__, 2));
|
||||
$api = new \Federator\Api();
|
||||
for ($i = 1; $i < $argc; ++$i) {
|
||||
switch ($argv[$i]) {
|
||||
case 'fetchoutbox':
|
||||
self::fetchOutbox($argv[$i + 1]);
|
||||
++$i;
|
||||
break;
|
||||
case 'upvote':
|
||||
self::upvote($api, $argv[$i + 1]);
|
||||
++$i;
|
||||
break;
|
||||
default:
|
||||
self::printUsage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* print usage of maintenance tool
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function printUsage()
|
||||
{
|
||||
echo "usage php maintenance.php <command> <parameter> [<command> <parameter>...]\n";
|
||||
echo "command can be one of:\n";
|
||||
echo " fetchoutbox - fetch users outbox. parameter: username\n";
|
||||
echo " upvote - upvote. parameter: URL to upvote\n";
|
||||
echo " downvote - downvote. parameter: URL to downvote\n";
|
||||
echo " comment - downvote. parameter: URL to comment, text to comment\n";
|
||||
echo " Run this after you updated the program files\n";
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* upvote given URL
|
||||
*
|
||||
* @param \Federator\Api $api api instance
|
||||
* @param string $_url URL to upvote
|
||||
* @note uses hardcoded source
|
||||
* @return void
|
||||
*/
|
||||
public static function upvote($api, $_url)
|
||||
{
|
||||
$dbh = $api->getDatabase();
|
||||
$inboxActivity = new \Federator\Data\ActivityPub\Common\Create();
|
||||
\Federator\Api\FedUsers\Inbox::postForUser(
|
||||
$dbh,
|
||||
$api->getConnector(),
|
||||
null,
|
||||
'grumpydevelop',
|
||||
$_url,
|
||||
$inboxActivity
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* do a webfinger request
|
||||
* @param string $_name name to query
|
||||
*/
|
||||
private static function webfinger($_name): mixed
|
||||
{
|
||||
// make webfinger request
|
||||
if (preg_match("/^([^@]+)@(.*)$/", $_name, $matches) != 1) {
|
||||
echo "username is malformed";
|
||||
return false;
|
||||
}
|
||||
$remoteURL = 'https://' . $matches[2] . '/.well-known/webfinger?resource=acct:' . urlencode($_name);
|
||||
$headers = ['Accept: application/activity+json'];
|
||||
[$response, $info] = \Federator\Main::getFromRemote($remoteURL, $headers);
|
||||
if ($info['http_code'] != 200) {
|
||||
return false;
|
||||
}
|
||||
$r = json_decode($response, true);
|
||||
if (isset($r['links'])) {
|
||||
foreach ($r['links'] as $link) {
|
||||
if (isset($link['rel']) && $link['rel'] === 'self') {
|
||||
$remoteURL = $link['href'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isset($remoteURL)) {
|
||||
echo "FedUser::getUserByName Failed to find self link in webfinger for " . $_name . "\n";
|
||||
return false;
|
||||
}
|
||||
// fetch the user
|
||||
$headers = ['Accept: application/activity+json'];
|
||||
[$response, $info] = \Federator\Main::getFromRemote($remoteURL, $headers);
|
||||
if ($info['http_code'] != 200) {
|
||||
echo "FedUser::getUserByName Failed to fetch user from remoteUrl for " . $_name . "\n";
|
||||
return false;
|
||||
}
|
||||
$r = json_decode($response, true);
|
||||
return $r;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Test::run($argc, $argv);
|
|
@ -109,21 +109,23 @@ class ContentNation implements Connector
|
|||
* get posts by given user
|
||||
*
|
||||
* @param string $userId user id
|
||||
* @param string $min min date
|
||||
* @param string $max max date
|
||||
* @param int $min min date
|
||||
* @param int $max max date
|
||||
* @param int $limit limit results
|
||||
* @unused-param $limit
|
||||
* @return \Federator\Data\ActivityPub\Common\Activity[]|false
|
||||
*/
|
||||
public function getRemotePostsByUser($userId, $min, $max)
|
||||
public function getRemotePostsByUser($userId, $min, $max, $limit)
|
||||
{
|
||||
if (preg_match("#^([^@]+)@([^/]+)#", $userId, $matches) == 1) {
|
||||
$userId = $matches[1];
|
||||
}
|
||||
$remoteURL = $this->service . '/api/profile/' . urlencode($userId) . '/activities';
|
||||
if ($min !== '') {
|
||||
$remoteURL .= '&minTS=' . urlencode($min);
|
||||
if ($min > 0) {
|
||||
$remoteURL .= '&minTS=' . intval($min, 10);
|
||||
}
|
||||
if ($max !== '') {
|
||||
$remoteURL .= '&maxTS=' . urlencode($max);
|
||||
if ($max > 0) {
|
||||
$remoteURL .= '&maxTS=' . intval($max, 10);
|
||||
}
|
||||
[$response, $info] = \Federator\Main::getFromRemote($remoteURL, []);
|
||||
if ($info['http_code'] != 200) {
|
||||
|
@ -148,12 +150,11 @@ class ContentNation implements Connector
|
|||
case 'Article':
|
||||
$create = new \Federator\Data\ActivityPub\Common\Create();
|
||||
$create->setAActor($ourUrl . '/' . $userId);
|
||||
$create->setID($activity['id'])
|
||||
->setPublished($activity['published'] ?? $activity['timestamp'])
|
||||
$create->setPublished($activity['published'] ?? $activity['timestamp'])
|
||||
->addTo($ourUrl . '/' . $userId . '/followers')
|
||||
->addCC("https://www.w3.org/ns/activitystreams#Public");
|
||||
$create->setURL($ourUrl . '/' . $activity['profilename'] . '/' . $activity['name']);
|
||||
$create->setID($ourUrl . '/' . $activity['profilename'] . '/' . $activity['id']);
|
||||
$create->setID($ourUrl . '/' . $activity['profilename'] . '/' . $activity['name']);
|
||||
$apArticle = new \Federator\Data\ActivityPub\Common\Article();
|
||||
if (array_key_exists('tags', $activity)) {
|
||||
foreach ($activity['tags'] as $tag) {
|
||||
|
|
|
@ -46,11 +46,12 @@ class DummyConnector implements Connector
|
|||
* get posts by given user
|
||||
*
|
||||
* @param string $id user id @unused-param
|
||||
* @param string $min min date @unused-param
|
||||
* @param string $max max date @unused-param
|
||||
* @param int $min min timestamp @unused-param
|
||||
* @param int $max max timestamp @unused-param
|
||||
* @param int $limit limit number of results @unused-param
|
||||
* @return \Federator\Data\ActivityPub\Common\Activity[]|false
|
||||
*/
|
||||
public function getRemotePostsByUser($id, $min, $max)
|
||||
public function getRemotePostsByUser($id, $min, $max, $limit)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -139,12 +139,13 @@ class RedisCache implements Cache
|
|||
* get posts by given user
|
||||
*
|
||||
* @param string $id user id @unused-param
|
||||
* @param string $min min date @unused-param
|
||||
* @param string $max max date @unused-param
|
||||
* @param int $min min timestamp @unused-param
|
||||
* @param int $max max timestamp @unused-param
|
||||
* @param int $limit limit results @unused-param
|
||||
|
||||
* @return \Federator\Data\ActivityPub\Common\Activity[]|false
|
||||
*/
|
||||
public function getRemotePostsByUser($id, $min, $max)
|
||||
public function getRemotePostsByUser($id, $min, $max, $limit)
|
||||
{
|
||||
error_log("rediscache::getRemotePostsByUser not implemented");
|
||||
return false;
|
||||
|
@ -273,10 +274,13 @@ class RedisCache implements Cache
|
|||
* save remote posts by user
|
||||
*
|
||||
* @param string $user user name @unused-param
|
||||
* @param int $min min timestamp @unused-param
|
||||
* @param int $max max timestamp @unused-param
|
||||
* @param int $limit limit results @unused-param
|
||||
* @param \Federator\Data\ActivityPub\Common\APObject[]|false $posts user posts @unused-param
|
||||
* @return void
|
||||
*/
|
||||
public function saveRemotePostsByUser($user, $posts)
|
||||
public function saveRemotePostsByUser($user, $min, $max, $limit, $posts)
|
||||
{
|
||||
error_log("rediscache::saveRemotePostsByUser not implemented");
|
||||
}
|
||||
|
@ -305,6 +309,9 @@ class RedisCache implements Cache
|
|||
*/
|
||||
public function saveRemoteUserByName($_name, $user)
|
||||
{
|
||||
if (!$this->connected) {
|
||||
$this->connect();
|
||||
}
|
||||
$key = self::createKey('u', $_name);
|
||||
$serialized = $user->toJson();
|
||||
$this->redis->setEx($key, $this->userTTL, $serialized);
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
{ldelim}
|
||||
"subject": "acct:{$username}@{$domain}",
|
||||
"aliases": [
|
||||
"https://{$domain}/@{$username}",
|
||||
"https://{$domain}/users/{$username}"
|
||||
"https://{$domain}/@{$username}"
|
||||
],
|
||||
"links": [
|
||||
{ldelim}"rel": "self", "type": "application/activity+json", "href": "https://{$domain}/{$username}"{rdelim},
|
||||
|
|
Loading…
Add table
Reference in a new issue