1
0

FollowerController.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Follower;
  4. use App\FollowRequest;
  5. use App\Jobs\FollowPipeline\FollowPipeline;
  6. use App\Profile;
  7. use Auth;
  8. use Illuminate\Http\Request;
  9. class FollowerController extends Controller
  10. {
  11. public function __construct()
  12. {
  13. $this->middleware('auth');
  14. }
  15. public function store(Request $request)
  16. {
  17. $this->validate($request, [
  18. 'item' => 'required|integer',
  19. ]);
  20. $item = $request->input('item');
  21. $this->handleFollowRequest($item);
  22. return redirect()->back();
  23. }
  24. protected function handleFollowRequest($item)
  25. {
  26. $user = Auth::user()->profile;
  27. $target = Profile::where('id', '!=', $user->id)->findOrFail($item);
  28. $private = (bool) $target->is_private;
  29. $isFollowing = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->count();
  30. if($private == true && $isFollowing == 0) {
  31. $follow = FollowRequest::firstOrCreate([
  32. 'follower_id' => $user->id,
  33. 'following_id' => $target->id
  34. ]);
  35. } elseif ($isFollowing == 0) {
  36. $follower = new Follower();
  37. $follower->profile_id = $user->id;
  38. $follower->following_id = $target->id;
  39. $follower->save();
  40. FollowPipeline::dispatch($follower);
  41. } else {
  42. $follower = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->firstOrFail();
  43. $follower->delete();
  44. }
  45. }
  46. }