diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AP.php | 5 | ||||
-rw-r--r-- | lib/Command/CacheRefresh.php | 3 | ||||
-rw-r--r-- | lib/Controller/ActivityPubController.php | 25 | ||||
-rw-r--r-- | lib/Cron/Cache.php | 5 | ||||
-rw-r--r-- | lib/Db/CacheActorsRequest.php | 40 | ||||
-rw-r--r-- | lib/Db/CacheActorsRequestBuilder.php | 3 | ||||
-rw-r--r-- | lib/Db/CoreRequestBuilder.php | 1 | ||||
-rw-r--r-- | lib/Db/FollowsRequest.php | 6 | ||||
-rw-r--r-- | lib/Interfaces/Object/FollowInterface.php | 3 | ||||
-rw-r--r-- | lib/Interfaces/Object/MentionInterface.php | 41 | ||||
-rw-r--r-- | lib/Interfaces/Object/NoteInterface.php | 54 | ||||
-rw-r--r-- | lib/Migration/Version1000Date20221118000001.php | 6 | ||||
-rw-r--r-- | lib/Migration/Version1000Date20230407000001.php | 59 | ||||
-rw-r--r-- | lib/Model/ActivityPub/Object/Mention.php | 58 | ||||
-rw-r--r-- | lib/Model/ActivityPub/OrderedCollection.php | 82 | ||||
-rw-r--r-- | lib/Model/ActivityPub/Stream.php | 9 | ||||
-rw-r--r-- | lib/Service/CacheActorService.php | 67 | ||||
-rw-r--r-- | lib/Service/FollowService.php | 75 | ||||
-rw-r--r-- | lib/Service/StreamService.php | 46 |
19 files changed, 470 insertions, 118 deletions
@@ -31,6 +31,7 @@ declare(strict_types=1); namespace OCA\Social; +use OCA\Social\Model\ActivityPub\OrderedCollection; use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; @@ -305,6 +306,10 @@ class AP { $item = new Note(); break; + case OrderedCollection::TYPE: + $item = new OrderedCollection(); + break; + case SocialAppNotification::TYPE: $item = new SocialAppNotification(); break; diff --git a/lib/Command/CacheRefresh.php b/lib/Command/CacheRefresh.php index b53b47a9..2629a566 100644 --- a/lib/Command/CacheRefresh.php +++ b/lib/Command/CacheRefresh.php @@ -85,6 +85,9 @@ class CacheRefresh extends Base { $result = $this->cacheActorService->manageCacheRemoteActors($input->getOption('force')); $output->writeLn($result . ' remote accounts updated'); + $result = $this->cacheActorService->manageDetailsRemoteActors($input->getOption('force')); + $output->writeLn($result . ' remote accounts details updated'); + $result = $this->documentService->manageCacheDocuments(); $output->writeLn($result . ' documents cached'); diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php index 65370503..498a6261 100644 --- a/lib/Controller/ActivityPubController.php +++ b/lib/Controller/ActivityPubController.php @@ -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); + } } diff --git a/lib/Cron/Cache.php b/lib/Cron/Cache.php index cf40cdf9..9a93565c 100644 --- a/lib/Cron/Cache.php +++ b/lib/Cron/Cache.php @@ -87,6 +87,11 @@ class Cache extends TimedJob { } try { + $this->cacheActorService->manageDetailsRemoteActors(); + } catch (Exception $e) { + } + + try { $this->documentService->manageCacheDocuments(); } catch (Exception $e) { } diff --git a/lib/Db/CacheActorsRequest.php b/lib/Db/CacheActorsRequest.php index 86266a46..1313aa04 100644 --- a/lib/Db/CacheActorsRequest.php +++ b/lib/Db/CacheActorsRequest.php @@ -30,6 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Db; +use DateInterval; use DateTime; use Exception; use OCA\Social\Exceptions\CacheActorDoesNotExistException; @@ -41,6 +42,7 @@ use OCP\DB\QueryBuilder\IQueryBuilder; class CacheActorsRequest extends CacheActorsRequestBuilder { public const CACHE_TTL = 60 * 24 * 10; // 10d + public const DETAILS_TTL = 60 * 18; // 18h /** @@ -101,9 +103,6 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { } - /** - * Insert cache about an Actor in database. - */ public function update(Person $actor): int { $qb = $this->getCacheActorsUpdateSql(); $qb->set('following', $qb->createNamedParameter($actor->getFollowing())) @@ -150,6 +149,24 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { } + public function updateDetails(Person $actor): int { + $qb = $this->getCacheActorsUpdateSql(); + $qb->set('details', $qb->createNamedParameter(json_encode($actor->getDetailsAll()))); + + try { + $qb->set( + 'details_update', + $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) + ); + } catch (Exception $e) { + } + + $qb->limitToIdString($actor->getId()); + + return $qb->executeStatement(); + } + + /** * get Cached version of an Actor, based on the UriId * @@ -212,7 +229,6 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { public function searchAccounts(string $search): array { $qb = $this->getCacheActorsSelectSql(); $qb->searchInAccount($search); - /** @var SocialQueryBuilder $qb */ $qb->leftJoinCacheDocuments('icon_id'); $this->leftJoinDetails($qb); $qb->limitResults(25); @@ -237,6 +253,22 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { /** + * @return Person[] + * @throws Exception + */ + public function getRemoteActorsToUpdateDetails(bool $force = false): array { + $qb = $this->getCacheActorsSelectSql(); + $qb->limitToLocal(false); + if (!$force) { + $date = new DateTime('now'); + $date->sub(new DateInterval('PT' . self::DETAILS_TTL . 'M')); + $qb->limitToDBFieldDateTime('details_update', $date, true); + } + + return $this->getCacheActorsFromRequest($qb); + } + + /** * delete cached version of an Actor, based on the UriId * * @param string $id diff --git a/lib/Db/CacheActorsRequestBuilder.php b/lib/Db/CacheActorsRequestBuilder.php index f099f264..3a712d72 100644 --- a/lib/Db/CacheActorsRequestBuilder.php +++ b/lib/Db/CacheActorsRequestBuilder.php @@ -80,7 +80,8 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder { $qb->select( 'ca.nid', 'ca.id', 'ca.account', 'ca.following', 'ca.followers', 'ca.inbox', 'ca.shared_inbox', 'ca.outbox', 'ca.featured', 'ca.url', 'ca.type', 'ca.preferred_username', - 'ca.name', 'ca.summary', 'ca.public_key', 'ca.local', 'ca.details', 'ca.source', 'ca.creation' + 'ca.name', 'ca.summary', 'ca.public_key', 'ca.local', 'ca.details', 'ca.source', 'ca.creation', + 'ca.details_update' ) ->from(self::TABLE_CACHE_ACTORS, 'ca'); diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php index 7cf52d55..1aa010c8 100644 --- a/lib/Db/CoreRequestBuilder.php +++ b/lib/Db/CoreRequestBuilder.php @@ -115,6 +115,7 @@ class CoreRequestBuilder { 'public_key', 'source', 'details', + 'details_update', 'creation' ], self::TABLE_CACHE_DOCUMENTS => [ diff --git a/lib/Db/FollowsRequest.php b/lib/Db/FollowsRequest.php index 920fe44b..4cf41653 100644 --- a/lib/Db/FollowsRequest.php +++ b/lib/Db/FollowsRequest.php @@ -136,8 +136,8 @@ class FollowsRequest extends FollowsRequestBuilder { */ public function getByPersons(string $actorId, string $remoteActorId): Follow { $qb = $this->getFollowsSelectSql(); - $this->limitToActorId($qb, $actorId); - $this->limitToObjectId($qb, $remoteActorId); + $qb->limitToActorIdPrim($qb->prim($actorId)); + $qb->limitToObjectIdPrim($qb->prim($remoteActorId)); return $this->getFollowFromRequest($qb); } @@ -223,6 +223,8 @@ class FollowsRequest extends FollowsRequestBuilder { $this->leftJoinDetails($qb, 'id', 'ca'); $qb->orderBy('f.creation', 'desc'); + // TODO: pagination + return $this->getFollowsFromRequest($qb); } diff --git a/lib/Interfaces/Object/FollowInterface.php b/lib/Interfaces/Object/FollowInterface.php index 648f0c8e..f831acd7 100644 --- a/lib/Interfaces/Object/FollowInterface.php +++ b/lib/Interfaces/Object/FollowInterface.php @@ -185,8 +185,7 @@ class FollowInterface extends AbstractActivityPubInterface implements IActivityP */ private function generateNotification(Follow $follow): void { /** @var SocialAppNotificationInterface $notificationInterface */ - $notificationInterface = - AP::$activityPub->getInterfaceFromType(SocialAppNotification::TYPE); + $notificationInterface = AP::$activityPub->getInterfaceFromType(SocialAppNotification::TYPE); try { $follower = $this->cacheActorService->getFromId($follow->getActorId()); diff --git a/lib/Interfaces/Object/MentionInterface.php b/lib/Interfaces/Object/MentionInterface.php new file mode 100644 index 00000000..5f116eb5 --- /dev/null +++ b/lib/Interfaces/Object/MentionInterface.php @@ -0,0 +1,41 @@ +<?php + +declare(strict_types=1); + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2023, Maxence Lange <maxence@artificial-owl.com> + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Social\Interfaces\Object; + +use OCA\Social\Interfaces\Activity\AbstractActivityPubInterface; +use OCA\Social\Interfaces\IActivityPubInterface; +use OCA\Social\Tools\Traits\TArrayTools; + +class MentionInterface extends AbstractActivityPubInterface implements IActivityPubInterface { + use TArrayTools; + + public function __construct() { + } +} diff --git a/lib/Interfaces/Object/NoteInterface.php b/lib/Interfaces/Object/NoteInterface.php index 646c729f..161a79b6 100644 --- a/lib/Interfaces/Object/NoteInterface.php +++ b/lib/Interfaces/Object/NoteInterface.php @@ -31,25 +31,40 @@ declare(strict_types=1); namespace OCA\Social\Interfaces\Object; +use OCA\Social\AP; +use OCA\Social\Db\CacheActorsRequest; use OCA\Social\Db\StreamRequest; +use OCA\Social\Exceptions\CacheActorDoesNotExistException; use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\ItemAlreadyExistsException; use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Interfaces\Activity\AbstractActivityPubInterface; use OCA\Social\Interfaces\IActivityPubInterface; +use OCA\Social\Interfaces\Internal\SocialAppNotificationInterface; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Activity\Create; use OCA\Social\Model\ActivityPub\Activity\Delete; +use OCA\Social\Model\ActivityPub\Internal\SocialAppNotification; +use OCA\Social\Model\ActivityPub\Object\Mention; use OCA\Social\Model\ActivityPub\Object\Note; use OCA\Social\Service\PushService; +use OCA\Social\Tools\Traits\TArrayTools; class NoteInterface extends AbstractActivityPubInterface implements IActivityPubInterface { + use TArrayTools; + private StreamRequest $streamRequest; + private CacheActorsRequest $cacheActorsRequest; private PushService $pushService; - public function __construct(StreamRequest $streamRequest, PushService $pushService) { + public function __construct( + StreamRequest $streamRequest, + CacheActorsRequest $cacheActorsRequest, + PushService $pushService + ) { $this->streamRequest = $streamRequest; + $this->cacheActorsRequest = $cacheActorsRequest; $this->pushService = $pushService; } @@ -91,6 +106,7 @@ class NoteInterface extends AbstractActivityPubInterface implements IActivityPub } catch (StreamNotFoundException $e) { $this->streamRequest->save($note); $this->updateDetails($note); + $this->generateNotification($note); $this->pushService->onNewStream($note->getId()); } } @@ -115,4 +131,40 @@ class NoteInterface extends AbstractActivityPubInterface implements IActivityPub } catch (StreamNotFoundException $e) { } } + + private function generateNotification(Note $note): void { + $mentions = $note->getTags('Mention'); + if (empty($mentions)) { + return; + } + + /** @var SocialAppNotificationInterface $notificationInterface */ + $notificationInterface = AP::$activityPub->getInterfaceFromType(SocialAppNotification::TYPE); + $post = $this->streamRequest->getStreamById($note->getId(), false, ACore::FORMAT_LOCAL); + + foreach ($mentions as $mention) { + try { + $recipient = $this->cacheActorsRequest->getFromId($this->get('href', $mention)); + if (!$recipient->isLocal()) { // only interested on local + throw new CacheActorDoesNotExistException(); + } + } catch (CacheActorDoesNotExistException $e) { + continue; + } + + /** @var SocialAppNotification $notification */ + $notification = AP::$activityPub->getItemFromType(SocialAppNotification::TYPE); + $notification->setDetailItem('post', $post); + $notification->addDetail('account', $post->getActor()->getAccount()); + $notification->setAttributedTo($recipient->getId()) + ->setSubType(Mention::TYPE) + ->setId($post->getId() . '/notification+mention') + ->setSummary('{account} mentioned you in a post') + ->setObjectId($post->getId()) + ->setTo($recipient->getId()) + ->setLocal(true); + + $notificationInterface->save($notification); + } + } } diff --git a/lib/Migration/Version1000Date20221118000001.php b/lib/Migration/Version1000Date20221118000001.php index 20753467..96cedf7a 100644 --- a/lib/Migration/Version1000Date20221118000001.php +++ b/lib/Migration/Version1000Date20221118000001.php @@ -896,6 +896,12 @@ class Version1000Date20221118000001 extends SimpleMigrationStep { 'notnull' => false, ] ); + $table->addColumn( + 'details_update', Types::DATETIME, + [ + 'notnull' => false, + ] + ); $table->setPrimaryKey(['nid']); $table->addUniqueIndex(['id_prim']); diff --git a/lib/Migration/Version1000Date20230407000001.php b/lib/Migration/Version1000Date20230407000001.php new file mode 100644 index 00000000..e7f9be9d --- /dev/null +++ b/lib/Migration/Version1000Date20230407000001.php @@ -0,0 +1,59 @@ +<?php + +declare(strict_types=1); + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2023, Maxence Lange <maxence@artificial-owl.com> + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Social\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version1000Date20230407000001 extends SimpleMigrationStep { + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + // fix nid as primary on social_cache_actor + if ($schema->hasTable('social_cache_actor')) { + $table = $schema->getTable('social_cache_actor'); + + if (!$table->hasColumn('details_update')) { + $table->addColumn( + 'details_update', Types::DATETIME, + [ + 'notnull' => false + ] + ); + } + } + + return $schema; + } +} diff --git a/lib/Model/ActivityPub/Object/Mention.php b/lib/Model/ActivityPub/Object/Mention.php new file mode 100644 index 00000000..2c5d7d15 --- /dev/null +++ b/lib/Model/ActivityPub/Object/Mention.php @@ -0,0 +1,58 @@ +<?php + +declare(strict_types=1); + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2023, Maxence Lange <maxence@artificial-owl.com> + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Social\Model\ActivityPub\Object; + +use JsonSerializable; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Stream; + +class Mention extends Stream implements JsonSerializable { + public const TYPE = 'Mention'; + + public function __construct(ACore $parent = null) { + parent::__construct($parent); + + $this->setType(self::TYPE); + } + + public function import(array $data): void { + parent::import($data); + } + + public function importFromDatabase(array $data): void { + parent::importFromDatabase($data); + } + + public function jsonSerialize(): array { + $result = parent::jsonSerialize(); + + return $result; + } +} diff --git a/lib/Model/ActivityPub/OrderedCollection.php b/lib/Model/ActivityPub/OrderedCollection.php index 8343f87d..d87e03dc 100644 --- a/lib/Model/ActivityPub/OrderedCollection.php +++ b/lib/Model/ActivityPub/OrderedCollection.php @@ -2,7 +2,6 @@ declare(strict_types=1); - /** * Nextcloud - Social Support * @@ -28,103 +27,72 @@ 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; } + public function getLast(): string { + return $this->last; + } + public function setLast(string $last): self { + $this->last = $last; + 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) { + public function import(array $data): self { parent::import($data); - } + $this->setFirst($this->validate(ACore::AS_USERNAME, 'first', $data, '')) + ->setLast($this->validate(ACore::AS_USERNAME, 'last', $data, '')) + ->setTotalItems($this->getInt('totalItems', $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() + ] + ) ); } } diff --git a/lib/Model/ActivityPub/Stream.php b/lib/Model/ActivityPub/Stream.php index 18ca8c53..ca34a237 100644 --- a/lib/Model/ActivityPub/Stream.php +++ b/lib/Model/ActivityPub/Stream.php @@ -43,6 +43,7 @@ use OCA\Social\Model\ActivityPub\Object\Document; use OCA\Social\Model\ActivityPub\Object\Follow; use OCA\Social\Model\ActivityPub\Object\Image; use OCA\Social\Model\ActivityPub\Object\Like; +use OCA\Social\Model\ActivityPub\Object\Mention; use OCA\Social\Model\Client\MediaAttachment; use OCA\Social\Model\StreamAction; use OCA\Social\Tools\IQueryRow; @@ -661,11 +662,19 @@ class Stream extends ACore implements IQueryRow, JsonSerializable { public function exportAsNotification(): array { + // TODO - implements: + // status = Someone you enabled notifications for has posted a status + // follow_request = Someone requested to follow you + // poll = A poll you have voted in or created has ended + // update = A status you boosted with has been edited switch ($this->getSubTyp |