-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Implement Follow management in Vue (#702)
* add manage follower button + correspondung sub page * Add api routes for follow listings * Added pluck to sort out followings/followers * no message * no message * fix userFollowX relations * Basic follower-view/api implementation in vue * fetchMore * reformatted follow-table * fix user_id * add "no followers/followings/requests" * Update app/Http/Controllers/Backend/User/FollowController.php Co-authored-by: Kris <[email protected]> Co-authored-by: Levin Herr <[email protected]>
- Loading branch information
1 parent
7019417
commit abbd61e
Showing
16 changed files
with
669 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\API\v1; | ||
|
||
use App\Exceptions\AlreadyFollowingException; | ||
use App\Exceptions\IdenticalModelException; | ||
use App\Exceptions\PermissionException; | ||
use App\Http\Controllers\API\ResponseController; | ||
use App\Http\Controllers\Backend\User\FollowController as FollowBackend; | ||
use App\Http\Controllers\Backend\User\FollowController as SettingsBackend; | ||
use App\Http\Controllers\UserController as UserBackend; | ||
use App\Http\Resources\UserResource; | ||
use App\Models\Follow; | ||
use App\Models\User; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Validation\Rule; | ||
|
||
class FollowController extends ResponseController | ||
{ | ||
|
||
public static function createFollow(Request $request, FollowController $instance): JsonResponse { | ||
$validated = $request->validate(['userId' => ['required', 'exists:users,id']]); | ||
$userToFollow = User::find($validated['userId']); | ||
|
||
try { | ||
$createFollowResponse = UserBackend::createOrRequestFollow(Auth::user(), $userToFollow); | ||
} catch (AlreadyFollowingException) { | ||
return $instance->sendv1Error(['message' => __('controller.user.follow-error')], 409); | ||
} catch (IdenticalModelException) { | ||
abort(409); | ||
} | ||
|
||
return $instance->sendv1Response(new UserResource($createFollowResponse), 204); | ||
} | ||
|
||
public static function destroyFollow(Request $request, FollowController $instance): JsonResponse { | ||
$validated = $request->validate(['userId' => ['required', 'exists:users,id']]); | ||
$userToUnfollow = User::find($validated['userId']); | ||
|
||
$destroyFollowResponse = UserBackend::destroyFollow(Auth::user(), $userToUnfollow); | ||
if ($destroyFollowResponse === false) { | ||
return $instance->sendv1Error(['message' => __('controller.user.follow-404')], 409); | ||
} | ||
|
||
$userToUnfollow->fresh(); | ||
return $instance->sendv1Response(new UserResource($userToUnfollow)); | ||
|
||
} | ||
|
||
public function getFollowers(): AnonymousResourceCollection { | ||
$followersResponse = FollowBackend::getFollowers(user: auth()->user()); | ||
return UserResource::collection($followersResponse); | ||
} | ||
|
||
public function getFollowRequests(): AnonymousResourceCollection { | ||
$followRequestResponse = FollowBackend::getFollowRequests(user: auth()->user()); | ||
return UserResource::collection($followRequestResponse); | ||
} | ||
|
||
public function getFollowings(): AnonymousResourceCollection { | ||
$followingResponse = FollowBackend::getFollowings(user: auth()->user()); | ||
return UserResource::collection($followingResponse); | ||
} | ||
|
||
public function removeFollower(Request $request): void { | ||
$validated = $request->validate([ | ||
'userId' => [ | ||
'required', | ||
Rule::in(auth()->user()->followers->pluck('user_id')), | ||
] | ||
]); | ||
|
||
$follow = Follow::where('user_id', $validated['userId']) | ||
->where('follow_id', auth()->user()->id) | ||
->firstOrFail(); | ||
|
||
try { | ||
$removeResponse = FollowBackend::removeFollower(follow: $follow, user: auth()->user()); | ||
} catch (PermissionException) { | ||
abort(403); | ||
} | ||
|
||
if ($removeResponse === true) { | ||
abort(204); | ||
} | ||
abort(500); | ||
} | ||
|
||
public function approveFollowRequest(Request $request) { | ||
$validated = $request->validate([ | ||
'userId' => [ | ||
'required', | ||
Rule::in(auth()->user()->followRequests->pluck('user_id')) | ||
] | ||
]); | ||
|
||
try { | ||
FollowBackend::approveFollower(auth()->user()->id, $validated['userId']); | ||
abort(204); | ||
} catch (ModelNotFoundException) { | ||
abort(404); | ||
} catch (AlreadyFollowingException $exception) { | ||
report($exception); | ||
} | ||
abort(500); | ||
} | ||
|
||
public function rejectFollowRequest(Request $request) { | ||
$validated = $request->validate([ | ||
'userId' => [ | ||
'required', | ||
Rule::in(auth()->user()->followRequests->pluck('user_id')) | ||
] | ||
]); | ||
try { | ||
FollowBackend::rejectFollower(auth()->user()->id, $validated['userId']); | ||
abort(204); | ||
} catch (ModelNotFoundException) { | ||
abort(404); | ||
} | ||
abort(500); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Backend\User; | ||
|
||
use App\Exceptions\AlreadyFollowingException; | ||
use App\Exceptions\PermissionException; | ||
use App\Http\Controllers\Controller; | ||
use App\Http\Controllers\UserController; | ||
use App\Models\Follow; | ||
use App\Models\FollowRequest; | ||
use App\Models\User; | ||
use Illuminate\Contracts\Pagination\Paginator; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
|
||
abstract class FollowController extends Controller | ||
{ | ||
public static function getFollowers(User $user): Paginator { | ||
return $user->userFollowers()->simplePaginate(perPage: 15); | ||
} | ||
|
||
public static function getFollowRequests(User $user): Paginator { | ||
return $user->userFollowRequests()->simplePaginate(perPage: 15); | ||
} | ||
|
||
public static function getFollowings(User $user): Paginator { | ||
return $user->userFollowings()->simplePaginate(perPage: 15); | ||
} | ||
|
||
/** | ||
* @param Follow $follow | ||
* @param User $user - The acting user | ||
* | ||
* @return bool|null | ||
* @throws PermissionException | ||
*/ | ||
public static function removeFollower(Follow $follow, User $user): bool|null { | ||
if ($user->cannot('delete', $follow)) { | ||
throw new PermissionException(); | ||
} | ||
return $follow->delete(); | ||
} | ||
|
||
/** | ||
* @param int $userId | ||
* @param int $followerID | ||
* | ||
* @return FollowRequest|null | ||
*/ | ||
public static function rejectFollower(int $userId, int $followerID): ?FollowRequest { | ||
$request = FollowRequest::where('user_id', $followerID)->where('follow_id', $userId)->firstOrFail(); | ||
|
||
$request->delete(); | ||
return $request; | ||
} | ||
|
||
/** | ||
* | ||
* @param int $userId The id of the user who is approving a follower | ||
* @param int $approverId The id of a to-be-approved follower | ||
* | ||
* @throws ModelNotFoundException|AlreadyFollowingException | ||
*/ | ||
public static function approveFollower(int $userId, int $approverId): bool { | ||
$request = FollowRequest::where('user_id', $approverId)->where('follow_id', $userId)->firstOrFail(); | ||
|
||
$follow = UserController::createFollow($request->user, $request->requestedFollow, true); | ||
|
||
if ($follow) { | ||
$request->delete(); | ||
} | ||
return $follow; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.