]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Statuses/Show.php
Merge pull request #11282 from annando/api-fix
[friendica.git] / src / Module / Api / Twitter / Statuses / Show.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Core\Logger;
25 use Friendica\Database\DBA;
26 use Friendica\Module\BaseApi;
27 use Friendica\DI;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Post;
30 use Friendica\Network\HTTPException\BadRequestException;
31
32 /**
33  * Returns a single status.
34  *
35  * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/get-statuses-show-id
36  */
37 class Show extends BaseApi
38 {
39         protected function rawContent(array $request = [])
40         {
41                 BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
42                 $uid = BaseApi::getCurrentUserID();
43
44                 $id = $this->getRequestValue($request, 'id', 0);
45                 $id = $this->getRequestValue($this->parameters, 'id', $id);
46                 if (empty($id)) {
47                         throw new BadRequestException('An id is missing.');
48                 }
49
50                 Logger::notice('API: api_statuses_show: ' . $id);
51
52                 $conversation = !empty($request['conversation']);
53
54                 // try to fetch the item for the local user - or the public item, if there is no local one
55                 $item = Post::selectFirst(['id'], ['uri-id' => $id, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
56                 if (!DBA::isResult($item)) {
57                         throw new BadRequestException(sprintf("There is no status with the uri-id %d for the given user.", $id));
58                 }
59
60                 $item_id = $item['id'];
61
62                 if ($conversation) {
63                         $condition = ['parent' => $item_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]];
64                         $params    = ['order' => ['uri-id' => true]];
65                 } else {
66                         $condition = ['id' => $item_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]];
67                         $params    = [];
68                 }
69
70                 $statuses = Post::selectForUser($uid, [], $condition, $params);
71
72                 /// @TODO How about copying this to above methods which don't check $r ?
73                 if (!DBA::isResult($statuses)) {
74                         throw new BadRequestException(sprintf("There is no status or conversation with the id %d.", $id));
75                 }
76
77                 $include_entities = $this->getRequestValue($request, 'include_entities', false);
78
79                 $ret = [];
80                 while ($status = DBA::fetch($statuses)) {
81                         $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray();
82                 }
83                 DBA::close($statuses);
84
85                 if ($conversation) {
86                         $data = ['status' => $ret];
87                         $this->response->exit('statuses', $data, $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
88                 } else {
89                         $data = ['status' => $ret[0]];
90                         $this->response->exit('status', $data, $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
91                 }
92         }
93 }