Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/20/head
Maxence Lange 2018-11-12 21:56:22 -01:00
rodzic 88ebb11dbd
commit 96d5e69dee
6 zmienionych plików z 276 dodań i 44 usunięć

Wyświetl plik

@ -12,25 +12,27 @@ return [
['name' => 'Navigation#navigate', 'url' => '/', 'verb' => 'GET'], ['name' => 'Navigation#navigate', 'url' => '/', 'verb' => 'GET'],
['name' => 'Navigation#timeline', 'url' => '/timeline/{path}', 'verb' => 'GET', 'requirements' => ['path' => '.+'], 'defaults' => ['path' => '']], ['name' => 'Navigation#timeline', 'url' => '/timeline/{path}', 'verb' => 'GET', 'requirements' => ['path' => '.+'], 'defaults' => ['path' => '']],
['name' => 'Navigation#account', 'url' => '/account/{path}', 'verb' => 'GET', 'requirements' => ['path' => '.+'], 'defaults' => ['path' => '']], ['name' => 'Navigation#account', 'url' => '/account/{path}', 'verb' => 'GET', 'requirements' => ['path' => '.+'], 'defaults' => ['path' => '']],
['name' => 'Navigation#public', 'url' => '/{username}', 'verb' => 'GET'], // ['name' => 'Navigation#public', 'url' => '/{username}', 'verb' => 'GET'],
// ['name' => 'Account#create', 'url' => '/local/account/{username}', 'verb' => 'POST'], // ['name' => 'Account#create', 'url' => '/local/account/{username}', 'verb' => 'POST'],
['name' => 'Account#info', 'url' => '/local/account/{username}', 'verb' => 'GET'], ['name' => 'Account#info', 'url' => '/local/account/{username}', 'verb' => 'GET'],
['name' => 'ActivityPub#sharedInbox', 'url' => '/inbox', 'verb' => 'POST'],
['name' => 'ActivityPub#actor', 'url' => '/users/{username}', 'verb' => 'GET'], ['name' => 'ActivityPub#actor', 'url' => '/users/{username}', 'verb' => 'GET'],
['name' => 'ActivityPub#aliasactor', 'url' => '/@{username}', 'verb' => 'GET'], ['name' => 'ActivityPub#actorAlias', 'url' => '/@{username}', 'verb' => 'GET'],
['name' => 'ActivityPub#inbox', 'url' => '/@{username}/inbox', 'verb' => 'POST'], ['name' => 'ActivityPub#inbox', 'url' => '/@{username}/inbox', 'verb' => 'POST'],
['name' => 'ActivityPub#sharedInbox', 'url' => '/inbox', 'verb' => 'POST'],
['name' => 'ActivityPub#outbox', 'url' => '/@{username}/outbox', 'verb' => 'POST'], ['name' => 'ActivityPub#outbox', 'url' => '/@{username}/outbox', 'verb' => 'POST'],
['name' => 'ActivityPub#followers', 'url' => '/@{username}/followers', 'verb' => 'GET'], ['name' => 'ActivityPub#followers', 'url' => '/@{username}/followers', 'verb' => 'GET'],
['name' => 'ActivityPub#following', 'url' => '/@{username}/following', 'verb' => 'GET'], ['name' => 'ActivityPub#following', 'url' => '/@{username}/following', 'verb' => 'GET'],
['name' => 'SocialPub#displayPost', 'url' => '/@{username}/{postId}', 'verb' => 'GET']
,
['name' => 'ActivityPub#test', 'url' => '/inbox/{username}', 'verb' => 'POST'], ['name' => 'ActivityPub#test', 'url' => '/inbox/{username}', 'verb' => 'POST'],
['name' => 'SocialPub#displayPost', 'url' => '/@{username}/{postId}', 'verb' => 'GET'],
['name' => 'Local#newPost', 'url' => '/api/v1/post', 'verb' => 'POST'],
['name' => 'Local#timeline', 'url' => '/api/v1/timeline', 'verb' => 'PUT'],
['name' => 'Local#direct', 'url' => '/api/v1/direct', 'verb' => 'PUT']
] ]
]; ];

Wyświetl plik

