]> git.mxchange.org Git - friendica.git/commitdiff
More data for the Mastodon status class
authorMichael <heluecht@pirati.ca>
Sun, 25 Oct 2020 16:13:31 +0000 (16:13 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 25 Oct 2020 16:13:31 +0000 (16:13 +0000)
src/DI.php
src/Factory/Api/Mastodon/Mention.php [new file with mode: 0644]
src/Factory/Api/Mastodon/Status.php
src/Factory/Api/Mastodon/Tag.php [new file with mode: 0644]
src/Object/Api/Mastodon/Card.php [new file with mode: 0644]
src/Object/Api/Mastodon/Mention.php [new file with mode: 0644]
src/Object/Api/Mastodon/Status.php
src/Object/Api/Mastodon/Status/UserAttributes.php [new file with mode: 0644]
src/Object/Api/Mastodon/Tag.php [new file with mode: 0644]

index 73f9b7811d37abf5e6f38752e5c2f599ebb4980c..e259c271a00ac4ef6dc82d13b4e21246b76d89eb 100644 (file)
@@ -279,6 +279,22 @@ abstract class DI
                return self::$dice->create(Factory\Api\Mastodon\Status::class);
        }
 
+       /**
+        * @return Factory\Api\Mastodon\Mention
+        */
+       public static function mstdnMention()
+       {
+               return self::$dice->create(Factory\Api\Mastodon\Mention::class);
+       }
+
+       /**
+        * @return Factory\Api\Mastodon\Tag
+        */
+       public static function mstdnTag()
+       {
+               return self::$dice->create(Factory\Api\Mastodon\Tag::class);
+       }
+
        /**
         * @return Factory\Api\Twitter\User
         */
