Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/40/head
Maxence Lange 2018-11-20 11:47:14 -01:00
rodzic 86e90ffef8
commit 62f713f434
5 zmienionych plików z 161 dodań i 99 usunięć

Wyświetl plik

@ -34,6 +34,7 @@ use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
use Exception;
use OCA\Social\AppInfo\Application;
use OCA\Social\Db\NotesRequest;
use OCA\Social\Exceptions\UnknownItemException;
use OCA\Social\Service\ActivityPub\FollowService;
use OCA\Social\Service\ActivityService;
use OCA\Social\Service\ActorService;
@ -125,6 +126,7 @@ class ActivityPubController extends Controller {
try {
$actor = $this->actorService->getActor($username);
// $actor->setTopLevel(true);
return $this->directSuccess($actor);
@ -161,7 +163,23 @@ class ActivityPubController extends Controller {
* @return Response
*/
public function sharedInbox(): Response {
return $this->success([]);
try {
$this->activityService->checkRequest($this->request);
$body = file_get_contents('php://input');
$this->miscService->log('Shared Inbox: ' . $body);
$activity = $this->importService->import($body);
try {
$this->importService->parse($activity);
} catch (UnknownItemException $e) {
}
return $this->success([]);
} catch (Exception $e) {
return $this->fail($e->getMessage());
}
}
@ -186,12 +204,12 @@ class ActivityPubController extends Controller {
$this->activityService->checkRequest($this->request);
$body = file_get_contents('php://input');
$this->miscService->log('Body: ' . $body);
$this->miscService->log('Inbox: ' . $body);
$activity = $this->importService->import($body);
try {
$this->importService->save($activity);
} catch (Exception $e) {
$this->importService->parse($activity);
} catch (UnknownItemException $e) {
}
return $this->success([]);
@ -248,6 +266,7 @@ class ActivityPubController extends Controller {
try {
$actor = $this->actorService->getActor($username);
$followers = $this->followService->getFollowers($actor);
// $followers->setTopLevel(true);
return $this->directSuccess($followers);

Wyświetl plik

@ -31,6 +31,7 @@ namespace OCA\Social\Db;
use DateTime;
use OCA\Social\Exceptions\NoteNotFoundException;
use OCA\Social\Model\ActivityPub\Note;
use OCA\Social\Service\ActivityService;
use OCA\Social\Service\ConfigService;
@ -61,52 +62,72 @@ class NotesRequest extends NotesRequestBuilder {
* @param Note $note
*
* @return int
* @throws \Exception
*/
public function save(Note $note): int {
try {
$dTime = new DateTime();
$dTime->setTimestamp($note->getPublishedTime());
$dTime = new DateTime();
$dTime->setTimestamp($note->getPublishedTime());
$qb = $this->getNotesInsertSql();
$qb->setValue('id', $qb->createNamedParameter($note->getId()))
->setValue('to', $qb->createNamedParameter($note->getTo()))
->setValue(
'to_array', $qb->createNamedParameter(
json_encode($note->getToArray(), JSON_UNESCAPED_SLASHES)
)
)
->setValue(
'cc', $qb->createNamedParameter(
json_encode($note->getCcArray(), JSON_UNESCAPED_SLASHES)
)
)
->setValue(
'bcc', $qb->createNamedParameter(
json_encode($note->getBccArray()), JSON_UNESCAPED_SLASHES
)
)
->setValue('content', $qb->createNamedParameter($note->getContent()))
->setValue('summary', $qb->createNamedParameter($note->getSummary()))
->setValue('published', $qb->createNamedParameter($note->getPublished()))
->setValue(
'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE)
)
->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo()))
->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo()))
->setValue('source', $qb->createNamedParameter($note->getSource()))
->setValue(
'creation',
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
);
$qb = $this->getNotesInsertSql();
$qb->setValue('id', $qb->createNamedParameter($note->getId()))
->setValue('to', $qb->createNamedParameter($note->getTo()))
->setValue(
'to_array', $qb->createNamedParameter(
json_encode($note->getToArray(), JSON_UNESCAPED_SLASHES)
)
)
->setValue(
'cc', $qb->createNamedParameter(
json_encode($note->getCcArray(), JSON_UNESCAPED_SLASHES)
)
)
->setValue(
'bcc', $qb->createNamedParameter(
json_encode($note->getBccArray()), JSON_UNESCAPED_SLASHES
)
)
->setValue('content', $qb->createNamedParameter($note->getContent()))
->setValue('summary', $qb->createNamedParameter($note->getSummary()))
->setValue('published', $qb->createNamedParameter($note->getPublished()))
->setValue(
'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE)
)
->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo()))
->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo()))
->setValue('source', $qb->createNamedParameter($note->getSource()))
->setValue(
'creation',
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
);
$qb->execute();
$qb->execute();
return $qb->getLastInsertId();
}
return $qb->getLastInsertId();
} catch (\Exception $e) {
throw $e;
/**
* @param string $id
*
* @return Note
* @throws NoteNotFoundException
*/
public function getFromId(string $id): Note {
if ($id === '') {
throw new NoteNotFoundException();
};
$qb = $this->getNotesSelectSql();
$this->limitToIdString($qb, $id);
$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();
if ($data === false) {
throw new NoteNotFoundException();
}
return $this->parseNotesSelectSql($data);
}
@ -153,4 +174,15 @@ class NotesRequest extends NotesRequestBuilder {
}
/**
* @param string $id
*/
public function deleteNoteById(string $id) {
$qb = $this->getNotesDeleteSql();
$this->limitToIdString($qb, $id);
$qb->execute();
}
}

