]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Mastodon/Account.php
we now have got an uri-id field for the contact table
[friendica.git] / src / Object / Api / Mastodon / Account.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Object\Api\Mastodon;
23
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;
32
33 /**
34  * Class Account
35  *
36  * @see https://docs.joinmastodon.org/entities/account
37  */
38 class Account extends BaseDataTransferObject
39 {
40         /** @var string */
41         protected $id;
42         /** @var string */
43         protected $username;
44         /** @var string */
45         protected $acct;
46         /** @var string */
47         protected $display_name;
48         /** @var bool */
49         protected $locked;
50         /** @var bool|null */
51         protected $bot = null;
52         /** @var bool */
53         protected $discoverable;
54         /** @var bool */
55         protected $group;
56         /** @var string|null (Datetime) */
57         protected $created_at;
58         /** @var string */
59         protected $note;
60         /** @var string (URL)*/
61         protected $url;
62         /** @var string (URL) */
63         protected $avatar;
64         /** @var string (URL) */
65         protected $avatar_static;
66         /** @var string (URL) */
67         protected $header;
68         /** @var string (URL) */
69         protected $header_static;
70         /** @var int */
71         protected $followers_count;
72         /** @var int */
73         protected $following_count;
74         /** @var int */
75         protected $statuses_count;
76         /** @var string|null (Datetime) */
77         protected $last_status_at = null;
78         /** @var Emoji[] */
79         protected $emojis;
80         /** @var Account|null */
81         protected $moved = null;
82         /** @var Field[]|null */
83         protected $fields = null;
84
85         /**
86          * Creates an account record from a public contact record. Expects all contact table fields to be set.
87          *
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
93          */
94         public function __construct(BaseURL $baseUrl, array $publicContact, Fields $fields, array $apcontact = [], array $userContact = [])
95         {
96                 $this->id              = (string)$publicContact['id'];
97                 $this->username        = $publicContact['nick'];
98                 $this->acct            =
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);
107
108                 $publicContactCreated = $publicContact['created'] ?: DBA::NULL_DATETIME;
109                 $userContactCreated = $userContact['created'] ?? DBA::NULL_DATETIME;
110
111                 $created = $userContactCreated < $publicContactCreated && ($userContactCreated != DBA::NULL_DATETIME) ? $userContactCreated : $publicContactCreated;
112                 $this->created_at      = DateTimeFormat::utc($created, DateTimeFormat::JSON);
113
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;
123
124                 $publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME;
125                 $userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME;
126
127                 $lastItem = $userContactLastItem > $publicContactLastItem ? $userContactLastItem : $publicContactLastItem;
128                 $this->last_status_at  = $lastItem != DBA::NULL_DATETIME ? DateTimeFormat::utc($lastItem, 'Y-m-d') : null;
129
130                 // No custom emojis per account in Friendica
131                 $this->emojis          = [];
132                 $this->fields          = $fields->getArrayCopy();
133
134         }
135
136         /**
137          * Returns the current entity as an array
138          *
139          * @return array
140          */
141         public function toArray(): array
142         {
143                 $account = parent::toArray();
144
145                 if (empty($account['moved'])) {
146                         unset($account['moved']);
147                 }
148
149                 return $account;
150         }
151 }