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