]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Twitter/Status.php
Merge remote-tracking branch 'upstream/develop' into api-status
[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\Database\Database;
27 use Friendica\Factory\Api\Twitter\User as TwitterUser;
28 use Friendica\Model\Post;
29 use Friendica\Model\Verb;
30 use Friendica\Network\HTTPException;
31 use Friendica\Protocol\Activity;
32 use ImagickException;
33 use Psr\Log\LoggerInterface;
34
35 class Status extends BaseFactory
36 {
37         /** @var Database */
38         private $dba;
39         /** @var TwitterUser */
40         private $twitterUser;
41
42         public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser)
43         {
44                 parent::__construct($logger);
45                 $this->dba         = $dba;
46                 $this->twitterUser = $twitteruser;
47         }
48
49         /**
50          * @param int $uriId Uri-ID of the item
51          * @param int $uid   Item user
52          *
53          * @return \Friendica\Object\Api\Mastodon\Status
54          * @throws HTTPException\InternalServerErrorException
55          * @throws ImagickException|HTTPException\NotFoundException
56          */
57         public function createFromUriId(int $uriId, $uid = 0): \Friendica\Object\Api\Twitter\Status
58         {
59                 $fields = ['id', 'parent', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network',
60                         'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity'];
61                 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
62                 if (!$item) {
63                         throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
64                 }
65
66                 $author = $this->twitterUser->createFromContactId($item['author-id'], $item['uid']);
67                 $owner  = $this->twitterUser->createFromContactId($item['owner-id'], $item['uid']);
68
69                 $friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => GRAVITY_COMMENT]);
70
71                 $geo = [];
72
73                 //$mentions    = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
74                 //$tags        = $this->mstdnTagFactory->createFromUriId($uriId);
75                 //$attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
76                 $entities             = [];
77                 $attachments          = [];
78                 $friendica_activities = [];
79
80                 $shared = BBCode::fetchShareAttributes($item['body']);
81                 if (!empty($shared['guid'])) {
82                         //$shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
83
84                         //$shared_uri_id = $shared_item['uri-id'] ?? 0;
85
86                         //$mentions    = array_merge($mentions, $this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy());
87                         //$tags        = array_merge($tags, $this->mstdnTagFactory->createFromUriId($shared_uri_id));
88                         //$attachments = array_merge($attachments, $this->mstdnAttachementFactory->createFromUriId($shared_uri_id));
89                         $entities             = [];
90                         $attachments          = [];
91                         $friendica_activities = [];
92                 }
93
94                 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
95                         $retweeted      = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
96                         $retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]);
97                         $item['title']  = $retweeted_item['title'] ?? $item['title'];
98                         $item['body']   = $retweeted_item['body'] ?? $item['body'];
99                         $author         = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $item['uid']);
100                 } else {
101                         $retweeted = [];
102                 }
103
104                 $quoted = [];
105         
106                 return new \Friendica\Object\Api\Twitter\Status($item, $author, $owner, $retweeted, $quoted, $attachments, $geo, $friendica_activities, $entities, $friendica_comments);
107         }
108 }