BookmarkController.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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($status->in_reply_to_id || $status->reblog_of_id, 404);
  23. abort_if(!in_array($status->scope, ['public', 'unlisted', 'private']), 404);
  24. abort_if(!in_array($status->type, ['photo','photo:album', 'video', 'video:album', 'photo:video:album']), 404);
  25. if($status->scope == 'private') {
  26. if($profile->id !== $status->profile_id && !FollowerService::follows($profile->id, $status->profile_id)) {
  27. if($exists = Bookmark::whereStatusId($status->id)->whereProfileId($profile->id)->first()) {
  28. BookmarkService::del($profile->id, $status->id);
  29. $exists->delete();
  30. if ($request->ajax()) {
  31. return ['code' => 200, 'msg' => 'Bookmark removed!'];
  32. } else {
  33. return redirect()->back();
  34. }
  35. }
  36. abort(404, 'Error: You cannot bookmark private posts from accounts you do not follow.');
  37. }
  38. }
  39. $bookmark = Bookmark::firstOrCreate(
  40. ['status_id' => $status->id], ['profile_id' => $profile->id]
  41. );
  42. if (!$bookmark->wasRecentlyCreated) {
  43. BookmarkService::del($profile->id, $status->id);
  44. $bookmark->delete();
  45. } else {
  46. BookmarkService::add($profile->id, $status->id);
  47. }
  48. if ($request->ajax()) {
  49. $response = ['code' => 200, 'msg' => 'Bookmark saved!'];
  50. } else {
  51. $response = redirect()->back();
  52. }
  53. return $response;
  54. }
  55. }