generate local copy of document

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/1655/head
Maxence Lange 2023-03-14 16:03:42 -01:00
rodzic f80cf33bcc
commit f699ec39b4
6 zmienionych plików z 61 dodań i 43 usunięć

Wyświetl plik

@ -354,7 +354,7 @@ class ActivityPubController extends Controller {
} }
if (!$this->checkSourceActivityStreams()) { if (!$this->checkSourceActivityStreams()) {
return $this->socialPubController->displayPost($username, $token); return $this->socialPubController->displayPost($username, (int)$token);
} }
try { try {

Wyświetl plik

@ -317,6 +317,7 @@ class ApiController extends Controller {
$this->logger->debug('[ApiController] mediaNew: ' . json_encode($file)); $this->logger->debug('[ApiController] mediaNew: ' . json_encode($file));
$document = new Document(); $document = new Document();
$document->setLocal(true);
$document->setAccount($this->viewer->getPreferredUsername()); $document->setAccount($this->viewer->getPreferredUsername());
$document->setUrlCloud($this->configService->getCloudUrl()); $document->setUrlCloud($this->configService->getCloudUrl());
$document->generateUniqueId('/documents/local'); $document->generateUniqueId('/documents/local');

Wyświetl plik

@ -39,17 +39,18 @@ use OCA\Social\Interfaces\IActivityPubInterface;
use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Actor\Person;
use OCA\Social\Model\ActivityPub\Object\Document; use OCA\Social\Model\ActivityPub\Object\Document;
use OCA\Social\Service\MiscService; use OCA\Social\Service\CacheDocumentService;
class DocumentInterface extends AbstractActivityPubInterface implements IActivityPubInterface { class DocumentInterface extends AbstractActivityPubInterface implements IActivityPubInterface {
protected CacheDocumentService $cacheDocumentService;
protected CacheDocumentsRequest $cacheDocumentsRequest; protected CacheDocumentsRequest $cacheDocumentsRequest;
protected MiscService $miscService;
public function __construct( public function __construct(
CacheDocumentsRequest $cacheDocumentsRequest, MiscService $miscService CacheDocumentService $cacheDocumentService,
CacheDocumentsRequest $cacheDocumentsRequest
) { ) {
$this->cacheDocumentService = $cacheDocumentService;
$this->cacheDocumentsRequest = $cacheDocumentsRequest; $this->cacheDocumentsRequest = $cacheDocumentsRequest;
$this->miscService = $miscService;
} }
/** /**
@ -74,6 +75,10 @@ class DocumentInterface extends AbstractActivityPubInterface implements IActivit
$this->cacheDocumentsRequest->getById($item->getId()); $this->cacheDocumentsRequest->getById($item->getId());
$this->cacheDocumentsRequest->update($item); $this->cacheDocumentsRequest->update($item);
} catch (CacheDocumentDoesNotExistException $e) { } 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 // parentId / url can only be empty on new document, meaning owner cannot be empty here
if (($item->getUrl() === '' && $item->getParentId() === '' && $item->getAccount() !== '') if (($item->getUrl() === '' && $item->getParentId() === '' && $item->getAccount() !== '')
|| !$this->cacheDocumentsRequest->isDuplicate($item)) { || !$this->cacheDocumentsRequest->isDuplicate($item)) {

Wyświetl plik

@ -33,6 +33,7 @@ namespace OCA\Social\Interfaces\Object;
use OCA\Social\Db\CacheDocumentsRequest; use OCA\Social\Db\CacheDocumentsRequest;
use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Interfaces\IActivityPubInterface;
use OCA\Social\Service\CacheDocumentService;
use OCA\Social\Service\MiscService; use OCA\Social\Service\MiscService;
class ImageInterface extends DocumentInterface implements IActivityPubInterface { class ImageInterface extends DocumentInterface implements IActivityPubInterface {
@ -43,8 +44,9 @@ class ImageInterface extends DocumentInterface implements IActivityPubInterface
* @param MiscService $miscService * @param MiscService $miscService
*/ */
public function __construct( public function __construct(
CacheDocumentsRequest $cacheDocumentsRequest, MiscService $miscService CacheDocumentsRequest $cacheDocumentsRequest,
CacheDocumentService $cacheDocumentService
) { ) {
parent::__construct($cacheDocumentsRequest, $miscService); parent::__construct($cacheDocumentService, $cacheDocumentsRequest);
} }
} }

Wyświetl plik

@ -33,10 +33,15 @@ namespace OCA\Social\Model\ActivityPub;
use DateTime; use DateTime;
use Exception; use Exception;
use JsonSerializable; use JsonSerializable;
use OCA\Social\AP;
use OCA\Social\Exceptions\InvalidResourceEntryException;
use OCA\Social\Exceptions\ItemAlreadyExistsException; use OCA\Social\Exceptions\ItemAlreadyExistsException;
use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Actor\Person;
use OCA\Social\Model\ActivityPub\Object\Announce; 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\Follow;
use OCA\Social\Model\ActivityPub\Object\Image;
use OCA\Social\Model\ActivityPub\Object\Like; use OCA\Social\Model\ActivityPub\Object\Like;
use OCA\Social\Model\Client\MediaAttachment; use OCA\Social\Model\Client\MediaAttachment;
use OCA\Social\Model\StreamAction; 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\Cache;
use OCA\Social\Tools\Model\CacheItem; use OCA\Social\Tools\Model\CacheItem;
use OCA\Social\Traits\TDetails; use OCA\Social\Traits\TDetails;
use OCP\IURLGenerator;
use OCP\Server;
/** /**
* Class Stream * Class Stream
@ -415,7 +422,7 @@ class Stream extends ACore implements IQueryRow, JsonSerializable {
$this->setConversation($this->validate(self::AS_ID, 'conversation', $data, '')); $this->setConversation($this->validate(self::AS_ID, 'conversation', $data, ''));
$this->setContent($this->get('content', $data, '')); $this->setContent($this->get('content', $data, ''));
try { try {
$this->importAttachments($this->getArray('attachments', $data, [])); $this->importAttachments($this->getArray('attachment', $data, []));
} catch (ItemAlreadyExistsException $e) { } catch (ItemAlreadyExistsException $e) {
} }
$this->convertPublished(); $this->convertPublished();
@ -426,39 +433,42 @@ class Stream extends ACore implements IQueryRow, JsonSerializable {
* @throws ItemAlreadyExistsException * @throws ItemAlreadyExistsException
*/ */
public function importAttachments(array $list): void { public function importAttachments(array $list): void {
$urlGenerator = Server::get(IURLGenerator::class);
$new = []; $new = [];
foreach ($list as $item) { foreach ($list as $item) {
// try { try {
// $attachment = AP::$activityPub->getItemFromData($item, $this); /** @var Document $attachment */
// } catch (Exception $e) { $attachment = AP::$activityPub->getItemFromData($item, $this);
// continue; } catch (Exception $e) {
// } continue;
// }
// if ($attachment->getType() !== Document::TYPE
// && $attachment->getType() !== Image::TYPE) { if ($attachment->getType() !== Document::TYPE
// continue; && $attachment->getType() !== Image::TYPE) {
// } continue;
// }
// try {
// $attachment->setUrl( try {
// $this->validateEntryString(ACore::AS_URL, $attachment->getUrl()) $attachment->setUrl(
// ); $this->validateEntryString(ACore::AS_URL, $attachment->getUrl())
// } catch (InvalidResourceEntryException $e) { );
// continue; } catch (InvalidResourceEntryException $e) {
// } continue;
// }
// if ($attachment->getUrl() === '') {
// continue; if ($attachment->getUrl() === '') {
// } continue;
// }
// try {
// $interface = AP::$activityPub->getInterfaceFromType($attachment->getType()); try {
// } catch (ItemUnknownException $e) { $interface = AP::$activityPub->getInterfaceFromType($attachment->getType());
// continue; } catch (ItemUnknownException $e) {
// } continue;
// }
// $interface->save($attachment);
// $new[] = $attachment; $interface->save($attachment);
$new[] = $attachment->convertToMediaAttachment($urlGenerator);
} }
$this->setAttachments($new); $this->setAttachments($new);

Wyświetl plik

@ -139,10 +139,10 @@ class MediaAttachment implements JsonSerializable {
public function import(array $data): self { public function import(array $data): self {
$this->setId($this->get('id', $data)); $this->setId($this->get('id', $data));
$this->setType($this->get('type', $data)); $this->setType($this->get('type', $data));
$this->setUrl($this->get('type', $data)); $this->setUrl($this->get('url', $data));
$this->setPreviewUrl($this->get('type', $data)); $this->setPreviewUrl($this->get('preview_url', $data));
$this->setRemoteUrl($this->get('type', $data)); $this->setRemoteUrl($this->get('remote_url', $data));
$this->setDescription($this->get('type', $data)); $this->setDescription($this->get('description', $data));
$this->setBlurHash($this->get('blurhash', $data)); $this->setBlurHash($this->get('blurhash', $data));
$meta = new AttachmentMeta(); $meta = new AttachmentMeta();