]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Twitter/Status.php
More objects added
[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\Factory\Api\Twitter\Hashtag;
31 use Friendica\Factory\Api\Twitter\Mention;
32 use Friendica\Factory\Api\Twitter\Url;
33 use Friendica\Model\Post;
34 use Friendica\Model\Verb;
35 use Friendica\Network\HTTPException;
36 use Friendica\Protocol\Activity;
37 use ImagickException;
38 use Psr\Log\LoggerInterface;
39
40 class Status extends BaseFactory
41 {
42         /** @var Database */
43         private $dba;
44         /** @var twitterUser entity */
45         private $twitterUser;
46         /** @var Hashtag entity */
47         private $hashtag;
48         /** @var Media entity */
49         private $media;
50         /** @var Url entity */
51         private $url;
52         /** @var Mention entity */
53         private $mention;
54         /** @var Activities entity */
55         private $activities;
56
57         public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser, Hashtag $hashtag, Media $media, Url $url, Mention $mention, Activities $activities)
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         }
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 createFromUriId(int $uriId, $uid = 0): \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, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
82                 if (!$item) {
83                         throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
84                 }
85
86                 $author = $this->twitterUser->createFromContactId($item['author-id'], $item['uid']);
87                 $owner  = $this->twitterUser->createFromContactId($item['owner-id'], $item['uid']);
88
89                 $friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => GRAVITY_COMMENT]);
90
91                 $text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::API), 0));
92
93                 $geo = [];
94
95                 if ($item['coord'] != '') {
96                         $coords = explode(' ', $item["coord"]);
97                         if (count($coords) == 2) {
98                                 $geo = [
99                                         'type' => 'Point',
100                                         'coordinates' => [(float) $coords[0], (float) $coords[1]]
101                                 ];
102                         }
103                 }
104
105                 $hashtags = $this->hashtag->createFromUriId($uriId, $text);
106                 $medias   = $this->media->createFromUriId($uriId, $text);
107                 $urls     = $this->url->createFromUriId($uriId, $text);
108                 $mentions = $this->mention->createFromUriId($uriId, $text);
109
110                 $friendica_activities = $this->activities->createFromUriId($uriId, $uid);
111
112                 $shared = BBCode::fetchShareAttributes($item['body']);
113                 if (!empty($shared['guid'])) {
114                         $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
115
116                         $shared_uri_id = $shared_item['uri-id'] ?? 0;
117
118                         $hashtags = array_merge($hashtags, $this->hashtag->createFromUriId($shared_uri_id, $text));
119                         $medias   = array_merge($medias, $this->media->createFromUriId($shared_uri_id, $text));
120                         $urls     = array_merge($urls, $this->url->createFromUriId($shared_uri_id, $text));
121                         $mentions = array_merge($mentions, $this->mention->createFromUriId($shared_uri_id, $text));
122
123                         $friendica_activities = [];
124                 }
125
126                 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
127                         $retweeted      = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
128                         $retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]);
129                         $item['title']  = $retweeted_item['title'] ?? $item['title'];
130                         $item['body']   = $retweeted_item['body']  ?? $item['body'];
131                         $author         = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $item['uid']);
132                 } else {
133                         $retweeted = [];
134                 }
135
136                 $quoted = []; // @todo
137
138                 $entities = ['hashtags' => $hashtags, 'media' => $medias, 'urls' => $urls, 'user_mentions' => $mentions];
139
140                 return new \Friendica\Object\Api\Twitter\Status($text, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $friendica_comments);
141         }
142 }