From fae487e94286b82f627fd839fdc0c491d1a472a5 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 9 Mar 2023 11:21:02 -0100 Subject: [PATCH 1/2] status context api Signed-off-by: Maxence Lange --- lib/Controller/ApiController.php | 10 ++++++++++ lib/Db/StreamRequest.php | 18 ++++++++++++++++++ lib/Model/Client/Status.php | 12 ++++++++++++ lib/Service/StreamService.php | 25 ++++++++++++++++++++++--- 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 2cd6c18a..9789f095 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -36,6 +36,7 @@ use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\AccountDoesNotExistException; use OCA\Social\Exceptions\ClientNotFoundException; use OCA\Social\Exceptions\InstanceDoesNotExistException; +use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Exceptions\UnknownProbeException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; @@ -260,6 +261,15 @@ class ApiController extends Controller { ); } + if ($status->getInReplyToId() > 0) { + try { + $replyTo = $this->streamService->getStreamByNid($status->getInReplyToId()); + $post->setReplyTo($replyTo->getId()); + } catch (StreamNotFoundException $e) { + $this->logger->debug('reply to post not found'); + } + } + $activity = $this->postService->createPost($post); $item = $this->streamService->getStreamById( diff --git a/lib/Db/StreamRequest.php b/lib/Db/StreamRequest.php index 3bb0ff03..f3c79918 100644 --- a/lib/Db/StreamRequest.php +++ b/lib/Db/StreamRequest.php @@ -955,4 +955,22 @@ class StreamRequest extends StreamRequestBuilder { public function getRelatedToActor(string $actorId) { } + + + /** + * @param string $id + * + * @return array + */ + public function getDescendants(string $id): array { + $qb = $this->getStreamSelectSql(ACore::FORMAT_LOCAL); + + $qb->filterType(SocialAppNotification::TYPE); + $qb->limitToViewer('sd', 'f', true); + $qb->limitToInReplyTo($id, true); + + $qb->filterDuplicate(); + + return $this->getStreamsFromRequest($qb); + } } diff --git a/lib/Model/Client/Status.php b/lib/Model/Client/Status.php index 19b72695..923c3086 100644 --- a/lib/Model/Client/Status.php +++ b/lib/Model/Client/Status.php @@ -39,6 +39,7 @@ class Status implements \JsonSerializable { private string $visibility = ''; private string $spoilerText = ''; private array $mediaIds = []; + private int $inReplyToId = 0; private string $status = ''; //"media_ids": [], @@ -134,6 +135,16 @@ class Status implements \JsonSerializable { return $this->mediaIds; } + public function setInReplyToId(int $inReplyToId): self { + $this->inReplyToId = $inReplyToId; + + return $this; + } + + public function getInReplyToId(): int { + return $this->inReplyToId; + } + /** * @param string $status @@ -160,6 +171,7 @@ class Status implements \JsonSerializable { $this->setVisibility($this->get('visibility', $data)); $this->setSpoilerText($this->get('spoiler_text', $data)); $this->setMediaIds($this->getArray('media_ids', $data)); + $this->setInReplyToId($this->getInt('in_reply_to_id', $data)); $this->setStatus($this->get('status', $data)); return $this; diff --git a/lib/Service/StreamService.php b/lib/Service/StreamService.php index e2c8168c..be618d49 100644 --- a/lib/Service/StreamService.php +++ b/lib/Service/StreamService.php @@ -59,6 +59,7 @@ class StreamService { private CacheActorService $cacheActorService; private ConfigService $configService; + private const ANCESTOR_LIMIT = 5; /** * NoteService constructor. @@ -349,11 +350,29 @@ class StreamService { return $this->streamRequest->getStreamById($id, $asViewer, $format); } - // TODO: returns context for status + + /** + * @param int $nid + * + * @return array + * @throws StreamNotFoundException + */ public function getContextByNid(int $nid): array { + $curr = $post = $this->streamRequest->getStreamByNid($nid); + + $ancestors = []; + for ($i = 0; $i < self::ANCESTOR_LIMIT; $i++) { + if ($curr->getInReplyTo() === '') { + break; + } + + $curr = $this->streamRequest->getStreamById($curr->getInReplyTo()); + $ancestors[] = $curr; + } + return [ - 'ancestors' => [], - 'descendants' => [] + 'ancestors' => array_reverse($ancestors), + 'descendants' => $this->streamRequest->getDescendants($post->getId()) ]; } From 08502956667685eaca6f727e7ae282796bd3cbec Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 9 Mar 2023 12:17:36 -0100 Subject: [PATCH 2/2] sizeof instead Signed-off-by: Maxence Lange --- lib/Model/ActivityPub/Stream.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Model/ActivityPub/Stream.php b/lib/Model/ActivityPub/Stream.php index 29ce3973..7373b93b 100644 --- a/lib/Model/ActivityPub/Stream.php +++ b/lib/Model/ActivityPub/Stream.php @@ -606,7 +606,7 @@ class Stream extends ACore implements IQueryRow, JsonSerializable { $status = null; if ($statusPost = $this->getDetails('post')) { - if (!empty($statusPost)) { + if (sizeof($statusPost) > 0) { $status = new Stream(); $status->importFromCache($statusPost); $status->setExportFormat(self::FORMAT_LOCAL);