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