]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Twitter/Status.php
Merge branch 'develop' into rewrite/gravity-constants
[friendica.git] / src / Factory / Api / Twitter / Status.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Factory\Api\Twitter;
23
24 use Friendica\BaseFactory;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Content\Text\HTML;
27 use Friendica\Database\Database;
28 use Friendica\Factory\Api\Friendica\Activities;
29 use Friendica\Factory\Api\Twitter\User as TwitterUser;
30 use Friendica\Model\Item;
31 use Friendica\Model\Post;
32 use Friendica\Model\Verb;
33 use Friendica\Network\HTTPException;
34 use Friendica\Protocol\Activity;
35 use ImagickException;
36 use Psr\Log\LoggerInterface;
37
38 class Status extends BaseFactory
39 {
40         /** @var Database */
41         private $dba;
42         /** @var twitterUser entity */
43         private $twitterUser;
44         /** @var Hashtag entity */
45         private $hashtag;
46         /** @var Media entity */
47         private $media;
48         /** @var Url entity */
49         private $url;
50         /** @var Mention entity */
51         private $mention;
52         /** @var Activities entity */
53         private $activities;
54         /** @var Activities entity */
55         private $attachment;
56
57         public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser, Hashtag $hashtag, Media $media, Url $url, Mention $mention, Activities $activities, Attachment $attachment)
58         {
59                 parent::__construct($logger);
60                 $this->dba         = $dba;
61                 $this->twitterUser = $twitteruser;
62                 $this->hashtag     = $hashtag;
63                 $this->media       = $media;
64                 $this->url         = $url;
65                 $this->mention     = $mention;
66                 $this->activities  = $activities;
67                 $this->attachment  = $attachment;
68         }
69
70         /**
71          * @param int $uriId Uri-ID of the item
72          * @param int $uid   Item user
73          * @param bool $include_entities Whether to include entities
74          *
75          * @return \Friendica\Object\Api\Twitter\Status
76          * @throws HTTPException\InternalServerErrorException
77          * @throws ImagickException|HTTPException\NotFoundException
78          */
79         public function createFromItemId(int $id, int $uid, bool $include_entities = false): \Friendica\Object\Api\Twitter\Status
80         {
81                 $fields = ['parent-uri-id', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'causer-id',
82                         'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network','post-reason', 'language', 'gravity',
83                         'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'uri', 'plink', 'private', 'vid', 'coord'];
84                 $item = Post::selectFirst($fields, ['id' => $id], ['order' => ['uid' => true]]);
85                 if (!$item) {
86                         throw new HTTPException\NotFoundException('Item with ID ' . $id . ' not found.');
87                 }
88                 return $this->createFromArray($item, $uid, $include_entities);
89         }
90
91         /**
92          * @param int $uriId Uri-ID of the item
93          * @param int $uid   Item user
94          * @param bool $include_entities Whether to include entities
95          *
96          * @return \Friendica\Object\Api\Twitter\Status
97          * @throws HTTPException\InternalServerErrorException
98          * @throws ImagickException|HTTPException\NotFoundException
99          */
100         public function createFromUriId(int $uriId, int $uid = 0, bool $include_entities = false): \Friendica\Object\Api\Twitter\Status
101         {
102                 $fields = ['parent-uri-id', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'causer-id',
103                         'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network','post-reason', 'language', 'gravity',
104                         'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'uri', 'plink', 'private', 'vid', 'coord'];
105                 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
106                 if (!$item) {
107                         throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
108                 }
109                 return $this->createFromArray($item, $uid, $include_entities);
110         }
111
112         /**
113          * @param array $item item array
114          * @param int   $uid  Item user
115          * @param bool $include_entities Whether to include entities
116          *
117          * @return \Friendica\Object\Api\Twitter\Status
118          * @throws HTTPException\InternalServerErrorException
119          * @throws ImagickException|HTTPException\NotFoundException
120          */
121         private function createFromArray(array $item, int $uid, bool $include_entities): \Friendica\Object\Api\Twitter\Status
122         {
123                 $author = $this->twitterUser->createFromContactId($item['author-id'], $uid, true);
124
125                 if (!empty($item['causer-id']) && ($item['post-reason'] == Item::PR_ANNOUNCEMENT)) {
126                         $owner = $this->twitterUser->createFromContactId($item['causer-id'], $uid, true);
127                 } else {
128                         $owner = $this->twitterUser->createFromContactId($item['owner-id'], $uid, true);
129                 }
130
131                 $friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => Item::GRAVITY_COMMENT]);
132
133                 $text  = '';
134                 $title = '';
135
136                 // Add the title to text / html if set
137                 if (!empty($item['title'])) {
138                         $text .= $item['title'] . ' ';
139                         $title = sprintf("[h4]%s[/h4]\n", $item['title']);
140                 }
141
142                 $statusnetHtml = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($title . ($item['raw-body'] ?? $item['body'])), BBCode::API);
143                 $friendicaHtml = BBCode::convertForUriId($item['uri-id'], $title . $item['body'], BBCode::EXTERNAL);
144
145                 $text .= Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
146
147                 $text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $text, BBCode::API), 0));
148
149                 $geo = [];
150
151                 if ($item['coord'] != '') {
152                         $coords = explode(' ', $item["coord"]);
153                         if (count($coords) == 2) {
154                                 $geo = [
155                                         'type'        => 'Point',
156                                         'coordinates' => [(float) $coords[0], (float) $coords[1]]
157                                 ];
158                         }
159                 }
160
161                 $liked = Post::exists([
162                         'thr-parent-id' => $item['uri-id'],
163                         'uid'           => $uid,
164                         'origin'        => true,
165                         'gravity'       => Item::GRAVITY_ACTIVITY,
166                         'vid'           => Verb::getID(Activity::LIKE),
167                         'deleted'       => false
168                 ]);
169
170                 if ($include_entities) {
171                         $hashtags = $this->hashtag->createFromUriId($item['uri-id'], $text);
172                         $medias   = $this->media->createFromUriId($item['uri-id'], $text);
173                         $urls     = $this->url->createFromUriId($item['uri-id'], $text);
174                         $mentions = $this->mention->createFromUriId($item['uri-id'], $text);
175                 } else {
176                         $attachments = $this->attachment->createFromUriId($item['uri-id'], $text);
177                 }
178
179                 $friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid);
180
181                 $shared = BBCode::fetchShareAttributes($item['body']);
182                 if (!empty($shared['guid'])) {
183                         $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
184
185                         $shared_uri_id = $shared_item['uri-id'] ?? 0;
186
187                         if ($include_entities) {
188                                 $hashtags = array_merge($hashtags, $this->hashtag->createFromUriId($shared_uri_id, $text));
189                                 $medias   = array_merge($medias, $this->media->createFromUriId($shared_uri_id, $text));
190                                 $urls     = array_merge($urls, $this->url->createFromUriId($shared_uri_id, $text));
191                                 $mentions = array_merge($mentions, $this->mention->createFromUriId($shared_uri_id, $text));
192                         } else {
193                                 $attachments = array_merge($attachments, $this->attachment->createFromUriId($shared_uri_id, $text));
194                         }
195                 }
196
197                 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
198                         $retweeted      = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
199                         $retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $uid]]);
200                         $item['title']  = $retweeted_item['title'] ?? $item['title'];
201                         $item['body']   = $retweeted_item['body']  ?? $item['body'];
202                         $author         = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $uid, true);
203                 } else {
204                         $retweeted = [];
205                 }
206
207                 $quoted = []; // @todo
208
209                 if ($include_entities) {
210                         $entities    = ['hashtags' => $hashtags, 'media' => $medias, 'urls' => $urls, 'user_mentions' => $mentions];
211                         $attachments = [];
212                 } else {
213                         $entities = [];
214                 }
215
216                 return new \Friendica\Object\Api\Twitter\Status($text, $statusnetHtml, $friendicaHtml, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $attachments,  $friendica_comments, $liked);
217         }
218 }