diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php index a65752b0..3b926342 100644 --- a/lib/Controller/LocalController.php +++ b/lib/Controller/LocalController.php @@ -39,6 +39,7 @@ use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Object\Note; +use OCA\Social\Model\ActivityPub\Stream; use OCA\Social\Model\Post; use OCA\Social\Service\AccountService; use OCA\Social\Service\CacheActorService; @@ -146,7 +147,7 @@ class LocalController extends Controller { $post->setReplyTo($this->get('replyTo', $data, '')); $post->setTo($this->getArray('to', $data, [])); $post->addTo($this->get('to', $data, '')); - $post->setType($this->get('type', $data, Note::TYPE_PUBLIC)); + $post->setType($this->get('type', $data, Stream::TYPE_PUBLIC)); /** @var ACore $activity */ $token = $this->postService->createPost($post, $activity); diff --git a/lib/Interfaces/Object/AnnounceInterface.php b/lib/Interfaces/Object/AnnounceInterface.php index abaf10e4..829452d1 100644 --- a/lib/Interfaces/Object/AnnounceInterface.php +++ b/lib/Interfaces/Object/AnnounceInterface.php @@ -31,16 +31,33 @@ declare(strict_types=1); namespace OCA\Social\Interfaces\Object; +use OCA\Social\Db\NotesRequest; +use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\ItemNotFoundException; +use OCA\Social\Exceptions\NoteNotFoundException; use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Object\Announce; +use OCA\Social\Model\ActivityPub\Stream; +use OCA\Social\Model\StreamQueue; use OCA\Social\Service\MiscService; +use OCA\Social\Service\StreamQueueService; +/** + * Class AnnounceInterface + * + * @package OCA\Social\Interfaces\Object + */ class AnnounceInterface implements IActivityPubInterface { + /** @var NotesRequest */ + private $notesRequest; + + /** @var StreamQueueService */ + private $streamQueueService; + /** @var MiscService */ private $miscService; @@ -48,9 +65,15 @@ class AnnounceInterface implements IActivityPubInterface { /** * AnnounceInterface constructor. * + * @param NotesRequest $notesRequest + * @param StreamQueueService $streamQueueService * @param MiscService $miscService */ - public function __construct(MiscService $miscService) { + public function __construct( + NotesRequest $notesRequest, StreamQueueService $streamQueueService, MiscService $miscService + ) { + $this->notesRequest = $notesRequest; + $this->streamQueueService = $streamQueueService; $this->miscService = $miscService; } @@ -60,25 +83,32 @@ class AnnounceInterface implements IActivityPubInterface { * @param ACore $item */ public function activity(Acore $activity, ACore $item) { + /** Stream $item */ + // TODO: Manage Undo Activity + $this->miscService->log('activity: ' . json_encode($activity)); } /** * @param ACore $item + * + * @throws InvalidOriginException */ public function processIncomingRequest(ACore $item) { -// if (!$item->gotObject()) { -// return; -// } - $object = $item->getObjectId(); - $this->miscService->log('___' . json_encode($object)); -// -// try { -// $service = AP::$activityPub->getInterfaceForItem($item->getObject()); -// $service->activity($item, $object); -// } catch (ItemUnknownException $e) { -// } -// + /** @var Stream $item */ + $item->checkOrigin($item->getId()); + + try { + $this->notesRequest->getNoteById($item->getId()); + } catch (NoteNotFoundException $e) { + $objectId = $item->getObjectId(); + $item->addCacheItem($objectId); + $this->notesRequest->save($item); + + $this->streamQueueService->generateStreamQueue( + $item->getRequestToken(), StreamQueue::TYPE_CACHE, $item->getId() + ); + } } diff --git a/lib/Model/ActivityPub/Object/Announce.php b/lib/Model/ActivityPub/Object/Announce.php index ede50958..1915b817 100644 --- a/lib/Model/ActivityPub/Object/Announce.php +++ b/lib/Model/ActivityPub/Object/Announce.php @@ -33,6 +33,7 @@ namespace OCA\Social\Model\ActivityPub\Object; use JsonSerializable; use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Stream; /** @@ -40,14 +41,12 @@ use OCA\Social\Model\ActivityPub\ACore; * * @package OCA\Social\Model\ActivityPub\Object */ -class Announce extends ACore implements JsonSerializable { +class Announce extends Stream implements JsonSerializable { const TYPE = 'Announce'; - - /** * Follow constructor. * diff --git a/lib/Model/ActivityPub/Object/Note.php b/lib/Model/ActivityPub/Object/Note.php index 2f6e9286..8ede31d3 100644 --- a/lib/Model/ActivityPub/Object/Note.php +++ b/lib/Model/ActivityPub/Object/Note.php @@ -30,9 +30,9 @@ declare(strict_types=1); namespace OCA\Social\Model\ActivityPub\Object; -use DateTime; use JsonSerializable; use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Stream; class Note extends Stream implements JsonSerializable { @@ -40,30 +40,6 @@ class Note extends Stream implements JsonSerializable { const TYPE = 'Note'; - const TYPE_PUBLIC = 'public'; - const TYPE_UNLISTED = 'unlisted'; - const TYPE_FOLLOWERS = 'followers'; - const TYPE_DIRECT = 'direct'; - - - /** @var string */ - private $content = ''; - - /** @var string */ - private $attributedTo = ''; - - /** @var string */ - private $inReplyTo = ''; - - /** @var bool */ - private $sensitive = false; - - /** @var string */ - private $conversation = ''; - - /** @var int */ - private $publishedTime = 0; - /** * Note constructor. @@ -77,139 +53,11 @@ class Note extends Stream implements JsonSerializable { } - /** - * @return string - */ - public function getContent(): string { - return $this->content; - } - - /** - * @param string $content - * - * @return Note - */ - public function setContent(string $content): Note { - $this->content = $content; - - return $this; - } - - - /** - * @return string - */ - public function getAttributedTo(): string { - return $this->attributedTo; - } - - /** - * @param string $attributedTo - * - * @return Note - */ - public function setAttributedTo(string $attributedTo): Note { - $this->attributedTo = $attributedTo; - - return $this; - } - - /** - * @return string - */ - public function getInReplyTo(): string { - return $this->inReplyTo; - } - - /** - * @param string $inReplyTo - * - * @return Note - */ - public function setInReplyTo(string $inReplyTo): Note { - $this->inReplyTo = $inReplyTo; - - return $this; - } - - - /** - * @return bool - */ - public function isSensitive(): bool { - return $this->sensitive; - } - - /** - * @param bool $sensitive - * - * @return Note - */ - public function setSensitive(bool $sensitive): Note { - $this->sensitive = $sensitive; - - return $this; - } - - - /** - * @return string - */ - public function getConversation(): string { - return $this->conversation; - } - - /** - * @param string $conversation - * - * @return Note - */ - public function setConversation(string $conversation): Note { - $this->conversation = $conversation; - - return $this; - } - - - /** - * @return int - */ - public function getPublishedTime(): int { - return $this->publishedTime; - } - - /** - * @param int $time - * - * @return Note - */ - public function setPublishedTime(int $time): Note { - $this->publishedTime = $time; - - return $this; - } - - /** - * - */ - public function convertPublished() { - $dTime = new DateTime($this->getPublished()); - $this->setPublishedTime($dTime->getTimestamp()); - } - - /** * @param array $data */ public function import(array $data) { parent::import($data); - - $this->setInReplyTo($this->validate(ACore::AS_ID, 'inReplyTo', $data, '')); - $this->setAttributedTo($this->validate(ACore::AS_ID, 'attributedTo', $data, '')); - $this->setSensitive($this->getBool('sensitive', $data, false)); - $this->setConversation($this->validate(ACore::AS_ID, 'conversation', $data, '')); - $this->setContent($this->get('content', $data, '')); - $this->convertPublished(); } @@ -218,14 +66,6 @@ class Note extends Stream implements JsonSerializable { */ public function importFromDatabase(array $data) { parent::importFromDatabase($data); - - $dTime = new DateTime($this->get('published_time', $data, 'yesterday')); - - $this->setContent($this->validate(self::AS_STRING, 'content', $data, ''));; - - $this->setPublishedTime($dTime->getTimestamp()); - $this->setAttributedTo($this->validate(self::AS_ID, 'attributed_to', $data, '')); - $this->setInReplyTo($this->validate(self::AS_ID, 'in_reply_to', $data)); } @@ -233,18 +73,7 @@ class Note extends Stream implements JsonSerializable { * @return array */ public function jsonSerialize(): array { - $this->addEntryInt('publishedTime', $this->getPublishedTime()); - - return array_merge( - parent::jsonSerialize(), - [ - 'content' => $this->getContent(), - 'attributedTo' => $this->getUrlSocial() . $this->getAttributedTo(), - 'inReplyTo' => $this->getInReplyTo(), - 'sensitive' => $this->isSensitive(), - 'conversation' => $this->getConversation() - ] - ); + return parent::jsonSerialize(); } } diff --git a/lib/Model/ActivityPub/Stream.php b/lib/Model/ActivityPub/Stream.php index a1688429..486fccf6 100644 --- a/lib/Model/ActivityPub/Stream.php +++ b/lib/Model/ActivityPub/Stream.php @@ -27,18 +27,47 @@ declare(strict_types=1); * */ -namespace OCA\Social\Model\ActivityPub\Object; +namespace OCA\Social\Model\ActivityPub; +use daita\MySmallPhpTools\Model\Cache; +use daita\MySmallPhpTools\Model\CacheItem; +use DateTime; use JsonSerializable; -use OCA\Social\Model\ActivityPub\ACore; class Stream extends ACore implements JsonSerializable { - /** @var array */ - private $cache = []; + const TYPE_PUBLIC = 'public'; + const TYPE_UNLISTED = 'unlisted'; + const TYPE_FOLLOWERS = 'followers'; + const TYPE_DIRECT = 'direct'; + + + /** @var string */ + private $activityId; + + /** @var string */ + private $content = ''; + + /** @var string */ + private $attributedTo = ''; + + /** @var string */ + private $inReplyTo = ''; + + /** @var bool */ + private $sensitive = false; + + /** @var string */ + private $conversation = ''; + + /** @var Cache */ + private $cache = null; + + /** @var int */ + private $publishedTime = 0; public function __construct($parent = null) { @@ -47,33 +76,240 @@ class Stream extends ACore implements JsonSerializable { /** - * @return array + * @return string */ - public function getCache(): array { + public function getActivityId(): string { + return $this->activityId; + } + + /** + * @param string $activityId + * + * @return Stream + */ + public function setActivityId(string $activityId): Stream { + $this->activityId = $activityId; + + return $this; + } + + + /** + * @return string + */ + public function getContent(): string { + return $this->content; + } + + /** + * @param string $content + * + * @return Stream + */ + public function setContent(string $content): Stream { + $this->content = $content; + + return $this; + } + + + /** + * @return string + */ + public function getAttributedTo(): string { + return $this->attributedTo; + } + + /** + * @param string $attributedTo + * + * @return Stream + */ + public function setAttributedTo(string $attributedTo): Stream { + $this->attributedTo = $attributedTo; + + return $this; + } + + /** + * @return string + */ + public function getInReplyTo(): string { + return $this->inReplyTo; + } + + /** + * @param string $inReplyTo + * + * @return Stream + */ + public function setInReplyTo(string $inReplyTo): Stream { + $this->inReplyTo = $inReplyTo; + + return $this; + } + + + /** + * @return bool + */ + public function isSensitive(): bool { + return $this->sensitive; + } + + /** + * @param bool $sensitive + * + * @return Stream + */ + public function setSensitive(bool $sensitive): Stream { + $this->sensitive = $sensitive; + + return $this; + } + + + /** + * @return string + */ + public function getConversation(): string { + return $this->conversation; + } + + /** + * @param string $conversation + * + * @return Stream + */ + public function setConversation(string $conversation): Stream { + $this->conversation = $conversation; + + return $this; + } + + + /** + * @return int + */ + public function getPublishedTime(): int { + return $this->publishedTime; + } + + /** + * @param int $time + * + * @return Stream + */ + public function setPublishedTime(int $time): Stream { + $this->publishedTime = $time; + + return $this; + } + + /** + * + */ + public function convertPublished() { + $dTime = new DateTime($this->getPublished()); + $this->setPublishedTime($dTime->getTimestamp()); + } + + + /** + * @return bool + */ + public function gotCache(): bool { + return ($this->cache !== null); + } + + /** + * @return Cache + */ + public function getCache(): Cache { return $this->cache; } /** - * @param array $cache + * @param Cache $cache * * @return Stream */ - public function setCache(array $cache): Stream { + public function setCache(Cache $cache): Stream { $this->cache = $cache; return $this; } + public function addCacheItem(string $url): Stream { + $cacheItem = new CacheItem($url); + + if (!$this->gotCache()) { + $this->setCache(new Cache()); + } + + $this->getCache() + ->addItem($cacheItem); + + return $this; + } + + + public function import(array $data) { + parent::import($data); + + $this->setInReplyTo($this->validate(self::AS_ID, 'inReplyTo', $data, '')); + $this->setAttributedTo($this->validate(self::AS_ID, 'attributedTo', $data, '')); + $this->setSensitive($this->getBool('sensitive', $data, false)); + $this->setConversation($this->validate(self::AS_ID, 'conversation', $data, '')); + $this->setContent($this->get('content', $data, '')); + $this->convertPublished(); + } + + + /** + * @param array $data + */ + public function importFromDatabase(array $data) { + parent::importFromDatabase($data); + + $dTime = new DateTime($this->get('published_time', $data, 'yesterday')); + + $this->setActivityId($this->validate(self::AS_ID, 'activity_id', $data, '')); + $this->setContent($this->validate(self::AS_STRING, 'content', $data, ''));; + $this->setPublishedTime($dTime->getTimestamp()); + $this->setAttributedTo($this->validate(self::AS_ID, 'attributed_to', $data, '')); + $this->setInReplyTo($this->validate(self::AS_ID, 'in_reply_to', $data)); + + $cache = new Cache(); + $cache->import($this->getArray('cache', $data, [])); + $this->setCache($cache); + } + + /** * @return array */ public function jsonSerialize(): array { - $result = parent::jsonSerialize(); + $this->addEntryInt('publishedTime', $this->getPublishedTime()); + + $result = array_merge( + parent::jsonSerialize(), + [ + 'content' => $this->getContent(), + 'attributedTo' => $this->getUrlSocial() . $this->getAttributedTo(), + 'inReplyTo' => $this->getInReplyTo(), + 'sensitive' => $this->isSensitive(), + 'conversation' => $this->getConversation() + ] + ); + if ($this->isCompleteDetails()) { array_merge( $result, - ['cache' => $this->getCache()] + [ + 'cache' => $this->getCache() + ] ); }