Implement follow request api endpoints

This commit is contained in:
Daniel Supernault 2022-06-11 03:31:24 -06:00
parent 4470981af7
commit b4dda5776b
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
1 changed files with 65 additions and 6 deletions

View File

@ -83,6 +83,8 @@ use App\Services\DiscoverService;
use App\Services\CustomEmojiService;
use App\Services\MarkerService;
use App\Models\Conversation;
use App\Jobs\FollowPipeline\FollowAcceptPipeline;
use App\Jobs\FollowPipeline\FollowRejectPipeline;
class ApiV1Controller extends Controller
{
@ -723,6 +725,13 @@ class ApiV1Controller extends Controller
->exists();
if($isFollowing == false) {
$followRequest = FollowRequest::whereFollowerId($user->profile_id)
->whereFollowingId($target->id)
->first();
if($followRequest) {
$followRequest->delete();
RelationshipService::refresh($target->id, $user->profile_id);
}
$resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
$res = $this->fractal->createData($resource)->toArray();
@ -1150,10 +1159,14 @@ class ApiV1Controller extends Controller
public function accountFollowRequests(Request $request)
{
abort_if(!$request->user(), 403);
$this->validate($request, [
'limit' => 'sometimes|integer|min:1|max:40'
]);
$user = $request->user();
$followRequests = FollowRequest::whereFollowingId($user->profile->id)->pluck('follower_id');
$followRequests = FollowRequest::whereFollowingId($user->profile->id)
->limit($request->input('limit', 40))
->pluck('follower_id');
$profiles = Profile::find($followRequests);
@ -1172,10 +1185,36 @@ class ApiV1Controller extends Controller
public function accountFollowRequestAccept(Request $request, $id)
{
abort_if(!$request->user(), 403);
$pid = $request->user()->profile_id;
$target = AccountService::getMastodon($id);
// todo
if(!$target) {
return response()->json(['error' => 'Record not found'], 404);
}
return response()->json([]);
$followRequest = FollowRequest::whereFollowingId($pid)->whereFollowerId($id)->first();
if(!$followRequest) {
return response()->json(['error' => 'Record not found'], 404);
}
$follower = $followRequest->follower;
$follow = new Follower();
$follow->profile_id = $follower->id;
$follow->following_id = $pid;
$follow->save();
if($follower->domain != null && $follower->private_key === null) {
FollowAcceptPipeline::dispatch($followRequest);
} else {
FollowPipeline::dispatch($follow);
$followRequest->delete();
}
RelationshipService::refresh($pid, $id);
$res = RelationshipService::get($pid, $id);
$res['followed_by'] = true;
return $this->json($res);
}
/**
@ -1188,10 +1227,30 @@ class ApiV1Controller extends Controller
public function accountFollowRequestReject(Request $request, $id)
{
abort_if(!$request->user(), 403);
$pid = $request->user()->profile_id;
$target = AccountService::getMastodon($id);
// todo
if(!$target) {
return response()->json(['error' => 'Record not found'], 404);
}
return response()->json([]);
$followRequest = FollowRequest::whereFollowingId($pid)->whereFollowerId($id)->first();
if(!$followRequest) {
return response()->json(['error' => 'Record not found'], 404);
}
$follower = $followRequest->follower;
if($follower->domain != null && $follower->private_key === null) {
FollowRejectPipeline::dispatch($followRequest);
} else {
$followRequest->delete();
}
RelationshipService::refresh($pid, $id);
$res = RelationshipService::get($pid, $id);
return $this->json($res);
}
/**