Update CollectionsController, add new self route

This commit is contained in:
Daniel Supernault 2024-05-22 02:21:07 -06:00
parent e47353f1fd
commit bc2495c676
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1
2 changed files with 135 additions and 111 deletions

View file

@ -2,72 +2,65 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Collection;
use Auth; use App\CollectionItem;
use App\{
Collection,
CollectionItem,
Profile,
Status
};
use League\Fractal;
use App\Transformer\Api\{
AccountTransformer,
StatusTransformer,
};
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\CollectionService; use App\Services\CollectionService;
use App\Services\FollowerService; use App\Services\FollowerService;
use App\Services\StatusService; use App\Services\StatusService;
use App\Status;
use Auth;
use Illuminate\Http\Request;
class CollectionController extends Controller class CollectionController extends Controller
{ {
public function create(Request $request) public function create(Request $request)
{ {
abort_if(!Auth::check(), 403); abort_if(! Auth::check(), 403);
$profile = Auth::user()->profile; $profile = Auth::user()->profile;
$collection = Collection::firstOrCreate([ $collection = Collection::firstOrCreate([
'profile_id' => $profile->id, 'profile_id' => $profile->id,
'published_at' => null 'published_at' => null,
]); ]);
$collection->visibility = 'draft'; $collection->visibility = 'draft';
$collection->save(); $collection->save();
return view('collection.create', compact('collection')); return view('collection.create', compact('collection'));
} }
public function show(Request $request, int $id) public function show(Request $request, int $id)
{ {
$user = $request->user(); $user = $request->user();
$collection = CollectionService::getCollection($id); $collection = CollectionService::getCollection($id);
abort_if(!$collection, 404); abort_if(! $collection, 404);
if($collection['published_at'] == null || $collection['visibility'] != 'public') { if ($collection['published_at'] == null || $collection['visibility'] != 'public') {
abort_if(!$user, 404); abort_if(! $user, 404);
if($user->profile_id != $collection['pid']) { if ($user->profile_id != $collection['pid']) {
if(!$user->is_admin) { if (! $user->is_admin) {
abort_if($collection['visibility'] != 'private', 404); abort_if($collection['visibility'] != 'private', 404);
abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404); abort_if(! FollowerService::follows($user->profile_id, $collection['pid']), 404);
} }
} }
} }
return view('collection.show', compact('collection'));
return view('collection.show', compact('collection'));
} }
public function index(Request $request) public function index(Request $request)
{ {
abort_if(!Auth::check(), 403); abort_if(! Auth::check(), 403);
return $request->all();
return $request->all();
} }
public function store(Request $request, $id) public function store(Request $request, $id)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'title' => 'nullable|max:50', 'title' => 'nullable|max:50',
'description' => 'nullable|max:500', 'description' => 'nullable|max:500',
'visibility' => 'nullable|string|in:public,private,draft' 'visibility' => 'nullable|string|in:public,private,draft',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
@ -78,20 +71,21 @@ class CollectionController extends Controller
$collection->save(); $collection->save();
CollectionService::deleteCollection($id); CollectionService::deleteCollection($id);
return CollectionService::setCollection($collection->id, $collection); return CollectionService::setCollection($collection->id, $collection);
} }
public function publish(Request $request, int $id) public function publish(Request $request, int $id)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'title' => 'nullable|max:50', 'title' => 'nullable|max:50',
'description' => 'nullable|max:500', 'description' => 'nullable|max:500',
'visibility' => 'required|alpha|in:public,private,draft' 'visibility' => 'required|alpha|in:public,private,draft',
]); ]);
$profile = Auth::user()->profile; $profile = Auth::user()->profile;
$collection = Collection::whereProfileId($profile->id)->findOrFail($id); $collection = Collection::whereProfileId($profile->id)->findOrFail($id);
if($collection->items()->count() == 0) { if ($collection->items()->count() == 0) {
abort(404); abort(404);
} }
$collection->title = strip_tags($request->input('title')); $collection->title = strip_tags($request->input('title'));
@ -99,12 +93,13 @@ class CollectionController extends Controller
$collection->visibility = $request->input('visibility'); $collection->visibility = $request->input('visibility');
$collection->published_at = now(); $collection->published_at = now();
$collection->save(); $collection->save();
return CollectionService::setCollection($collection->id, $collection); return CollectionService::setCollection($collection->id, $collection);
} }
public function delete(Request $request, int $id) public function delete(Request $request, int $id)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$user = $request->user(); $user = $request->user();
$collection = Collection::whereProfileId($user->profile_id)->findOrFail($id); $collection = Collection::whereProfileId($user->profile_id)->findOrFail($id);
@ -113,7 +108,7 @@ class CollectionController extends Controller
CollectionService::deleteCollection($id); CollectionService::deleteCollection($id);
if($request->wantsJson()) { if ($request->wantsJson()) {
return 200; return 200;
} }
@ -122,13 +117,13 @@ class CollectionController extends Controller
public function storeId(Request $request) public function storeId(Request $request)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'collection_id' => 'required|int|min:1|exists:collections,id', 'collection_id' => 'required|int|min:1|exists:collections,id',
'post_id' => 'required|int|min:1' 'post_id' => 'required|int|min:1',
]); ]);
$profileId = $request->user()->profile_id; $profileId = $request->user()->profile_id;
$collectionId = $request->input('collection_id'); $collectionId = $request->input('collection_id');
$postId = $request->input('post_id'); $postId = $request->input('post_id');
@ -136,20 +131,20 @@ class CollectionController extends Controller
$collection = Collection::whereProfileId($profileId)->findOrFail($collectionId); $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
$count = $collection->items()->count(); $count = $collection->items()->count();
if($count) { if ($count) {
CollectionItem::whereCollectionId($collection->id) CollectionItem::whereCollectionId($collection->id)
->get() ->get()
->filter(function($col) { ->filter(function ($col) {
return StatusService::get($col->object_id, false) == null; return StatusService::get($col->object_id, false) == null;
}) })
->each(function($col) use($collectionId) { ->each(function ($col) use ($collectionId) {
CollectionService::removeItem($collectionId, $col->object_id); CollectionService::removeItem($collectionId, $col->object_id);
$col->delete(); $col->delete();
}); });
} }
$max = config('pixelfed.max_collection_length'); $max = config('pixelfed.max_collection_length');
if($count >= $max) { if ($count >= $max) {
abort(400, 'You can only add '.$max.' posts per collection'); abort(400, 'You can only add '.$max.' posts per collection');
} }
@ -160,10 +155,10 @@ class CollectionController extends Controller
$item = CollectionItem::firstOrCreate([ $item = CollectionItem::firstOrCreate([
'collection_id' => $collection->id, 'collection_id' => $collection->id,
'object_type' => 'App\Status', 'object_type' => 'App\Status',
'object_id' => $status->id 'object_id' => $status->id,
],[ ], [
'order' => $count, 'order' => $count,
]); ]);
CollectionService::deleteCollection($collection->id); CollectionService::deleteCollection($collection->id);
@ -177,112 +172,112 @@ class CollectionController extends Controller
public function getCollection(Request $request, $id) public function getCollection(Request $request, $id)
{ {
$user = $request->user(); $user = $request->user();
$collection = CollectionService::getCollection($id); $collection = CollectionService::getCollection($id);
if(!$collection) { if (! $collection) {
return response()->json([], 404); return response()->json([], 404);
} }
if($collection['published_at'] == null || $collection['visibility'] != 'public') { if ($collection['published_at'] == null || $collection['visibility'] != 'public') {
abort_unless($user, 404); abort_unless($user, 404);
if($user->profile_id != $collection['pid']) { if ($user->profile_id != $collection['pid']) {
if(!$user->is_admin) { if (! $user->is_admin) {
abort_if($collection['visibility'] != 'private', 404); abort_if($collection['visibility'] != 'private', 404);
abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404); abort_if(! FollowerService::follows($user->profile_id, $collection['pid']), 404);
} }
} }
} }
return $collection; return $collection;
} }
public function getItems(Request $request, int $id) public function getItems(Request $request, int $id)
{ {
$user = $request->user(); $user = $request->user();
$collection = CollectionService::getCollection($id); $collection = CollectionService::getCollection($id);
if(!$collection) { if (! $collection) {
return response()->json([], 404); return response()->json([], 404);
} }
if($collection['published_at'] == null || $collection['visibility'] != 'public') { if ($collection['published_at'] == null || $collection['visibility'] != 'public') {
abort_unless($user, 404); abort_unless($user, 404);
if($user->profile_id != $collection['pid']) { if ($user->profile_id != $collection['pid']) {
if(!$user->is_admin) { if (! $user->is_admin) {
abort_if($collection['visibility'] != 'private', 404); abort_if($collection['visibility'] != 'private', 404);
abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404); abort_if(! FollowerService::follows($user->profile_id, $collection['pid']), 404);
} }
} }
} }
$page = $request->input('page') ?? 1; $page = $request->input('page') ?? 1;
$start = $page == 1 ? 0 : ($page * 10 - 10); $start = $page == 1 ? 0 : ($page * 10 - 10);
$end = $start + 10; $end = $start + 10;
$items = CollectionService::getItems($id, $start, $end); $items = CollectionService::getItems($id, $start, $end);
return collect($items) return collect($items)
->map(function($id) { ->map(function ($id) {
return StatusService::get($id, false); return StatusService::get($id, false);
}) })
->filter(function($item) { ->filter(function ($item) {
return $item && ($item['visibility'] == 'public' || $item['visibility'] == 'unlisted') && isset($item['account'], $item['media_attachments']); return $item && ($item['visibility'] == 'public' || $item['visibility'] == 'unlisted') && isset($item['account'], $item['media_attachments']);
}) })
->values(); ->values();
} }
public function getUserCollections(Request $request, int $id) public function getUserCollections(Request $request, int $id)
{ {
$user = $request->user(); $user = $request->user();
$pid = $user ? $user->profile_id : null; $pid = $user ? $user->profile_id : null;
$follows = false; $follows = false;
$visibility = ['public']; $visibility = ['public'];
$profile = AccountService::get($id, true); $profile = AccountService::get($id, true);
if(!$profile || !isset($profile['id'])) { if (! $profile || ! isset($profile['id'])) {
return response()->json([], 404); return response()->json([], 404);
} }
if($pid) { if ($pid) {
$follows = FollowerService::follows($pid, $profile['id']); $follows = FollowerService::follows($pid, $profile['id']);
} }
if($profile['locked']) { if ($profile['locked']) {
abort_if(!$pid, 404); abort_if(! $pid, 404);
if(!$user->is_admin) { if (! $user->is_admin) {
abort_if($profile['id'] != $pid && $follows == false, 404); abort_if($profile['id'] != $pid && $follows == false, 404);
} }
} }
$owner = $pid ? $pid == $profile['id'] : false; $owner = $pid ? $pid == $profile['id'] : false;
if($follows) { if ($follows) {
$visibility = ['public', 'private']; $visibility = ['public', 'private'];
} }
if($pid && $pid == $profile['id']) { if ($pid && $pid == $profile['id']) {
$visibility = ['public', 'private', 'draft']; $visibility = ['public', 'private', 'draft'];
} }
return Collection::whereProfileId($profile['id']) return Collection::whereProfileId($profile['id'])
->whereIn('visibility', $visibility) ->whereIn('visibility', $visibility)
->when(!$owner, function($q, $owner) { ->when(! $owner, function ($q, $owner) {
return $q->whereNotNull('published_at'); return $q->whereNotNull('published_at');
}) })
->orderByDesc('id') ->orderByDesc('id')
->paginate(9) ->paginate(9)
->map(function($collection) { ->map(function ($collection) {
return CollectionService::getCollection($collection->id); return CollectionService::getCollection($collection->id);
}); });
} }
public function deleteId(Request $request) public function deleteId(Request $request)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'collection_id' => 'required|int|min:1|exists:collections,id', 'collection_id' => 'required|int|min:1|exists:collections,id',
'post_id' => 'required|int|min:1' 'post_id' => 'required|int|min:1',
]); ]);
$profileId = $request->user()->profile_id; $profileId = $request->user()->profile_id;
$collectionId = $request->input('collection_id'); $collectionId = $request->input('collection_id');
$postId = $request->input('post_id'); $postId = $request->input('post_id');
@ -290,7 +285,7 @@ class CollectionController extends Controller
$collection = Collection::whereProfileId($profileId)->findOrFail($collectionId); $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
$count = $collection->items()->count(); $count = $collection->items()->count();
if($count == 1) { if ($count == 1) {
abort(400, 'You cannot delete the only post of a collection!'); abort(400, 'You cannot delete the only post of a collection!');
} }
@ -308,7 +303,7 @@ class CollectionController extends Controller
CollectionItem::whereCollectionId($collection->id) CollectionItem::whereCollectionId($collection->id)
->orderBy('created_at') ->orderBy('created_at')
->get() ->get()
->each(function($item, $index) { ->each(function ($item, $index) {
$item->order = $index; $item->order = $index;
$item->save(); $item->save();
}); });
@ -319,4 +314,31 @@ class CollectionController extends Controller
return 200; return 200;
} }
public function getSelfCollections(Request $request)
{
abort_if(! $request->user(), 404);
$user = $request->user();
$pid = $user->profile_id;
$profile = AccountService::get($pid, true);
if (! $profile || ! isset($profile['id'])) {
return response()->json([], 404);
}
return Collection::whereProfileId($pid)
->orderByDesc('id')
->paginate(9)
->map(function ($collection) {
$c = CollectionService::getCollection($collection->id);
$c['items'] = collect(CollectionService::getItems($collection->id))
->map(function ($id) {
return StatusService::get($id, false);
})->filter()->values();
return $c;
})
->filter()
->values();
}
} }

