]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Twitter/Status.php
Use the owner, not the author
[friendica.git] / src / Object / Api / Twitter / Status.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\Model\Item;
27 use Friendica\Util\DateTimeFormat;
28
29 /**
30  * Class Status
31  *
32  * @see https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/tweet
33  */
34 class Status extends BaseDataTransferObject
35 {
36         /** @var string */
37         protected $text;
38         /** @var bool */
39         protected $truncated;
40         /** @var string (Datetime) */
41         protected $created_at;
42         /** @var int|null */
43         protected $in_reply_to_status_id = null;
44         /** @var string|null */
45         protected $in_reply_to_status_id_str = null;
46         /** @var string */
47         protected $source;
48         /** @var int */
49         protected $id;
50         /** @var string */
51         protected $id_str;
52         /** @var int|null */
53         protected $in_reply_to_user_id = null;
54         /** @var string|null */
55         protected $in_reply_to_user_id_str = null;
56         /** @var string|null */
57         protected $in_reply_to_screen_name = null;
58         /** @var array|null */
59         protected $geo;
60         /** @var bool */
61         protected $favorited = false;
62         /** @var User */
63         protected $user;
64         /** @var User */
65         protected $friendica_author;
66         /** @var User */
67         protected $friendica_owner;
68         /** @var bool */
69         protected $friendica_private;
70         /** @var string */
71         protected $statusnet_html;
72         /** @var int */
73         protected $statusnet_conversation_id;
74         /** @var string */
75         protected $external_url;
76         /** @var array */
77         protected $friendica_activities;
78         /** @var string */
79         protected $friendica_title;
80         /** @var string */
81         protected $friendica_html;
82         /** @var int */
83         protected $friendica_comments;
84         /** @var Status|null */
85         protected $retweeted_status = null;
86         /** @var Status|null */
87         protected $quoted_status = null;
88         /** @var array */
89         protected $attachments;
90         /** @var array */
91         protected $entities;
92         /** @var array */
93         protected $extended_entities;
94
95         /**
96          * Creates a status record from an item record.
97          *
98          * @param array   $item
99          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
100          */
101         public function __construct(string $text, string $statusnetHtml, string $friendicaHtml, array $item, User $author, User $owner, array $retweeted, array $quoted, array $geo, array $friendica_activities, array $entities, array $attachments, int $friendica_comments, bool $liked)
102         {
103                 $this->id                        = (int)$item['uri-id'];
104                 $this->id_str                    = (string)$item['uri-id'];
105                 $this->statusnet_conversation_id = (int)$item['parent-uri-id'];
106
107                 $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::API);
108
109                 if ($item['gravity'] == Item::GRAVITY_COMMENT) {
110                         $this->in_reply_to_status_id     = (int)$item['thr-parent-id'];
111                         $this->in_reply_to_status_id_str = (string)$item['thr-parent-id'];
112                         $this->in_reply_to_user_id       = (int)$item['parent-author-id'];
113                         $this->in_reply_to_user_id_str   = (string)$item['parent-author-id'];
114                         $this->in_reply_to_screen_name   = $item['parent-author-nick'];
115                 }
116
117                 $this->text                 = $text;
118                 $this->friendica_title      = $item['title'];
119                 $this->statusnet_html       = $statusnetHtml;
120                 $this->friendica_html       = $friendicaHtml;
121                 $this->user                 = $owner->toArray();
122                 $this->friendica_author     = $author->toArray();
123                 $this->friendica_owner      = $owner->toArray();
124                 $this->truncated            = false;
125                 $this->friendica_private    = $item['private'] == Item::PRIVATE;
126                 $this->retweeted_status     = $retweeted;
127                 $this->quoted_status        = $quoted;
128                 $this->external_url         = $item['plink'];
129                 $this->favorited            = $liked;
130                 $this->friendica_comments   = $friendica_comments;
131                 $this->source               = $item['app'];
132                 $this->geo                  = $geo;
133                 $this->friendica_activities = $friendica_activities;
134                 $this->attachments          = $attachments;
135                 $this->entities             = $entities;
136                 $this->extended_entities    = $entities;
137
138                 $origin = ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']);
139
140                 if (empty($this->source)) {
141                         $this->source = $origin;
142                 } elseif ($origin != $this->source) {
143                         $this->source = trim($this->source. ' (' . $origin . ')');
144                 }
145         }
146
147         /**
148          * Returns the current entity as an array
149          *
150          * @return array
151          */
152         public function toArray(): array
153         {
154                 $status = parent::toArray();
155
156                 if (empty($status['retweeted_status'])) {
157                         unset($status['retweeted_status']);
158                 }
159
160                 if (empty($status['quoted_status'])) {
161                         unset($status['quoted_status']);
162                 }
163
164                 if (empty($status['geo'])) {
165                         $status['geo'] = null;
166                 }
167
168                 if (empty($status['entities'])) {
169                         $status['entities'] = null;
170                 }
171
172                 if (empty($status['extended_entities'])) {
173                         $status['extended_entities'] = null;
174                 }
175
176                 if (empty($status['attachments'])) {
177                         $status['attachments'] = null;
178                 }
179
180
181                 return $status;
182         }
183 }