mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-25 15:55:22 +00:00
Update ApiV1Controller, move tags endpoints to TagsController
This commit is contained in:
parent
287f903bf3
commit
aa166ab11a
3 changed files with 213 additions and 171 deletions
|
@ -3713,171 +3713,4 @@ class ApiV1Controller extends Controller
|
||||||
|
|
||||||
return $this->json([]);
|
return $this->json([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* GET /api/v1/followed_tags
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getFollowedTags(Request $request)
|
|
||||||
{
|
|
||||||
abort_if(!$request->user(), 403);
|
|
||||||
|
|
||||||
$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'])
|
|
||||||
->orderByDesc('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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* POST /api/v1/tags/:id/follow
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @return object
|
|
||||||
*/
|
|
||||||
public function followHashtag(Request $request, $id)
|
|
||||||
{
|
|
||||||
abort_if(!$request->user(), 403);
|
|
||||||
|
|
||||||
$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();
|
|
||||||
|
|
||||||
abort_if(!$tag, 422, 'Unknown hashtag');
|
|
||||||
|
|
||||||
abort_if(
|
|
||||||
HashtagFollow::whereProfileId($pid)->count() >= HashtagFollow::MAX_LIMIT,
|
|
||||||
422,
|
|
||||||
'You cannot follow more than ' . HashtagFollow::MAX_LIMIT . ' hashtags.'
|
|
||||||
);
|
|
||||||
|
|
||||||
$follows = HashtagFollow::updateOrCreate(
|
|
||||||
[
|
|
||||||
'profile_id' => $account['id'],
|
|
||||||
'hashtag_id' => $tag->id
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'user_id' => $request->user()->id
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
HashtagService::follow($pid, $tag->id);
|
|
||||||
HashtagFollowService::add($tag->id, $pid);
|
|
||||||
|
|
||||||
return response()->json(FollowedTagResource::make($follows)->toArray($request));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* POST /api/v1/tags/:id/unfollow
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @return object
|
|
||||||
*/
|
|
||||||
public function unfollowHashtag(Request $request, $id)
|
|
||||||
{
|
|
||||||
abort_if(!$request->user(), 403);
|
|
||||||
|
|
||||||
$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();
|
|
||||||
|
|
||||||
abort_if(!$tag, 422, 'Unknown hashtag');
|
|
||||||
|
|
||||||
$follows = HashtagFollow::whereProfileId($pid)
|
|
||||||
->whereHashtagId($tag->id)
|
|
||||||
->first();
|
|
||||||
|
|
||||||
if(!$follows) {
|
|
||||||
return [
|
|
||||||
'name' => $tag->name,
|
|
||||||
'url' => config('app.url') . '/i/web/hashtag/' . $tag->slug,
|
|
||||||
'history' => [],
|
|
||||||
'following' => false
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
if($follows) {
|
|
||||||
HashtagService::unfollow($pid, $tag->id);
|
|
||||||
HashtagFollowService::unfollow($tag->id, $pid);
|
|
||||||
HashtagUnfollowPipeline::dispatch($tag->id, $pid, $tag->slug)->onQueue('feed');
|
|
||||||
$follows->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
$res = FollowedTagResource::make($follows)->toArray($request);
|
|
||||||
$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);
|
|
||||||
|
|
||||||
$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
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
$res = [
|
|
||||||
'name' => $tag->name,
|
|
||||||
'url' => config('app.url') . '/i/web/hashtag/' . $tag->slug,
|
|
||||||
'history' => [],
|
|
||||||
'following' => HashtagService::isFollowing($pid, $tag->id)
|
|
||||||
];
|
|
||||||
|
|
||||||
if($request->has(self::PF_API_ENTITY_KEY)) {
|
|
||||||
$res['count'] = HashtagService::count($tag->id);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->json($res);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
207
app/Http/Controllers/Api/V1/TagsController.php
Normal file
207
app/Http/Controllers/Api/V1/TagsController.php
Normal file
|
@ -0,0 +1,207 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api\V1;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Hashtag;
|
||||||
|
use App\HashtagFollow;
|
||||||
|
use App\StatusHashtag;
|
||||||
|
use App\Services\AccountService;
|
||||||
|
use App\Services\HashtagService;
|
||||||
|
use App\Services\HashtagFollowService;
|
||||||
|
use App\Services\HashtagRelatedService;
|
||||||
|
use App\Http\Resources\MastoApi\FollowedTagResource;
|
||||||
|
use App\Jobs\HomeFeedPipeline\FeedWarmCachePipeline;
|
||||||
|
use App\Jobs\HomeFeedPipeline\HashtagUnfollowPipeline;
|
||||||
|
|
||||||
|
class TagsController extends Controller
|
||||||
|
{
|
||||||
|
const PF_API_ENTITY_KEY = "_pe";
|
||||||
|
|
||||||
|
public function json($res, $code = 200, $headers = [])
|
||||||
|
{
|
||||||
|
return response()->json($res, $code, $headers, JSON_UNESCAPED_SLASHES);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /api/v1/tags/:id/related
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function relatedTags(Request $request, $tag)
|
||||||
|
{
|
||||||
|
abort_unless($request->user(), 403);
|
||||||
|
$tag = Hashtag::whereSlug($tag)->firstOrFail();
|
||||||
|
return HashtagRelatedService::get($tag->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST /api/v1/tags/:id/follow
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function followHashtag(Request $request, $id)
|
||||||
|
{
|
||||||
|
abort_if(!$request->user(), 403);
|
||||||
|
|
||||||
|
$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();
|
||||||
|
|
||||||
|
abort_if(!$tag, 422, 'Unknown hashtag');
|
||||||
|
|
||||||
|
abort_if(
|
||||||
|
HashtagFollow::whereProfileId($pid)->count() >= HashtagFollow::MAX_LIMIT,
|
||||||
|
422,
|
||||||
|
'You cannot follow more than ' . HashtagFollow::MAX_LIMIT . ' hashtags.'
|
||||||
|
);
|
||||||
|
|
||||||
|
$follows = HashtagFollow::updateOrCreate(
|
||||||
|
[
|
||||||
|
'profile_id' => $account['id'],
|
||||||
|
'hashtag_id' => $tag->id
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'user_id' => $request->user()->id
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
HashtagService::follow($pid, $tag->id);
|
||||||
|
HashtagFollowService::add($tag->id, $pid);
|
||||||
|
|
||||||
|
return response()->json(FollowedTagResource::make($follows)->toArray($request));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST /api/v1/tags/:id/unfollow
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function unfollowHashtag(Request $request, $id)
|
||||||
|
{
|
||||||
|
abort_if(!$request->user(), 403);
|
||||||
|
|
||||||
|
$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();
|
||||||
|
|
||||||
|
abort_if(!$tag, 422, 'Unknown hashtag');
|
||||||
|
|
||||||
|
$follows = HashtagFollow::whereProfileId($pid)
|
||||||
|
->whereHashtagId($tag->id)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if(!$follows) {
|
||||||
|
return [
|
||||||
|
'name' => $tag->name,
|
||||||
|
'url' => config('app.url') . '/i/web/hashtag/' . $tag->slug,
|
||||||
|
'history' => [],
|
||||||
|
'following' => false
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if($follows) {
|
||||||
|
HashtagService::unfollow($pid, $tag->id);
|
||||||
|
HashtagFollowService::unfollow($tag->id, $pid);
|
||||||
|
HashtagUnfollowPipeline::dispatch($tag->id, $pid, $tag->slug)->onQueue('feed');
|
||||||
|
$follows->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
$res = FollowedTagResource::make($follows)->toArray($request);
|
||||||
|
$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);
|
||||||
|
|
||||||
|
$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
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$res = [
|
||||||
|
'name' => $tag->name,
|
||||||
|
'url' => config('app.url') . '/i/web/hashtag/' . $tag->slug,
|
||||||
|
'history' => [],
|
||||||
|
'following' => HashtagService::isFollowing($pid, $tag->id)
|
||||||
|
];
|
||||||
|
|
||||||
|
if($request->has(self::PF_API_ENTITY_KEY)) {
|
||||||
|
$res['count'] = HashtagService::count($tag->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->json($res);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /api/v1/followed_tags
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getFollowedTags(Request $request)
|
||||||
|
{
|
||||||
|
abort_if(!$request->user(), 403);
|
||||||
|
|
||||||
|
$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'])
|
||||||
|
->orderByDesc('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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Http\Middleware\DeprecatedEndpoint;
|
use App\Http\Middleware\DeprecatedEndpoint;
|
||||||
|
use App\Http\Controllers\Api\V1\TagsController;
|
||||||
|
|
||||||
$middleware = ['auth:api','validemail'];
|
$middleware = ['auth:api','validemail'];
|
||||||
|
|
||||||
|
@ -92,10 +93,11 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
|
||||||
Route::get('markers', 'Api\ApiV1Controller@getMarkers')->middleware($middleware);
|
Route::get('markers', 'Api\ApiV1Controller@getMarkers')->middleware($middleware);
|
||||||
Route::post('markers', 'Api\ApiV1Controller@setMarkers')->middleware($middleware);
|
Route::post('markers', 'Api\ApiV1Controller@setMarkers')->middleware($middleware);
|
||||||
|
|
||||||
Route::get('followed_tags', 'Api\ApiV1Controller@getFollowedTags')->middleware($middleware);
|
Route::get('followed_tags', [TagsController::class, 'getFollowedTags'])->middleware($middleware);
|
||||||
Route::post('tags/{id}/follow', 'Api\ApiV1Controller@followHashtag')->middleware($middleware);
|
Route::post('tags/{id}/follow', [TagsController::class, 'followHashtag'])->middleware($middleware);
|
||||||
Route::post('tags/{id}/unfollow', 'Api\ApiV1Controller@unfollowHashtag')->middleware($middleware);
|
Route::post('tags/{id}/unfollow', [TagsController::class, 'unfollowHashtag'])->middleware($middleware);
|
||||||
Route::get('tags/{id}', 'Api\ApiV1Controller@getHashtag')->middleware($middleware);
|
Route::get('tags/{id}/related', [TagsController::class, 'relatedTags'])->middleware($middleware);
|
||||||
|
Route::get('tags/{id}', [TagsController::class, 'getHashtag'])->middleware($middleware);
|
||||||
|
|
||||||
Route::get('statuses/{id}/history', 'StatusEditController@history')->middleware($middleware);
|
Route::get('statuses/{id}/history', 'StatusEditController@history')->middleware($middleware);
|
||||||
Route::put('statuses/{id}', 'StatusEditController@store')->middleware($middleware);
|
Route::put('statuses/{id}', 'StatusEditController@store')->middleware($middleware);
|
||||||
|
|
Loading…
Reference in a new issue