View file

@ -133,6 +133,7 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
Route::post('update/{id}', 'CollectionController@store')->middleware($middleware); Route::post('update/{id}', 'CollectionController@store')->middleware($middleware);
Route::delete('delete/{id}', 'CollectionController@delete')->middleware($middleware); Route::delete('delete/{id}', 'CollectionController@delete')->middleware($middleware);
Route::post('remove', 'CollectionController@deleteId')->middleware($middleware); Route::post('remove', 'CollectionController@deleteId')->middleware($middleware);
Route::get('self', 'CollectionController@getSelfCollections')->middleware($middleware);
}); });
Route::group(['prefix' => 'direct'], function () use($middleware) { Route::group(['prefix' => 'direct'], function () use($middleware) {
@ -264,6 +265,7 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
Route::post('update/{id}', 'CollectionController@store')->middleware($middleware); Route::post('update/{id}', 'CollectionController@store')->middleware($middleware);
Route::delete('delete/{id}', 'CollectionController@delete')->middleware($middleware); Route::delete('delete/{id}', 'CollectionController@delete')->middleware($middleware);
Route::post('remove', 'CollectionController@deleteId')->middleware($middleware); Route::post('remove', 'CollectionController@deleteId')->middleware($middleware);
Route::get('self', 'CollectionController@getSelfCollections')->middleware($middleware);
}); });
Route::group(['prefix' => 'compose'], function () use($middleware) { Route::group(['prefix' => 'compose'], function () use($middleware) {