3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Mastodon\Accounts;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
29 use Friendica\Model\Conversation;
30 use Friendica\Model\Item;
31 use Friendica\Model\Post;
32 use Friendica\Model\Verb;
33 use Friendica\Module\BaseApi;
34 use Friendica\Object\Api\Mastodon\TimelineOrderByTypes;
35 use Friendica\Protocol\Activity;
38 * @see https://docs.joinmastodon.org/methods/accounts/
40 class Statuses extends BaseApi
43 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
45 protected function rawContent(array $request = [])
47 $uid = self::getCurrentUserID();
49 if (empty($this->parameters['id'])) {
50 DI::mstdnError()->UnprocessableEntity();
53 $id = $this->parameters['id'];
54 if (!DBA::exists('contact', ['id' => $id, 'uid' => 0])) {
55 $this->logErrorAndJsonExit(404, $this->errorFactory->RecordNotFound());
58 $request = $this->getRequest([
59 'only_media' => false, // Show only statuses with media attached? Defaults to false.
60 'max_id' => null, // Return results older than this id
61 'since_id' => null, // Return results newer than this id
62 'min_id' => null, // Return results immediately newer than this id
63 'limit' => 20, // Maximum number of results to return. Defaults to 20.
64 'pinned' => false, // Only pinned posts
65 'exclude_replies' => false, // Don't show comments
66 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users.
67 'exclude_reblogs' => false, // Undocumented parameter
68 'tagged' => false, // Undocumented parameter
69 'friendica_order' => TimelineOrderByTypes::ID, // order options (defaults to ID)
72 if ($request['pinned']) {
73 $condition = ['author-id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED], 'type' => Post\Collection::FEATURED];
74 } elseif ($request['only_media']) {
75 $condition = ['author-id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED], 'type' => [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]];
77 $condition = ['author-id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED],
78 'uid' => 0, 'network' => Protocol::FEDERATED];
80 $condition = ["`author-id` = ? AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))", $id, $uid];
83 $condition = $this->addPagingConditions($request, $condition);
84 $params = $this->buildOrderAndLimitParams($request);
86 if (!$request['pinned'] && !$request['only_media']) {
87 if ($request['exclude_replies']) {
88 $condition = DBA::mergeConditions($condition, ["(`gravity` = ? OR (`gravity` = ? AND `vid` = ? AND `protocol` != ?))",
89 Item::GRAVITY_PARENT, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA]);
91 $condition = DBA::mergeConditions($condition, ["(`gravity` IN (?, ?) OR (`gravity` = ? AND `vid` = ? AND `protocol` != ?))",
92 Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA]);
94 } elseif ($request['exclude_replies']) {
95 $condition = DBA::mergeConditions($condition, ['gravity' => Item::GRAVITY_PARENT]);
98 if ($request['pinned']) {
99 $items = DBA::select('collection-view', ['uri-id'], $condition, $params);
100 } elseif ($request['only_media']) {
101 $items = DBA::select('media-view', ['uri-id'], $condition, $params);
103 $items = Post::selectForUser($uid, ['uri-id'], $condition, $params);
106 $display_quotes = self::appSupportsQuotes();
109 while ($item = Post::fetch($items)) {
111 $status = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
112 $this->updateBoundaries($status, $item, $request['friendica_order']);
113 $statuses[] = $status;
114 } catch (\Throwable $th) {
115 Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
120 if (!empty($request['min_id'])) {
121 $statuses = array_reverse($statuses);
124 self::setLinkHeader($request['friendica_order'] != TimelineOrderByTypes::ID);
125 $this->jsonExit($statuses);