Merge pull request #1720 from nextcloud/enh/noid/activitypub-collections

basic implementation of collections
pull/1721/head
Maxence Lange 2023-04-11 08:21:04 -01:00 zatwierdzone przez GitHub
commit 54aaef5f80
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
5 zmienionych plików z 102 dodań i 83 usunięć

Wyświetl plik

@ -278,7 +278,17 @@ class ActivityPubController extends Controller {
* @return Response
*/
public function outbox(string $username): Response {
return $this->success([$username]);
// if (!$this->checkSourceActivityStreams()) {
// return $this->socialPubController->outbox($username);
// }
try {
$actor = $this->cacheActorService->getFromLocalAccount($username);
return $this->directSuccess($this->streamService->getOutboxCollection($actor));
} catch (Exception $e) {
return $this->fail($e);
}
}
@ -301,11 +311,8 @@ class ActivityPubController extends Controller {
try {
$actor = $this->cacheActorService->getFromLocalAccount($username);
$followers = $this->followService->getFollowersCollection($actor);
// $followers->setTopLevel(true);
return $this->directSuccess($followers);
return $this->directSuccess($this->followService->getFollowersCollection($actor));
} catch (Exception $e) {
return $this->fail($e);
}
@ -329,7 +336,13 @@ class ActivityPubController extends Controller {
return $this->socialPubController->following($username);
}
return $this->success([$username]);
try {
$actor = $this->cacheActorService->getFromLocalAccount($username);
return $this->directSuccess($this->followService->getFollowingCollection($actor));
} catch (Exception $e) {
return $this->fail($e);
}
}

Wyświetl plik

@ -223,6 +223,8 @@ class FollowsRequest extends FollowsRequestBuilder {
$this->leftJoinDetails($qb, 'id', 'ca');
$qb->orderBy('f.creation', 'desc');
// TODO: pagination
return $this->getFollowsFromRequest($qb);
}

Wyświetl plik

@ -2,7 +2,6 @@
declare(strict_types=1);
/**
* Nextcloud - Social Support
*
@ -28,103 +27,69 @@ declare(strict_types=1);
*
*/
namespace OCA\Social\Model\ActivityPub;
use JsonSerializable;
/**
* Class OrderedCollection
*
* @package OCA\Social\Model\ActivityPub
*/
class OrderedCollection extends ACore implements JsonSerializable {
public const TYPE = 'OrderedCollection';
private int $totalItems = 0;
private string $first = '';
private string $last = '';
/**
* Activity constructor.
*
* @param ACore $parent
*/
public function __construct($parent = null) {
parent::__construct($parent);
$this->setType(self::TYPE);
}
/**
* @return int
*/
public function getTotalItems(): int {
return $this->totalItems;
}
/**
* @param int $totalItems
*
* @return OrderedCollection
*/
public function setTotalItems(int $totalItems): OrderedCollection {
public function setTotalItems(int $totalItems): self {
$this->totalItems = $totalItems;
return $this;
}
/**
* @return string
*/
public function getFirst(): string {
return $this->first;
}
/**
* @param string $first
*
* @return OrderedCollection
*/
public function setFirst(string $first): OrderedCollection {
public function setFirst(string $first): self {
$this->first = $first;
return $this;
}
//"id": "https://pub.pontapreta.net/users/admin/following",
//"type": "OrderedCollection",
//"totalItems": 1,
//"first": "https://pub.pontapreta.net/users/admin/following?page=1"
/**
* @param array $data
*/
public function import(array $data) {
parent::import($data);
public function getLast(): string {
return $this->last;
}
public function setLast(string $last): self {
$this->last = $last;
return $this;
}
public function import(array $data): self {
parent::import($data);
return $this;
}
/**
* @return array
*/
public function jsonSerialize(): array {
return array_merge(
parent::jsonSerialize(),
[
'totalItems' => $this->getTotalItems(),
'first' => $this->getFirst()
]
return array_filter(
array_merge(
parent::jsonSerialize(),
[
'totalItems' => $this->getTotalItems(),
'first' => $this->getFirst(),
'last' => $this->getLast()
]
)
);
}
}

Wyświetl plik

@ -56,18 +56,19 @@ use OCA\Social\Tools\Exceptions\RequestResultNotJsonException;
use OCA\Social\Tools\Exceptions\RequestResultSizeException;
use OCA\Social\Tools\Exceptions\RequestServerException;
use OCA\Social\Tools\Traits\TArrayTools;
use OCP\IURLGenerator;
use Psr\Log\LoggerInterface;
class FollowService {
use TArrayTools;
private IURLGenerator $urlGenerator;
private FollowsRequest $followsRequest;
private ActivityService $activityService;
private CacheActorService $cacheActorService;
private ConfigService $configService;
private LoggerInterface $logger;
private ?Person $viewer = null;
@ -81,12 +82,14 @@ class FollowService {
* @param LoggerInterface $logger
*/
public function __construct(
IURLGenerator $urlGenerator,
FollowsRequest $followsRequest,
ActivityService $activityService,
CacheActorService $cacheActorService,
ConfigService $configService,
LoggerInterface $logger
) {
$this->urlGenerator = $urlGenerator;
$this->followsRequest = $followsRequest;
$this->activityService = $activityService;
$this->cacheActorService = $cacheActorService;
@ -250,8 +253,14 @@ class FollowService {
public function getFollowersCollection(Person $actor): OrderedCollection {
$collection = new OrderedCollection();
$collection->setId($actor->getFollowers());
$collection->setTotalItems(20);
$collection->setFirst('...');
$collection->setTotalItems($this->getInt('followers', $actor->getDetails('count')));
$first = $this->urlGenerator->linkToRouteAbsolute(
'social.ActivityPub.followers',
['username' => $actor->getPreferredUsername()]
)
. '?page=1';
$collection->setFirst($first);
return $collection;
}
@ -276,9 +285,15 @@ class FollowService {
*/
public function getFollowingCollection(Person $actor): OrderedCollection {
$collection = new OrderedCollection();
// $collection->setId($actor->getFollowers());
// $collection->setTotalItems(20);
// $collection->setFirst('...');
$collection->setId($actor->getFollowing());
$collection->setTotalItems($this->getInt('following', $actor->getDetails('count')));
$first = $this->urlGenerator->linkToRouteAbsolute(
'social.ActivityPub.following',
['username' => $actor->getPreferredUsername()]
)
. '?page=1';
$collection->setFirst($first);
return $collection;
}

Wyświetl plik

@ -42,6 +42,7 @@ use OCA\Social\Exceptions\UnauthorizedFediverseException;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Actor\Person;
use OCA\Social\Model\ActivityPub\Object\Note;
use OCA\Social\Model\ActivityPub\OrderedCollection;
use OCA\Social\Model\ActivityPub\Stream;
use OCA\Social\Model\Client\Options\ProbeOptions;
use OCA\Social\Model\InstancePath;
@ -52,8 +53,13 @@ use OCA\Social\Tools\Exceptions\RequestNetworkException;
use OCA\Social\Tools\Exceptions\RequestResultNotJsonException;
use OCA\Social\Tools\Exceptions\RequestResultSizeException;
use OCA\Social\Tools\Exceptions\RequestServerException;
use OCA\Social\Tools\Traits\TArrayTools;
use OCP\IURLGenerator;
class StreamService {
use TArrayTools;
private IUrlGenerator $urlGenerator;
private StreamRequest $streamRequest;
private ActivityService $activityService;
private CacheActorService $cacheActorService;
@ -61,20 +67,14 @@ class StreamService {
private const ANCESTOR_LIMIT = 5;
/**
* NoteService constructor.
*
* @param StreamRequest $streamRequest
* @param ActivityService $activityService
* @param CacheActorService $cacheActorService
* @param ConfigService $configService
*/
public function __construct(
IUrlGenerator $urlGenerator,
StreamRequest $streamRequest,
ActivityService $activityService,
CacheActorService $cacheActorService,
ConfigService $configService
) {
$this->urlGenerator = $urlGenerator;
$this->streamRequest = $streamRequest;
$this->activityService = $activityService;
$this->cacheActorService = $cacheActorService;
@ -282,8 +282,6 @@ class StreamService {
}
/**
* @param Note $note
* @param string $replyTo
@ -403,7 +401,11 @@ class StreamService {
* @throws StreamNotFoundException
* @throws DateTimeException
*/
public function getRepliesByParentId(string $id, int $since = 0, int $limit = 5, bool $asViewer = false
public function getRepliesByParentId(
string $id,
int $since = 0,
int $limit = 5,
bool $asViewer = false
): array {
return $this->streamRequest->getRepliesByParentId($id, $since, $limit, $asViewer);
}
@ -563,4 +565,26 @@ class StreamService {
return $this->cacheActorService->getFromId($note->getAttributedTo());
}
/**
* @param Person $actor
*
* @return OrderedCollection
*/
public function getOutboxCollection(Person $actor): OrderedCollection {
$collection = new OrderedCollection();
$collection->setId($actor->getOutbox());
$collection->setTotalItems($this->getInt('post', $actor->getDetails('count')));
$link = $this->urlGenerator->linkToRouteAbsolute(
'social.ActivityPub.outbox',
['username' => $actor->getPreferredUsername()]
);
$collection->setFirst($link . '?page=1');
$collection->setLast($link . '?page=1&min_id=0');
return $collection;
}
}