From 521b3b4c826e95447e7e361481059b47ee1e3df5 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 9 May 2023 01:25:39 -0600 Subject: [PATCH] Add /api/v1/tags/:id api endpoint --- app/Http/Controllers/Api/ApiV1Controller.php | 39 ++++++++++++++++++++ routes/api.php | 1 + 2 files changed, 40 insertions(+) diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index d7139ea21..4e13fb312 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -3829,6 +3829,7 @@ class ApiV1Controller extends Controller $limit = $request->input('limit', 100); $res = HashtagFollow::whereProfileId($account['id']) + ->orderByDesc('id') ->cursorPaginate($limit)->withQueryString(); $pagination = false; @@ -3932,4 +3933,42 @@ class ApiV1Controller extends Controller $res['following'] = false; return response()->json($res); } + + /** + * GET /api/v1/tags/:id + * + * + * @return object + */ + public function getHashtag(Request $request, $id) + { + abort_if(!$request->user(), 403); + + if(config('pixelfed.bouncer.cloud_ips.ban_api')) { + abort_if(BouncerService::checkIp($request->ip()), 404); + } + $pid = $request->user()->profile_id; + $account = AccountService::get($pid); + + $operator = config('database.default') == 'pgsql' ? 'ilike' : 'like'; + $tag = Hashtag::where('name', $operator, $id) + ->orWhere('slug', $operator, $id) + ->first(); + + if(!$tag) { + return [ + 'name' => $id, + 'url' => config('app.url') . '/i/web/hashtag/' . $id, + 'history' => [], + 'following' => false + ]; + } + + return [ + 'name' => $tag->name, + 'url' => config('app.url') . '/i/web/hashtag/' . $tag->slug, + 'history' => [], + 'following' => HashtagService::isFollowing($pid, $tag->id) + ]; + } } diff --git a/routes/api.php b/routes/api.php index 4805e84ed..b3746a85c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -93,6 +93,7 @@ Route::group(['prefix' => 'api'], function() use($middleware) { Route::get('followed_tags', 'Api\ApiV1Controller@getFollowedTags')->middleware($middleware); Route::post('tags/{id}/follow', 'Api\ApiV1Controller@followHashtag')->middleware($middleware); Route::post('tags/{id}/unfollow', 'Api\ApiV1Controller@unfollowHashtag')->middleware($middleware); + Route::get('tags/{id}', 'Api\ApiV1Controller@getHashtag')->middleware($middleware); }); Route::group(['prefix' => 'v2'], function() use($middleware) {