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