]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Statuses/Retweet.php
API: Added more functions, fixed function names
[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\Module\BaseApi;
31 use Friendica\Network\HTTPException\BadRequestException;
32 use Friendica\Network\HTTPException\ForbiddenException;
33 use Friendica\Network\HTTPException\InternalServerErrorException;
34
35 /**
36  * Repeats a status.
37  *
38  * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-retweet-id
39  */
40 class Retweet extends BaseApi
41 {
42         protected function post(array $request = [], array $post = [])
43         {
44                 self::checkAllowedScope(self::SCOPE_WRITE);
45                 $uid = self::getCurrentUserID();
46
47                 $id = $request['id'] ?? 0;
48
49                 if (empty($id)) {
50                         throw new BadRequestException('Item id not specified');
51                 }
52
53                 $fields = ['uri-id', 'network', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink'];
54                 $item = Post::selectFirst($fields, ['id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
55         
56                 if (DBA::isResult($item) && !empty($item['body'])) {
57                         if (in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::TWITTER])) {
58                                 if (!Item::performActivity($id, 'announce', $uid)) {
59                                         throw new InternalServerErrorException();
60                                 }
61         
62                                 $item_id = $id;
63                         } else {
64                                 if (strpos($item['body'], "[/share]") !== false) {
65                                         $pos = strpos($item['body'], "[share");
66                                         $post = substr($item['body'], $pos);
67                                 } else {
68                                         $post = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'], $item['plink'], $item['created'], $item['guid']);
69         
70                                         if (!empty($item['title'])) {
71                                                 $post .= '[h3]' . $item['title'] . "[/h3]\n";
72                                         }
73         
74                                         $post .= $item['body'];
75                                         $post .= "[/share]";
76                                 }
77                                 $item = [
78                                         'uid'  => $uid,
79                                         'body' => $post,
80                                         'app'  => $request['source'] ?? '',
81                                 ];
82
83                                 if (empty($item['app']) && !empty(self::getCurrentApplication()['name'])) {
84                                         $item['app'] = self::getCurrentApplication()['name'];
85                                 }
86         
87                                 $item_id = Item::insert($item, true);
88                         }
89                 } else {
90                         throw new ForbiddenException();
91                 }
92         
93                 $status_info = DI::twitterStatus()->createFromItemId($item_id, $uid)->toArray();
94
95                 DI::apiResponse()->exit('status', ['status' => $status_info], $this->parameters['extension'] ?? null);
96         }
97 }