Skip to content

Commit

Permalink
Make wellknow work with new infra
Browse files Browse the repository at this point in the history
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
  • Loading branch information
CarlSchwan committed Apr 14, 2022
1 parent f07c9bb commit 0037bb3
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 373 deletions.
16 changes: 4 additions & 12 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
use OCA\Social\Search\UnifiedSearchProvider;
use OCA\Social\Service\ConfigService;
use OCA\Social\Service\UpdateService;
use OCA\Social\WellKnown\WebfingerHandler;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\QueryException;
use OCP\IDBConnection;
use OCP\IServerContainer;
use Throwable;

Expand All @@ -53,16 +55,8 @@
* @package OCA\Social\AppInfo
*/
class Application extends App implements IBootstrap {


const APP_NAME = 'social';


/**
* Application constructor.
*
* @param array $params
*/
public function __construct(array $params = []) {
parent::__construct(self::APP_NAME, $params);
}
Expand All @@ -73,9 +67,7 @@ public function __construct(array $params = []) {
*/
public function register(IRegistrationContext $context): void {
$context->registerSearchProvider(UnifiedSearchProvider::class);

// TODO: nc21, uncomment
// $context->registerEventListener(WellKnownEvent::class, WellKnownListener::class);
$context->registerWellKnownHandler(WebfingerHandler::class);
}


Expand Down Expand Up @@ -114,7 +106,7 @@ protected function checkUpgradeStatus(IServerContainer $container) {
return;
}

$schema = new SchemaWrapper($container->getDatabaseConnection());
$schema = new SchemaWrapper($container->get(IDBConnection::class));
if ($schema->hasTable('social_a2_stream')) {
$updateService->checkUpdateStatus();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/CoreRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function __construct(
) {
$this->dbConnection = $connection;
$this->logger = $logger;
$this->loggerInterface = $loggerInterface;
$this->loggerInterface = $logger;
$this->urlGenerator = $urlGenerator;
$this->configService = $configService;
$this->miscService = $miscService;
Expand Down
83 changes: 0 additions & 83 deletions lib/Listeners/WellKnownListener.php

This file was deleted.

148 changes: 0 additions & 148 deletions lib/Model/WebfingerLink.php

This file was deleted.

77 changes: 77 additions & 0 deletions lib/WellKnown/WebfingerHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace OCA\Social\WellKnown;

use OCA\Social\Db\CacheActorsRequest;
use OCA\Social\Exceptions\CacheActorDoesNotExistException;
use OCA\Social\Service\CacheActorService;
use OCA\Social\Service\ConfigService;
use OCA\Social\Service\FediverseService;
use OCP\Http\WellKnown\IHandler;
use OCP\Http\WellKnown\IRequestContext;
use OCP\Http\WellKnown\IResponse;
use OCP\Http\WellKnown\JrdResponse;
use OCP\IURLGenerator;

class WebfingerHandler implements IHandler {
private IURLGenerator $urlGenerator;
private CacheActorsRequest $cacheActorsRequest;
private CacheActorService $cacheActorService;
private FediverseService $fediverseService;
private ConfigService $configService;

public function __construct(
IURLGenerator $urlGenerator, CacheActorsRequest $cacheActorsRequest,
CacheActorService $cacheActorService, FediverseService $fediverseService,
ConfigService $configService
) {

$this->urlGenerator = $urlGenerator;
$this->cacheActorsRequest = $cacheActorsRequest;
$this->cacheActorService = $cacheActorService;
$this->fediverseService = $fediverseService;
$this->configService = $configService;
}

public function handle(string $service, IRequestContext $context, ?IResponse $previousResponse): ?IResponse {
// See https://docs.joinmastodon.org/spec/webfinger/

$this->fediverseService->jailed();
$subject = $context->getHttpRequest()->getParam('resource');

if (strpos($subject, 'acct:') === 0) {
$subject = substr($subject, 5);
}

try {
$actor = $this->cacheActorService->getFromLocalAccount($subject);
} catch (CacheActorDoesNotExistException $e) {
$actor = $this->cacheActorsRequest->getFromId($subject);
if (!$actor->isLocal()) {
throw new CacheActorDoesNotExistException();
}
}

$response = new JrdResponse($subject);

// ActivityPub profile
$href = $this->configService->getSocialUrl() . '@' . $actor->getPreferredUsername();
$href = rtrim($href, '/');
$response->addAlias($href);
$response->addLink('self', 'application/activity+json', $href);

// Nextcloud profile page
$profilePageUrl = $this->urlGenerator->linkToRouteAbsolute('core.ProfilePage.index', [
'targetUserId' => $actor->getPreferredUsername()
]);
$response->addAlias($profilePageUrl);
$response->addLink('http://webfinger.net/rel/profile-page', 'text/html', $profilePageUrl);

// Ostatus subscribe url
// JrdResponse doesn't support template
// $subscribe = $this->urlGenerator->linkToRouteAbsolute('social.OStatus.subscribe') . '?uri={uri}';
// $response->addLink('http://ostatus.org/schema/1.0/subscribe', $subscribe);

return $response;
}
}
Loading

0 comments on commit 0037bb3

Please sign in to comment.