2018-05-31 21:56:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
use App\Bookmark;
|
2024-09-11 08:49:31 +00:00
|
|
|
use App\Services\AccountService;
|
2022-01-29 01:27:53 +00:00
|
|
|
use App\Services\BookmarkService;
|
2023-01-19 13:37:24 +00:00
|
|
|
use App\Services\FollowerService;
|
2024-01-11 10:22:35 +00:00
|
|
|
use App\Services\UserRoleService;
|
2024-09-11 08:49:31 +00:00
|
|
|
use App\Status;
|
|
|
|
use Illuminate\Http\Request;
|
2018-05-31 21:56:46 +00:00
|
|
|
|
|
|
|
class BookmarkController extends Controller
|
|
|
|
{
|
2024-01-11 10:22:35 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'item' => 'required|integer|min:1',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $request->user();
|
|
|
|
$status = Status::findOrFail($request->input('item'));
|
2024-09-11 08:49:31 +00:00
|
|
|
$account = AccountService::get($status->profile_id);
|
|
|
|
abort_if(isset($account['moved'], $account['moved']['id']), 422, 'Cannot bookmark or unbookmark a post from an account that has migrated');
|
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-bookmark', $user->id), 403, 'Invalid permissions for this action');
|
2024-01-11 10:22:35 +00:00
|
|
|
abort_if($status->in_reply_to_id || $status->reblog_of_id, 404);
|
2024-09-11 08:49:31 +00:00
|
|
|
abort_if(! in_array($status->scope, ['public', 'unlisted', 'private']), 404);
|
|
|
|
abort_if(! in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']), 404);
|
2024-01-11 10:22:35 +00:00
|
|
|
|
2024-09-11 08:49:31 +00:00
|
|
|
if ($status->scope == 'private') {
|
|
|
|
if ($user->profile_id !== $status->profile_id && ! FollowerService::follows($user->profile_id, $status->profile_id)) {
|
|
|
|
if ($exists = Bookmark::whereStatusId($status->id)->whereProfileId($user->profile_id)->first()) {
|
2024-01-11 10:22:35 +00:00
|
|
|
BookmarkService::del($user->profile_id, $status->id);
|
|
|
|
$exists->delete();
|
|
|
|
|
|
|
|
if ($request->ajax()) {
|
|
|
|
return ['code' => 200, 'msg' => 'Bookmark removed!'];
|
|
|
|
} else {
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abort(404, 'Error: You cannot bookmark private posts from accounts you do not follow.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$bookmark = Bookmark::firstOrCreate(
|
|
|
|
['status_id' => $status->id], ['profile_id' => $user->profile_id]
|
|
|
|
);
|
|
|
|
|
2024-09-11 08:49:31 +00:00
|
|
|
if (! $bookmark->wasRecentlyCreated) {
|
2024-01-11 10:22:35 +00:00
|
|
|
BookmarkService::del($user->profile_id, $status->id);
|
|
|
|
$bookmark->delete();
|
|
|
|
} else {
|
|
|
|
BookmarkService::add($user->profile_id, $status->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $request->expectsJson() ? ['code' => 200, 'msg' => 'Bookmark saved!'] : redirect()->back();
|
|
|
|
}
|
2018-05-31 21:56:46 +00:00
|
|
|
}
|