3 * @copyright Copyright (C) 2020, Friendica
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\BaseEntity;
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;
35 * @see https://docs.joinmastodon.org/entities/account
37 class Account extends BaseEntity
46 protected $display_name;
50 protected $bot = null;
52 protected $discoverable;
55 /** @var string|null (Datetime) */
56 protected $created_at;
59 /** @var string (URL)*/
61 /** @var string (URL) */
63 /** @var string (URL) */
64 protected $avatar_static;
65 /** @var string (URL) */
67 /** @var string (URL) */
68 protected $header_static;
70 protected $followers_count;
72 protected $following_count;
74 protected $statuses_count;
75 /** @var string|null (Datetime) */
76 protected $last_status_at = null;
79 /** @var Account|null */
80 protected $moved = null;
81 /** @var Field[]|null */
82 protected $fields = null;
85 * Creates an account record from a public contact record. Expects all contact table fields to be set.
87 * @param BaseURL $baseUrl
88 * @param array $publicContact Full contact table record with uid = 0
89 * @param array $apcontact Optional full apcontact table record
90 * @param array $userContact Optional full contact table record with uid != 0
91 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
93 public function __construct(BaseURL $baseUrl, array $publicContact, Fields $fields, array $apcontact = [], array $userContact = [])
95 $this->id = (string)$publicContact['id'];
96 $this->username = $publicContact['nick'];
98 strpos($publicContact['url'], $baseUrl->get() . '/') === 0 ?
99 $publicContact['nick'] :
100 $publicContact['addr'];
101 $this->display_name = $publicContact['name'];
102 $this->locked = !empty($apcontact['manually-approve']);
103 $this->bot = ($publicContact['contact-type'] == Contact::TYPE_NEWS);
104 $this->discoverable = !$publicContact['unsearchable'];
105 $this->group = ($publicContact['contact-type'] == Contact::TYPE_COMMUNITY);
106 $this->created_at = DateTimeFormat::utc($publicContact['created'], DateTimeFormat::ATOM);
107 $this->note = BBCode::convert($publicContact['about'], false);
108 $this->url = $publicContact['url'];
109 $this->avatar = $userContact['avatar'] ?? $publicContact['avatar'];
110 $this->avatar_static = $userContact['avatar'] ?? $publicContact['avatar'];
111 // No header picture in Friendica
113 $this->header_static = '';
114 $this->followers_count = $apcontact['followers_count'] ?? 0;
115 $this->following_count = $apcontact['following_count'] ?? 0;
116 $this->statuses_count = $apcontact['statuses_count'] ?? 0;
118 $publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME;
119 $userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME;
121 $lastItem = $userContactLastItem > $publicContactLastItem ? $userContactLastItem : $publicContactLastItem;
122 $this->last_status_at = $lastItem != DBA::NULL_DATETIME ? DateTimeFormat::utc($lastItem, 'Y-m-d') : null;
124 // No custom emojis per account in Friendica
126 // No metadata fields in Friendica
127 $this->fields = $fields->getArrayCopy();