From 637924a13f751ef55addae69a7bd677f825cfea0 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Wed, 11 May 2022 18:22:54 +0200 Subject: [PATCH 1/2] Port away from private OC::$server->get And fix a few psalm issues Signed-off-by: Carl Schwan --- composer.lock | 12 +- lib/AP.php | 269 +++++++++--------------- lib/Controller/NavigationController.php | 5 +- lib/Controller/OAuthController.php | 1 + lib/Cron/Cache.php | 29 +-- lib/Cron/Chunk.php | 13 +- lib/Cron/Queue.php | 20 +- lib/Db/InstancesRequest.php | 3 +- lib/Db/StreamTagsRequest.php | 4 +- lib/Service/InstanceService.php | 6 +- lib/Service/PushService.php | 6 - lib/Service/SignatureService.php | 5 +- lib/hostmeta.php | 10 +- tests/psalm-baseline.xml | 18 +- 14 files changed, 152 insertions(+), 249 deletions(-) diff --git a/composer.lock b/composer.lock index 6a03d974..77023d38 100644 --- a/composer.lock +++ b/composer.lock @@ -323,12 +323,12 @@ "source": { "type": "git", "url": "https://github.com/ChristophWurst/nextcloud_composer.git", - "reference": "5e2c78469a4f669ed11016cf8a6668792efcbedf" + "reference": "5e9719488fe558db974ef45f289a6911fe2c6850" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/ChristophWurst/nextcloud_composer/zipball/5e2c78469a4f669ed11016cf8a6668792efcbedf", - "reference": "5e2c78469a4f669ed11016cf8a6668792efcbedf", + "url": "https://api.github.com/repos/ChristophWurst/nextcloud_composer/zipball/5e9719488fe558db974ef45f289a6911fe2c6850", + "reference": "5e9719488fe558db974ef45f289a6911fe2c6850", "shasum": "" }, "require": { @@ -341,7 +341,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "24.0.0-dev" + "dev-master": "25.0.0-dev" } }, "notification-url": "https://packagist.org/downloads/", @@ -359,7 +359,7 @@ "issues": "https://github.com/ChristophWurst/nextcloud_composer/issues", "source": "https://github.com/ChristophWurst/nextcloud_composer/tree/master" }, - "time": "2022-04-15T01:52:26+00:00" + "time": "2022-05-11T02:03:08+00:00" }, { "name": "composer/package-versions-deprecated", @@ -4999,5 +4999,5 @@ "platform-overrides": { "php": "7.4.0" }, - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" } diff --git a/lib/AP.php b/lib/AP.php index 80065b06..bad3fdba 100644 --- a/lib/AP.php +++ b/lib/AP.php @@ -32,7 +32,6 @@ declare(strict_types=1); namespace OCA\Social; use daita\MySmallPhpTools\Traits\TArrayTools; -use OC; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; use OCA\Social\Exceptions\SocialAppConfigException; @@ -84,6 +83,8 @@ use OCA\Social\Model\ActivityPub\Object\Tombstone; use OCA\Social\Model\ActivityPub\Stream; use OCA\Social\Service\ConfigService; use OCP\AppFramework\QueryException; +use OCP\Server; +use Psr\Log\LoggerInterface; /** * Class AP @@ -93,123 +94,92 @@ use OCP\AppFramework\QueryException; class AP { use TArrayTools; - public const REDUNDANCY_LIMIT = 10; + public AcceptInterface $acceptInterface; + public AddInterface $addInterface; + public AnnounceInterface $announceInterface; + public BlockInterface $blockInterface; + public CreateInterface $createInterface; + public DeleteInterface $deleteInterface; + public DocumentInterface $documentInterface; + public FollowInterface $followInterface; + public ImageInterface $imageInterface; + public LikeInterface $likeInterface; + public PersonInterface $personInterface; + public NoteInterface $noteInterface; + public GroupInterface $groupInterface; + public OrganizationInterface $organizationInterface; + public ApplicationInterface $applicationInterface; + public RejectInterface $rejectInterface; + public RemoveInterface $removeInterface; + public ServiceInterface $serviceInterface; + public UndoInterface $undoInterface; + public UpdateInterface $updateInterface; + public SocialAppNotificationInterface $notificationInterface; + public ConfigService $configService; + public static ?AP $activityPub = null; + private SocialAppNotificationInterface $socialAppNotificationInterface; - /** @var AcceptInterface */ - public $acceptInterface; - - /** @var AddInterface */ - public $addInterface; - - /** @var AnnounceInterface */ - public $announceInterface; - - /** @var BlockInterface */ - public $blockInterface; - - /** @var CreateInterface */ - public $createInterface; - - /** @var DeleteInterface */ - public $deleteInterface; - - /** @var DocumentInterface */ - public $documentInterface; - - /** @var FollowInterface */ - public $followInterface; - - /** @var ImageInterface */ - public $imageInterface; - - /** @var LikeInterface */ - public $likeInterface; - - /** @var PersonInterface */ - public $personInterface; - - /** @var NoteInterface */ - public $noteInterface; - - /** @var RejectInterface */ - public $rejectInterface; - - /** @var RemoveInterface */ - public $removeInterface; - - /** @var ServiceInterface */ - public $serviceInterface; - - /** @var UndoInterface */ - public $undoInterface; - - /** @var UpdateInterface */ - public $updateInterface; - - /** @var SocialAppNotificationInterface */ - public $notificationInterface; - - /** @var ConfigService */ - public $configService; - - - /** @var AP */ - public static $activityPub = null; - - - /** - * AP constructor. - */ - public function __construct() { + public function __construct( + AcceptInterface $acceptInterface, + AddInterface $addInterface, + AnnounceInterface $announceInterface, + BlockInterface $blockInterface, + CreateInterface $createInterface, + DeleteInterface $deleteInterface, + DocumentInterface $documentInterface, + FollowInterface $followInterface, + ImageInterface $imageInterface, + LikeInterface $likeInterface, + NoteInterface $noteInterface, + SocialAppNotificationInterface $socialAppNotificationInterface, + PersonInterface $personInterface, + ServiceInterface $serviceInterface, + GroupInterface $groupInterface, + OrganizationInterface $organizationInterface, + ApplicationInterface $applicationInterface, + RejectInterface $rejectInterface, + RemoveInterface $removeInterface, + UndoInterface $undoInterface, + UpdateInterface $updateInterface, + ConfigService $configService + ) { + $this->acceptInterface = $acceptInterface; + $this->addInterface = $addInterface; + $this->announceInterface = $announceInterface; + $this->blockInterface = $blockInterface; + $this->createInterface = $createInterface; + $this->deleteInterface = $deleteInterface; + $this->documentInterface = $documentInterface; + $this->followInterface = $followInterface; + $this->imageInterface = $imageInterface; + $this->likeInterface = $likeInterface; + $this->noteInterface = $noteInterface; + $this->socialAppNotificationInterface = $socialAppNotificationInterface; + $this->personInterface = $personInterface; + $this->serviceInterface = $serviceInterface; + $this->groupInterface = $groupInterface; + $this->organizationInterface = $organizationInterface; + $this->applicationInterface = $applicationInterface; + $this->rejectInterface = $rejectInterface; + $this->removeInterface = $removeInterface; + $this->undoInterface = $undoInterface; + $this->updateInterface = $updateInterface; + $this->configService = $configService; } - - /** - * - */ public static function init() { - $ap = new AP(); try { - $ap->acceptInterface = OC::$server->query(AcceptInterface::class); - $ap->addInterface = OC::$server->query(AddInterface::class); - $ap->announceInterface = OC::$server->query(AnnounceInterface::class); - $ap->blockInterface = OC::$server->query(BlockInterface::class); - $ap->createInterface = OC::$server->query(CreateInterface::class); - $ap->deleteInterface = OC::$server->query(DeleteInterface::class); - $ap->documentInterface = OC::$server->query(DocumentInterface::class); - $ap->followInterface = OC::$server->query(FollowInterface::class); - $ap->imageInterface = OC::$server->query(ImageInterface::class); - $ap->likeInterface = OC::$server->query(LikeInterface::class); - $ap->noteInterface = OC::$server->query(NoteInterface::class); - $ap->notificationInterface = OC::$server->query(SocialAppNotificationInterface::class); - $ap->personInterface = OC::$server->query(PersonInterface::class); - $ap->serviceInterface = OC::$server->query(ServiceInterface::class); - $ap->groupInterface = OC::$server->query(GroupInterface::class); - $ap->groupInterface = OC::$server->query(OrganizationInterface::class); - $ap->groupInterface = OC::$server->query(ApplicationInterface::class); - $ap->rejectInterface = OC::$server->query(RejectInterface::class); - $ap->removeInterface = OC::$server->query(RemoveInterface::class); - $ap->undoInterface = OC::$server->query(UndoInterface::class); - $ap->updateInterface = OC::$server->query(UpdateInterface::class); - - $ap->configService = OC::$server->query(ConfigService::class); - - AP::$activityPub = $ap; + AP::$activityPub = Server::get(AP::class); } catch (QueryException $e) { - OC::$server->getLogger() - ->logException($e); + Server::get(LoggerInterface::class) + ->errir($e->getMessage(), ['exception' => $e]); } } /** - * @param array $data - * @param ACore $parent - * @param int $level - * - * @return ACore * @throws RedundancyLimitException * @throws SocialAppConfigException * @throws ItemUnknownException @@ -232,10 +202,6 @@ class AP { /** - * @param array $data - * @param ACore $item - * @param int $level - * * @throws RedundancyLimitException * @throws SocialAppConfigException */ @@ -258,10 +224,6 @@ class AP { /** - * @param array $data - * @param ACore $item - * @param int $level - * * @throws RedundancyLimitException * @throws SocialAppConfigException */ @@ -279,9 +241,6 @@ class AP { /** - * @param array $data - * - * @return ACore * @throws SocialAppConfigException * @throws ItemUnknownException */ @@ -425,103 +384,75 @@ class AP { public function getInterfaceFromType(string $type): IActivityPubInterface { switch ($type) { case Accept::TYPE: - $interface = $this->acceptInterface; - break; + return $this->acceptInterface; case Add::TYPE: - $interface = $this->addInterface; - break; + return $this->addInterface; case Announce::TYPE: - $interface = $this->announceInterface; - break; + return $this->announceInterface; case Block::TYPE: - $interface = $this->blockInterface; - break; + return $this->blockInterface; case Create::TYPE: - $interface = $this->createInterface; - break; + return $this->createInterface; case Delete::TYPE: - $interface = $this->deleteInterface; - break; + return $this->deleteInterface; case Document::TYPE: - $interface = $this->documentInterface; - break; + return $this->documentInterface; case Follow::TYPE: - $interface = $this->followInterface; - break; + return $this->followInterface; case Image::TYPE: - $interface = $this->imageInterface; - break; + return $this->imageInterface; case Like::TYPE: - $interface = $this->likeInterface; - break; + return $this->likeInterface; case Note::TYPE: - $interface = $this->noteInterface; - break; + return $this->noteInterface; case SocialAppNotification::TYPE: - $interface = $this->notificationInterface; - break; + return $this->notificationInterface; case Person::TYPE: - $interface = $this->personInterface; - break; + return $this->personInterface; case Reject::TYPE: - $interface = $this->rejectInterface; - break; + return $this->rejectInterface; case Remove::TYPE: - $interface = $this->removeInterface; - break; + return $this->removeInterface; case Service::TYPE: - $interface = $this->serviceInterface; - break; + return $this->serviceInterface; case Undo::TYPE: - $interface = $this->undoInterface; - break; + return $this->undoInterface; case Update::TYPE: - $interface = $this->updateInterface; - break; + return $this->updateInterface; default: throw new ItemUnknownException(); } - - return $interface; } - - /** - * @param ACore $item - * - * @return bool - */ public function isActor(ACore $item): bool { - $types = - [ - Person::TYPE, - Service::TYPE, - Group::TYPE, - Organization::TYPE, - Application::TYPE - ]; + $types = [ + Person::TYPE, + Service::TYPE, + Group::TYPE, + Organization::TYPE, + Application::TYPE + ]; return (in_array($item->getType(), $types)); } } - AP::init(); diff --git a/lib/Controller/NavigationController.php b/lib/Controller/NavigationController.php index 0f95c0bf..910ff0a0 100644 --- a/lib/Controller/NavigationController.php +++ b/lib/Controller/NavigationController.php @@ -34,7 +34,6 @@ namespace OCA\Social\Controller; use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; -use OC; use OCP\AppFramework\Http; use OC\User\NoUserException; use OCA\Social\AppInfo\Application; @@ -55,6 +54,8 @@ use OCP\IInitialStateService; use OCP\IL10N; use OCP\IRequest; use OCP\IURLGenerator; +use OCP\IGroupManager; +use OCP\Server; /** * Class NavigationController @@ -119,7 +120,7 @@ class NavigationController extends Controller { 'public' => false, 'firstrun' => false, 'setup' => false, - 'isAdmin' => OC::$server->getGroupManager() + 'isAdmin' => Server::get(IGroupManager::class) ->isAdmin($this->userId), 'cliUrl' => $this->getCliUrl() ]; diff --git a/lib/Controller/OAuthController.php b/lib/Controller/OAuthController.php index 9ffda8fa..b9be8560 100644 --- a/lib/Controller/OAuthController.php +++ b/lib/Controller/OAuthController.php @@ -211,6 +211,7 @@ class OAuthController extends Controller { // TODO : finalize result if no redirect_url return new DataResponse( [ + 'code' => $code, // 'access_token' => '', // "token_type" => "Bearer", // "scope" => "read write follow push", diff --git a/lib/Cron/Cache.php b/lib/Cron/Cache.php index 2a5aacab..27796efa 100644 --- a/lib/Cron/Cache.php +++ b/lib/Cron/Cache.php @@ -34,7 +34,6 @@ namespace OCA\Social\Cron; use Exception; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; -use OCA\Social\AppInfo\Application; use OCA\Social\Service\AccountService; use OCA\Social\Service\CacheActorService; use OCA\Social\Service\DocumentService; @@ -47,36 +46,26 @@ use OCP\AppFramework\QueryException; * @package OCA\Social\Cron */ class Cache extends TimedJob { - private ?AccountService $accountService = null; - private ?CacheActorService $cacheActorService = null; - private ?DocumentService $documentService = null; - private ?HashtagService $hashtagService = null; + private AccountService $accountService; + private CacheActorService $cacheActorService; + private DocumentService $documentService; + private HashtagService $hashtagService; - public function __construct(ITimeFactory $time) { + public function __construct(ITimeFactory $time, AccountService $accountService, CacheActorService $cacheActorService, DocumentService $documentService, HashtagService $hashtagService) { parent::__construct($time); $this->setInterval(12 * 60); // 12 minutes + $this->accountService = $accountService; + $this->cacheActorService = $cacheActorService; + $this->documentService = $documentService; + $this->hashtagService = $hashtagService; } - /** * @param mixed $argument * * @throws QueryException */ protected function run($argument) { - $app = \OC::$server->get(Application::class); - $c = $app->getContainer(); - - $this->accountService = $c->get(AccountService::class); - $this->cacheActorService = $c->get(CacheActorService::class); - $this->documentService = $c->get(DocumentService::class); - $this->hashtagService = $c->get(HashtagService::class); - - $this->manageCache(); - } - - - private function manageCache() { try { $this->accountService->blindKeyRotation(); } catch (Exception $e) { diff --git a/lib/Cron/Chunk.php b/lib/Cron/Chunk.php index 9631c897..b7c87ab3 100644 --- a/lib/Cron/Chunk.php +++ b/lib/Cron/Chunk.php @@ -33,7 +33,6 @@ namespace OCA\Social\Cron; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; -use OCA\Social\AppInfo\Application; use OCA\Social\Service\ConfigService; use OCP\AppFramework\QueryException; @@ -43,11 +42,12 @@ use OCP\AppFramework\QueryException; * @package OCA\Social\Cron */ class Chunk extends TimedJob { - private ?ConfigService $configService = null; + private ConfigService $configService; - public function __construct(ITimeFactory $time) { + public function __construct(ITimeFactory $time, ConfigService $configService) { parent::__construct($time); $this->setInterval(12 * 3600); // 12 hours + $this->configService = $configService; } @@ -57,11 +57,6 @@ class Chunk extends TimedJob { * @throws QueryException */ protected function run($argument) { - $app = \OC::$server->query(Application::class); - $c = $app->getContainer(); - - $this->configService = $c->query(ConfigService::class); - $size = (int)$this->configService->getAppValue(ConfigService::DATABASE_CHUNK_SIZE); $this->morphChunks($size); } @@ -70,6 +65,6 @@ class Chunk extends TimedJob { /** * @param int $size */ - private function morphChunks(int $size) { + private function morphChunks(int $size): void { } } diff --git a/lib/Cron/Queue.php b/lib/Cron/Queue.php index 9c516974..dabba1c3 100644 --- a/lib/Cron/Queue.php +++ b/lib/Cron/Queue.php @@ -33,7 +33,6 @@ namespace OCA\Social\Cron; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; -use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Service\ActivityService; use OCA\Social\Service\RequestQueueService; @@ -46,16 +45,19 @@ use OCP\AppFramework\QueryException; * @package OCA\Social\Cron */ class Queue extends TimedJob { - private ?ActivityService $activityService = null; - private ?RequestQueueService $requestQueueService = null; - private ?StreamQueueService $streamQueueService = null; + private ActivityService $activityService; + private RequestQueueService $requestQueueService; + private StreamQueueService $streamQueueService; /** * Cache constructor. */ - public function __construct(ITimeFactory $time) { + public function __construct(ITimeFactory $time, RequestQueueService $requestQueueService, StreamQueueService $streamQueueService, ActivityService $activityService) { parent::__construct($time); $this->setInterval(12 * 60); // 12 minutes + $this->requestQueueService = $requestQueueService; + $this->streamQueueService = $streamQueueService; + $this->activityService = $activityService; } @@ -65,14 +67,6 @@ class Queue extends TimedJob { * @throws QueryException */ protected function run($argument) { - /** @var Application $app */ - $app = \OC::$server->get(Application::class); - $c = $app->getContainer(); - - $this->requestQueueService = $c->get(RequestQueueService::class); - $this->streamQueueService = $c->get(StreamQueueService::class); - $this->activityService = $c->get(ActivityService::class); - $this->manageRequestQueue(); $this->manageStreamQueue(); } diff --git a/lib/Db/InstancesRequest.php b/lib/Db/InstancesRequest.php index 02f8c19b..e1492d8e 100644 --- a/lib/Db/InstancesRequest.php +++ b/lib/Db/InstancesRequest.php @@ -55,8 +55,7 @@ class InstancesRequest extends InstancesRequestBuilder { $qb = $this->getInstanceInsertSql(); $qb->setValue('uri', $qb->createNamedParameter($instance->getUri())); - - $qb->execute(); + $qb->executeStatement(); } diff --git a/lib/Db/StreamTagsRequest.php b/lib/Db/StreamTagsRequest.php index 76649f0b..1a4f0bce 100644 --- a/lib/Db/StreamTagsRequest.php +++ b/lib/Db/StreamTagsRequest.php @@ -34,7 +34,9 @@ namespace OCA\Social\Db; use daita\MySmallPhpTools\Traits\TStringTools; use OCA\Social\Model\ActivityPub\Object\Note; use OCA\Social\Model\ActivityPub\Stream; +use OCP\Server; use OCP\DB\Exception as DBException; +use Psr\Log\LoggerInterface; /** * Class StreamTagsRequest @@ -58,7 +60,7 @@ class StreamTagsRequest extends StreamTagsRequestBuilder { try { $qb->executeStatement(); } catch (DBException $e) { - \OC::$server->getLogger() + Server::get(LoggerInterface::class) ->log(1, 'Social - Duplicate hashtag on Stream ' . json_encode($stream)); } } diff --git a/lib/Service/InstanceService.php b/lib/Service/InstanceService.php index 23f57893..f12654ad 100644 --- a/lib/Service/InstanceService.php +++ b/lib/Service/InstanceService.php @@ -40,14 +40,10 @@ use OCA\Social\Model\Instance; class InstanceService { use TArrayTools; - - private $instancesRequest; - + private InstancesRequest$instancesRequest; private ConfigService $configService; - private MiscService $miscService; - public function __construct( InstancesRequest $instancesRequest, ConfigService $configService, MiscService $miscService ) { diff --git a/lib/Service/PushService.php b/lib/Service/PushService.php index b6bb1645..59f703b9 100644 --- a/lib/Service/PushService.php +++ b/lib/Service/PushService.php @@ -53,18 +53,12 @@ class PushService { use TAsync; private DetailsService $detailsService; - private StreamService $streamService; - private MiscService $miscService; /** * PushService constructor. - * - * @param DetailsService $detailsService - * @param StreamService $streamService - * @param MiscService $miscService */ public function __construct( DetailsService $detailsService, StreamService $streamService, MiscService $miscService diff --git a/lib/Service/SignatureService.php b/lib/Service/SignatureService.php index 32b90608..b58ff31c 100644 --- a/lib/Service/SignatureService.php +++ b/lib/Service/SignatureService.php @@ -42,7 +42,6 @@ use daita\MySmallPhpTools\Traits\TArrayTools; use DateTime; use Exception; use JsonLdException; -use OC; use OCA\Social\AppInfo\Application; use OCA\Social\Db\ActorsRequest; use OCA\Social\Exceptions\ActorDoesNotExistException; @@ -59,11 +58,13 @@ use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\LinkedDataSignature; use OCA\Social\Model\RequestQueue; +use OCP\Files\AppData\IFactory; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; use OCP\IRequest; +use OCP\Server; use stdClass; class SignatureService { @@ -573,7 +574,7 @@ class SignatureService { private static function getContextCacheFolder(): ISimpleFolder { $path = 'context'; - $appData = OC::$server->getAppDataDir(Application::APP_NAME); + $appData = Server::get(IFactory::class)->get(Application::APP_NAME); try { $folder = $appData->getFolder($path); } catch (NotFoundException $e) { diff --git a/lib/hostmeta.php b/lib/hostmeta.php index f16eaae1..7b98292a 100644 --- a/lib/hostmeta.php +++ b/lib/hostmeta.php @@ -31,20 +31,20 @@ namespace OCA\Social; use OCA\Social\Exceptions\SocialAppConfigException; use Exception; -use OC; +use OCP\Server; use OCA\Social\Service\ConfigService; use OCA\Social\Service\FediverseService; +use Psr\Log\LoggerInterface; require_once(__DIR__ . '/../vendor/autoload.php'); try { - $fediverseService = OC::$server->query(FediverseService::class); + $fediverseService = Server::get(FediverseService::class); /** @var ConfigService $configService */ - $configService = OC::$server->query(ConfigService::class); + $configService = Server::get(ConfigService::class); $fediverseService->jailed(); } catch (Exception $e) { - OC::$server->getLogger() - ->log(1, 'Exception on hostmeta - ' . $e->getMessage()); + Server::get(LoggerInterface::class)->log(1, 'Exception on hostmeta - ' . $e->getMessage()); http_response_code(404); exit; } diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index 820766ca..a64f984a 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -1,14 +1,14 @@ - + $level - - $ap->groupInterface - $ap->groupInterface - $ap->groupInterface - + + + + $schema + @@ -137,9 +137,9 @@ $varr[0] !== null - - getAppDataDir - + + IFactory + From 2e0bef515ec674d0ee4caf63cb26f3b815ea960e Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Wed, 11 May 2022 19:13:46 +0200 Subject: [PATCH 2/2] Create instance if it doesn't exist Currently mostly with dummy data, we need to expose these options to the admin Signed-off-by: Carl Schwan --- lib/Db/InstancesRequest.php | 15 ++++++++++++++- lib/Db/SocialCrossQueryBuilder.php | 2 +- lib/Model/Instance.php | 10 +--------- lib/Service/InstanceService.php | 31 +++++++++++++++++------------- lib/Service/SignatureService.php | 4 ++-- 5 files changed, 36 insertions(+), 26 deletions(-) diff --git a/lib/Db/InstancesRequest.php b/lib/Db/InstancesRequest.php index e1492d8e..1eb68c98 100644 --- a/lib/Db/InstancesRequest.php +++ b/lib/Db/InstancesRequest.php @@ -35,6 +35,7 @@ use daita\MySmallPhpTools\Traits\TArrayTools; use OCA\Social\Exceptions\InstanceDoesNotExistException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\Instance; +use OCP\DB\QueryBuilder\IQueryBuilder; /** * Class InstancesRequest @@ -54,7 +55,19 @@ class InstancesRequest extends InstancesRequestBuilder { // $instance->setCreation($now->getTimestamp()); $qb = $this->getInstanceInsertSql(); - $qb->setValue('uri', $qb->createNamedParameter($instance->getUri())); + $qb->setValue('uri', $qb->createNamedParameter($instance->getUri())) + ->setValue('local', $qb->createNamedParameter($instance->isLocal()), IQueryBuilder::PARAM_BOOL) + ->setValue('title', $qb->createNamedParameter($instance->getTitle())) + ->setValue('version', $qb->createNamedParameter($instance->getVersion())) + ->setValue('short_description', $qb->createNamedParameter($instance->getShortDescription())) + ->setValue('description', $qb->createNamedParameter($instance->getDescription())) + ->setValue('email', $qb->createNamedParameter($instance->getEmail())) + ->setValue('urls', $qb->createNamedParameter(json_encode($instance->getUrls()))) + ->setValue('stats', $qb->createNamedParameter(json_encode($instance->getStats()))) + ->setValue('usage', $qb->createNamedParameter(json_encode($instance->getUsage()))) + ->setValue('image', $qb->createNamedParameter($instance->getImage())) + ->setValue('languages', $qb->createNamedParameter(json_encode($instance->getImage()))) + ->setValue('account_prim', $qb->createNamedParameter($instance->getAccountPrim() ? $this->prim($instance->getAccountPrim()) : null)); $qb->executeStatement(); } diff --git a/lib/Db/SocialCrossQueryBuilder.php b/lib/Db/SocialCrossQueryBuilder.php index 474a510c..b30ad4e0 100644 --- a/lib/Db/SocialCrossQueryBuilder.php +++ b/lib/Db/SocialCrossQueryBuilder.php @@ -96,7 +96,7 @@ class SocialCrossQueryBuilder extends SocialCoreQueryBuilder { $expr = $this->expr(); if ($link !== '') { - $this->innerJoin( + $this->leftJoin( $this->getDefaultSelectAlias(), CoreRequestBuilder::TABLE_CACHE_ACTORS, $pf, $expr->eq('ca.id_prim', $link) ); diff --git a/lib/Model/Instance.php b/lib/Model/Instance.php index b99c4e50..ec1a4c26 100644 --- a/lib/Model/Instance.php +++ b/lib/Model/Instance.php @@ -246,18 +246,10 @@ class Instance implements IQueryRow, JsonSerializable { return $this; } - /** - * @return string - */ - public function getAccountPrim(): string { + public function getAccountPrim(): ?string { return $this->accountPrim; } - /** - * @param string $prim - * - * @return Instance - */ public function setAccountPrim(string $prim): self { $this->accountPrim = $prim; diff --git a/lib/Service/InstanceService.php b/lib/Service/InstanceService.php index f12654ad..e1fa915e 100644 --- a/lib/Service/InstanceService.php +++ b/lib/Service/InstanceService.php @@ -36,33 +36,40 @@ use OCA\Social\Db\InstancesRequest; use OCA\Social\Exceptions\InstanceDoesNotExistException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\Instance; +use OCP\IConfig; class InstanceService { use TArrayTools; - private InstancesRequest$instancesRequest; + private InstancesRequest $instancesRequest; private ConfigService $configService; private MiscService $miscService; + private IConfig $config; public function __construct( - InstancesRequest $instancesRequest, ConfigService $configService, MiscService $miscService + InstancesRequest $instancesRequest, + ConfigService $configService, + MiscService $miscService, + IConfig $config ) { $this->instancesRequest = $instancesRequest; $this->configService = $configService; $this->miscService = $miscService; + $this->config = $config; } - - /** - * - */ - public function createLocal(): void { + public function createLocal(): Instance { + $instance = new Instance(); + $instance->setLocal(true) + ->setVersion($this->config->getAppValue('social', 'installed_version', '0.0')) + ->setApprovalRequired(false) + ->setDescription($this->config->getAppValue('theming', 'slogan', 'a safe home for your data')) + ->setTitle($this->config->getAppValue('theming', 'name', 'Nextcloud Social')); + $this->instancesRequest->save($instance); + return $instance; } /** - * @param int $format - * - * @return Instance * @throws InstanceDoesNotExistException */ public function getLocal(int $format = ACore::FORMAT_LOCAL): Instance { @@ -71,8 +78,6 @@ class InstanceService { } catch (InstanceDoesNotExistException $e) { } - $this->createLocal(); - - return $this->instancesRequest->getLocal($format); + return $this->createLocal(); } } diff --git a/lib/Service/SignatureService.php b/lib/Service/SignatureService.php index b58ff31c..5c1b4e86 100644 --- a/lib/Service/SignatureService.php +++ b/lib/Service/SignatureService.php @@ -58,7 +58,7 @@ use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\LinkedDataSignature; use OCA\Social\Model\RequestQueue; -use OCP\Files\AppData\IFactory; +use OCP\Files\AppData\IAppDataFactory; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\Files\SimpleFS\ISimpleFile; @@ -574,7 +574,7 @@ class SignatureService { private static function getContextCacheFolder(): ISimpleFolder { $path = 'context'; - $appData = Server::get(IFactory::class)->get(Application::APP_NAME); + $appData = Server::get(IAppDataFactory::class)->get(Application::APP_NAME); try { $folder = $appData->getFolder($path); } catch (NotFoundException $e) {