3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Object\Api\Mastodon;
24 use Friendica\App\BaseURL;
25 use Friendica\BaseDataTransferObject;
26 use Friendica\Collection\Api\Mastodon\Fields;
27 use Friendica\Content\Text\BBCode;
28 use Friendica\Database\DBA;
29 use Friendica\Model\Contact;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Util\Proxy;
36 * @see https://docs.joinmastodon.org/entities/account
38 class Account extends BaseDataTransferObject
47 protected $display_name;
51 protected $bot = null;
53 protected $discoverable;
56 /** @var string|null (Datetime) */
57 protected $created_at;
60 /** @var string (URL)*/
62 /** @var string (URL) */
64 /** @var string (URL) */
65 protected $avatar_static;
66 /** @var string (URL) */
68 /** @var string (URL) */
69 protected $header_static;
71 protected $followers_count;
73 protected $following_count;
75 protected $statuses_count;
76 /** @var string|null (Datetime) */
77 protected $last_status_at = null;
80 /** @var Account|null */
81 protected $moved = null;
82 /** @var Field[]|null */
83 protected $fields = null;
86 * Creates an account record from a public contact record. Expects all contact table fields to be set.
88 * @param BaseURL $baseUrl
89 * @param array $account entry of "account-user-view"
90 * @param Fields $fields Profile fields
91 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
93 public function __construct(BaseURL $baseUrl, array $account, Fields $fields)
95 $this->id = (string)$account['pid'];
96 $this->username = $account['nick'];
98 strpos($account['url'], $baseUrl->get() . '/') === 0 ?
101 $this->display_name = $account['name'];
102 $this->locked = (bool)$account['manually-approve'];
103 $this->bot = ($account['contact-type'] == Contact::TYPE_NEWS);
104 $this->discoverable = !$account['unsearchable'];
105 $this->group = ($account['contact-type'] == Contact::TYPE_COMMUNITY);
107 $this->created_at = DateTimeFormat::utc($account['created'] ?: DBA::NULL_DATETIME, DateTimeFormat::JSON);
109 $this->note = BBCode::convertForUriId($account['uri-id'], $account['about'], BBCode::EXTERNAL);
110 $this->url = $account['url'];
111 $this->avatar = Contact::getAvatarUrlForId($account['id'] ?? 0 ?: $account['pid'], Proxy::SIZE_SMALL, $account['updated'], $account['guid'] ?? '');
112 $this->avatar_static = Contact::getAvatarUrlForId($account['id'] ?? 0 ?: $account['pid'], Proxy::SIZE_SMALL, $account['updated'], $account['guid'] ?? '', true);
113 $this->header = Contact::getHeaderUrlForId($account['id'] ?? 0 ?: $account['pid'], '', $account['updated'], $account['guid'] ?? '');
114 $this->header_static = Contact::getHeaderUrlForId($account['id'] ?? 0 ?: $account['pid'], '', $account['updated'], $account['guid'] ?? '', true);
115 $this->followers_count = $account['ap-followers_count'] ?? $account['diaspora-interacted_count'] ?? 0;
116 $this->following_count = $account['ap-following_count'] ?? $account['diaspora-interacting_count'] ?? 0;
117 $this->statuses_count = $account['ap-statuses_count'] ?? $account['diaspora-post_count'] ?? 0;
119 $lastItem = $account['last-item'] ?: DBA::NULL_DATETIME;
120 $this->last_status_at = $lastItem != DBA::NULL_DATETIME ? DateTimeFormat::utc($lastItem, 'Y-m-d') : null;
122 // No custom emojis per account in Friendica
124 $this->fields = $fields->getArrayCopy();
128 * Returns the current entity as an array
132 public function toArray(): array
134 $account = parent::toArray();
136 if (empty($account['moved'])) {
137 unset($account['moved']);