3 * @copyright Copyright (C) 2010-2021, 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 $publicContact Full contact table record with uid = 0
90 * @param array $apcontact Optional full apcontact table record
91 * @param array $userContact Optional full contact table record with uid != 0
92 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
94 public function __construct(BaseURL $baseUrl, array $publicContact, Fields $fields, array $apcontact = [], array $userContact = [])
96 $this->id = (string)$publicContact['id'];
97 $this->username = $publicContact['nick'];
99 strpos($publicContact['url'], $baseUrl->get() . '/') === 0 ?
100 $publicContact['nick'] :
101 $publicContact['addr'];
102 $this->display_name = $publicContact['name'];
103 $this->locked = (bool)$publicContact['manually-approve'] ?? !empty($apcontact['manually-approve']);
104 $this->bot = ($publicContact['contact-type'] == Contact::TYPE_NEWS);
105 $this->discoverable = !$publicContact['unsearchable'];
106 $this->group = ($publicContact['contact-type'] == Contact::TYPE_COMMUNITY);
108 $publicContactCreated = $publicContact['created'] ?: DBA::NULL_DATETIME;
109 $userContactCreated = $userContact['created'] ?? DBA::NULL_DATETIME;
111 $created = $userContactCreated < $publicContactCreated && ($userContactCreated != DBA::NULL_DATETIME) ? $userContactCreated : $publicContactCreated;
112 $this->created_at = DateTimeFormat::utc($created, DateTimeFormat::JSON);
114 $this->note = BBCode::convertForItem($publicContact['uri-id'] ?? 0, $publicContact['about'], BBCode::API);
115 $this->url = $publicContact['url'];
116 $this->avatar = Contact::getAvatarUrlForId($userContact['id'] ?? 0 ?: $publicContact['id'], Proxy::SIZE_SMALL, $userContact['updated'] ?? '' ?: $publicContact['updated']);
117 $this->avatar_static = $this->avatar;
118 $this->header = Contact::getHeaderUrlForId($userContact['id'] ?? 0 ?: $publicContact['id'], '', $userContact['updated'] ?? '' ?: $publicContact['updated']);
119 $this->header_static = $this->header;
120 $this->followers_count = $apcontact['followers_count'] ?? 0;
121 $this->following_count = $apcontact['following_count'] ?? 0;
122 $this->statuses_count = $apcontact['statuses_count'] ?? 0;
124 $publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME;
125 $userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME;
127 $lastItem = $userContactLastItem > $publicContactLastItem ? $userContactLastItem : $publicContactLastItem;
128 $this->last_status_at = $lastItem != DBA::NULL_DATETIME ? DateTimeFormat::utc($lastItem, 'Y-m-d') : null;
130 // No custom emojis per account in Friendica
132 $this->fields = $fields->getArrayCopy();
137 * Returns the current entity as an array
141 public function toArray(): array
143 $account = parent::toArray();
145 if (empty($account['moved'])) {
146 unset($account['moved']);