]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/GNUSocial/Statusnet/Conversation.php
Merge pull request #11141 from urbalazs/language-names
[friendica.git] / src / Module / Api / GNUSocial / Statusnet / Conversation.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\GNUSocial\Statusnet;
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 conversation
34  */
35 class Conversation extends BaseApi
36 {
37         protected function rawContent(array $request = [])
38         {
39                 BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
40                 $uid = BaseApi::getCurrentUserID();
41
42                 // params
43                 $id               = $this->getRequestValue($this->parameters, 'id', 0);
44                 $since_id         = $this->getRequestValue($request, 'since_id', 0, 0);
45                 $max_id           = $this->getRequestValue($request, 'max_id', 0, 0);
46                 $count            = $this->getRequestValue($request, 'count', 20, 1, 100);
47                 $page             = $this->getRequestValue($request, 'page', 1, 1);
48                 $include_entities = $this->getRequestValue($request, 'include_entities', false);
49
50                 $start = max(0, ($page - 1) * $count);
51
52                 if ($id == 0) {
53                         $id = $request['id'] ?? 0;
54                 }
55
56                 Logger::info(BaseApi::LOG_PREFIX . '{subaction}', ['module' => 'api', 'action' => 'conversation', 'subaction' => 'show', 'id' => $id]);
57
58                 // try to fetch the item for the local user - or the public item, if there is no local one
59                 $item = Post::selectFirst(['parent-uri-id'], ['id' => $id]);
60                 if (!DBA::isResult($item)) {
61                         throw new BadRequestException("There is no status with the id $id.");
62                 }
63
64                 $parent = Post::selectFirst(['id'], ['uri-id' => $item['parent-uri-id'], 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
65                 if (!DBA::isResult($parent)) {
66                         throw new BadRequestException("There is no status with this id.");
67                 }
68
69                 $id = $parent['id'];
70
71                 $condition = ["`parent` = ? AND `uid` IN (0, ?) AND `gravity` IN (?, ?) AND `id` > ?",
72                         $id, $uid, GRAVITY_PARENT, GRAVITY_COMMENT, $since_id];
73
74                 if ($max_id > 0) {
75                         $condition[0] .= " AND `id` <= ?";
76                         $condition[] = $max_id;
77                 }
78
79                 $params   = ['order' => ['id' => true], 'limit' => [$start, $count]];
80                 $statuses = Post::selectForUser($uid, [], $condition, $params);
81
82                 if (!DBA::isResult($statuses)) {
83                         throw new BadRequestException("There is no status with id $id.");
84                 }
85
86                 $ret = [];
87                 while ($status = DBA::fetch($statuses)) {
88                         $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray();
89                 }
90                 DBA::close($statuses);
91
92                 $this->response->exit('statuses', ['status' => $ret], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
93         }
94 }