]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Mastodon/Account.php
Move Mastodon API entities to src/Object
[friendica.git] / src / Object / Api / Mastodon / Account.php
1 <?php
2
3 namespace Friendica\Object\Api\Mastodon;
4
5 use Friendica\App\BaseURL;
6 use Friendica\BaseEntity;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Database\DBA;
9 use Friendica\Model\Contact;
10 use Friendica\Util\DateTimeFormat;
11
12 /**
13  * Class Account
14  *
15  * @see https://docs.joinmastodon.org/entities/account
16  */
17 class Account extends BaseEntity
18 {
19         /** @var string */
20         protected $id;
21         /** @var string */
22         protected $username;
23         /** @var string */
24         protected $acct;
25         /** @var string */
26         protected $display_name;
27         /** @var bool */
28         protected $locked;
29         /** @var string (Datetime) */
30         protected $created_at;
31         /** @var int */
32         protected $followers_count;
33         /** @var int */
34         protected $following_count;
35         /** @var int */
36         protected $statuses_count;
37         /** @var string */
38         protected $note;
39         /** @var string (URL)*/
40         protected $url;
41         /** @var string (URL) */
42         protected $avatar;
43         /** @var string (URL) */
44         protected $avatar_static;
45         /** @var string (URL) */
46         protected $header;
47         /** @var string (URL) */
48         protected $header_static;
49         /** @var Emoji[] */
50         protected $emojis;
51         /** @var Account|null */
52         protected $moved = null;
53         /** @var Field[]|null */
54         protected $fields = null;
55         /** @var bool|null */
56         protected $bot = null;
57         /** @var bool */
58         protected $group;
59         /** @var bool */
60         protected $discoverable;
61         /** @var string|null (Datetime) */
62         protected $last_status_at = null;
63
64         /**
65          * Creates an account record from a public contact record. Expects all contact table fields to be set.
66          *
67          * @param BaseURL $baseUrl
68          * @param array   $publicContact Full contact table record with uid = 0
69          * @param array   $apcontact     Optional full apcontact table record
70          * @param array   $userContact   Optional full contact table record with uid != 0
71          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
72          */
73         public function __construct(BaseURL $baseUrl, array $publicContact, array $apcontact = [], array $userContact = [])
74         {
75                 $this->id              = $publicContact['id'];
76                 $this->username        = $publicContact['nick'];
77                 $this->acct            =
78                         strpos($publicContact['url'], $baseUrl->get() . '/') === 0 ?
79                                 $publicContact['nick'] :
80                                 $publicContact['addr'];
81                 $this->display_name    = $publicContact['name'];
82                 $this->locked          = !empty($apcontact['manually-approve']);
83                 $this->created_at      = DateTimeFormat::utc($publicContact['created'], DateTimeFormat::ATOM);
84                 $this->followers_count = $apcontact['followers_count'] ?? 0;
85                 $this->following_count = $apcontact['following_count'] ?? 0;
86                 $this->statuses_count  = $apcontact['statuses_count'] ?? 0;
87                 $this->note            = BBCode::convert($publicContact['about'], false);
88                 $this->url             = $publicContact['url'];
89                 $this->avatar          = $userContact['avatar'] ?? $publicContact['avatar'];
90                 $this->avatar_static   = $userContact['avatar'] ?? $publicContact['avatar'];
91                 // No header picture in Friendica
92                 $this->header          = '';
93                 $this->header_static   = '';
94                 // No custom emojis per account in Friendica
95                 $this->emojis          = [];
96                 // No metadata fields in Friendica
97                 $this->fields          = [];
98                 $this->bot             = ($publicContact['contact-type'] == Contact::TYPE_NEWS);
99                 $this->group           = ($publicContact['contact-type'] == Contact::TYPE_COMMUNITY);
100                 $this->discoverable    = !$publicContact['unsearchable'];
101
102                 $publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME;
103                 $userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME;
104
105                 $lastItem = $userContactLastItem > $publicContactLastItem ? $userContactLastItem : $publicContactLastItem;
106                 $this->last_status_at  = $lastItem != DBA::NULL_DATETIME ? DateTimeFormat::utc($lastItem, DateTimeFormat::ATOM) : null;
107         }
108 }