federation and attachments

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/1709/head
Maxence Lange 2023-04-02 12:03:25 -01:00
rodzic cbab0d3594
commit 37bbde39e4
8 zmienionych plików z 63 dodań i 14 usunięć

Wyświetl plik

@ -116,8 +116,6 @@ return [
['name' => 'Local#postLike', 'url' => '/api/v1/post/like', 'verb' => 'POST'],
['name' => 'Local#postUnlike', 'url' => '/api/v1/post/like', 'verb' => 'DELETE'],
['name' => 'Local#postBoost', 'url' => '/api/v1/post/boost', 'verb' => 'POST'],
['name' => 'Local#postUnboost', 'url' => '/api/v1/post/boost', 'verb' => 'DELETE'],
['name' => 'Local#actionFollow', 'url' => '/api/v1/current/follow', 'verb' => 'PUT'],
['name' => 'Local#actionUnfollow', 'url' => '/api/v1/current/follow', 'verb' => 'DELETE'],

Wyświetl plik

@ -253,7 +253,10 @@ class ApiController extends Controller {
if (!empty($status->getMediaIds())) {
$post->setMedias(
array_map(function (Document $document): MediaAttachment {
return $document->convertToMediaAttachment($this->urlGenerator);
return $document->convertToMediaAttachment(
$this->urlGenerator,
ACore::FORMAT_ACTIVITYPUB
);
}, $this->documentService->getMediaFromArray(
$status->getMediaIds(),
$this->viewer->getPreferredUsername()

Wyświetl plik

@ -75,10 +75,15 @@ class StreamRequest extends StreamRequestBuilder {
$qb = $this->saveStream($stream);
if ($stream->getType() === Note::TYPE) {
/** @var Note $stream */
$attachments = [];
foreach ($stream->getAttachments() as $item) {
$attachments[] = $item->asLocal(); // get attachment ready for local
}
$qb->setValue('hashtags', $qb->createNamedParameter(json_encode($stream->getHashtags())))
->setValue(
'attachments', $qb->createNamedParameter(
json_encode($stream->getAttachments(), JSON_UNESCAPED_SLASHES)
'attachments', $qb->createNamedParameter(json_encode($attachments, JSON_UNESCAPED_SLASHES)
)
);
}

Wyświetl plik

@ -369,9 +369,13 @@ class Document extends ACore implements JsonSerializable {
/**
* @return MediaAttachment
*/
public function convertToMediaAttachment(?IURLGenerator $urlGenerator = null): MediaAttachment {
public function convertToMediaAttachment(
?IURLGenerator $urlGenerator = null,
int $exportFormat = self::FORMAT_LOCAL
): MediaAttachment {
$media = new MediaAttachment();
$media->setId((string)$this->getNid());
$media->setId((string)$this->getNid())
->setExportFormat($exportFormat);
$mime = '';
if (strpos($this->getMediaType(), '/')) {

Wyświetl plik

@ -702,11 +702,8 @@ class Stream extends ACore implements IQueryRow, JsonSerializable {
public function jsonSerialize(): array {
$result = parent::jsonSerialize();
$result['media_attachments'] = $this->getAttachments();
// if ($this->isCompleteDetails()) {
// $result['attachments'] = $this->getAttachments();
// }
// $result['media_attachments'] = $this->getAttachments();
$result['attachment'] = $this->getAttachments();
return $result;
}

Wyświetl plik

@ -30,6 +30,8 @@ declare(strict_types=1);
namespace OCA\Social\Model\Client;
use JsonSerializable;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Object\Document;
use OCA\Social\Tools\Traits\TArrayTools;
class MediaAttachment implements JsonSerializable {
@ -44,6 +46,7 @@ class MediaAttachment implements JsonSerializable {
private ?AttachmentMeta $meta = null;
private string $description = '';
private string $blurHash = '';
private int $exportFormat = ACore::FORMAT_LOCAL;
public function setId(string $id): self {
$this->id = $id;
@ -136,6 +139,17 @@ class MediaAttachment implements JsonSerializable {
}
public function setExportFormat(int $exportFormat): self {
$this->exportFormat = $exportFormat;
return $this;
}
public function getExportFormat(): int {
return $this->exportFormat;
}
public function import(array $data): self {
$this->setId($this->get('id', $data));
$this->setType($this->get('type', $data));
@ -153,6 +167,14 @@ class MediaAttachment implements JsonSerializable {
}
public function jsonSerialize(): array {
if ($this->getExportFormat() === ACore::FORMAT_LOCAL) {
return $this->asLocal();
}
return $this->asDocument();
}
public function asLocal(): array {
return array_filter(
[
'id' => $this->getId(),
@ -166,4 +188,24 @@ class MediaAttachment implements JsonSerializable {
]
);
}
/**
* quick implementation of converting MediaAttachment to Document. Can be improved.
*
* @return array
*/
public function asDocument(): array {
$original = $this->getMeta()->getOriginal();
return
[
'type' => Document::TYPE,
'mediaType' => '',
'url' => $this->getUrl(),
'name' => null,
'blurhash' => $this->getBlurHash(),
'width' => ($original === null) ? 0 : $original->getWidth() ?? 0,
'height' => ($original === null) ? 0 : $original->getHeight() ?? 0
];
}
}

Wyświetl plik

@ -120,7 +120,9 @@ class BoostService {
throw new StreamNotFoundException('Stream is not Public');
}
$announce->setTo(ACore::CONTEXT_PUBLIC);
$announce->addCc($actor->getFollowers());
$announce->addcc($note->getAttributedTo());
$announce->setObjectId($note->getId());
$announce->setRequestToken($this->uuid());

Wyświetl plik

@ -109,8 +109,6 @@ class PostService {
$note->setAttachments($post->getMedias());
$note->setVisibility($post->getType());
// $this->generateDocumentsFromAttachments($note, $post);
$this->streamService->replyTo($note, $post->getReplyTo());
$this->streamService->addRecipients($note, $post->getType(), $post->getTo());
$this->streamService->addHashtags($note, $post->getHashtags());