Add /api/v1/tags/:id/follow and /api/v1/tags/:id/unfollow api endpoints

This commit is contained in:
Daniel Supernault 2023-05-09 01:08:09 -06:00
parent 175a848665
commit 4d997bb959
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 91 additions and 0 deletions

View file

@ -70,6 +70,7 @@ use App\Services\{
BouncerService, BouncerService,
CollectionService, CollectionService,
FollowerService, FollowerService,
HashtagService,
InstanceService, InstanceService,
LikeService, LikeService,
NetworkTimelineService, NetworkTimelineService,
@ -3847,4 +3848,88 @@ class ApiV1Controller extends Controller
} }
return response()->json(FollowedTagResource::collection($res)->collection); 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);
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();
abort_if(!$tag, 422, 'Unknown hashtag');
$follows = HashtagFollow::updateOrCreate(
[
'profile_id' => $account['id'],
'hashtag_id' => $tag->id
],
[
'user_id' => $request->user()->id
]
);
HashtagService::follow($pid, $tag->id);
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);
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();
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);
$follows->delete();
}
$res = FollowedTagResource::make($follows)->toArray($request);
$res['following'] = false;
return response()->json($res);
}
} }

View file

@ -64,4 +64,9 @@ class HashtagService {
{ {
return Redis::zrem(self::FOLLOW_KEY . $pid, $hid); return Redis::zrem(self::FOLLOW_KEY . $pid, $hid);
} }
public static function following($pid, $start = 0, $limit = 10)
{
return Redis::zrevrange(self::FOLLOW_KEY . $pid, $start, $limit);
}
} }

View file

@ -407,6 +407,7 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact
Route::get('web/username/{id}', 'SpaController@usernameRedirect'); Route::get('web/username/{id}', 'SpaController@usernameRedirect');
Route::get('web/post/{id}', 'SpaController@webPost'); Route::get('web/post/{id}', 'SpaController@webPost');
Route::get('web/profile/{id}', 'SpaController@webProfile'); Route::get('web/profile/{id}', 'SpaController@webProfile');
Route::get('web/{q}', 'SpaController@index')->where('q', '.*'); Route::get('web/{q}', 'SpaController@index')->where('q', '.*');
Route::get('web', 'SpaController@index'); Route::get('web', 'SpaController@index');
}); });