BookmarkController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. use App\Services\UserRoleService;
  10. class BookmarkController extends Controller
  11. {
  12. public function __construct()
  13. {
  14. $this->middleware('auth');
  15. }
  16. public function store(Request $request)
  17. {
  18. $this->validate($request, [
  19. 'item' => 'required|integer|min:1',
  20. ]);
  21. $user = $request->user();
  22. $status = Status::findOrFail($request->input('item'));
  23. abort_if($user->has_roles && !UserRoleService::can('can-bookmark', $user->id), 403, 'Invalid permissions for this action');
  24. abort_if($status->in_reply_to_id || $status->reblog_of_id, 404);
  25. abort_if(!in_array($status->scope, ['public', 'unlisted', 'private']), 404);
  26. abort_if(!in_array($status->type, ['photo','photo:album', 'video', 'video:album', 'photo:video:album']), 404);
  27. if($status->scope == 'private') {
  28. if($user->profile_id !== $status->profile_id && !FollowerService::follows($user->profile_id, $status->profile_id)) {
  29. if($exists = Bookmark::whereStatusId($status->id)->whereProfileId($user->profile_id)->first()) {
  30. BookmarkService::del($user->profile_id, $status->id);
  31. $exists->delete();
  32. if ($request->ajax()) {
  33. return ['code' => 200, 'msg' => 'Bookmark removed!'];
  34. } else {
  35. return redirect()->back();
  36. }
  37. }
  38. abort(404, 'Error: You cannot bookmark private posts from accounts you do not follow.');
  39. }
  40. }
  41. $bookmark = Bookmark::firstOrCreate(
  42. ['status_id' => $status->id], ['profile_id' => $user->profile_id]
  43. );
  44. if (!$bookmark->wasRecentlyCreated) {
  45. BookmarkService::del($user->profile_id, $status->id);
  46. $bookmark->delete();
  47. } else {
  48. BookmarkService::add($user->profile_id, $status->id);
  49. }
  50. return $request->expectsJson() ? ['code' => 200, 'msg' => 'Bookmark saved!'] : redirect()->back();
  51. }
  52. }