]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Statuses/Retweet.php
Merge remote-tracking branch 'upstream/2021.12-rc' into api-fixes
[friendica.git] / src / Module / Api / Twitter / Statuses / Retweet.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\Module\Api\Twitter\Statuses;
23
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Item;
29 use Friendica\Model\Post;
30 use Friendica\Model\User;
31 use Friendica\Module\BaseApi;
32 use Friendica\Network\HTTPException\BadRequestException;
33 use Friendica\Network\HTTPException\ForbiddenException;
34 use Friendica\Network\HTTPException\InternalServerErrorException;
35
36 /**
37  * Repeats a status.
38  *
39  * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-retweet-id
40  */
41 class Retweet extends BaseApi
42 {
43         protected function post(array $request = [], array $post = [])
44         {
45                 self::checkAllowedScope(self::SCOPE_WRITE);
46                 $uid = self::getCurrentUserID();
47
48                 $id = $request['id'] ?? 0;
49
50                 if (empty($id)) {
51                         throw new BadRequestException('Item id not specified');
52                 }
53
54                 $fields = ['uri-id', 'network', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink'];
55                 $item   = Post::selectFirst($fields, ['id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
56
57                 if (DBA::isResult($item) && !empty($item['body'])) {
58                         if (in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::TWITTER])) {
59                                 if (!Item::performActivity($id, 'announce', $uid)) {
60                                         throw new InternalServerErrorException();
61                                 }
62
63                                 $item_id = $id;
64                         } else {
65                                 if (strpos($item['body'], "[/share]") !== false) {
66                                         $pos  = strpos($item['body'], "[share");
67                                         $post = substr($item['body'], $pos);
68                                 } else {
69                                         $post = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'], $item['plink'], $item['created'], $item['guid']);
70
71                                         if (!empty($item['title'])) {
72                                                 $post .= '[h3]' . $item['title'] . "[/h3]\n";
73                                         }
74
75                                         $post .= $item['body'];
76                                         $post .= "[/share]";
77                                 }
78                                 $item = [
79                                         'uid'  => $uid,
80                                         'body' => $post,
81                                         'app'  => $request['source'] ?? '',
82                                 ];
83
84                                 $owner = User::getOwnerDataById($uid);
85
86                                 $item['allow_cid'] = $owner['allow_cid'];
87                                 $item['allow_gid'] = $owner['allow_gid'];
88                                 $item['deny_cid']  = $owner['deny_cid'];
89                                 $item['deny_gid']  = $owner['deny_gid'];
90
91                                 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
92                                         $item['private'] = Item::PRIVATE;
93                                 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
94                                         $item['private'] = Item::UNLISTED;
95                                 } else {
96                                         $item['private'] = Item::PUBLIC;
97                                 }
98
99                                 if (empty($item['app']) && !empty(self::getCurrentApplication()['name'])) {
100                                         $item['app'] = self::getCurrentApplication()['name'];
101                                 }
102
103                                 $item_id = Item::insert($item, true);
104                         }
105                 } else {
106                         throw new ForbiddenException();
107                 }
108
109                 $status_info = DI::twitterStatus()->createFromItemId($item_id, $uid)->toArray();
110
111                 DI::apiResponse()->exit('status', ['status' => $status_info], $this->parameters['extension'] ?? null);
112         }
113 }