Add /api/v1/followed_tags api endpoint

This commit is contained in:
Daniel Supernault 2023-05-09 00:21:46 -06:00
parent fc1a385cfd
commit 175a848665
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 81 additions and 1 deletions

View file

@ -19,6 +19,7 @@ use App\{
Follower,
FollowRequest,
Hashtag,
HashtagFollow,
Instance,
Like,
Media,
@ -99,6 +100,7 @@ use App\Jobs\FollowPipeline\FollowRejectPipeline;
use Illuminate\Support\Facades\RateLimiter;
use Purify;
use Carbon\Carbon;
use App\Http\Resources\MastoApi\FollowedTagResource;
class ApiV1Controller extends Controller
{
@ -3802,4 +3804,47 @@ class ApiV1Controller extends Controller
return $this->json([]);
}
/**
* GET /api/v1/followed_tags
*
*
* @return array
*/
public function getFollowedTags(Request $request)
{
abort_if(!$request->user(), 403);
if(config('pixelfed.bouncer.cloud_ips.ban_api')) {
abort_if(BouncerService::checkIp($request->ip()), 404);
}
$account = AccountService::get($request->user()->profile_id);
$this->validate($request, [
'cursor' => 'sometimes',
'limit' => 'sometimes|integer|min:1|max:200'
]);
$limit = $request->input('limit', 100);
$res = HashtagFollow::whereProfileId($account['id'])
->cursorPaginate($limit)->withQueryString();
$pagination = false;
$prevPage = $res->nextPageUrl();
$nextPage = $res->previousPageUrl();
if($nextPage && $prevPage) {
$pagination = '<' . $nextPage . '>; rel="next", <' . $prevPage . '>; rel="prev"';
} else if($nextPage && !$prevPage) {
$pagination = '<' . $nextPage . '>; rel="next"';
} else if(!$nextPage && $prevPage) {
$pagination = '<' . $prevPage . '>; rel="prev"';
}
if($pagination) {
return response()->json(FollowedTagResource::collection($res)->collection)
->header('Link', $pagination);
}
return response()->json(FollowedTagResource::collection($res)->collection);
}
}

View file

@ -0,0 +1,33 @@
<?php
namespace App\Http\Resources\MastoApi;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\JsonResponse;
use Cache;
use App\Services\HashtagService;
class FollowedTagResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
$tag = HashtagService::get($this->hashtag_id);
if(!$tag || !isset($tag['name'])) {
return [];
}
return [
'name' => $tag['name'],
'url' => config('app.url') . '/i/web/hashtag/' . $tag['slug'],
'history' => [],
'following' => true,
];
}
}

View file

@ -89,6 +89,8 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
Route::get('announcements', 'Api\ApiV1Controller@getAnnouncements')->middleware($middleware);
Route::get('markers', 'Api\ApiV1Controller@getMarkers')->middleware($middleware);
Route::post('markers', 'Api\ApiV1Controller@setMarkers')->middleware($middleware);
Route::get('followed_tags', 'Api\ApiV1Controller@getFollowedTags')->middleware($middleware);
});
Route::group(['prefix' => 'v2'], function() use($middleware) {