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