3 * @copyright Copyright (C) 2010-2021, 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\Timelines;
24 use Friendica\Core\Protocol;
25 use Friendica\Core\System;
26 use Friendica\Database\DBA;
28 use Friendica\Model\Post;
29 use Friendica\Module\BaseApi;
30 use Friendica\Network\HTTPException;
33 * @see https://docs.joinmastodon.org/methods/timelines/
35 class Tag extends BaseApi
38 * @throws HTTPException\InternalServerErrorException
40 protected function rawContent(array $request = [])
42 self::checkAllowedScope(self::SCOPE_READ);
43 $uid = self::getCurrentUserID();
45 if (empty($this->parameters['hashtag'])) {
46 DI::mstdnError()->UnprocessableEntity();
50 * @todo Respect missing parameters
51 * @see https://github.com/tootsuite/mastodon/blob/main/app/controllers/api/v1/timelines/tag_controller.rb
53 * There seem to be the parameters "any", "all", and "none".
56 $request = self::getRequest([
57 'local' => false, // If true, return only local statuses. Defaults to false.
58 'remote' => false, // Show only remote statuses? Defaults to false.
59 'only_media' => false, // If true, return only statuses with media attachments. Defaults to false.
60 'max_id' => 0, // Return results older than this ID.
61 'since_id' => 0, // Return results newer than this ID.
62 'min_id' => 0, // Return results immediately newer than this ID.
63 'limit' => 20, // Maximum number of results to return. Defaults to 20.
64 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users.
65 'exclude_replies' => false, // Don't show comments
68 $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];
70 $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
71 AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
72 $this->parameters['hashtag'], 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
74 if ($request['local']) {
75 $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]);
78 if ($request['remote']) {
79 $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]);
82 if ($request['only_media']) {
83 $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))",
84 Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]);
87 if ($request['exclude_replies']) {
88 $condition = DBA::mergeConditions($condition, ['gravity' => GRAVITY_PARENT]);
91 if (!empty($request['max_id'])) {
92 $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
95 if (!empty($request['since_id'])) {
96 $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
99 if (!empty($request['min_id'])) {
100 $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
102 $params['order'] = ['uri-id'];
105 $items = DBA::select('tag-search-view', ['uri-id'], $condition, $params);
108 while ($item = Post::fetch($items)) {
109 self::setBoundaries($item['uri-id']);
110 $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
114 if (!empty($request['min_id'])) {
115 array_reverse($statuses);
118 self::setLinkHeader();
119 System::jsonExit($statuses);