2018-11-12 22:56:22 +00:00
|
|
|
<?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;
|
2018-11-16 10:47:59 +00:00
|
|
|
use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
|
2018-11-12 22:56:22 +00:00
|
|
|
use Exception;
|
|
|
|
use OCA\Social\AppInfo\Application;
|
2018-11-20 22:38:36 +00:00
|
|
|
use OCA\Social\Exceptions\InvalidResourceException;
|
2018-11-12 22:56:22 +00:00
|
|
|
use OCA\Social\Model\Post;
|
2018-11-16 11:48:31 +00:00
|
|
|
use OCA\Social\Service\ActivityPub\FollowService;
|
2018-11-12 22:56:22 +00:00
|
|
|
use OCA\Social\Service\ActivityPub\NoteService;
|
2018-11-14 11:00:05 +00:00
|
|
|
use OCA\Social\Service\ActivityPub\PersonService;
|
2018-11-12 22:56:22 +00:00
|
|
|
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;
|
|
|
|
|
2018-11-14 11:00:05 +00:00
|
|
|
/** @var PersonService */
|
|
|
|
private $personService;
|
|
|
|
|
2018-11-16 11:48:31 +00:00
|
|
|
/** @var FollowService */
|
|
|
|
private $followService;
|
|
|
|
|
2018-11-12 22:56:22 +00:00
|
|
|
/** @var ActorService */
|
|
|
|
private $actorService;
|
|
|
|
|
|
|
|
/** @var PostService */
|
|
|
|
private $postService;
|
|
|
|
|
|
|
|
/** @var NoteService */
|
|
|
|
private $noteService;
|
|
|
|
|
|
|
|
/** @var MiscService */
|
|
|
|
private $miscService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NavigationController constructor.
|
|
|
|
*
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param string $userId
|
2018-11-14 11:00:05 +00:00
|
|
|
* @param PersonService $personService
|
2018-11-16 11:48:31 +00:00
|
|
|
* @param FollowService $followService
|
2018-11-12 22:56:22 +00:00
|
|
|
* @param ActorService $actorService
|
|
|
|
* @param PostService $postService
|
|
|
|
* @param NoteService $noteService
|
|
|
|
* @param MiscService $miscService
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2018-11-16 11:48:31 +00:00
|
|
|
IRequest $request, string $userId, PersonService $personService,
|
|
|
|
FollowService $followService, ActorService $actorService,
|
|
|
|
PostService $postService, NoteService $noteService,
|
2018-11-12 22:56:22 +00:00
|
|
|
MiscService $miscService
|
|
|
|
) {
|
|
|
|
parent::__construct(Application::APP_NAME, $request);
|
|
|
|
|
|
|
|
$this->userId = $userId;
|
|
|
|
|
|
|
|
$this->actorService = $actorService;
|
2018-11-14 11:00:05 +00:00
|
|
|
$this->personService = $personService;
|
2018-11-16 11:48:31 +00:00
|
|
|
$this->followService = $followService;
|
2018-11-12 22:56:22 +00:00
|
|
|
$this->postService = $postService;
|
|
|
|
$this->noteService = $noteService;
|
|
|
|
$this->miscService = $miscService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new post.
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
2018-11-20 22:38:36 +00:00
|
|
|
public function postCreate(array $data): DataResponse {
|
2018-11-12 22:56:22 +00:00
|
|
|
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, ''));
|
2018-11-13 18:05:58 +00:00
|
|
|
$post->setType($this->get('type', $data, NoteService::TYPE_PUBLIC));
|
2018-11-12 22:56:22 +00:00
|
|
|
|
|
|
|
$result = $this->postService->createPost($post);
|
|
|
|
|
|
|
|
return $this->success($result);
|
|
|
|
} catch (Exception $e) {
|
2018-11-20 22:38:36 +00:00
|
|
|
return $this->fail($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new post.
|
|
|
|
*
|
|
|
|
* // TODO: Delete the NoCSRF check
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function postDelete(string $id): DataResponse {
|
|
|
|
try {
|
|
|
|
$note = $this->noteService->getNoteById($id);
|
|
|
|
$actor = $this->actorService->getActorFromUserId($this->userId);
|
|
|
|
|
|
|
|
if ($note->getAttributedTo() !== $actor->getId()) {
|
|
|
|
throw new InvalidResourceException('user have no rights');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->noteService->delete($note);
|
|
|
|
|
|
|
|
return $this->success();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $this->fail($e);
|
2018-11-12 22:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get timeline
|
|
|
|
*
|
2018-11-14 11:00:05 +00:00
|
|
|
* // TODO: Delete the NoCSRF check
|
|
|
|
*
|
2018-11-12 22:56:22 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
2018-11-13 14:20:43 +00:00
|
|
|
public function timeline($since = 0, $limit = 5): DataResponse {
|
2018-11-12 22:56:22 +00:00
|
|
|
|
|
|
|
// $this->miscService->log('timeline: ' . json_encode($data));
|
|
|
|
|
|
|
|
try {
|
2018-11-14 11:06:51 +00:00
|
|
|
$posts = $this->noteService->getTimeline((int)$since, (int)$limit);
|
2018-11-12 22:56:22 +00:00
|
|
|
|
|
|
|
return $this->success($posts);
|
|
|
|
} catch (Exception $e) {
|
2018-11-20 22:38:36 +00:00
|
|
|
return $this->fail($e);
|
2018-11-12 22:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) {
|
2018-11-20 22:38:36 +00:00
|
|
|
return $this->fail($e);
|
2018-11-12 22:56:22 +00:00
|
|
|
}
|
2018-11-14 11:00:05 +00:00
|
|
|
}
|
2018-11-12 22:56:22 +00:00
|
|
|
|
|
|
|
|
2018-11-14 11:00:05 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* // TODO: Delete the NoCSRF check
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $search
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
2018-11-16 11:48:31 +00:00
|
|
|
public function accountsSearch(string $search): DataResponse {
|
2018-11-14 11:00:05 +00:00
|
|
|
try {
|
2018-11-15 11:26:25 +00:00
|
|
|
$accounts = $this->personService->searchCachedAccounts($search);
|
2018-11-14 11:00:05 +00:00
|
|
|
|
|
|
|
return $this->success(['accounts' => $accounts]);
|
|
|
|
} catch (Exception $e) {
|
2018-11-20 22:38:36 +00:00
|
|
|
return $this->fail($e);
|
2018-11-14 11:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-16 11:48:31 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* // TODO: Delete the NoCSRF check
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $account
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function accountFollow(string $account): DataResponse {
|
|
|
|
try {
|
|
|
|
$actor = $this->actorService->getActorFromUserId($this->userId);
|
|
|
|
$this->followService->followAccount($actor, $account);
|
|
|
|
|
|
|
|
return $this->success([]);
|
|
|
|
} catch (Exception $e) {
|
2018-11-20 22:38:36 +00:00
|
|
|
return $this->fail($e);
|
2018-11-16 11:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* // TODO: Delete the NoCSRF check
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $account
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function accountUnfollow(string $account): DataResponse {
|
|
|
|
try {
|
|
|
|
$actor = $this->actorService->getActorFromUserId($this->userId);
|
|
|
|
$this->followService->unfollowAccount($actor, $account);
|
|
|
|
|
|
|
|
return $this->success([]);
|
|
|
|
} catch (Exception $e) {
|
2018-11-20 22:38:36 +00:00
|
|
|
return $this->fail($e);
|
2018-11-16 11:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-14 11:00:05 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* // TODO: Delete the NoCSRF check
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function actorInfo(string $id): DataResponse {
|
|
|
|
try {
|
|
|
|
$actor = $this->personService->getFromId($id);
|
|
|
|
|
|
|
|
return $this->success(['actor' => $actor]);
|
|
|
|
} catch (Exception $e) {
|
2018-11-20 22:38:36 +00:00
|
|
|
return $this->fail($e);
|
2018-11-14 11:00:05 +00:00
|
|
|
}
|
2018-11-12 22:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|