BookmarkController.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Bookmark;
  4. use App\Status;
  5. use Auth;
  6. use Illuminate\Http\Request;
  7. use App\Services\BookmarkService;
  8. use App\Services\FollowerService;
  9. class BookmarkController 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|min:1',
  19. ]);
  20. $profile = Auth::user()->profile;
  21. $status = Status::findOrFail($request->input('item'));
  22. abort_if(!in_array($status->scope, ['public', 'unlisted', 'private']), 404);
  23. if($status->scope == 'private') {
  24. abort_if(
  25. $profile->id !== $status->profile_id && !FollowerService::follows($profile->id, $status->profile_id),
  26. 404,
  27. 'Error: Cannot bookmark private posts from accounts you do not follow.'
  28. );
  29. }
  30. $bookmark = Bookmark::firstOrCreate(
  31. ['status_id' => $status->id], ['profile_id' => $profile->id]
  32. );
  33. if (!$bookmark->wasRecentlyCreated) {
  34. BookmarkService::del($profile->id, $status->id);
  35. $bookmark->delete();
  36. } else {
  37. BookmarkService::add($profile->id, $status->id);
  38. }
  39. if ($request->ajax()) {
  40. $response = ['code' => 200, 'msg' => 'Bookmark saved!'];
  41. } else {
  42. $response = redirect()->back();
  43. }
  44. return $response;
  45. }
  46. }