]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Twitter/Status.php
Coding standards
[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\Url;
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
55         public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser, Hashtag $hashtag, Media $media, Url $url, Mention $mention, Activities $activities)
56         {
57                 parent::__construct($logger);
58                 $this->dba         = $dba;
59                 $this->twitterUser = $twitteruser;
60                 $this->hashtag     = $hashtag;
61                 $this->media       = $media;
62                 $this->url         = $url;
63                 $this->mention     = $mention;
64                 $this->activities  = $activities;
65         }
66
67         /**
68          * @param int $uriId Uri-ID of the item
69          * @param int $uid   Item user
70          *
71          * @return \Friendica\Object\Api\Mastodon\Status
72          * @throws HTTPException\InternalServerErrorException
73          * @throws ImagickException|HTTPException\NotFoundException
74          */
75         public function createFromUriId(int $uriId, $uid = 0): \Friendica\Object\Api\Twitter\Status
76         {
77                 $fields = ['id', 'parent', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network',
78                         'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'coord'];
79                 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
80                 if (!$item) {
81                         throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
82                 }
83
84                 $author = $this->twitterUser->createFromContactId($item['author-id'], $item['uid']);
85                 $owner  = $this->twitterUser->createFromContactId($item['owner-id'], $item['uid']);
86
87                 $friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => GRAVITY_COMMENT]);
88
89                 $text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::API), 0));
90
91                 $geo = [];
92
93                 if ($item['coord'] != '') {
94                         $coords = explode(' ', $item["coord"]);
95                         if (count($coords) == 2) {
96                                 $geo = [
97                                         'type'        => 'Point',
98                                         'coordinates' => [(float) $coords[0], (float) $coords[1]]
99                                 ];
100                         }
101                 }
102
103                 $hashtags = $this->hashtag->createFromUriId($uriId, $text);
104                 $medias   = $this->media->createFromUriId($uriId, $text);
105                 $urls     = $this->url->createFromUriId($uriId, $text);
106                 $mentions = $this->mention->createFromUriId($uriId, $text);
107
108                 $friendica_activities = $this->activities->createFromUriId($uriId, $uid);
109
110                 $shared = BBCode::fetchShareAttributes($item['body']);
111                 if (!empty($shared['guid'])) {
112                         $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
113
114                         $shared_uri_id = $shared_item['uri-id'] ?? 0;
115
116                         $hashtags = array_merge($hashtags, $this->hashtag->createFromUriId($shared_uri_id, $text));
117                         $medias   = array_merge($medias, $this->media->createFromUriId($shared_uri_id, $text));
118                         $urls     = array_merge($urls, $this->url->createFromUriId($shared_uri_id, $text));
119                         $mentions = array_merge($mentions, $this->mention->createFromUriId($shared_uri_id, $text));
120
121                         $friendica_activities = [];
122                 }
123
124                 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
125                         $retweeted      = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
126                         $retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]);
127                         $item['title']  = $retweeted_item['title'] ?? $item['title'];
128                         $item['body']   = $retweeted_item['body']  ?? $item['body'];
129                         $author         = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $item['uid']);
130                 } else {
131                         $retweeted = [];
132                 }
133
134                 $quoted = []; // @todo
135
136                 $entities = ['hashtags' => $hashtags, 'media' => $medias, 'urls' => $urls, 'user_mentions' => $mentions];
137
138                 return new \Friendica\Object\Api\Twitter\Status($text, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $friendica_comments);
139         }
140 }