Wyświetl plik

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

Wyświetl plik

@ -33,7 +33,9 @@ namespace OCA\Social\Service\ActivityPub;
use Exception;
use OC\User\NoUserException;
use OCA\Social\Db\NotesRequest;
use OCA\Social\Exceptions\ActivityCantBeVerifiedException;
use OCA\Social\Exceptions\ActorDoesNotExistException;
use OCA\Social\Exceptions\NoteNotFoundException;
use OCA\Social\Exceptions\RequestException;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Model\ActivityPub\ACore;
@ -226,9 +228,9 @@ class NoteService implements ICoreService {
*
* @param ACore $note
*
* @throws Exception
* @throws ActivityCantBeVerifiedException
*/
public function save(ACore $note) {
public function parse(ACore $note) {
/** @var Note $note */
if ($note->isRoot()) {
return;
@ -241,9 +243,21 @@ class NoteService implements ICoreService {
if ($parent->getType() === Create::TYPE) {
$parent->verify(($note->getAttributedTo()));
$this->notesRequest->save($note);
try {
$this->notesRequest->getFromId($note->getId());
} catch (NoteNotFoundException $e) {
$this->notesRequest->save($note);
}
}
}
/**
* @param ACore $item
*/
public function delete(ACore $item) {
/** @var Note $item */
$this->notesRequest->deleteNoteById($item->getId());
}

Wyświetl plik

@ -35,6 +35,7 @@ use daita\MySmallPhpTools\Traits\TArrayTools;
use DateTime;
use Exception;
use OCA\Social\Db\ActorsRequest;
use OCA\Social\Db\NotesRequest;
use OCA\Social\Exceptions\ActorDoesNotExistException;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Exceptions\RequestException;
@ -65,6 +66,9 @@ class ActivityService {
/** @var ActorsRequest */
private $actorsRequest;
/** @var NotesRequest */
private $notesRequest;
/** @var ActorService */
private $actorService;
@ -88,6 +92,7 @@ class ActivityService {
* ActivityService constructor.
*
* @param ActorsRequest $actorsRequest
* @param NotesRequest $notesRequest
* @param CurlService $curlService
* @param ActorService $actorService
* @param PersonService $personService
@ -96,13 +101,15 @@ class ActivityService {
* @param MiscService $miscService
*/
public function __construct(
ActorsRequest $actorsRequest, CurlService $curlService, ActorService $actorService,
ActorsRequest $actorsRequest, NotesRequest $notesRequest, CurlService $curlService,
ActorService $actorService,
PersonService $personService, InstanceService $instanceService,
ConfigService $configService,
MiscService $miscService
) {
$this->curlService = $curlService;
$this->actorsRequest = $actorsRequest;
$this->notesRequest = $notesRequest;
$this->actorService = $actorService;
$this->personService = $personService;
$this->instanceService = $instanceService;
@ -111,12 +118,6 @@ class ActivityService {
}
public function test() {
}
/**
* @param Person $actor
* @param ACore $item
@ -126,6 +127,7 @@ class ActivityService {
* @return array
* @throws RequestException
* @throws SocialAppConfigException
* @throws ActorDoesNotExistException
*/
public function createActivity(Person $actor, ACore $item, int $type, ACore &$activity = null
): array {
@ -153,12 +155,41 @@ class ActivityService {
}
/**
* @param string $id
*
* @return ACore
* @throws InvalidResourceException
*/
public function getItem(string $id): ACore {
if ($id === '') {
throw new InvalidResourceException();
}
$requests = [
$this->notesRequest
];
foreach ($requests as $request) {
try {
$toDelete = $request->getFromId($id);
return $toDelete;
} catch (Exception $e) {
}
}
throw new InvalidResourceException();
}
/**
* @param ACore $activity
* @param int $type
*
* @throws RequestException
* @throws SocialAppConfigException
* @throws ActorDoesNotExistException
*/
public function manageRequest(ACore $activity, int $type) {
$result = $this->request($activity, $type);
@ -175,6 +206,7 @@ class ActivityService {
* @return array
* @throws RequestException
* @throws SocialAppConfigException
* @throws ActorDoesNotExistException
*/
public function request(ACore &$activity, int $type) {
$this->setupCore($activity);
@ -206,7 +238,7 @@ class ActivityService {
): array {
$document = json_encode($activity);
$date = gmdate(self::DATE_FORMAT);
$localActor = $this->getActorFromActivity($activity);
$localActor = $this->getActorFromItem($activity);
$localActorLink =
$this->configService->getUrlRoot() . '@' . $localActor->getPreferredUsername();
@ -255,49 +287,6 @@ class ActivityService {
}
// /**
// * @param Core $activity
// *
// * @return array
// */
// private function getHostsFromActivity(Core $activity) {
//
// $hosts = [];
// $hosts[] = $this->getHostFromUriId($activity->getTo());
// foreach ($activity->getToArray() as $to) {
// $hosts[] = $this->getHostFromUriId($to);
// }
//
// if ($activity instanceof Note) {
// /** @var Note $activity */
// $hosts[] = $this->getHostFromUriId($activity->getInReplyTo());
// }
//
// $hosts = $this->cleaningHosts($hosts);
//
// return $hosts;
// }
// /**
// * @param array $hosts
// *
// * @return array
// */
// private function cleaningHosts(array $hosts) {
// $ret = [];
// foreach ($hosts as $host) {
// if ($host === '') {
// continue;
// }
//
// $ret[] = $host;
// }
//
// return $ret;
// }
/**
* @param ACore $activity
*
@ -305,7 +294,7 @@ class ActivityService {
* @throws SocialAppConfigException
* @throws ActorDoesNotExistException
*/
private function getActorFromActivity(Acore $activity): Person {
private function getActorFromItem(Acore $activity): Person {
if ($activity->gotActor()) {
return $activity->getActor();
}
@ -413,7 +402,7 @@ class ActivityService {
$coreService = $activity->savingAs();
if ($coreService !== null) {
$coreService->save($activity);
$coreService->parse($activity);
}
if ($activity->gotObject()) {