streamService = $streamService; $this->accountService = $accountService; $this->activityService = $activityService; $this->cacheDocumentService = $cacheDocumentService; $this->configService = $configService; $this->miscService = $miscService; $this->logger = $logger; } /** * @param Post $post * @param string $token * * @return ACore * @throws InvalidOriginException * @throws InvalidResourceException * @throws ItemUnknownException * @throws MalformedArrayException * @throws RedundancyLimitException * @throws RequestContentException * @throws RequestNetworkException * @throws RequestResultNotJsonException * @throws RequestResultSizeException * @throws RequestServerException * @throws SocialAppConfigException * @throws StreamNotFoundException * @throws UnauthorizedFediverseException */ public function createPost(Post $post, string &$token = ''): ?ACore { $this->fixRecipientAndHashtags($post); $note = new Note(); $actor = $post->getActor(); $this->streamService->assignItem($note, $actor, $post->getType()); $note->setAttributedTo($actor->getId()); $note->setContent(htmlentities($post->getContent(), ENT_QUOTES)); $note->setAttachments($post->getMedias()); $note->setVisibility($post->getType()); $this->streamService->replyTo($note, $post->getReplyTo()); $this->streamService->addRecipients($note, $post->getType(), $post->getTo()); $this->streamService->addHashtags($note, $post->getHashtags()); // $this->streamService->addAttachments($note, $post->getDocuments()); $token = $this->activityService->createActivity($actor, $note, $activity); $this->accountService->cacheLocalActorDetailCount($actor); $this->logger->debug('Activity: ' . json_encode($activity)); return $activity; } /** * @param Note $note * @param Post $post */ private function generateDocumentsFromAttachments(Note $note, Post $post) { $documents = []; if (!isset($_FILES['attachments'])) { return; } if (is_array($_FILES['attachments']['error'])) { foreach ($_FILES['attachments']['error'] as $key => $error) { if ($error == UPLOAD_ERR_OK) { try { $document = $this->generateDocumentFromAttachment($note, $key); $service = AP::$activityPub->getInterfaceForItem($document); $service->save($document); $documents[] = $document; } catch (Exception $e) { } } } } else { try { $tmp_name = $_FILES["attachments"]["tmp_name"]; $name = basename($_FILES["attachments"]["name"]); $tmpFile = tmpfile(); $tmpPath = stream_get_meta_data($tmpFile)['uri']; if (move_uploaded_file($tmp_name, $tmpPath)) { $document = new Document(); $document->setUrlCloud($this->configService->getCloudUrl()); $document->generateUniqueId('/documents/local'); $document->setParentId($note->getId()); $document->setPublic(true); $this->cacheDocumentService->saveFromTempToCache($document, $tmpPath); } $service = AP::$activityPub->getInterfaceForItem($document); $service->save($document); $documents[] = $document; } catch (Exception $e) { $this->logger->error($e->getMessage(), [ 'exception' => $e, ]); } } $post->setDocuments($documents); } /** * @param Note $note * @param string $attachment * * @return Document * @throws CacheContentMimeTypeException * @throws NotFoundException * @throws NotPermittedException * @throws SocialAppConfigException * @throws UrlCloudException */ private function generateDocumentFromAttachment(Note $note, int $key): Document { $tmp_name = $_FILES["attachments"]["tmp_name"][$key]; $name = basename($_FILES["attachments"]["name"][$key]); $tmpFile = tmpfile(); $tmpPath = stream_get_meta_data($tmpFile)['uri']; if (move_uploaded_file($tmp_name, $tmpPath)) { $document = new Document(); $document->setUrlCloud($this->configService->getCloudUrl()); $document->generateUniqueId('/documents/local'); $document->setParentId($note->getId()); $document->setPublic(true); $this->cacheDocumentService->saveFromTempToCache($document, $tmpPath); } return $document; } /** * @param Post $post */ public function fixRecipientAndHashtags(Post $post) { preg_match_all('/(?!\b)@([^\s]+)/', $post->getContent(), $matchesTo); preg_match_all('/(?!\b)#([^\s]+)/', $post->getContent(), $matchesHash); foreach ($matchesTo[1] as $to) { $post->addTo($to); } foreach ($matchesHash[1] as $hash) { $post->addHashtag($hash); } } }