]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Mastodon/Account.php
Merge pull request #10093 from urbalazs/copyright-2021
[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
32 /**
33  * Class Account
34  *
35  * @see https://docs.joinmastodon.org/entities/account
36  */
37 class Account extends BaseDataTransferObject
38 {
39         /** @var string */
40         protected $id;
41         /** @var string */
42         protected $username;
43         /** @var string */
44         protected $acct;
45         /** @var string */
46         protected $display_name;
47         /** @var bool */
48         protected $locked;
49         /** @var bool|null */
50         protected $bot = null;
51         /** @var bool */
52         protected $discoverable;
53         /** @var bool */
54         protected $group;
55         /** @var string|null (Datetime) */
56         protected $created_at;
57         /** @var string */
58         protected $note;
59         /** @var string (URL)*/
60         protected $url;
61         /** @var string (URL) */
62         protected $avatar;
63         /** @var string (URL) */
64         protected $avatar_static;
65         /** @var string (URL) */
66         protected $header;
67         /** @var string (URL) */
68         protected $header_static;
69         /** @var int */
70         protected $followers_count;
71         /** @var int */
72         protected $following_count;
73         /** @var int */
74         protected $statuses_count;
75         /** @var string|null (Datetime) */
76         protected $last_status_at = null;
77         /** @var Emoji[] */
78         protected $emojis;
79         /** @var Account|null */
80         protected $moved = null;
81         /** @var Field[]|null */
82         protected $fields = null;
83
84         /**
85          * Creates an account record from a public contact record. Expects all contact table fields to be set.
86          *
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
92          */
93         public function __construct(BaseURL $baseUrl, array $publicContact, Fields $fields, array $apcontact = [], array $userContact = [])
94         {
95                 $this->id              = (string)$publicContact['id'];
96                 $this->username        = $publicContact['nick'];
97                 $this->acct            =
98                         strpos($publicContact['url'], $baseUrl->get() . '/') === 0 ?
99                                 $publicContact['nick'] :
100                                 $publicContact['addr'];
101                 $this->display_name    = $publicContact['name'];
102                 $this->locked          = (bool)$publicContact['manually-approve'] ?? !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
107                 $publicContactCreated = $publicContact['created'] ?: DBA::NULL_DATETIME;
108                 $userContactCreated = $userContact['created'] ?? DBA::NULL_DATETIME;
109
110                 $created = $userContactCreated < $publicContactCreated && ($userContactCreated != DBA::NULL_DATETIME) ? $userContactCreated : $publicContactCreated;
111                 $this->created_at      = DateTimeFormat::utc($created, DateTimeFormat::ATOM);
112
113                 $this->note            = BBCode::convert($publicContact['about'], false);
114                 $this->url             = $publicContact['url'];
115                 $this->avatar          = $userContact['avatar'] ?? $publicContact['avatar'];
116                 $this->avatar_static   = $userContact['avatar'] ?? $publicContact['avatar'];
117                 // No header picture in Friendica
118                 $this->header          = '';
119                 $this->header_static   = '';
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 }