]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Mastodon/Account.php
Merge pull request #1 from friendica/develop
[friendica.git] / src / Object / Api / Mastodon / Account.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\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;
31
32 /**
33  * Class Account
34  *
35  * @see https://docs.joinmastodon.org/entities/account
36  */
37 class Account extends BaseEntity
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 string (Datetime) */
50         protected $created_at;
51         /** @var int */
52         protected $followers_count;
53         /** @var int */
54         protected $following_count;
55         /** @var int */
56         protected $statuses_count;
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 Emoji[] */
70         protected $emojis;
71         /** @var Account|null */
72         protected $moved = null;
73         /** @var Field[]|null */
74         protected $fields = null;
75         /** @var bool|null */
76         protected $bot = null;
77         /** @var bool */
78         protected $group;
79         /** @var bool */
80         protected $discoverable;
81         /** @var string|null (Datetime) */
82         protected $last_status_at = 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              = $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          = !empty($apcontact['manually-approve']);
103                 $this->created_at      = DateTimeFormat::utc($publicContact['created'], DateTimeFormat::ATOM);
104                 $this->followers_count = $apcontact['followers_count'] ?? 0;
105                 $this->following_count = $apcontact['following_count'] ?? 0;
106                 $this->statuses_count  = $apcontact['statuses_count'] ?? 0;
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
112                 $this->header          = '';
113                 $this->header_static   = '';
114                 // No custom emojis per account in Friendica
115                 $this->emojis          = [];
116                 // No metadata fields in Friendica
117                 $this->fields          = $fields->getArrayCopy();
118                 $this->bot             = ($publicContact['contact-type'] == Contact::TYPE_NEWS);
119                 $this->group           = ($publicContact['contact-type'] == Contact::TYPE_COMMUNITY);
120                 $this->discoverable    = !$publicContact['unsearchable'];
121
122                 $publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME;
123                 $userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME;
124
125                 $lastItem = $userContactLastItem > $publicContactLastItem ? $userContactLastItem : $publicContactLastItem;
126                 $this->last_status_at  = $lastItem != DBA::NULL_DATETIME ? DateTimeFormat::utc($lastItem, DateTimeFormat::ATOM) : null;
127         }
128 }