@ -27,8 +27,10 @@ declare(strict_types=1);
* *
*/ */
namespace OCA\Social\Controller; namespace OCA\Social\Controller;
use daita\MySmallPhpTools\Traits\TNCDataResponse; use daita\MySmallPhpTools\Traits\TNCDataResponse;
use Exception; use Exception;
use OCA\Social\AppInfo\Application; use OCA\Social\AppInfo\Application;
@ -39,7 +41,6 @@ use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty; use OCP\Accounts\IAccountProperty;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\Response;
use OCP\IRequest; use OCP\IRequest;
use OCP\IUserManager; use OCP\IUserManager;
@ -47,11 +48,16 @@ use OCP\IUserManager;
class AccountController extends Controller { class AccountController extends Controller {
use TNCDataResponse; use TNCDataResponse;
/** @var string */ /** @var string */
private $userId; private $userId;
/** @var IUserManager */
private $userManager;
/** @var ConfigService */ /** @var ConfigService */
private $configService; private $configService;
@ -69,24 +75,27 @@ class AccountController extends Controller {
* AccountController constructor. * AccountController constructor.
* *
* @param IRequest $request * @param IRequest $request
* @param string $userId * @param IUserManager $userManager
* @param ConfigService $configService * @param ConfigService $configService
* @param ActorService $actorService * @param ActorService $actorService
* @param MiscService $miscService * @param MiscService $miscService
* @param IAccountManager $accountManager
* @param string $userId
*/ */
public function __construct( public function __construct(
IRequest $request, ConfigService $configService, IRequest $request, $userId, IUserManager $userManager, ConfigService $configService,
ActorService $actorService, MiscService $miscService, ActorService $actorService, MiscService $miscService,
IAccountManager $accountManager, IUserManager $userManager, string $userId = null IAccountManager $accountManager
) { ) {
parent::__construct(Application::APP_NAME, $request); parent::__construct(Application::APP_NAME, $request);
$this->userId = $userId; $this->userId = $userId;
$this->userManager = $userManager;
$this->accountManager = $accountManager;
$this->configService = $configService; $this->configService = $configService;
$this->actorService = $actorService; $this->actorService = $actorService;
$this->miscService = $miscService; $this->miscService = $miscService;
$this->accountManager = $accountManager;
$this->userManager = $userManager;
} }
@ -94,8 +103,6 @@ class AccountController extends Controller {
* Called by the frontend to create a new Social account * Called by the frontend to create a new Social account
* *
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired
* @NoAdminRequired
* @NoSubAdminRequired * @NoSubAdminRequired
* *
* @param string $username * @param string $username
@ -112,13 +119,15 @@ class AccountController extends Controller {
} }
} }
/** /**
* @PublicPage * @PublicPage
* @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
* @NoAdminRequired * @NoAdminRequired
* @NoSubAdminRequired * @NoSubAdminRequired
*
* @param string $username * @param string $username
*
* @return DataResponse * @return DataResponse
*/ */
public function info(string $username): Response { public function info(string $username): Response {
@ -131,16 +140,25 @@ class AccountController extends Controller {
$props['posts'] = 1; $props['posts'] = 1;
$props['following'] = 2; $props['following'] = 2;
$props['followers'] = 3; $props['followers'] = 3;
return new DataResponse($props); return new DataResponse($props);
} }
$account = $this->accountManager->getAccount($user); $account = $this->accountManager->getAccount($user);
/** @var IAccountProperty[] $props */ /** @var IAccountProperty[] $props */
$props = $account->getFilteredProperties(IAccountManager::VISIBILITY_PUBLIC, null); $props = $account->getFilteredProperties(IAccountManager::VISIBILITY_PUBLIC, null);
if ($this->userId !== null) { if ($this->userId !== null) {
$props = array_merge($props, $account->getFilteredProperties(IAccountManager::VISIBILITY_CONTACTS_ONLY, null)); $props = array_merge(
$props,
$account->getFilteredProperties(IAccountManager::VISIBILITY_CONTACTS_ONLY, null)
);
} }
if (\array_key_exists('avatar', $props)) { if (\array_key_exists('avatar', $props)) {
$props['avatar']->setValue(\OC::$server->getURLGenerator()->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $username, 'size' => 128])); $props['avatar']->setValue(
\OC::$server->getURLGenerator()
->linkToRouteAbsolute(
'core.avatar.getAvatar', ['userId' => $username, 'size' => 128]
)
);
} }
// Add counters // Add counters
@ -148,6 +166,7 @@ class AccountController extends Controller {
$props['posts'] = 1; $props['posts'] = 1;
$props['following'] = 2; $props['following'] = 2;
$props['followers'] = 3; $props['followers'] = 3;
return new DataResponse($props); return new DataResponse($props);
} }

