summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2018-11-20 11:47:14 -0100
committerMaxence Lange <maxence@artificial-owl.com>2018-11-20 11:47:14 -0100
commit62f713f434f40e632883d9076390ea43921d244d (patch)
tree609cbbe1adc673f4d38c25c9a3823f677f1b2142
parent86e90ffef8f7ae85e3115c9a0fba887b636b8138 (diff)
managing notes
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
-rw-r--r--lib/Controller/ActivityPubController.php27
-rw-r--r--lib/Db/NotesRequest.php118
-rw-r--r--lib/Exceptions/NoteNotFoundException.php8
-rw-r--r--lib/Service/ActivityPub/NoteService.php20
-rw-r--r--lib/Service/ActivityService.php95
5 files changed, 165 insertions, 103 deletions
diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php
index fc7d2917..89ea905a 100644
--- a/lib/Controller/ActivityPubController.php
+++ b/lib/Controller/ActivityPubController.php
@@ -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);
diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php
index 83994763..c0651496 100644
--- a/lib/Db/NotesRequest.php
+++ b/lib/Db/NotesRequest.php
@@ -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());
-
- $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();
-
- return $qb->getLastInsertId();
- } catch (\Exception $e) {
- throw $e;
+ $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->execute();
+
+ return $qb->getLastInsertId();
+ }
+
+
+ /**
+ * @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();
+ }
+
}
+
diff --git a/lib/Exceptions/NoteNotFoundException.php b/lib/Exceptions/NoteNotFoundException.php
new file mode 100644
index 00000000..0bd86298
--- /dev/null
+++ b/lib/Exceptions/NoteNotFoundException.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace OCA\Social\Exceptions;
+
+class NoteNotFoundException extends \Exception {
+
+}
+
diff --git a/lib/Service/ActivityPub/NoteService.php b/lib/Service/ActivityPub/NoteService.php
index 86cefa1d..cd2a8c81 100644
--- a/lib/Service/ActivityPub/NoteService.php
+++ b/lib/Service/ActivityPub/NoteService.php
@@ -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());
}
diff --git a/lib/Service/ActivityService.php b/lib/Service/ActivityService.php
index 8d23ecf7..13b9e404 100644
--- a/lib/Service/ActivityService.php
+++ b/lib/Service/ActivityService.php
@@ -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 {
@@ -154,11 +156,40 @@ 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()) {