From fc786859f1194c3afe42bff9e2e6d5ade709213b Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Mon, 13 Mar 2023 10:11:04 -0100 Subject: [PATCH] import as local Signed-off-by: Maxence Lange --- lib/Controller/SocialPubController.php | 11 ++- lib/Db/SocialCrossQueryBuilder.php | 1 + lib/Interfaces/Object/AnnounceInterface.php | 27 +++--- lib/Interfaces/Object/LikeInterface.php | 10 ++- lib/Model/ActivityPub/ACore.php | 5 +- lib/Model/ActivityPub/Actor/Person.php | 96 +++++++++++++++------ lib/Model/ActivityPub/Object/Announce.php | 2 +- lib/Model/ActivityPub/Stream.php | 56 ++++++++++-- lib/Model/Client/AttachmentMeta.php | 23 ++++- lib/Model/Client/MediaAttachment.php | 16 ++++ 10 files changed, 191 insertions(+), 56 deletions(-) diff --git a/lib/Controller/SocialPubController.php b/lib/Controller/SocialPubController.php index 41b66b7c..24634f8a 100644 --- a/lib/Controller/SocialPubController.php +++ b/lib/Controller/SocialPubController.php @@ -172,18 +172,21 @@ class SocialPubController extends Controller { * @throws SocialAppConfigException * @throws StreamNotFoundException */ - public function displayPost(string $username, string $token): TemplateResponse { + public function displayPost(string $username, int $token): TemplateResponse { try { $viewer = $this->accountService->getCurrentViewer(); $this->streamService->setViewer($viewer); } catch (AccountDoesNotExistException $e) { } - $postId = $this->configService->getSocialUrl() . '@' . $username . '/' . $token; +// $postId = $this->configService->getSocialUrl() . '@' . $username . '/' . $token; + + $stream = $this->streamService->getStreamByNid($token); + if (strtolower($stream->getActor()->getDisplayName()) !== strtolower($username)) { + throw new StreamNotFoundException(); + } - $stream = $this->streamService->getStreamById($postId, true); $data = [ - 'id' => $postId, 'application' => 'Social' ]; diff --git a/lib/Db/SocialCrossQueryBuilder.php b/lib/Db/SocialCrossQueryBuilder.php index 239e0404..86fde0ef 100644 --- a/lib/Db/SocialCrossQueryBuilder.php +++ b/lib/Db/SocialCrossQueryBuilder.php @@ -109,6 +109,7 @@ class SocialCrossQueryBuilder extends SocialCoreQueryBuilder { } $this->selectAlias($pf . '.id', 'cacheactor_id') + ->selectAlias($pf . '.nid', 'cacheactor_nid') ->selectAlias($pf . '.type', 'cacheactor_type') ->selectAlias($pf . '.account', 'cacheactor_account') ->selectAlias($pf . '.following', 'cacheactor_following') diff --git a/lib/Interfaces/Object/AnnounceInterface.php b/lib/Interfaces/Object/AnnounceInterface.php index b3748c7d..41f33c50 100644 --- a/lib/Interfaces/Object/AnnounceInterface.php +++ b/lib/Interfaces/Object/AnnounceInterface.php @@ -31,9 +31,6 @@ declare(strict_types=1); namespace OCA\Social\Interfaces\Object; -use OCA\Social\Tools\Exceptions\CacheItemNotFoundException; -use OCA\Social\Tools\Exceptions\MalformedArrayException; -use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OCA\Social\AP; use OCA\Social\Db\ActionsRequest; @@ -44,11 +41,6 @@ use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use OCA\Social\Tools\Exceptions\RequestContentException; -use OCA\Social\Tools\Exceptions\RequestNetworkException; -use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; -use OCA\Social\Tools\Exceptions\RequestResultSizeException; -use OCA\Social\Tools\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Exceptions\UnauthorizedFediverseException; @@ -65,6 +57,14 @@ use OCA\Social\Model\StreamQueue; use OCA\Social\Service\CacheActorService; use OCA\Social\Service\MiscService; use OCA\Social\Service\StreamQueueService; +use OCA\Social\Tools\Exceptions\CacheItemNotFoundException; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; +use OCA\Social\Tools\Traits\TArrayTools; /** * Class AnnounceInterface @@ -178,9 +178,9 @@ class AnnounceInterface extends AbstractActivityPubInterface implements IActivit } try { - $post = $this->streamRequest->getStreamById($item->getObjectId()); + $post = $this->streamRequest->getStreamById($item->getObjectId(), false, ACore::FORMAT_LOCAL); } catch (StreamNotFoundException $e) { - return; // should not happens. + return; // should not happen. } $this->updateDetails($post); @@ -256,7 +256,11 @@ class AnnounceInterface extends AbstractActivityPubInterface implements IActivit $actor = $this->cacheActorService->getFromId($item->getActorId()); } - $post = $this->streamRequest->getStreamById($item->getObjectId()); + $post = $this->streamRequest->getStreamById( + $item->getObjectId(), + false, + ACore::FORMAT_LOCAL + ); $this->updateDetails($post); $this->generateNotification($post, $actor); } catch (Exception $e) { @@ -321,7 +325,6 @@ class AnnounceInterface extends AbstractActivityPubInterface implements IActivit // $notification->setDetail('url', ''); $notification->setDetailItem('post', $post); - $notification->setDetailInt('nid', $post->getNid()); $notification->addDetail('accounts', $author->getAccount()); $notification->setAttributedTo($author->getId()) ->setSubType(Announce::TYPE) diff --git a/lib/Interfaces/Object/LikeInterface.php b/lib/Interfaces/Object/LikeInterface.php index 226c3cd1..da08ab8e 100644 --- a/lib/Interfaces/Object/LikeInterface.php +++ b/lib/Interfaces/Object/LikeInterface.php @@ -35,11 +35,11 @@ use Exception; use OCA\Social\AP; use OCA\Social\Db\ActionsRequest; use OCA\Social\Db\StreamRequest; +use OCA\Social\Exceptions\ActionDoesNotExistException; use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\ItemAlreadyExistsException; use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\ItemUnknownException; -use OCA\Social\Exceptions\ActionDoesNotExistException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Interfaces\Activity\AbstractActivityPubInterface; @@ -135,7 +135,12 @@ class LikeInterface extends AbstractActivityPubInterface implements IActivityPub $actor = $this->cacheActorService->getFromId($item->getActorId()); } - $post = $this->streamRequest->getStreamById($item->getObjectId()); + $post = $this->streamRequest->getStreamById( + $item->getObjectId(), + false, + ACore::FORMAT_LOCAL + ); + $post->setCompleteDetails(true); $this->updateDetails($post); $this->generateNotification($post, $actor); } catch (Exception $e) { @@ -204,7 +209,6 @@ class LikeInterface extends AbstractActivityPubInterface implements IActivityPub $notification = AP::$activityPub->getItemFromType(SocialAppNotification::TYPE); // $notification->setDetail('url', ''); $notification->setDetailItem('post', $post); - $notification->setDetailInt('nid', $post->getNid()); $notification->addDetail('accounts', $author->getAccount()); $notification->setAttributedTo($author->getId()) ->setSubType(Like::TYPE) diff --git a/lib/Model/ActivityPub/ACore.php b/lib/Model/ActivityPub/ACore.php index e6aac2df..a30d2197 100644 --- a/lib/Model/ActivityPub/ACore.php +++ b/lib/Model/ActivityPub/ACore.php @@ -638,10 +638,11 @@ class ACore extends Item implements JsonSerializable { } /** + * based on json generated/exported as LOCAL * @param array $data */ - public function importFromCache(array $data) { - $this->import($data); + public function importFromLocal(array $data) { + $this->setNid($this->getInt('id', $data)); } /** diff --git a/lib/Model/ActivityPub/Actor/Person.php b/lib/Model/ActivityPub/Actor/Person.php index 302301f2..158aa692 100644 --- a/lib/Model/ActivityPub/Actor/Person.php +++ b/lib/Model/ActivityPub/Actor/Person.php @@ -636,6 +636,47 @@ class Person extends ACore implements IQueryRow, JsonSerializable { } } + /** + * @param array $data + * + * @return $this + */ + public function importFromLocal(array $data): self { + parent::importFromLocal($data); + + $this->setId($this->get('url', $data)); + $this->setPreferredUsername($this->get('username', $data)); + $this->setAccount($this->get('acct', $data)); + $this->setDisplayName($this->get('display_name', $data)); + $this->setLocked($this->getBool('locked', $data)); + $this->setBot($this->getBool('bot', $data)); + $this->setDiscoverable($this->getBool('discoverable', $data)); + $this->setDescription($this->get('note', $data)); + $this->setUrl($this->get('url', $data)); + + $this->setAvatar($this->get('avatar', $data)); + $this->setHeader($this->get('header', $data)); + + $this->setPrivacy($this->get('source.privacy', $data)); + $this->setSensitive($this->getBool('source.sensitive', $data)); + $this->setLanguage($this->get('source.language', $data)); + + try { + $dTime = new DateTime($this->get('created_at', $data, 'yesterday')); + $this->setCreation($dTime->getTimestamp()); + } catch (Exception $e) { + } + + $count = [ + 'followers' => $this->getInt('followers_count', $data), + 'following' => $this->getInt('following_count', $data), + 'post' => $this->getInt('statuses_count', $data), + 'last_post_creation' => $this->get('last_status_at', $data) + ]; + $this->setDetailArray('count', $count); + + return $this; + } /** * @param array $data @@ -720,34 +761,35 @@ class Person extends ACore implements IQueryRow, JsonSerializable { $details = $this->getDetailsAll(); $result = [ - "username" => $this->getPreferredUsername(), - "acct" => $this->isLocal() ? $this->getPreferredUsername() : $this->getAccount(), - "display_name" => $this->getDisplayName(), - "locked" => $this->isLocked(), - "bot" => $this->isBot(), - "discoverable" => $this->isDiscoverable(), - "group" => false, - "created_at" => date('Y-m-d\TH:i:s', $this->getCreation()) . '.000Z', - "note" => $this->getDescription(), - "url" => $this->getId(), - "avatar" => $this->getAvatar(), - "avatar_static" => $this->getAvatar(), - "header" => $this->getHeader(), - "header_static" => $this->getHeader(), - "followers_count" => $this->getInt('count.followers', $details), - "following_count" => $this->getInt('count.following', $details), - "statuses_count" => $this->getInt('count.post', $details), - "last_status_at" => $this->get('last_post_creation', $details), - "source" => [ - "privacy" => $this->getPrivacy(), - "sensitive" => $this->isSensitive(), - "language" => $this->getLanguage(), - "note" => $this->getDescription(), - "fields" => [], - "follow_requests_count" => 0 + 'id' => (string)$this->getNid(), + 'username' => $this->getPreferredUsername(), + 'acct' => $this->isLocal() ? $this->getPreferredUsername() : $this->getAccount(), + 'display_name' => $this->getDisplayName(), + 'locked' => $this->isLocked(), + 'bot' => $this->isBot(), + 'discoverable' => $this->isDiscoverable(), + 'group' => false, + 'created_at' => date('Y-m-d\TH:i:s', $this->getCreation()) . '.000Z', + 'note' => $this->getDescription(), + 'url' => $this->getId(), + 'avatar' => $this->getAvatar(), + 'avatar_static' => $this->getAvatar(), + 'header' => $this->getHeader(), + 'header_static' => $this->getHeader(), + 'followers_count' => $this->getInt('count.followers', $details), + 'following_count' => $this->getInt('count.following', $details), + 'statuses_count' => $this->getInt('count.post', $details), + 'last_status_at' => $this->get('last_post_creation', $details), + 'source' => [ + 'privacy' => $this->getPrivacy(), + 'sensitive' => $this->isSensitive(), + 'language' => $this->getLanguage(), + 'note' => $this->getDescription(), + 'fields' => [], + 'follow_requests_count' => 0 ], - "emojis" => [], - "fields" => [] + 'emojis' => [], + 'fields' => [] ]; return array_merge(parent::exportAsLocal(), $result); diff --git a/lib/Model/ActivityPub/Object/Announce.php b/lib/Model/ActivityPub/Object/Announce.php index b14db201..a2a3a241 100644 --- a/lib/Model/ActivityPub/Object/Announce.php +++ b/lib/Model/ActivityPub/Object/Announce.php @@ -82,7 +82,7 @@ class Announce extends Stream implements JsonSerializable { $object = $object->getObject(); /** @var Stream $item */ $item = AP::$activityPub->getItemFromType($this->get('type', $object, Stream::TYPE)); - $item->importFromCache($object); + $item->importFromLocal($object); $result['reblog'] = $item->exportAsLocal(); $result['content'] = $item->getContent(); } diff --git a/lib/Model/ActivityPub/Stream.php b/lib/Model/ActivityPub/Stream.php index 233a9819..faf7bd64 100644 --- a/lib/Model/ActivityPub/Stream.php +++ b/lib/Model/ActivityPub/Stream.php @@ -115,6 +115,7 @@ class Stream extends ACore implements IQueryRow, JsonSerializable { return $this->content; } + /** * @param string $content * @@ -491,13 +492,57 @@ class Stream extends ACore implements IQueryRow, JsonSerializable { $this->setCache($cache); } - public function importFromCache(array $data) { - parent::importFromCache($data); + public function importFromLocal(array $data) { + parent::importFromLocal($data); + $this->setId($this->get('url', $data)); + $this->setUrl($this->get('url', $data)); + $this->setLocal($this->getBool('local', $data)); + $this->setContent($this->get('content', $data)); + $this->setSensitive($this->getBool('sensitive', $data)); + $this->setSpoilerText($this->get('spoiler_text', $data)); + $this->setVisibility($this->get('visibility', $data)); + $this->setLanguage($this->get('language', $data)); + + $action = new StreamAction(); + $action->setValues( + [ + StreamAction::LIKED => $this->getBool('favourited', $data), + StreamAction::BOOSTED => $this->getBool('reblogged', $data) + ] + ); + $this->setAction($action); + + try { + $dTime = new DateTime($this->get('created_at', $data, 'yesterday')); + $this->setPublishedTime($dTime->getTimestamp()); + } catch (Exception $e) { + } + +// "in_reply_to_id" => null, +// "in_reply_to_account_id" => null, +// 'replies_count' => 0, +// 'reblogs_count' => 0, +// 'favourites_count' => 0, +// 'muted' => false, +// 'bookmarked' => false, +// "reblog" => null, +// 'noindex' => false + + $attachments = []; + foreach ($this->getArray('media_attachments', $data) as $dataAttachment) { + $attachment = new MediaAttachment(); + $attachment->import($dataAttachment); + $attachments[] = $attachment; + } + $this->setAttachments($attachments); + + // import from cache with new format ! $actor = new Person(); - $actor->importFromCache($data['actor_info'] ?? []); + $actor->importFromLocal($this->getArray('account', $data)); + $actor->setExportFormat(ACore::FORMAT_LOCAL); $this->setActor($actor); - $this->setCompleteDetails(true); +// $this->setCompleteDetails(true); } @@ -608,8 +653,7 @@ class Stream extends ACore implements IQueryRow, JsonSerializable { $statusPost = $this->getDetails('post'); if (sizeof($statusPost) > 0) { $status = new Stream(); - $status->importFromCache($statusPost); - $status->setNid($this->getDetailInt('nid')); + $status->importFromLocal($statusPost); $status->setExportFormat(self::FORMAT_LOCAL); } diff --git a/lib/Model/Client/AttachmentMeta.php b/lib/Model/Client/AttachmentMeta.php index c3424735..af789b61 100644 --- a/lib/Model/Client/AttachmentMeta.php +++ b/lib/Model/Client/AttachmentMeta.php @@ -204,8 +204,29 @@ class AttachmentMeta implements JsonSerializable { return $this->blurHash; } - public function import(array $data): self { + if (array_key_exists('width', $data)) { + $this->setWidth($this->getInt('width', $data)); + } + if (array_key_exists('height', $data)) { + $this->setHeight($this->getInt('height', $data)); + } + if (array_key_exists('aspect', $data)) { + $this->setAspect($this->getInt('aspect', $data)); + } + if (array_key_exists('duration', $data)) { + $this->setDuration($this->getInt('duration', $data)); + } + + $this->setSize($this->get('size', $data)); + $this->setLength($this->get('length', $data)); + $this->setFps($this->getFloat('fps', $data)); + $this->setAudioEncode($this->get('audio_encode', $data)); + $this->setAudioBitrate($this->get('audio_bitrate', $data)); + $this->setAudioChannels($this->get('audio_channels', $data)); + $this->setDescription($this->get('description', $data)); + $this->setBlurHash($this->get('blurhash', $data)); + $original = new AttachmentMetaDim(); $original->import($this->getArray('original', $data)); $this->setOriginal($original); diff --git a/lib/Model/Client/MediaAttachment.php b/lib/Model/Client/MediaAttachment.php index f3041a70..65614ed2 100644 --- a/lib/Model/Client/MediaAttachment.php +++ b/lib/Model/Client/MediaAttachment.php @@ -136,6 +136,22 @@ class MediaAttachment implements JsonSerializable { } + public function import(array $data): self { + $this->setId($this->get('id', $data)); + $this->setType($this->get('type', $data)); + $this->setUrl($this->get('type', $data)); + $this->setPreviewUrl($this->get('type', $data)); + $this->setRemoteUrl($this->get('type', $data)); + $this->setDescription($this->get('type', $data)); + $this->setBlurHash($this->get('blurhash', $data)); + + $meta = new AttachmentMeta(); + $meta->import($this->getArray('meta', $data)); + $this->setMeta($meta); + + return $this; + } + public function jsonSerialize(): array { return array_filter( [