]> git.mxchange.org Git - friendica.git/commitdiff
Move Mastodon API entities to src/Object
authorHypolite Petovan <hypolite@mrpetovan.com>
Tue, 28 Jan 2020 01:01:32 +0000 (20:01 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 28 Jan 2020 01:01:32 +0000 (20:01 -0500)
21 files changed:
src/Api/BaseEntity.php [deleted file]
src/Api/Entity/Mastodon/Account.php [deleted file]
src/Api/Entity/Mastodon/Emoji.php [deleted file]
src/Api/Entity/Mastodon/Field.php [deleted file]
src/Api/Entity/Mastodon/FollowRequest.php [deleted file]
src/Api/Entity/Mastodon/Instance.php [deleted file]
src/Api/Entity/Mastodon/Relationship.php [deleted file]
src/Api/Entity/Mastodon/Stats.php [deleted file]
src/BaseEntity.php [new file with mode: 0644]
src/Factory/Mastodon/Account.php
src/Factory/Mastodon/FollowRequest.php
src/Factory/Mastodon/Relationship.php
src/Module/Api/Mastodon/FollowRequests.php
src/Module/Api/Mastodon/Instance.php
src/Object/Api/Mastodon/Account.php [new file with mode: 0644]
src/Object/Api/Mastodon/Emoji.php [new file with mode: 0644]
src/Object/Api/Mastodon/Field.php [new file with mode: 0644]
src/Object/Api/Mastodon/FollowRequest.php [new file with mode: 0644]
src/Object/Api/Mastodon/Instance.php [new file with mode: 0644]
src/Object/Api/Mastodon/Relationship.php [new file with mode: 0644]
src/Object/Api/Mastodon/Stats.php [new file with mode: 0644]

diff --git a/src/Api/BaseEntity.php b/src/Api/BaseEntity.php
deleted file mode 100644 (file)
index 4bc1594..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-<?php
-
-namespace Friendica\Api;
-
-/**
- * The API entity classes are meant as data transfer objects. As such, their member should be protected.
- * Then the JsonSerializable interface ensures the protected members will be included in a JSON encode situation.
- *
- * Constructors are supposed to take as arguments the Friendica dependencies/model/collection/data it needs to
- * populate the class members.
- */
-abstract class BaseEntity implements \JsonSerializable
-{
-       public function jsonSerialize()
-       {
-               return get_object_vars($this);
-       }
-}
diff --git a/src/Api/Entity/Mastodon/Account.php b/src/Api/Entity/Mastodon/Account.php
deleted file mode 100644 (file)
index 8123cf5..0000000
+++ /dev/null
@@ -1,108 +0,0 @@
-<?php
-
-namespace Friendica\Api\Entity\Mastodon;
-
-use Friendica\Api\BaseEntity;
-use Friendica\App\BaseURL;
-use Friendica\Content\Text\BBCode;
-use Friendica\Database\DBA;
-use Friendica\Model\Contact;
-use Friendica\Util\DateTimeFormat;
-
-/**
- * Class Account
- *
- * @see https://docs.joinmastodon.org/entities/account
- */
-class Account extends BaseEntity
-{
-       /** @var string */
-       protected $id;
-       /** @var string */
-       protected $username;
-       /** @var string */
-       protected $acct;
-       /** @var string */
-       protected $display_name;
-       /** @var bool */
-       protected $locked;
-       /** @var string (Datetime) */
-       protected $created_at;
-       /** @var int */
-       protected $followers_count;
-       /** @var int */
-       protected $following_count;
-       /** @var int */
-       protected $statuses_count;
-       /** @var string */
-       protected $note;
-       /** @var string (URL)*/
-       protected $url;
-       /** @var string (URL) */
-       protected $avatar;
-       /** @var string (URL) */
-       protected $avatar_static;
-       /** @var string (URL) */
-       protected $header;
-       /** @var string (URL) */
-       protected $header_static;
-       /** @var Emoji[] */
-       protected $emojis;
-       /** @var Account|null */
-       protected $moved = null;
-       /** @var Field[]|null */
-       protected $fields = null;
-       /** @var bool|null */
-       protected $bot = null;
-       /** @var bool */
-       protected $group;
-       /** @var bool */
-       protected $discoverable;
-       /** @var string|null (Datetime) */
-       protected $last_status_at = null;
-
-       /**
-        * Creates an account record from a public contact record. Expects all contact table fields to be set.
-        *
-        * @param BaseURL $baseUrl
-        * @param array   $publicContact Full contact table record with uid = 0
-        * @param array   $apcontact     Optional full apcontact table record
-        * @param array   $userContact   Optional full contact table record with uid != 0
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        */
-       public function __construct(BaseURL $baseUrl, array $publicContact, array $apcontact = [], array $userContact = [])
-       {
-               $this->id              = $publicContact['id'];
-               $this->username        = $publicContact['nick'];
-               $this->acct            =
-                       strpos($publicContact['url'], $baseUrl->get() . '/') === 0 ?
-                               $publicContact['nick'] :
-                               $publicContact['addr'];
-               $this->display_name    = $publicContact['name'];
-               $this->locked          = !empty($apcontact['manually-approve']);
-               $this->created_at      = DateTimeFormat::utc($publicContact['created'], DateTimeFormat::ATOM);
-               $this->followers_count = $apcontact['followers_count'] ?? 0;
-               $this->following_count = $apcontact['following_count'] ?? 0;
-               $this->statuses_count  = $apcontact['statuses_count'] ?? 0;
-               $this->note            = BBCode::convert($publicContact['about'], false);
-               $this->url             = $publicContact['url'];
-               $this->avatar          = $userContact['avatar'] ?? $publicContact['avatar'];
-               $this->avatar_static   = $userContact['avatar'] ?? $publicContact['avatar'];
-               // No header picture in Friendica
-               $this->header          = '';
-               $this->header_static   = '';
-               // No custom emojis per account in Friendica
-               $this->emojis          = [];
-               // No metadata fields in Friendica
-               $this->fields          = [];
-               $this->bot             = ($publicContact['contact-type'] == Contact::TYPE_NEWS);
-               $this->group           = ($publicContact['contact-type'] == Contact::TYPE_COMMUNITY);
-               $this->discoverable    = !$publicContact['unsearchable'];
-
-               $publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME;
-               $userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME;
-
-               $lastItem = $userContactLastItem > $publicContactLastItem ? $userContactLastItem : $publicContactLastItem;
-               $this->last_status_at  = $lastItem != DBA::NULL_DATETIME ? DateTimeFormat::utc($lastItem, DateTimeFormat::ATOM) : null;
-       }
-}
diff --git a/src/Api/Entity/Mastodon/Emoji.php b/src/Api/Entity/Mastodon/Emoji.php
deleted file mode 100644 (file)
index f3dbfa5..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php
-
-namespace Friendica\Api\Entity\Mastodon;
-
-use Friendica\Api\BaseEntity;
-
-/**
- * Class Emoji
- *
- * @see https://docs.joinmastodon.org/api/entities/#emoji
- */
-class Emoji extends BaseEntity
-{
-       /** @var string */
-       protected $shortcode;
-       /** @var string (URL)*/
-       protected $static_url;
-       /** @var string (URL)*/
-       protected $url;
-       /** @var bool */
-       protected $visible_in_picker;
-}
diff --git a/src/Api/Entity/Mastodon/Field.php b/src/Api/Entity/Mastodon/Field.php
deleted file mode 100644 (file)
index f01128a..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-
-namespace Friendica\Api\Entity\Mastodon;
-
-use Friendica\Api\BaseEntity;
-
-/**
- * Class Field
- *
- * @see https://docs.joinmastodon.org/api/entities/#field
- */
-class Field extends BaseEntity
-{
-       /** @var string */
-       protected $name;
-       /** @var string (HTML) */
-       protected $value;
-       /** @var string (Datetime)*/
-       protected $verified_at;
-}
diff --git a/src/Api/Entity/Mastodon/FollowRequest.php b/src/Api/Entity/Mastodon/FollowRequest.php
deleted file mode 100644 (file)
index a677a69..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-<?php
-
-namespace Friendica\Api\Entity\Mastodon;
-
-use Friendica\App\BaseURL;
-use Friendica\Model\Introduction;
-
-/**
- * Virtual entity to separate Accounts from Follow Requests.
- * In the Mastodon API they are one and the same.
- */
-class FollowRequest extends Account
-{
-       /**
-        * Creates a follow request entity from an introduction record.
-        *
-        * The account ID is set to the Introduction ID to allow for later interaction with follow requests.
-        *
-        * @param BaseURL $baseUrl
-        * @param int     $introduction_id Introduction record id
-        * @param array   $publicContact   Full contact table record with uid = 0
-        * @param array   $apcontact       Optional full apcontact table record
-        * @param array   $userContact     Optional full contact table record with uid != 0
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        */
-       public function __construct(BaseURL $baseUrl, int $introduction_id, array $publicContact, array $apcontact = [], array $userContact = [])
-       {
-               parent::__construct($baseUrl, $publicContact, $apcontact, $userContact);
-
-               $this->id = $introduction_id;
-       }
-}
diff --git a/src/Api/Entity/Mastodon/Instance.php b/src/Api/Entity/Mastodon/Instance.php
deleted file mode 100644 (file)
index b122f78..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-<?php
-
-namespace Friendica\Api\Entity\Mastodon;
-
-use Friendica\Api\BaseEntity;
-use Friendica\Database\DBA;
-use Friendica\DI;
-use Friendica\Model\User;
-use Friendica\Module\Register;
-
-/**
- * Class Instance
- *
- * @see https://docs.joinmastodon.org/api/entities/#instance
- */
-class Instance extends BaseEntity
-{
-       /** @var string (URL) */
-       protected $uri;
-       /** @var string */
-       protected $title;
-       /** @var string */
-       protected $description;
-       /** @var string */
-       protected $email;
-       /** @var string */
-       protected $version;
-       /** @var array */
-       protected $urls;
-       /** @var Stats */
-       protected $stats;
-       /** @var string|null */
-       protected $thumbnail = null;
-       /** @var array */
-       protected $languages;
-       /** @var int */
-       protected $max_toot_chars;
-       /** @var bool */
-       protected $registrations;
-       /** @var bool */
-       protected $approval_required;
-       /** @var Account|null */
-       protected $contact_account = null;
-
-       /**
-        * Creates an instance record
-        *
-        * @return Instance
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        * @throws \ImagickException
-        */
-       public static function get()
-       {
-               $register_policy = intval(DI::config()->get('config', 'register_policy'));
-
-               $baseUrl = DI::baseUrl();
-
-               $instance = new Instance();
-               $instance->uri = $baseUrl->get();
-               $instance->title = DI::config()->get('config', 'sitename');
-               $instance->description = DI::config()->get('config', 'info');
-               $instance->email = DI::config()->get('config', 'admin_email');
-               $instance->version = FRIENDICA_VERSION;
-               $instance->urls = []; // Not supported
-               $instance->stats = Stats::get();
-               $instance->thumbnail = $baseUrl->get() . (DI::config()->get('system', 'shortcut_icon') ?? 'images/friendica-32.png');
-               $instance->languages = [DI::config()->get('system', 'language')];
-               $instance->max_toot_chars = (int)DI::config()->get('config', 'api_import_size', DI::config()->get('config', 'max_import_size'));
-               $instance->registrations = ($register_policy != Register::CLOSED);
-               $instance->approval_required = ($register_policy == Register::APPROVE);
-               $instance->contact_account = [];
-
-               if (!empty(DI::config()->get('config', 'admin_email'))) {
-                       $adminList = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
-                       $administrator = User::getByEmail($adminList[0], ['nickname']);
-                       if (!empty($administrator)) {
-                               $adminContact = DBA::selectFirst('contact', ['id'], ['nick' => $administrator['nickname'], 'self' => true]);
-                               $instance->contact_account = DI::mstdnAccount()->createFromContactId($adminContact['id']);
-                       }
-               }
-
-               return $instance;
-       }
-}
diff --git a/src/Api/Entity/Mastodon/Relationship.php b/src/Api/Entity/Mastodon/Relationship.php
deleted file mode 100644 (file)
index 44a7d70..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-<?php
-
-namespace Friendica\Api\Entity\Mastodon;
-
-use Friendica\Api\BaseEntity;
-use Friendica\Model\Contact;
-use Friendica\Util\Network;
-
-/**
- * Class Relationship
- *
- * @see https://docs.joinmastodon.org/api/entities/#relationship
- */
-class Relationship extends BaseEntity
-{
-       /** @var int */
-       protected $id;
-       /** @var bool */
-       protected $following = false;
-       /** @var bool */
-       protected $followed_by = false;
-       /** @var bool */
-       protected $blocking = false;
-       /** @var bool */
-       protected $muting = false;
-       /** @var bool */
-       protected $muting_notifications = false;
-       /** @var bool */
-       protected $requested = false;
-       /** @var bool */
-       protected $domain_blocking = false;
-       /**
-        * Unsupported
-        * @var bool
-        */
-       protected $showing_reblogs = true;
-       /**
-        * Unsupported
-        * @var bool
-        */
-       protected $endorsed = false;
-
-       /**
-        * @param int   $userContactId Contact row Id with uid != 0
-        * @param array $userContact   Full Contact table record with uid != 0
-        */
-       public function __construct(int $userContactId, array $userContact = [])
-       {
-               $this->id                   = $userContactId;
-               $this->following            = in_array($userContact['rel'] ?? 0, [Contact::SHARING, Contact::FRIEND]);
-               $this->followed_by          = in_array($userContact['rel'] ?? 0, [Contact::FOLLOWER, Contact::FRIEND]);
-               $this->blocking             = (bool)$userContact['blocked'] ?? false;
-               $this->muting               = (bool)$userContact['readonly'] ?? false;
-               $this->muting_notifications = (bool)$userContact['readonly'] ?? false;
-               $this->requested            = (bool)$userContact['pending'] ?? false;
-               $this->domain_blocking      = Network::isUrlBlocked($userContact['url'] ?? '');
-
-               return $this;
-       }
-}
diff --git a/src/Api/Entity/Mastodon/Stats.php b/src/Api/Entity/Mastodon/Stats.php
deleted file mode 100644 (file)
index 16c9893..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-<?php
-
-namespace Friendica\Api\Entity\Mastodon;
-
-use Friendica\Api\BaseEntity;
-use Friendica\Core\Protocol;
-use Friendica\Database\DBA;
-use Friendica\DI;
-
-/**
- * Class Stats
- *
- * @see https://docs.joinmastodon.org/api/entities/#stats
- */
-class Stats extends BaseEntity
-{
-       /** @var int */
-       protected $user_count = 0;
-       /** @var int */
-       protected $status_count = 0;
-       /** @var int */
-       protected $domain_count = 0;
-
-       /**
-        * Creates a stats record
-        *
-        * @return Stats
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        */
-       public static function get() {
-               $stats = new Stats();
-               if (!empty(DI::config()->get('system', 'nodeinfo'))) {
-                       $stats->user_count = intval(DI::config()->get('nodeinfo', 'total_users'));
-                       $stats->status_count = DI::config()->get('nodeinfo', 'local_posts') + DI::config()->get('nodeinfo', 'local_comments');
-                       $stats->domain_count = DBA::count('gserver', ["`network` in (?, ?) AND `last_contact` >= `last_failure`", Protocol::DFRN, Protocol::ACTIVITYPUB]);
-               }
-               return $stats;
-       }
-}
diff --git a/src/BaseEntity.php b/src/BaseEntity.php
new file mode 100644 (file)
index 0000000..9f0cb31
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+namespace Friendica;
+
+/**
+ * The API entity classes are meant as data transfer objects. As such, their member should be protected.
+ * Then the JsonSerializable interface ensures the protected members will be included in a JSON encode situation.
+ *
+ * Constructors are supposed to take as arguments the Friendica dependencies/model/collection/data it needs to
+ * populate the class members.
+ */
+abstract class BaseEntity implements \JsonSerializable
+{
+       public function jsonSerialize()
+       {
+               return get_object_vars($this);
+       }
+}
index e38654b29503105bfc9ece2573229a938d1e8c19..e777e76fe92c1c8ce59584bd77eea4705ec32d22 100644 (file)
@@ -24,7 +24,7 @@ class Account extends BaseFactory
        /**
         * @param int $contactId
         * @param int $uid        User Id
-        * @return \Friendica\Api\Entity\Mastodon\Account
+        * @return \Friendica\Object\Api\Mastodon\Account
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
@@ -41,6 +41,6 @@ class Account extends BaseFactory
 
                $apcontact = APContact::getByURL($publicContact['url'], false);
 
-               return new \Friendica\Api\Entity\Mastodon\Account($this->baseUrl, $publicContact, $apcontact, $userContact);
+               return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $apcontact, $userContact);
        }
 }
index bbaa3135c9b49d68d94d75e8d38313b8f19d8449..3aabe413d6ea0aba6fc700896c6843f8e886fb95 100644 (file)
@@ -24,7 +24,7 @@ class FollowRequest extends BaseFactory
 
        /**
         * @param Introduction $introduction
-        * @return \Friendica\Api\Entity\Mastodon\FollowRequest
+        * @return \Friendica\Object\Api\Mastodon\FollowRequest
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
@@ -42,6 +42,6 @@ class FollowRequest extends BaseFactory
 
                $apcontact = APContact::getByURL($publicContact['url'], false);
 
-               return new \Friendica\Api\Entity\Mastodon\FollowRequest($this->baseUrl, $introduction->id, $publicContact, $apcontact, $userContact);
+               return new \Friendica\Object\Api\Mastodon\FollowRequest($this->baseUrl, $introduction->id, $publicContact, $apcontact, $userContact);
        }
 }
index 25c9d4c8ce3127525927d411124562407637922e..7a8b216d4e8bee61146815de7b28a8af9e56beda 100644 (file)
@@ -2,7 +2,7 @@
 
 namespace Friendica\Factory\Mastodon;
 
-use Friendica\Api\Entity\Mastodon\Relationship as RelationshipEntity;
+use Friendica\Object\Api\Mastodon\Relationship as RelationshipEntity;
 use Friendica\BaseFactory;
 use Friendica\Model\Contact;
 
index fc384f79793d959b0aa787ed9150cef47bfb82a9..79fe487834f0d6c5440dd7665fc1432f337e767c 100644 (file)
@@ -2,8 +2,8 @@
 
 namespace Friendica\Module\Api\Mastodon;
 
-use Friendica\Api\Entity\Mastodon;
-use Friendica\Api\Entity\Mastodon\Relationship;
+use Friendica\Object\Api\Mastodon;
+use Friendica\Object\Api\Mastodon\Relationship;
 use Friendica\Core\System;
 use Friendica\DI;
 use Friendica\Model\Contact;
index cc7639f460be6d6977123353694bf712a292efb2..c3d1b93ac0e215d4ac6d2b5b10fd510b1847bd4e 100644 (file)
@@ -2,7 +2,7 @@
 
 namespace Friendica\Module\Api\Mastodon;
 
-use Friendica\Api\Entity\Mastodon\Instance as InstanceEntity;
+use Friendica\Object\Api\Mastodon\Instance as InstanceEntity;
 use Friendica\Core\System;
 use Friendica\Module\Base\Api;
 
diff --git a/src/Object/Api/Mastodon/Account.php b/src/Object/Api/Mastodon/Account.php
new file mode 100644 (file)
index 0000000..38eefee
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+
+namespace Friendica\Object\Api\Mastodon;
+
+use Friendica\App\BaseURL;
+use Friendica\BaseEntity;
+use Friendica\Content\Text\BBCode;
+use Friendica\Database\DBA;
+use Friendica\Model\Contact;
+use Friendica\Util\DateTimeFormat;
+
+/**
+ * Class Account
+ *
+ * @see https://docs.joinmastodon.org/entities/account
+ */
+class Account extends BaseEntity
+{
+       /** @var string */
+       protected $id;
+       /** @var string */
+       protected $username;
+       /** @var string */
+       protected $acct;
+       /** @var string */
+       protected $display_name;
+       /** @var bool */
+       protected $locked;
+       /** @var string (Datetime) */
+       protected $created_at;
+       /** @var int */
+       protected $followers_count;
+       /** @var int */
+       protected $following_count;
+       /** @var int */
+       protected $statuses_count;
+       /** @var string */
+       protected $note;
+       /** @var string (URL)*/
+       protected $url;
+       /** @var string (URL) */
+       protected $avatar;
+       /** @var string (URL) */
+       protected $avatar_static;
+       /** @var string (URL) */
+       protected $header;
+       /** @var string (URL) */
+       protected $header_static;
+       /** @var Emoji[] */
+       protected $emojis;
+       /** @var Account|null */
+       protected $moved = null;
+       /** @var Field[]|null */
+       protected $fields = null;
+       /** @var bool|null */
+       protected $bot = null;
+       /** @var bool */
+       protected $group;
+       /** @var bool */
+       protected $discoverable;
+       /** @var string|null (Datetime) */
+       protected $last_status_at = null;
+
+       /**
+        * Creates an account record from a public contact record. Expects all contact table fields to be set.
+        *
+        * @param BaseURL $baseUrl
+        * @param array   $publicContact Full contact table record with uid = 0
+        * @param array   $apcontact     Optional full apcontact table record
+        * @param array   $userContact   Optional full contact table record with uid != 0
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       public function __construct(BaseURL $baseUrl, array $publicContact, array $apcontact = [], array $userContact = [])
+       {
+               $this->id              = $publicContact['id'];
+               $this->username        = $publicContact['nick'];
+               $this->acct            =
+                       strpos($publicContact['url'], $baseUrl->get() . '/') === 0 ?
+                               $publicContact['nick'] :
+                               $publicContact['addr'];
+               $this->display_name    = $publicContact['name'];
+               $this->locked          = !empty($apcontact['manually-approve']);
+               $this->created_at      = DateTimeFormat::utc($publicContact['created'], DateTimeFormat::ATOM);
+               $this->followers_count = $apcontact['followers_count'] ?? 0;
+               $this->following_count = $apcontact['following_count'] ?? 0;
+               $this->statuses_count  = $apcontact['statuses_count'] ?? 0;
+               $this->note            = BBCode::convert($publicContact['about'], false);
+               $this->url             = $publicContact['url'];
+               $this->avatar          = $userContact['avatar'] ?? $publicContact['avatar'];
+               $this->avatar_static   = $userContact['avatar'] ?? $publicContact['avatar'];
+               // No header picture in Friendica
+               $this->header          = '';
+               $this->header_static   = '';
+               // No custom emojis per account in Friendica
+               $this->emojis          = [];
+               // No metadata fields in Friendica
+               $this->fields          = [];
+               $this->bot             = ($publicContact['contact-type'] == Contact::TYPE_NEWS);
+               $this->group           = ($publicContact['contact-type'] == Contact::TYPE_COMMUNITY);
+               $this->discoverable    = !$publicContact['unsearchable'];
+
+               $publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME;
+               $userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME;
+
+               $lastItem = $userContactLastItem > $publicContactLastItem ? $userContactLastItem : $publicContactLastItem;
+               $this->last_status_at  = $lastItem != DBA::NULL_DATETIME ? DateTimeFormat::utc($lastItem, DateTimeFormat::ATOM) : null;
+       }
+}
diff --git a/src/Object/Api/Mastodon/Emoji.php b/src/Object/Api/Mastodon/Emoji.php
new file mode 100644 (file)
index 0000000..725a72a
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+namespace Friendica\Object\Api\Mastodon;
+
+use Friendica\BaseEntity;
+
+/**
+ * Class Emoji
+ *
+ * @see https://docs.joinmastodon.org/api/entities/#emoji
+ */
+class Emoji extends BaseEntity
+{
+       /** @var string */
+       protected $shortcode;
+       /** @var string (URL) */
+       protected $static_url;
+       /** @var string (URL) */
+       protected $url;
+       /** @var bool */
+       protected $visible_in_picker;
+}
diff --git a/src/Object/Api/Mastodon/Field.php b/src/Object/Api/Mastodon/Field.php
new file mode 100644 (file)
index 0000000..07cd2e5
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+
+namespace Friendica\Object\Api\Mastodon;
+
+use Friendica\BaseEntity;
+
+/**
+ * Class Field
+ *
+ * @see https://docs.joinmastodon.org/api/entities/#field
+ */
+class Field extends BaseEntity
+{
+       /** @var string */
+       protected $name;
+       /** @var string (HTML) */
+       protected $value;
+       /** @var string (Datetime)*/
+       protected $verified_at;
+}
diff --git a/src/Object/Api/Mastodon/FollowRequest.php b/src/Object/Api/Mastodon/FollowRequest.php
new file mode 100644 (file)
index 0000000..ed72f07
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+
+namespace Friendica\Object\Api\Mastodon;
+
+use Friendica\App\BaseURL;
+
+/**
+ * Virtual entity to separate Accounts from Follow Requests.
+ * In the Mastodon API they are one and the same.
+ */
+class FollowRequest extends Account
+{
+       /**
+        * Creates a follow request entity from an introduction record.
+        *
+        * The account ID is set to the Introduction ID to allow for later interaction with follow requests.
+        *
+        * @param BaseURL $baseUrl
+        * @param int     $introduction_id Introduction record id
+        * @param array   $publicContact   Full contact table record with uid = 0
+        * @param array   $apcontact       Optional full apcontact table record
+        * @param array   $userContact     Optional full contact table record with uid != 0
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       public function __construct(BaseURL $baseUrl, int $introduction_id, array $publicContact, array $apcontact = [], array $userContact = [])
+       {
+               parent::__construct($baseUrl, $publicContact, $apcontact, $userContact);
+
+               $this->id = $introduction_id;
+       }
+}
diff --git a/src/Object/Api/Mastodon/Instance.php b/src/Object/Api/Mastodon/Instance.php
new file mode 100644 (file)
index 0000000..63c8b28
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+
+namespace Friendica\Object\Api\Mastodon;
+
+use Friendica\BaseEntity;
+use Friendica\Database\DBA;
+use Friendica\DI;
+use Friendica\Model\User;
+use Friendica\Module\Register;
+
+/**
+ * Class Instance
+ *
+ * @see https://docs.joinmastodon.org/api/entities/#instance
+ */
+class Instance extends BaseEntity
+{
+       /** @var string (URL) */
+       protected $uri;
+       /** @var string */
+       protected $title;
+       /** @var string */
+       protected $description;
+       /** @var string */
+       protected $email;
+       /** @var string */
+       protected $version;
+       /** @var array */
+       protected $urls;
+       /** @var Stats */
+       protected $stats;
+       /** @var string|null */
+       protected $thumbnail = null;
+       /** @var array */
+       protected $languages;
+       /** @var int */
+       protected $max_toot_chars;
+       /** @var bool */
+       protected $registrations;
+       /** @var bool */
+       protected $approval_required;
+       /** @var Account|null */
+       protected $contact_account = null;
+
+       /**
+        * Creates an instance record
+        *
+        * @return Instance
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function get()
+       {
+               $register_policy = intval(DI::config()->get('config', 'register_policy'));
+
+               $baseUrl = DI::baseUrl();
+
+               $instance = new Instance();
+               $instance->uri = $baseUrl->get();
+               $instance->title = DI::config()->get('config', 'sitename');
+               $instance->description = DI::config()->get('config', 'info');
+               $instance->email = DI::config()->get('config', 'admin_email');
+               $instance->version = FRIENDICA_VERSION;
+               $instance->urls = []; // Not supported
+               $instance->stats = Stats::get();
+               $instance->thumbnail = $baseUrl->get() . (DI::config()->get('system', 'shortcut_icon') ?? 'images/friendica-32.png');
+               $instance->languages = [DI::config()->get('system', 'language')];
+               $instance->max_toot_chars = (int)DI::config()->get('config', 'api_import_size', DI::config()->get('config', 'max_import_size'));
+               $instance->registrations = ($register_policy != Register::CLOSED);
+               $instance->approval_required = ($register_policy == Register::APPROVE);
+               $instance->contact_account = [];
+
+               if (!empty(DI::config()->get('config', 'admin_email'))) {
+                       $adminList = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
+                       $administrator = User::getByEmail($adminList[0], ['nickname']);
+                       if (!empty($administrator)) {
+                               $adminContact = DBA::selectFirst('contact', ['id'], ['nick' => $administrator['nickname'], 'self' => true]);
+                               $instance->contact_account = DI::mstdnAccount()->createFromContactId($adminContact['id']);
+                       }
+               }
+
+               return $instance;
+       }
+}
diff --git a/src/Object/Api/Mastodon/Relationship.php b/src/Object/Api/Mastodon/Relationship.php
new file mode 100644 (file)
index 0000000..146ad21
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+
+namespace Friendica\Object\Api\Mastodon;
+
+use Friendica\BaseEntity;
+use Friendica\Model\Contact;
+use Friendica\Util\Network;
+
+/**
+ * Class Relationship
+ *
+ * @see https://docs.joinmastodon.org/api/entities/#relationship
+ */
+class Relationship extends BaseEntity
+{
+       /** @var int */
+       protected $id;
+       /** @var bool */
+       protected $following = false;
+       /** @var bool */
+       protected $followed_by = false;
+       /** @var bool */
+       protected $blocking = false;
+       /** @var bool */
+       protected $muting = false;
+       /** @var bool */
+       protected $muting_notifications = false;
+       /** @var bool */
+       protected $requested = false;
+       /** @var bool */
+       protected $domain_blocking = false;
+       /**
+        * Unsupported
+        * @var bool
+        */
+       protected $showing_reblogs = true;
+       /**
+        * Unsupported
+        * @var bool
+        */
+       protected $endorsed = false;
+
+       /**
+        * @param int   $userContactId Contact row Id with uid != 0
+        * @param array $userContact   Full Contact table record with uid != 0
+        */
+       public function __construct(int $userContactId, array $userContact = [])
+       {
+               $this->id                   = $userContactId;
+               $this->following            = in_array($userContact['rel'] ?? 0, [Contact::SHARING, Contact::FRIEND]);
+               $this->followed_by          = in_array($userContact['rel'] ?? 0, [Contact::FOLLOWER, Contact::FRIEND]);
+               $this->blocking             = (bool)$userContact['blocked'] ?? false;
+               $this->muting               = (bool)$userContact['readonly'] ?? false;
+               $this->muting_notifications = (bool)$userContact['readonly'] ?? false;
+               $this->requested            = (bool)$userContact['pending'] ?? false;
+               $this->domain_blocking      = Network::isUrlBlocked($userContact['url'] ?? '');
+
+               return $this;
+       }
+}
diff --git a/src/Object/Api/Mastodon/Stats.php b/src/Object/Api/Mastodon/Stats.php
new file mode 100644 (file)
index 0000000..24e2636
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+namespace Friendica\Object\Api\Mastodon;
+
+use Friendica\BaseEntity;
+use Friendica\Core\Protocol;
+use Friendica\Database\DBA;
+use Friendica\DI;
+
+/**
+ * Class Stats
+ *
+ * @see https://docs.joinmastodon.org/api/entities/#stats
+ */
+class Stats extends BaseEntity
+{
+       /** @var int */
+       protected $user_count = 0;
+       /** @var int */
+       protected $status_count = 0;
+       /** @var int */
+       protected $domain_count = 0;
+
+       /**
+        * Creates a stats record
+        *
+        * @return Stats
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       public static function get() {
+               $stats = new Stats();
+               if (!empty(DI::config()->get('system', 'nodeinfo'))) {
+                       $stats->user_count = intval(DI::config()->get('nodeinfo', 'total_users'));
+                       $stats->status_count = DI::config()->get('nodeinfo', 'local_posts') + DI::config()->get('nodeinfo', 'local_comments');
+                       $stats->domain_count = DBA::count('gserver', ["`network` in (?, ?) AND `last_contact` >= `last_failure`", Protocol::DFRN, Protocol::ACTIVITYPUB]);
+               }
+               return $stats;
+       }
+}