diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php index 8cc0f6a4..65370503 100644 --- a/lib/Controller/ActivityPubController.php +++ b/lib/Controller/ActivityPubController.php @@ -354,7 +354,7 @@ class ActivityPubController extends Controller { } if (!$this->checkSourceActivityStreams()) { - return $this->socialPubController->displayPost($username, $token); + return $this->socialPubController->displayPost($username, (int)$token); } try { diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 8a124e9f..2ebd185d 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -317,6 +317,7 @@ class ApiController extends Controller { $this->logger->debug('[ApiController] mediaNew: ' . json_encode($file)); $document = new Document(); + $document->setLocal(true); $document->setAccount($this->viewer->getPreferredUsername()); $document->setUrlCloud($this->configService->getCloudUrl()); $document->generateUniqueId('/documents/local'); diff --git a/lib/Interfaces/Object/DocumentInterface.php b/lib/Interfaces/Object/DocumentInterface.php index 5d0be746..c6414889 100644 --- a/lib/Interfaces/Object/DocumentInterface.php +++ b/lib/Interfaces/Object/DocumentInterface.php @@ -39,17 +39,18 @@ use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Object\Document; -use OCA\Social\Service\MiscService; +use OCA\Social\Service\CacheDocumentService; class DocumentInterface extends AbstractActivityPubInterface implements IActivityPubInterface { + protected CacheDocumentService $cacheDocumentService; protected CacheDocumentsRequest $cacheDocumentsRequest; - protected MiscService $miscService; public function __construct( - CacheDocumentsRequest $cacheDocumentsRequest, MiscService $miscService + CacheDocumentService $cacheDocumentService, + CacheDocumentsRequest $cacheDocumentsRequest ) { + $this->cacheDocumentService = $cacheDocumentService; $this->cacheDocumentsRequest = $cacheDocumentsRequest; - $this->miscService = $miscService; } /** @@ -74,6 +75,10 @@ class DocumentInterface extends AbstractActivityPubInterface implements IActivit $this->cacheDocumentsRequest->getById($item->getId()); $this->cacheDocumentsRequest->update($item); } catch (CacheDocumentDoesNotExistException $e) { + if (!$item->isLocal()) { + $this->cacheDocumentService->saveRemoteFileToCache($item); // create local copy + } + // parentId / url can only be empty on new document, meaning owner cannot be empty here if (($item->getUrl() === '' && $item->getParentId() === '' && $item->getAccount() !== '') || !$this->cacheDocumentsRequest->isDuplicate($item)) { diff --git a/lib/Interfaces/Object/ImageInterface.php b/lib/Interfaces/Object/ImageInterface.php index 031d3ec2..e2b37e09 100644 --- a/lib/Interfaces/Object/ImageInterface.php +++ b/lib/Interfaces/Object/ImageInterface.php @@ -33,6 +33,7 @@ namespace OCA\Social\Interfaces\Object; use OCA\Social\Db\CacheDocumentsRequest; use OCA\Social\Interfaces\IActivityPubInterface; +use OCA\Social\Service\CacheDocumentService; use OCA\Social\Service\MiscService; class ImageInterface extends DocumentInterface implements IActivityPubInterface { @@ -43,8 +44,9 @@ class ImageInterface extends DocumentInterface implements IActivityPubInterface * @param MiscService $miscService */ public function __construct( - CacheDocumentsRequest $cacheDocumentsRequest, MiscService $miscService + CacheDocumentsRequest $cacheDocumentsRequest, + CacheDocumentService $cacheDocumentService ) { - parent::__construct($cacheDocumentsRequest, $miscService); + parent::__construct($cacheDocumentService, $cacheDocumentsRequest); } } diff --git a/lib/Model/ActivityPub/Stream.php b/lib/Model/ActivityPub/Stream.php index faf7bd64..ad55eee1 100644 --- a/lib/Model/ActivityPub/Stream.php +++ b/lib/Model/ActivityPub/Stream.php @@ -33,10 +33,15 @@ namespace OCA\Social\Model\ActivityPub; use DateTime; use Exception; use JsonSerializable; +use OCA\Social\AP; +use OCA\Social\Exceptions\InvalidResourceEntryException; use OCA\Social\Exceptions\ItemAlreadyExistsException; +use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Object\Announce; +use OCA\Social\Model\ActivityPub\Object\Document; use OCA\Social\Model\ActivityPub\Object\Follow; +use OCA\Social\Model\ActivityPub\Object\Image; use OCA\Social\Model\ActivityPub\Object\Like; use OCA\Social\Model\Client\MediaAttachment; use OCA\Social\Model\StreamAction; @@ -44,6 +49,8 @@ use OCA\Social\Tools\IQueryRow; use OCA\Social\Tools\Model\Cache; use OCA\Social\Tools\Model\CacheItem; use OCA\Social\Traits\TDetails; +use OCP\IURLGenerator; +use OCP\Server; /** * Class Stream @@ -415,7 +422,7 @@ class Stream extends ACore implements IQueryRow, JsonSerializable { $this->setConversation($this->validate(self::AS_ID, 'conversation', $data, '')); $this->setContent($this->get('content', $data, '')); try { - $this->importAttachments($this->getArray('attachments', $data, [])); + $this->importAttachments($this->getArray('attachment', $data, [])); } catch (ItemAlreadyExistsException $e) { } $this->convertPublished(); @@ -426,39 +433,42 @@ class Stream extends ACore implements IQueryRow, JsonSerializable { * @throws ItemAlreadyExistsException */ public function importAttachments(array $list): void { + $urlGenerator = Server::get(IURLGenerator::class); + $new = []; foreach ($list as $item) { -// try { -// $attachment = AP::$activityPub->getItemFromData($item, $this); -// } catch (Exception $e) { -// continue; -// } -// -// if ($attachment->getType() !== Document::TYPE -// && $attachment->getType() !== Image::TYPE) { -// continue; -// } -// -// try { -// $attachment->setUrl( -// $this->validateEntryString(ACore::AS_URL, $attachment->getUrl()) -// ); -// } catch (InvalidResourceEntryException $e) { -// continue; -// } -// -// if ($attachment->getUrl() === '') { -// continue; -// } -// -// try { -// $interface = AP::$activityPub->getInterfaceFromType($attachment->getType()); -// } catch (ItemUnknownException $e) { -// continue; -// } -// -// $interface->save($attachment); -// $new[] = $attachment; + try { + /** @var Document $attachment */ + $attachment = AP::$activityPub->getItemFromData($item, $this); + } catch (Exception $e) { + continue; + } + + if ($attachment->getType() !== Document::TYPE + && $attachment->getType() !== Image::TYPE) { + continue; + } + + try { + $attachment->setUrl( + $this->validateEntryString(ACore::AS_URL, $attachment->getUrl()) + ); + } catch (InvalidResourceEntryException $e) { + continue; + } + + if ($attachment->getUrl() === '') { + continue; + } + + try { + $interface = AP::$activityPub->getInterfaceFromType($attachment->getType()); + } catch (ItemUnknownException $e) { + continue; + } + + $interface->save($attachment); + $new[] = $attachment->convertToMediaAttachment($urlGenerator); } $this->setAttachments($new); diff --git a/lib/Model/Client/MediaAttachment.php b/lib/Model/Client/MediaAttachment.php index 65614ed2..fba53f30 100644 --- a/lib/Model/Client/MediaAttachment.php +++ b/lib/Model/Client/MediaAttachment.php @@ -139,10 +139,10 @@ 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->setUrl($this->get('url', $data)); + $this->setPreviewUrl($this->get('preview_url', $data)); + $this->setRemoteUrl($this->get('remote_url', $data)); + $this->setDescription($this->get('description', $data)); $this->setBlurHash($this->get('blurhash', $data)); $meta = new AttachmentMeta();