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