Wyświetl plik

@ -33,8 +33,11 @@ namespace OCA\Social\Controller;
use daita\MySmallPhpTools\Traits\TNCDataResponse; use daita\MySmallPhpTools\Traits\TNCDataResponse;
use Exception; use Exception;
use OCA\Social\AppInfo\Application; use OCA\Social\AppInfo\Application;
use OCA\Social\Service\ActivityPubService; use OCA\Social\Db\NotesRequest;
use OCA\Social\Service\ActivityPub\FollowService;
use OCA\Social\Service\ActivityService;
use OCA\Social\Service\ActorService; use OCA\Social\Service\ActorService;
use OCA\Social\Service\ImportService;
use OCA\Social\Service\MiscService; use OCA\Social\Service\MiscService;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\Response;
@ -50,12 +53,21 @@ class ActivityPubController extends Controller {
/** @var SocialPubController */ /** @var SocialPubController */
private $socialPubController; private $socialPubController;
/** @var ActivityPubService */ /** @var ActivityService */
private $activityPubService; private $activityService;
/** @var ImportService */
private $importService;
/** @var FollowService */
private $followService;
/** @var ActorService */ /** @var ActorService */
private $actorService; private $actorService;
/** @var NotesRequest */
private $notesRequest;
/** @var MiscService */ /** @var MiscService */
private $miscService; private $miscService;
@ -63,22 +75,30 @@ class ActivityPubController extends Controller {
/** /**
* ActivityPubController constructor. * ActivityPubController constructor.
* *
* @param SocialPubController $socialPubController
* @param ActivityPubService $activityPubService
* @param ActorService $actorService
* @param IRequest $request * @param IRequest $request
* @param SocialPubController $socialPubController
* @param ActivityService $activityService
* @param ImportService $importService
* @param FollowService $followService
* @param ActorService $actorService
* @param NotesRequest $notesRequest
* @param MiscService $miscService * @param MiscService $miscService
*/ */
public function __construct( public function __construct(
SocialPubController $socialPubController, ActivityPubService $activityPubService, IRequest $request, SocialPubController $socialPubController,
ActorService $actorService, IRequest $request, ActivityService $activityService, ImportService $importService,
FollowService $followService, ActorService $actorService, NotesRequest $notesRequest,
MiscService $miscService MiscService $miscService
) { ) {
parent::__construct(Application::APP_NAME, $request); parent::__construct(Application::APP_NAME, $request);
$this->socialPubController = $socialPubController; $this->socialPubController = $socialPubController;
$this->activityPubService = $activityPubService;
$this->activityService = $activityService;
$this->importService = $importService;
$this->followService = $followService;
$this->actorService = $actorService; $this->actorService = $actorService;
$this->notesRequest = $notesRequest;
$this->miscService = $miscService; $this->miscService = $miscService;
} }
@ -104,9 +124,8 @@ class ActivityPubController extends Controller {
} }
try { try {
// $this->activityPubService->generateActor($userId);
$actor = $this->actorService->getActor($username); $actor = $this->actorService->getActor($username);
$actor->setTopLevel(true);
return $this->directSuccess($actor); return $this->directSuccess($actor);
} catch (Exception $e) { } catch (Exception $e) {
@ -114,6 +133,7 @@ class ActivityPubController extends Controller {
} }
} }
/** /**
* Alias to the actor() method. * Alias to the actor() method.
* *
@ -127,7 +147,7 @@ class ActivityPubController extends Controller {
* *
* @return Response * @return Response
*/ */
public function aliasactor(string $username): Response { public function actorAlias(string $username): Response {
return $this->actor($username); return $this->actor($username);
} }
@ -155,18 +175,24 @@ class ActivityPubController extends Controller {
* @NoCSRFRequired * @NoCSRFRequired
* @PublicPage * @PublicPage
* *
* @param $username * @param string $username
* *
* @return Response * @return Response
*/ */
public function inbox(string $username): Response { public function inbox(string $username): Response {
try { try {
$this->actorService->getActor($username); $actor = $this->actorService->getActor($username);
$this->activityPubService->checkRequest($this->request); $this->activityService->checkRequest($this->request);
// $this->noteService->receiving(file_get_contents('php://input'));
$body = file_get_contents('php://input'); $body = file_get_contents('php://input');
$activity = $this->importService->import($body);
try {
$this->activityService->save($activity);
} catch (Exception $e) {
}
return $this->success([]); return $this->success([]);
} catch (Exception $e) { } catch (Exception $e) {
return $this->fail($e->getMessage()); return $this->fail($e->getMessage());
@ -212,12 +238,21 @@ class ActivityPubController extends Controller {
* *
* @return Response * @return Response
*/ */
public function followers(string $username): Response { public function followers(string $username, $data): Response {
if (!$this->checkSourceActivityStreams()) { if (!$this->checkSourceActivityStreams()) {
return $this->socialPubController->followers($username); return $this->socialPubController->followers($username);
} }
return $this->success([$username]); try {
$actor = $this->actorService->getActor($username);
$followers = $this->followService->getFollowers($actor);
$followers->setTopLevel(true);
return $this->directSuccess($followers);
} catch (Exception $e) {
return $this->fail($e->getMessage());
}
} }

Wyświetl plik

@ -0,0 +1,171 @@
<?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 2018, 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\Controller;
use daita\MySmallPhpTools\Traits\TArrayTools;
use daita\MySmallPhpTools\Traits\TNCDataResponse;
use Exception;
use OCA\Social\AppInfo\Application;
use OCA\Social\Model\Post;
use OCA\Social\Service\ActivityPub\NoteService;
use OCA\Social\Service\ActorService;
use OCA\Social\Service\MiscService;
use OCA\Social\Service\PostService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
/**
* Class LocalController
*
* @package OCA\Social\Controller
*/
class LocalController extends Controller {
use TArrayTools;
use TNCDataResponse;
/** @var string */
private $userId;
/** @var ActorService */
private $actorService;
/** @var PostService */
private $postService;
/** @var NoteService */
private $noteService;
/** @var MiscService */
private $miscService;
/**
* NavigationController constructor.
*
* @param IRequest $request
* @param string $userId
* @param ActorService $actorService
* @param PostService $postService
* @param NoteService $noteService
* @param MiscService $miscService
*/
public function __construct(
IRequest $request, string $userId, ActorService $actorService, PostService $postService,
NoteService $noteService,
MiscService $miscService
) {
parent::__construct(Application::APP_NAME, $request);
$this->userId = $userId;
$this->actorService = $actorService;
$this->postService = $postService;
$this->noteService = $noteService;
$this->miscService = $miscService;
}
/**
* Create a new post.
*
* @NoAdminRequired
* @NoSubAdminRequired
*
* @param array $data
*
* @return DataResponse
*/
public function newPost(array $data): DataResponse {
try {
$post = new Post($this->userId);
$post->setContent($this->get('content', $data, ''));
$post->setReplyTo($this->get('replyTo', $data, ''));
$post->setTo($this->getArray('to', $data, []));
$post->addTo($this->get('to', $data, ''));
$result = $this->postService->createPost($post);
return $this->success($result);
} catch (Exception $e) {
return $this->fail($e->getMessage());
}
}
/**
* Get timeline
*
* @NoCSRFRequired
* @NoAdminRequired
* @NoSubAdminRequired
*
* @return DataResponse
*/
public function timeline(): DataResponse {
// $this->miscService->log('timeline: ' . json_encode($data));
try {
$posts = $this->noteService->getTimeline();
return $this->success($posts);
} catch (Exception $e) {
return $this->fail($e->getMessage());
}
}
/**
* @NoAdminRequired
* @NoSubAdminRequired
*
* @return DataResponse
*/
public function direct(): DataResponse {
try {
$actor = $this->actorService->getActorFromUserId($this->userId);
$posts = $this->noteService->getNotesForActor($actor);
return $this->success($posts);
} catch (Exception $e) {
return $this->fail($e->getMessage());
}
}
}

Wyświetl plik

@ -30,6 +30,7 @@ declare(strict_types=1);
namespace OCA\Social\Controller; namespace OCA\Social\Controller;
use daita\MySmallPhpTools\Traits\TArrayTools;
use OC\Accounts\AccountManager; use OC\Accounts\AccountManager;
use OC\User\NoUserException; use OC\User\NoUserException;
use OCA\Social\AppInfo\Application; use OCA\Social\AppInfo\Application;
@ -47,6 +48,9 @@ use OCP\IURLGenerator;
class NavigationController extends Controller { class NavigationController extends Controller {
use TArrayTools;
/** @var string */ /** @var string */
private $userId; private $userId;
@ -114,8 +118,6 @@ class NavigationController extends Controller {
// we do nothing // we do nothing
} }
return new TemplateResponse(Application::APP_NAME, 'main', $data); return new TemplateResponse(Application::APP_NAME, 'main', $data);
} }
@ -152,10 +154,12 @@ class NavigationController extends Controller {
* @PublicPage * @PublicPage
* *
* @param $username * @param $username
*
* @return RedirectResponse|PublicTemplateResponse * @return RedirectResponse|PublicTemplateResponse
*/ */
public function public($username) { public function public($username) {
if (\OC::$server->getUserSession()->isLoggedIn()) { if (\OC::$server->getUserSession()
->isLoggedIn()) {
return $this->navigate(); return $this->navigate();
} }
@ -166,6 +170,7 @@ class NavigationController extends Controller {
]; ];
$page = new PublicTemplateResponse(Application::APP_NAME, 'main', $data); $page = new PublicTemplateResponse(Application::APP_NAME, 'main', $data);
$page->setHeaderTitle($this->l10n->t('Social') . ' ' . $username); $page->setHeaderTitle($this->l10n->t('Social') . ' ' . $username);
return $page; return $page;
} }

Wyświetl plik

@ -32,7 +32,7 @@ namespace OCA\Social\Controller;
use daita\MySmallPhpTools\Traits\TNCDataResponse; use daita\MySmallPhpTools\Traits\TNCDataResponse;
use OCA\Social\AppInfo\Application; use OCA\Social\AppInfo\Application;
use OCA\Social\Service\ActivityPubService; use OCA\Social\Service\ActivityService;
use OCA\Social\Service\ActorService; use OCA\Social\Service\ActorService;
use OCA\Social\Service\MiscService; use OCA\Social\Service\MiscService;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
@ -45,8 +45,8 @@ class SocialPubController extends Controller {
use TNCDataResponse; use TNCDataResponse;
/** @var ActivityPubService */ /** @var ActivityService */
private $activityPubService; private $activityService;
/** @var ActorService */ /** @var ActorService */
private $actorService; private $actorService;
@ -58,18 +58,18 @@ class SocialPubController extends Controller {
/** /**
* SocialPubController constructor. * SocialPubController constructor.
* *
* @param ActivityPubService $activityPubService * @param ActivityService $activityService
* @param ActorService $actorService * @param ActorService $actorService
* @param IRequest $request * @param IRequest $request
* @param MiscService $miscService * @param MiscService $miscService
*/ */
public function __construct( public function __construct(
ActivityPubService $activityPubService, ActorService $actorService, IRequest $request, ActivityService $activityService, ActorService $actorService, IRequest $request,
MiscService $miscService MiscService $miscService
) { ) {
parent::__construct(Application::APP_NAME, $request); parent::__construct(Application::APP_NAME, $request);
$this->activityPubService = $activityPubService; $this->activityService = $activityService;
$this->actorService = $actorService; $this->actorService = $actorService;
$this->miscService = $miscService; $this->miscService = $miscService;
} }