saving follows in database

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
alpha1
Maxence Lange 2018-11-16 10:48:31 -01:00
rodzic 31867cfbb7
commit ee7e735722
8 zmienionych plików z 194 dodań i 33 usunięć

Wyświetl plik

@ -35,7 +35,9 @@ return [
['name' => 'Local#newPost', 'url' => '/api/v1/post', 'verb' => 'POST'],
['name' => 'Local#timeline', 'url' => '/api/v1/timeline', 'verb' => 'GET'],
['name' => 'Local#direct', 'url' => '/api/v1/direct', 'verb' => 'PUT'],
['name' => 'Local#accountSearch', 'url' => '/api/v1/accounts/search', 'verb' => 'GET'],
['name' => 'Local#accountsSearch', 'url' => '/api/v1/accounts/search', 'verb' => 'GET'],
['name' => 'Local#accountFollow', 'url' => '/api/v1/account/follow', 'verb' => 'PUT'],
['name' => 'Local#accountUnfollow', 'url' => '/api/v1/account/follow', 'verb' => 'DELETE'],
['name' => 'Local#actorInfo', 'url' => '/api/v1/actor/info', 'verb' => 'GET']
]

Wyświetl plik

@ -35,6 +35,7 @@ use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
use Exception;
use OCA\Social\AppInfo\Application;
use OCA\Social\Model\Post;
use OCA\Social\Service\ActivityPub\FollowService;
use OCA\Social\Service\ActivityPub\NoteService;
use OCA\Social\Service\ActivityPub\PersonService;
use OCA\Social\Service\ActorService;
@ -63,6 +64,9 @@ class LocalController extends Controller {
/** @var PersonService */
private $personService;
/** @var FollowService */
private $followService;
/** @var ActorService */
private $actorService;
@ -82,15 +86,16 @@ class LocalController extends Controller {
* @param IRequest $request
* @param string $userId
* @param PersonService $personService
* @param FollowService $followService
* @param ActorService $actorService
* @param PostService $postService
* @param NoteService $noteService
* @param MiscService $miscService
*/
public function __construct(
IRequest $request, string $userId, PersonService $personService, ActorService $actorService,
PostService $postService,
NoteService $noteService,
IRequest $request, string $userId, PersonService $personService,
FollowService $followService, ActorService $actorService,
PostService $postService, NoteService $noteService,
MiscService $miscService
) {
parent::__construct(Application::APP_NAME, $request);
@ -99,6 +104,7 @@ class LocalController extends Controller {
$this->actorService = $actorService;
$this->personService = $personService;
$this->followService = $followService;
$this->postService = $postService;
$this->noteService = $noteService;
$this->miscService = $miscService;
@ -189,7 +195,7 @@ class LocalController extends Controller {
*
* @return DataResponse
*/
public function accountSearch(string $search): DataResponse {
public function accountsSearch(string $search): DataResponse {
try {
$accounts = $this->personService->searchCachedAccounts($search);
@ -200,6 +206,56 @@ class LocalController extends Controller {
}
/**
*
* // 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);
// $accounts = $this->personService->searchCachedAccounts($account);
return $this->success([]);
} catch (Exception $e) {
return $this->fail($e->getMessage());
}
}
/**
*
* // 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) {
return $this->fail($e->getMessage());
}
}
/**
*
* // TODO: Delete the NoCSRF check

Wyświetl plik

@ -153,6 +153,28 @@ class CoreRequestBuilder {
}
/**
* Limit the request to the ServiceId
*
* @param IQueryBuilder $qb
* @param string $actorId
*/
protected function limitToActorId(IQueryBuilder &$qb, string $actorId) {
$this->limitToDBField($qb, 'actor_id', $actorId);
}
/**
* Limit the request to the ServiceId
*
* @param IQueryBuilder $qb
* @param string $objectId
*/
protected function limitToObjectId(IQueryBuilder &$qb, string $objectId) {
$this->limitToDBField($qb, 'object_id', $objectId);
}
/**
* Limit the request to the account
*

Wyświetl plik

@ -32,7 +32,9 @@ namespace OCA\Social\Db;
use Exception;
use OCA\Social\Exceptions\FollowDoesNotExistException;
use OCA\Social\Model\ActivityPub\Follow;
use OCA\Social\Model\ActivityPub\Person;
/**
@ -47,42 +49,49 @@ class FollowsRequest extends FollowsRequestBuilder {
* Insert a new Note in the database.
*
* @param Follow $follow
*
* @return int
* @throws Exception
*/
public function save(Follow $follow): int {
public function save(Follow $follow) {
$qb = $this->getFollowsInsertSql();
$qb->setValue('id', $qb->createNamedParameter($follow->getId()))
->setValue('actor_id', $qb->createNamedParameter($follow->getActorId()))
->setValue('object_id', $qb->createNamedParameter($follow->getObjectId()));
try {
$qb = $this->getFollowsInsertSql();
$qb->setValue('id', $qb->createNamedParameter($follow->getId()))
->setValue('actor_id', $qb->createNamedParameter($follow->getActorId()))
->setValue('object_id', $qb->createNamedParameter($follow->getObjectId()));
$qb->execute();
}
$qb->execute();
return $qb->getLastInsertId();
} catch (Exception $e) {
throw $e;
/**
* @param Person $actor
* @param Person $remote
*
* @return Follow
* @throws FollowDoesNotExistException
*/
public function getByPersons(Person $actor, Person $remote) {
$qb = $this->getFollowsSelectSql();
$this->limitToActorId($qb, $actor->getId());
$this->limitToObjectId($qb, $remote->getId());
$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();
if ($data === false) {
throw new FollowDoesNotExistException();
}
return $this->parseFollowsSelectSql($data);
}
/**
* @param Follow $follow
*
* @throws Exception
*/
public function delete(Follow $follow) {
$qb = $this->getFollowsDeleteSql();
$this->limitToIdString($qb, $follow->getId());
try {
$qb = $this->getFollowsDeleteSql();
$this->limitToIdString($qb, $follow->getId());
$qb->execute();
} catch (Exception $e) {
throw $e;
}
$qb->execute();
}

Wyświetl plik

@ -0,0 +1,8 @@
<?php
namespace OCA\Social\Exceptions;
class FollowDoesNotExistException extends \Exception {
}

Wyświetl plik

@ -133,7 +133,6 @@ abstract class ACore implements JsonSerializable {
return $this->id;
}
/**
* @param string $id
*
@ -145,6 +144,15 @@ abstract class ACore implements JsonSerializable {
return $this;
}
public function generateUniqueId(string $base) {
$uuid = sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff), mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
$this->setId($base . '/' . $uuid);
}
/**
* @return string

Wyświetl plik

@ -33,10 +33,14 @@ namespace OCA\Social\Service\ActivityPub;
use Exception;
use OCA\Social\Db\FollowsRequest;
use OCA\Social\Exceptions\FollowDoesNotExistException;
use OCA\Social\Exceptions\RequestException;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Person;
use OCA\Social\Model\ActivityPub\Follow;
use OCA\Social\Model\ActivityPub\OrderedCollection;
use OCA\Social\Model\ActivityPub\Person;
use OCA\Social\Service\ConfigService;
use OCA\Social\Service\ICoreService;
use OCA\Social\Service\MiscService;
@ -47,6 +51,12 @@ class FollowService implements ICoreService {
/** @var FollowsRequest */
private $followsRequest;
/** @var PersonService */
private $personService;
/** @var ConfigService */
private $configService;
/** @var MiscService */
private $miscService;
@ -55,14 +65,60 @@ class FollowService implements ICoreService {
* NoteService constructor.
*
* @param FollowsRequest $followsRequest
* @param PersonService $personService
* @param ConfigService $configService
* @param MiscService $miscService
*/
public function __construct(FollowsRequest $followsRequest, MiscService $miscService) {
public function __construct(
FollowsRequest $followsRequest, PersonService $personService, ConfigService $configService,
MiscService $miscService
) {
$this->followsRequest = $followsRequest;
$this->personService = $personService;
$this->configService = $configService;
$this->miscService = $miscService;
}
/**
* @param Person $actor
* @param string $account
*
* @throws RequestException
* @throws SocialAppConfigException
*/
public function followAccount(Person $actor, string $account) {
$remoteActor = $this->personService->getFromAccount($account);
$follow = new Follow();
$follow->generateUniqueId($this->configService->getCloudAddress());
$follow->setActorId($actor->getId());
$follow->setObjectId($remoteActor->getId());
try {
$this->followsRequest->getByPersons($actor, $remoteActor);
} catch (FollowDoesNotExistException $e) {
$this->followsRequest->save($follow);
}
}
/**
* @param Person $actor
* @param string $account
*
* @throws RequestException
*/
public function unfollowAccount(Person $actor, string $account) {
$remoteActor = $this->personService->getFromAccount($account);
try {
$follow = $this->followsRequest->getByPersons($actor, $remoteActor);
$this->followsRequest->delete($follow);
} catch (FollowDoesNotExistException $e) {
}
}
/**
* @param Person $actor
*

Wyświetl plik

@ -234,7 +234,7 @@ class ConfigService {
return $result;
}
return $address;
return $this->withoutEndSlash($address, false, false);
}
@ -243,7 +243,7 @@ class ConfigService {
* @throws SocialAppConfigException
*/
public function getRoot(): string {
return $this->withoutEndSlash($this->getCloudAddress(), false, false) . '/apps/social/';
return $this->getCloudAddress() . '/apps/social/';
}