diff --git a/src/Factory/Api/Mastodon/Mention.php b/src/Factory/Api/Mastodon/Mention.php
new file mode 100644 (file)
index 0000000..5ab82d7
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Factory\Api\Mastodon;
+
+use Friendica\App\BaseURL;
+use Friendica\BaseFactory;
+use Friendica\Model\Contact;
+use Friendica\Model\Tag;
+use Friendica\Network\HTTPException;
+use Friendica\Repository\ProfileField;
+use Psr\Log\LoggerInterface;
+
+class Mention extends BaseFactory
+{
+       /** @var BaseURL */
+       protected $baseUrl;
+       /** @var ProfileField */
+       protected $profileField;
+       /** @var Field */
+       protected $mstdnField;
+
+       public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
+       {
+               parent::__construct($logger);
+
+               $this->baseUrl = $baseURL;
+               $this->profileField = $profileField;
+               $this->mstdnField = $mstdnField;
+       }
+
+       /**
+        * @param int $uriId Uri-ID of the item
+        * @return array
+        * @throws HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public function createFromUriId(int $uriId)
+       {
+               $mentions = [];
+               $tags = Tag::getByURIId($uriId, [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION]);
+               foreach ($tags as $tag) {
+                       $contact = Contact::getByURL($tag['url'], false);
+                       $mention = new \Friendica\Object\Api\Mastodon\Mention($this->baseUrl, $tag, $contact);
+                       $mentions[] = $mention->toArray();
+               }
+               return $mentions;
+       }
+}
index c31c211a59ec7bf03b168885bdab8e8d24a4fd75..0069c91b32619e57385b84598a4f243be74ce981 100644 (file)
@@ -23,6 +23,7 @@ namespace Friendica\Factory\Api\Mastodon;
 
 use Friendica\App\BaseURL;
 use Friendica\BaseFactory;
+use Friendica\Content\Text\BBCode;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Item;
@@ -68,6 +69,22 @@ class Status extends BaseFactory
                        DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)])
                );
 
-               return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts);
+               $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
+                       DBA::exists('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)]),
+                       DBA::exists('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]),
+                       DBA::exists('thread', ['iid' => $item['id'], 'uid' => $item['uid'], 'ignored' => true]),
+                       (bool)$item['starred'],
+                       DBA::exists('user-item', ['iid' => $item['id'], 'uid' => $item['uid'], 'pinned' => true])
+               );
+
+               $sensitive = DBA::exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']);
+               $application = new \Friendica\Object\Api\Mastodon\Application($item['app']);
+               $mentions = DI::mstdnMention()->createFromUriId($uriId);
+               $tags = DI::mstdnTag()->createFromUriId($uriId);
+
+               $attachment = BBCode::getAttachmentData($item['body']);
+               $card = new \Friendica\Object\Api\Mastodon\Card($attachment);
+
+               return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card);
        }
 }
diff --git a/src/Factory/Api/Mastodon/Tag.php b/src/Factory/Api/Mastodon/Tag.php
new file mode 100644 (file)
index 0000000..9b81e6d
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Factory\Api\Mastodon;
+
+use Friendica\App\BaseURL;
+use Friendica\BaseFactory;
+use Friendica\Model\Tag as TagModel;
+use Friendica\Network\HTTPException;
+use Friendica\Repository\ProfileField;
+use Psr\Log\LoggerInterface;
+
+class Tag extends BaseFactory
+{
+       /** @var BaseURL */
+       protected $baseUrl;
+       /** @var ProfileField */
+       protected $profileField;
+       /** @var Field */
+       protected $mstdnField;
+
+       public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
+       {
+               parent::__construct($logger);
+
+               $this->baseUrl = $baseURL;
+               $this->profileField = $profileField;
+               $this->mstdnField = $mstdnField;
+       }
+
+       /**
+        * @param int $uriId Uri-ID of the item
+        * @return array
+        * @throws HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public function createFromUriId(int $uriId)
+       {
+               $hashtags = [];
+               $tags = TagModel::getByURIId($uriId, [TagModel::HASHTAG]);
+               foreach ($tags as $tag) {
+                       $hashtag = new \Friendica\Object\Api\Mastodon\Tag($this->baseUrl, $tag);
+                       $hashtags[] = $hashtag->toArray();
+               }
+               return $hashtags;
+       }
+}
diff --git a/src/Object/Api/Mastodon/Card.php b/src/Object/Api/Mastodon/Card.php
new file mode 100644 (file)
index 0000000..990f6a6
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Object\Api\Mastodon;
+
+use Friendica\BaseEntity;
+use Friendica\Content\Text\BBCode;
+use Friendica\Object\Api\Mastodon\Status\Counts;
+use Friendica\Object\Api\Mastodon\Status\UserAttributes;
+use Friendica\Util\DateTimeFormat;
+
+/**
+ * Class Card
+ *
+ * @see https://docs.joinmastodon.org/entities/card
+ */
+class Card extends BaseEntity
+{
+       /** @var string */
+       protected $url;
+       /** @var string */
+       protected $title;
+       /** @var string */
+       protected $description;
+       /** @var string */
+       protected $type;
+       /** @var string */
+       protected $image;
+
+       /**
+        * Creates a status record from an item record.
+        *
+        * @param array   $attachment Attachment record
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       public function __construct(array $attachment)
+       {
+               $this->url         = $attachment['url'] ?? '';
+               $this->title       = $attachment['title'] ?? '';
+               $this->description = $attachment['description'] ?? '';
+               $this->type        = $attachment['type'] ?? '';
+               $this->image       = $attachment['image'] ?? '';
+       }
+}
diff --git a/src/Object/Api/Mastodon/Mention.php b/src/Object/Api/Mastodon/Mention.php
new file mode 100644 (file)
index 0000000..22e623e
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Object\Api\Mastodon;
+
+use Friendica\App\BaseURL;
+use Friendica\BaseEntity;
+
+/**
+ * Class Mention
+ *
+ * @see https://docs.joinmastodon.org/entities/mention
+ */
+class Mention extends BaseEntity
+{
+       /** @var string */
+       protected $id;
+       /** @var string */
+       protected $username;
+       /** @var string */
+       protected $url = null;
+       /** @var string */
+       protected $acct = null;
+
+       /**
+        * Creates a mention record from an tag-view record.
+        *
+        * @param BaseURL $baseUrl
+        * @param array   $tag     tag-view record
+        * @param array   $contact contact table record
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       public function __construct(BaseURL $baseUrl, array $tag, array $contact)
+       {
+               $this->id       = $contact['id'] ?? 0;
+               $this->username = $tag['name'];
+               $this->url      = $tag['url'];
+
+               if (!empty($contact)) {
+                       $this->acct =
+                               strpos($contact['url'], $baseUrl->get() . '/') === 0 ?
+                                       $contact['nick'] :
+                                       $contact['addr'];
+               } else {
+                       $this->acct = '';
+               }
+       }
+}
index aa26fa1986aaa2a7fcd85977c46d772019084674..cc9108fc8cfcbaadf9e817f8fe650ef382fd6dfe 100644 (file)
@@ -24,6 +24,7 @@ namespace Friendica\Object\Api\Mastodon;
 use Friendica\BaseEntity;
 use Friendica\Content\Text\BBCode;
 use Friendica\Object\Api\Mastodon\Status\Counts;
+use Friendica\Object\Api\Mastodon\Status\UserAttributes;
 use Friendica\Util\DateTimeFormat;
 
 /**
@@ -96,7 +97,7 @@ class Status extends BaseEntity
         * @param array   $item
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function __construct(array $item, Account $account, Counts $counts)
+       public function __construct(array $item, Account $account, Counts $counts, UserAttributes $userAttributes, bool $sensitive, Application $application, array $mentions, array $tags, Card $card)
        {
                $this->id         = (string)$item['uri-id'];
                $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM);
@@ -106,32 +107,34 @@ class Status extends BaseEntity
                        $this->in_reply_to_account_id = (string)$item['parent-author-id'];
                }
 
-               $this->sensitive = false;
+               $this->sensitive = $sensitive;
                $this->spoiler_text = $item['title'];
 
                $visibility = ['public', 'private', 'unlisted'];
                $this->visibility = $visibility[$item['private']];
 
-               $this->language = null;
+               $languages = json_decode($item['language'], true);
+               $this->language = is_array($languages) ? array_key_first($languages) : null;
+
                $this->uri = $item['uri'];
                $this->url = $item['plink'] ?? null;
                $this->replies_count = $counts->replies;
                $this->reblogs_count = $counts->reblogs;
                $this->favourites_count = $counts->favourites;
-               $this->favourited = false;
-               $this->reblogged = false;
-               $this->muted = false;
-               $this->bookmarked = false;
-               $this->pinned = false;
+               $this->favourited = $userAttributes->favourited;
+               $this->reblogged = $userAttributes->reblogged;
+               $this->muted = $userAttributes->muted;
+               $this->bookmarked = $userAttributes->bookmarked;
+               $this->pinned = $userAttributes->pinned;
                $this->content = BBCode::convert($item['body'], false);
-               $this->reblog = null;
-               $this->application = null;
+               $this->reblog = null; /// @todo
+               $this->application = $application->toArray();
                $this->account = $account->toArray();
-               $this->media_attachments = [];
-               $this->mentions = [];
-               $this->tags = [];
+               $this->media_attachments = []; /// @todo
+               $this->mentions = $mentions;
+               $this->tags = $tags;
                $this->emojis = [];
-               $this->card = null;
+               $this->card = $card->toArray();
                $this->poll = null;
        }
 }
diff --git a/src/Object/Api/Mastodon/Status/UserAttributes.php b/src/Object/Api/Mastodon/Status/UserAttributes.php
new file mode 100644 (file)
index 0000000..f33cc51
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Object\Api\Mastodon\Status;
+
+/**
+ * Class UserAttributes
+ *
+ * @see https://docs.joinmastodon.org/entities/status
+ */
+class UserAttributes
+{
+       /** @var bool */
+       protected $favourited;
+       /** @var bool */
+       protected $reblogged;
+       /** @var bool */
+       protected $muted;
+       /** @var bool */
+       protected $bookmarked;
+       /** @var bool */
+       protected $pinned;
+
+       /**
+        * Creates a authorized user attributes object
+        *
+        * @param bool $favourited
+        * @param bool $reblogged
+        * @param bool $muted
+        * @param bool $bookmarked
+        * @param bool $pinned
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       public function __construct(bool $favourited, bool $reblogged, bool $muted, bool $bookmarked, bool $pinned)
+       {
+               $this->favourited = $favourited;
+               $this->reblogged = $reblogged;
+               $this->muted = $muted;
+               $this->bookmarked = $bookmarked;
+               $this->pinned = $pinned;
+       }
+
+       public function __get($name) {
+        return $this->$name;
+    }
+}
diff --git a/src/Object/Api/Mastodon/Tag.php b/src/Object/Api/Mastodon/Tag.php
new file mode 100644 (file)
index 0000000..0cb9107
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Object\Api\Mastodon;
+
+use Friendica\App\BaseURL;
+use Friendica\BaseEntity;
+
+/**
+ * Class Tag
+ *
+ * @see https://docs.joinmastodon.org/entities/tag
+ */
+class Tag extends BaseEntity
+{
+       /** @var string */
+       protected $name;
+       /** @var string */
+       protected $url = null;
+
+       /**
+        * Creates a hashtag record from an tag-view record.
+        *
+        * @param BaseURL $baseUrl
+        * @param array   $tag     tag-view record
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       public function __construct(BaseURL $baseUrl, array $tag)
+       {
+               $this->name = $tag['name'];
+               $this->url  = $baseUrl . '/search?tag=' . urlencode($tag['name']);
+       }
+}