]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Statuses/Show.php
Reenable Twitter/Statuses tests
[friendica.git] / src / Module / Api / Twitter / Statuses / Show.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\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                 if (empty($this->parameters['id'])) {
45                         $id = intval($request['id'] ?? 0);
46                 } else {
47                         $id = (int)$this->parameters['id'];
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                 $uri_item = Post::selectFirst(['uri-id'], ['id' => $id]);
56                 if (!DBA::isResult($uri_item)) {
57                         throw new BadRequestException(sprintf("There is no status with the id %d", $id));
58                 }
59
60                 $item = Post::selectFirst(['id'], ['uri-id' => $uri_item['uri-id'], 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
61                 if (!DBA::isResult($item)) {
62                         throw new BadRequestException(sprintf("There is no status with the uri-id %d for the given user.", $uri_item['uri-id']));
63                 }
64
65                 $id = $item['id'];
66
67                 if ($conversation) {
68                         $condition = ['parent' => $id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]];
69                         $params    = ['order' => ['id' => true]];
70                 } else {
71                         $condition = ['id' => $id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]];
72                         $params    = [];
73                 }
74
75                 $statuses = Post::selectForUser($uid, [], $condition, $params);
76
77                 /// @TODO How about copying this to above methods which don't check $r ?
78                 if (!DBA::isResult($statuses)) {
79                         throw new BadRequestException(sprintf("There is no status or conversation with the id %d.", $id));
80                 }
81
82                 $include_entities = strtolower(($request['include_entities'] ?? 'false') == 'true');
83
84                 $ret = [];
85                 while ($status = DBA::fetch($statuses)) {
86                         $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray();
87                 }
88                 DBA::close($statuses);
89
90                 if ($conversation) {
91                         $data = ['status' => $ret];
92                         $this->response->exit('statuses', $data, $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
93                 } else {
94                         $data = ['status' => $ret[0]];
95                         $this->response->exit('status', $data, $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
96                 }
97         }
98 }