]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Twitter/Status.php
More objects added
[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 int */
38         protected $id;
39         /** @var string */
40         protected $id_str;
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 int|null */
48         protected $in_reply_to_user_id = null;
49         /** @var string|null */
50         protected $in_reply_to_user_id_str = null;
51         /** @var string|null */
52         protected $in_reply_to_screen_name = null;
53         /** @var User */
54         protected $user;
55         /** @var User */
56         protected $friendica_author;
57         /** @var User */
58         protected $friendica_owner;
59         /** @var bool */
60         protected $favorited = false;
61         /** @var Status|null */
62         protected $retweeted_status = null;
63         /** @var Status|null */
64         protected $quoted_status = null;
65         /** @var string */
66         protected $text;
67         /** @var string */
68         protected $statusnet_html;
69         /** @var string */
70         protected $friendica_html;
71         /** @var string */
72         protected $friendica_title;
73         /** @var bool */
74         protected $truncated;
75         /** @var int */
76         protected $friendica_comments;
77         /** @var string */
78         protected $source;
79         /** @var string */
80         protected $external_url;
81         /** @var int */
82         protected $statusnet_conversation_id;
83         /** @var bool */
84         protected $friendica_private;
85         protected $geo;
86         /** @var array */
87         protected $friendica_activities;
88         /** @var array */
89         protected $entities;
90         /** @var array */
91         protected $extended_entities;
92
93         /**
94          * Creates a status record from an item record.
95          *
96          * @param array   $item
97          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
98          */
99         public function __construct(string $text, array $item, User $author, User $owner, array $retweeted, array $quoted, array $geo, array $friendica_activities, array $entities, int $friendica_comments)
100         {
101                 $this->id                        = (int)$item['id'];
102                 $this->id_str                    = (string)$item['id'];
103                 $this->statusnet_conversation_id = (int)$item['parent'];
104
105                 $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::API);
106
107                 if ($item['gravity'] == GRAVITY_COMMENT) {
108                         $this->in_reply_to_status_id     = (int)$item['thr-parent-id'];
109                         $this->in_reply_to_status_id_str = (string)$item['thr-parent-id'];
110                         $this->in_reply_to_user_id       = (int)$item['parent-author-id'];
111                         $this->in_reply_to_user_id_str   = (string)$item['parent-author-id'];
112                         $this->in_reply_to_screen_name   = $item['parent-author-nick'];
113                 }
114
115                 $this->text                 = $text;
116                 $this->friendica_title      = $item['title'];
117                 $this->statusnet_html       = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($item['raw-body'] ?? $item['body']), BBCode::API);
118                 $this->friendica_html       = BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::EXTERNAL);
119                 $this->user                 = $author->toArray();
120                 $this->friendica_author     = $author->toArray();
121                 $this->friendica_owner      = $owner->toArray();
122                 $this->truncated            = false;
123                 $this->friendica_private    = $item['private'] == Item::PRIVATE;
124                 $this->retweeted_status     = $retweeted;
125                 $this->quoted_status        = $quoted;
126                 $this->external_url         = $item['plink'];
127                 $this->favorited            = (bool)$item['starred'];
128                 $this->friendica_comments   = $friendica_comments;
129                 $this->source               = $item['app'] ?: 'web';
130                 $this->geo                  = $geo;
131                 $this->friendica_activities = $friendica_activities;
132                 $this->entities             = $entities;
133                 $this->extended_entities    = $entities;
134
135                 if ($this->source == 'web') {
136                         $this->source = ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']);
137                 } elseif (ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']) != $this->source) {
138                         $this->source = trim($this->source. ' (' . ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']) . ')');
139                 }
140         }
141
142         /**
143          * Returns the current entity as an array
144          *
145          * @return array
146          */
147         public function toArray(): array
148         {
149                 $status = parent::toArray();
150
151                 if (empty($status['retweeted_status'])) {
152                         unset($status['retweeted_status']);
153                 }
154
155                 if (empty($status['quoted_status'])) {
156                         unset($status['quoted_status']);
157                 }
158
159                 return $status;
160         }
161 }