]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Twitter/User.php
Use the owner, not the author
[friendica.git] / src / Object / Api / Twitter / User.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Twitter;
23
24 use Friendica\BaseDataTransferObject;
25 use Friendica\Content\ContactSelector;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Model\Contact;
28 use Friendica\Network\HTTPException\InternalServerErrorException;
29 use Friendica\Util\DateTimeFormat;
30 use Friendica\Util\Proxy;
31
32 /**
33  * @see https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object
34  */
35 class User extends BaseDataTransferObject
36 {
37         /** @var int */
38         protected $id;
39         /** @var string */
40         protected $id_str;
41         /** @var string */
42         protected $name;
43         /** @var string */
44         protected $screen_name;
45         /** @var string|null */
46         protected $location;
47         /** @var array */
48         protected $derived;
49         /** @var string|null */
50         protected $url;
51         /** @var array */
52         protected $entities;
53         /** @var string|null */
54         protected $description;
55         /** @var bool */
56         protected $protected;
57         /** @var bool */
58         protected $verified;
59         /** @var int */
60         protected $followers_count;
61         /** @var int */
62         protected $friends_count;
63         /** @var int */
64         protected $listed_count;
65         /** @var int */
66         protected $favourites_count;
67         /** @var int */
68         protected $statuses_count;
69         /** @var string */
70         protected $created_at;
71         /** @var string */
72         protected $profile_banner_url;
73         /** @var string */
74         protected $profile_image_url_https;
75         /** @var bool */
76         protected $default_profile;
77         /** @var bool */
78         protected $default_profile_image;
79         /** @var Status */
80         protected $status;
81         /** @var array */
82         protected $withheld_in_countries;
83         /** @var string */
84         protected $withheld_scope;
85         /** @var string */
86         protected $profile_image_url;
87         /** @var bool */
88         protected $follow_request_sent;
89         /** @var string */
90         protected $profile_image_url_large;
91         /** @var string */
92         protected $profile_image_url_profile_size;
93         /** @var int */
94         protected $utc_offset;
95         /** @var string */
96         protected $time_zone;
97         /** @var bool */
98         protected $geo_enabled;
99         /** @var null */
100         protected $lang;
101         /** @var bool */
102         protected $contributors_enabled;
103         /** @var bool */
104         protected $is_translator;
105         /** @var bool */
106         protected $is_translation_enabled;
107         /** @var bool */
108         protected $following;
109         /** @var bool */
110         protected $statusnet_blocking;
111         /** @var bool */
112         protected $notifications;
113         /** @var int */
114         protected $uid;
115         /** @var int */
116         protected $pid;
117         /** @var int */
118         protected $cid;
119         /** @var mixed */
120         protected $statusnet_profile_url;
121
122         /**
123          * Missing fields:
124          *
125          * - profile_sidebar_fill_color
126          * - profile_link_color
127          * - profile_background_color
128          */
129
130         /**
131          * @param array $publicContact         Full contact table record with uid = 0
132          * @param array $apcontact             Optional full apcontact table record
133          * @param array $userContact           Optional full contact table record with uid != 0
134          * @param null  $status
135          * @param bool  $include_user_entities Whether to add the entities property
136          *
137          * @throws InternalServerErrorException
138          */
139         public function __construct(array $publicContact, array $apcontact = [], array $userContact = [], $status = null, bool $include_user_entities = true)
140         {
141                 $uid = $userContact['uid'] ?? 0;
142
143                 $this->id                      = (int)$publicContact['id'];
144                 $this->id_str                  = (string) $publicContact['id'];
145                 $this->name                    = $publicContact['name'] ?: $publicContact['nick'];
146                 $this->screen_name             = $publicContact['nick'] ?: $publicContact['name'];
147                 $this->location                = $publicContact['location'] ?:
148                         ContactSelector::networkToName($publicContact['network'], $publicContact['url'], $publicContact['protocol']);
149                 $this->derived                 = [];
150                 $this->url                     = $publicContact['url'];
151                 // No entities needed since we don't perform any shortening in the URL or description
152                 $this->entities            = [
153                         'url' => ['urls' => []],
154                         'description' => ['urls' => []],
155                 ];
156                 if (!$include_user_entities) {
157                         unset($this->entities);
158                 }
159                 $this->description             = (!empty($publicContact['about']) ? BBCode::toPlaintext($publicContact['about']) : '');
160                 $this->profile_image_url_https = Contact::getAvatarUrlForUrl($publicContact['url'], $uid, Proxy::SIZE_MICRO);
161                 $this->protected               = false;
162                 $this->followers_count         = $apcontact['followers_count'] ?? 0;
163                 $this->friends_count           = $apcontact['following_count'] ?? 0;
164                 $this->listed_count            = 0;
165                 $this->created_at              = DateTimeFormat::utc($publicContact['created'], DateTimeFormat::API);
166                 $this->favourites_count        = 0;
167                 $this->verified                = $uid != 0;
168                 $this->statuses_count          = $apcontact['statuses_count'] ?? 0;
169                 $this->profile_banner_url      = Contact::getHeaderUrlForId($publicContact['id'], '', $publicContact['updated']);
170                 $this->default_profile         = false;
171                 $this->default_profile_image   = false;
172
173                 if (!empty($status)) {
174                         $this->status = $status;
175                 } else {
176                         unset($this->status);
177                 }
178
179                 //  Unused optional fields
180                 unset($this->withheld_in_countries);
181                 unset($this->withheld_scope);
182
183                 // Deprecated
184                 $this->profile_image_url              = Contact::getAvatarUrlForUrl($publicContact['url'], $uid, Proxy::SIZE_MICRO);
185                 $this->profile_image_url_profile_size = Contact::getAvatarUrlForUrl($publicContact['url'], $uid, Proxy::SIZE_THUMB);
186                 $this->profile_image_url_large        = Contact::getAvatarUrlForUrl($publicContact['url'], $uid, Proxy::SIZE_LARGE);
187                 $this->utc_offset                     = 0;
188                 $this->time_zone                      = 'UTC';
189                 $this->geo_enabled                    = false;
190                 $this->lang                           = null;
191                 $this->contributors_enabled           = false;
192                 $this->is_translator                  = false;
193                 $this->is_translation_enabled         = false;
194                 $this->following                      = in_array($userContact['rel'] ?? Contact::NOTHING, [Contact::FOLLOWER, Contact::FRIEND]);
195                 $this->follow_request_sent            = false;
196                 $this->statusnet_blocking             = false;
197                 $this->notifications                  = false;
198
199                 // Friendica-specific
200                 $this->uid                   = (int)$uid;
201                 $this->cid                   = (int)($userContact['id'] ?? 0);
202                 $this->pid                   = (int)$publicContact['id'];
203                 $this->statusnet_profile_url = $publicContact['url'];
204         }
205 }