Update CollectionController

This commit is contained in:
Daniel Supernault 2022-02-16 04:42:02 -07:00
parent d2188e7ab3
commit f0d48ade0d
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -33,14 +33,14 @@ class CollectionController extends Controller
return view('collection.create', compact('collection')); return view('collection.create', compact('collection'));
} }
public function show(Request $request, int $collection) public function show(Request $request, int $id)
{ {
$collection = Collection::with('profile')->whereNotNull('published_at')->findOrFail($collection); $user = $request->user();
if($collection->profile->status != null) { $collection = Collection::findOrFail($id);
abort(404); if($collection->published_at == null || $collection->visibility != 'public') {
} if(!$user || $user->profile_id != $collection->profile_id) {
if($collection->visibility !== 'public') { abort_unless($user && $user->is_admin, 404);
abort_if(!Auth::check() || Auth::user()->profile_id != $collection->profile_id, 404); }
} }
return view('collection.show', compact('collection')); return view('collection.show', compact('collection'));
} }
@ -144,20 +144,23 @@ class CollectionController extends Controller
public function get(Request $request, $id) public function get(Request $request, $id)
{ {
$profile = Auth::check() ? Auth::user()->profile : []; $user = $request->user();
$collection = Collection::findOrFail($id);
$collection = Collection::whereVisibility('public')->findOrFail($id); if($collection->published_at == null || $collection->visibility != 'public') {
if($collection->published_at == null) { if(!$user || $user->profile_id != $collection->profile_id) {
if(!Auth::check() || $profile->id !== $collection->profile_id) { abort_unless($user && $user->is_admin, 404);
abort(404);
} }
} }
return [ return [
'id' => $collection->id, 'id' => (string) $collection->id,
'title' => $collection->title, 'visibility' => $collection->visibility,
'description' => $collection->description, 'title' => $collection->title,
'visibility' => $collection->visibility 'description' => $collection->description,
'thumb' => $collection->posts()->first()->thumb(),
'url' => $collection->url(),
'post_count' => $collection->posts()->count(),
'published_at' => $collection->published_at
]; ];
} }
@ -183,26 +186,28 @@ class CollectionController extends Controller
public function getUserCollections(Request $request, int $id) public function getUserCollections(Request $request, int $id)
{ {
$user = $request->user();
$pid = $user ? $user->profile_id : null;
$profile = Profile::whereNull('status') $profile = Profile::whereNull('status')
->whereNull('domain') ->whereNull('domain')
->findOrFail($id); ->findOrFail($id);
if($profile->is_private) { if($profile->is_private) {
abort_if(!Auth::check(), 404); abort_if(!$pid, 404);
abort_if(!$profile->followedBy(Auth::user()->profile) && $profile->id != Auth::user()->profile_id, 404); abort_if(!$profile->id != $pid, 404);
} }
return $profile $visibility = $pid == $profile->id ? ['public', 'private'] : ['public'];
->collections()
->has('posts') return Collection::whereProfileId($profile->id)
->with('posts') ->whereIn('visibility', $visibility)
->whereVisibility('public') ->orderByDesc('id')
->whereNotNull('published_at')
->orderByDesc('published_at')
->paginate(9) ->paginate(9)
->map(function($collection) { ->map(function($collection) {
return [ return [
'id' => (string) $collection->id, 'id' => (string) $collection->id,
'visibility' => $collection->visibility,
'title' => $collection->title, 'title' => $collection->title,
'description' => $collection->description, 'description' => $collection->description,
'thumb' => $collection->posts()->first()->thumb(), 'thumb' => $collection->posts()->first()->thumb(),