2018-11-09 01:25:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2019-07-16 05:47:40 +00:00
|
|
|
use Auth;
|
|
|
|
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;
|
2022-09-29 05:15:54 +00:00
|
|
|
use App\Services\AccountService;
|
2022-03-09 03:57:18 +00:00
|
|
|
use App\Services\CollectionService;
|
|
|
|
use App\Services\FollowerService;
|
2022-02-16 07:58:19 +00:00
|
|
|
use App\Services\StatusService;
|
2018-11-09 01:25:22 +00:00
|
|
|
|
|
|
|
class CollectionController extends Controller
|
|
|
|
{
|
2019-07-16 05:47:40 +00:00
|
|
|
public function create(Request $request)
|
|
|
|
{
|
|
|
|
abort_if(!Auth::check(), 403);
|
|
|
|
$profile = Auth::user()->profile;
|
|
|
|
|
|
|
|
$collection = Collection::firstOrCreate([
|
|
|
|
'profile_id' => $profile->id,
|
|
|
|
'published_at' => null
|
|
|
|
]);
|
2022-03-09 03:57:18 +00:00
|
|
|
$collection->visibility = 'draft';
|
|
|
|
$collection->save();
|
2019-07-16 05:47:40 +00:00
|
|
|
return view('collection.create', compact('collection'));
|
|
|
|
}
|
|
|
|
|
2022-02-16 11:42:02 +00:00
|
|
|
public function show(Request $request, int $id)
|
2019-07-16 05:47:40 +00:00
|
|
|
{
|
2022-02-16 11:42:02 +00:00
|
|
|
$user = $request->user();
|
2022-03-09 03:57:18 +00:00
|
|
|
$collection = CollectionService::getCollection($id);
|
|
|
|
abort_if(!$collection, 404);
|
|
|
|
if($collection['published_at'] == null || $collection['visibility'] != 'public') {
|
|
|
|
abort_if(!$user, 404);
|
|
|
|
if($user->profile_id != $collection['pid']) {
|
|
|
|
if(!$user->is_admin) {
|
|
|
|
abort_if($collection['visibility'] != 'private', 404);
|
|
|
|
abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-16 05:47:40 +00:00
|
|
|
return view('collection.show', compact('collection'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
|
|
|
abort_if(!Auth::check(), 403);
|
|
|
|
return $request->all();
|
|
|
|
}
|
|
|
|
|
2020-02-18 07:36:49 +00:00
|
|
|
public function store(Request $request, $id)
|
2019-07-16 05:47:40 +00:00
|
|
|
{
|
2022-10-01 11:36:07 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2019-07-16 05:47:40 +00:00
|
|
|
$this->validate($request, [
|
2022-10-01 09:45:53 +00:00
|
|
|
'title' => 'nullable|max:50',
|
|
|
|
'description' => 'nullable|max:500',
|
2022-03-09 03:57:18 +00:00
|
|
|
'visibility' => 'nullable|string|in:public,private,draft'
|
2019-07-16 05:47:40 +00:00
|
|
|
]);
|
|
|
|
|
2022-10-01 11:36:07 +00:00
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$collection = Collection::whereProfileId($pid)->findOrFail($id);
|
|
|
|
$collection->title = strip_tags($request->input('title'));
|
|
|
|
$collection->description = strip_tags($request->input('description'));
|
|
|
|
$collection->visibility = $request->input('visibility');
|
2019-07-16 05:47:40 +00:00
|
|
|
$collection->save();
|
|
|
|
|
2022-10-03 00:55:16 +00:00
|
|
|
CollectionService::deleteCollection($id);
|
2022-03-09 03:57:18 +00:00
|
|
|
return CollectionService::setCollection($collection->id, $collection);
|
2019-07-16 05:47:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 02:24:53 +00:00
|
|
|
public function publish(Request $request, int $id)
|
2019-07-16 05:47:40 +00:00
|
|
|
{
|
2022-10-01 11:36:07 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2019-07-18 02:24:53 +00:00
|
|
|
$this->validate($request, [
|
2022-10-01 09:45:53 +00:00
|
|
|
'title' => 'nullable|max:50',
|
|
|
|
'description' => 'nullable|max:500',
|
2022-03-09 03:57:18 +00:00
|
|
|
'visibility' => 'required|alpha|in:public,private,draft'
|
2019-07-18 02:24:53 +00:00
|
|
|
]);
|
2019-07-16 05:47:40 +00:00
|
|
|
$profile = Auth::user()->profile;
|
|
|
|
$collection = Collection::whereProfileId($profile->id)->findOrFail($id);
|
2019-07-18 05:28:16 +00:00
|
|
|
if($collection->items()->count() == 0) {
|
|
|
|
abort(404);
|
|
|
|
}
|
2022-10-03 00:55:16 +00:00
|
|
|
$collection->title = strip_tags($request->input('title'));
|
2022-10-01 11:36:07 +00:00
|
|
|
$collection->description = strip_tags($request->input('description'));
|
2022-10-03 00:55:16 +00:00
|
|
|
$collection->visibility = $request->input('visibility');
|
2019-07-16 05:47:40 +00:00
|
|
|
$collection->published_at = now();
|
|
|
|
$collection->save();
|
2022-03-09 03:57:18 +00:00
|
|
|
return CollectionService::setCollection($collection->id, $collection);
|
2019-07-16 05:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(Request $request, int $id)
|
|
|
|
{
|
2022-10-01 11:36:07 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
|
|
|
$user = $request->user();
|
2019-07-16 05:47:40 +00:00
|
|
|
|
|
|
|
$collection = Collection::whereProfileId($user->profile_id)->findOrFail($id);
|
|
|
|
$collection->items()->delete();
|
|
|
|
$collection->delete();
|
|
|
|
|
2022-10-01 11:36:07 +00:00
|
|
|
CollectionService::deleteCollection($id);
|
|
|
|
|
2019-08-02 04:34:39 +00:00
|
|
|
if($request->wantsJson()) {
|
|
|
|
return 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect('/');
|
2019-07-16 05:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function storeId(Request $request)
|
|
|
|
{
|
2022-10-01 11:36:07 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
|
|
|
|
2019-07-16 05:47:40 +00:00
|
|
|
$this->validate($request, [
|
|
|
|
'collection_id' => 'required|int|min:1|exists:collections,id',
|
2022-10-01 11:36:07 +00:00
|
|
|
'post_id' => 'required|int|min:1'
|
2019-07-16 05:47:40 +00:00
|
|
|
]);
|
|
|
|
|
2022-10-01 11:36:07 +00:00
|
|
|
$profileId = $request->user()->profile_id;
|
2019-07-16 05:47:40 +00:00
|
|
|
$collectionId = $request->input('collection_id');
|
|
|
|
$postId = $request->input('post_id');
|
|
|
|
|
|
|
|
$collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
|
|
|
|
$count = $collection->items()->count();
|
|
|
|
|
2022-06-19 05:47:16 +00:00
|
|
|
if($count) {
|
|
|
|
CollectionItem::whereCollectionId($collection->id)
|
|
|
|
->get()
|
|
|
|
->filter(function($col) {
|
|
|
|
return StatusService::get($col->object_id, false) == null;
|
|
|
|
})
|
|
|
|
->each(function($col) use($collectionId) {
|
|
|
|
CollectionService::removeItem($collectionId, $col->object_id);
|
|
|
|
$col->delete();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-10 08:02:56 +00:00
|
|
|
$max = config('pixelfed.max_collection_length');
|
|
|
|
if($count >= $max) {
|
|
|
|
abort(400, 'You can only add '.$max.' posts per collection');
|
2019-07-16 05:47:40 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 18:27:24 +00:00
|
|
|
$status = Status::whereIn('scope', ['public', 'unlisted'])
|
2022-10-01 11:36:07 +00:00
|
|
|
->whereProfileId($profileId)
|
2019-07-21 01:21:50 +00:00
|
|
|
->whereIn('type', ['photo', 'photo:album', 'video'])
|
2019-07-16 05:47:40 +00:00
|
|
|
->findOrFail($postId);
|
|
|
|
|
|
|
|
$item = CollectionItem::firstOrCreate([
|
|
|
|
'collection_id' => $collection->id,
|
|
|
|
'object_type' => 'App\Status',
|
|
|
|
'object_id' => $status->id
|
|
|
|
],[
|
|
|
|
'order' => $count,
|
|
|
|
]);
|
|
|
|
|
2023-11-30 00:19:04 +00:00
|
|
|
CollectionService::deleteCollection($collection->id);
|
2022-03-09 03:57:18 +00:00
|
|
|
|
2022-10-01 06:07:28 +00:00
|
|
|
$collection->updated_at = now();
|
|
|
|
$collection->save();
|
|
|
|
CollectionService::setCollection($collection->id, $collection);
|
|
|
|
|
2023-11-09 18:27:24 +00:00
|
|
|
return StatusService::get($status->id, false);
|
2019-07-16 05:47:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 03:57:18 +00:00
|
|
|
public function getCollection(Request $request, $id)
|
2019-07-16 05:47:40 +00:00
|
|
|
{
|
2022-03-09 03:57:18 +00:00
|
|
|
$user = $request->user();
|
|
|
|
$collection = CollectionService::getCollection($id);
|
2022-10-01 04:23:58 +00:00
|
|
|
|
|
|
|
if(!$collection) {
|
|
|
|
return response()->json([], 404);
|
|
|
|
}
|
|
|
|
|
2022-03-09 03:57:18 +00:00
|
|
|
if($collection['published_at'] == null || $collection['visibility'] != 'public') {
|
|
|
|
abort_unless($user, 404);
|
|
|
|
if($user->profile_id != $collection['pid']) {
|
|
|
|
if(!$user->is_admin) {
|
|
|
|
abort_if($collection['visibility'] != 'private', 404);
|
|
|
|
abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
2019-07-16 05:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getItems(Request $request, int $id)
|
|
|
|
{
|
2022-03-09 03:57:18 +00:00
|
|
|
$user = $request->user();
|
|
|
|
$collection = CollectionService::getCollection($id);
|
2022-10-01 04:23:58 +00:00
|
|
|
|
|
|
|
if(!$collection) {
|
|
|
|
return response()->json([], 404);
|
|
|
|
}
|
|
|
|
|
2022-03-09 03:57:18 +00:00
|
|
|
if($collection['published_at'] == null || $collection['visibility'] != 'public') {
|
|
|
|
abort_unless($user, 404);
|
|
|
|
if($user->profile_id != $collection['pid']) {
|
|
|
|
if(!$user->is_admin) {
|
|
|
|
abort_if($collection['visibility'] != 'private', 404);
|
|
|
|
abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$page = $request->input('page') ?? 1;
|
|
|
|
$start = $page == 1 ? 0 : ($page * 10 - 10);
|
|
|
|
$end = $start + 10;
|
|
|
|
$items = CollectionService::getItems($id, $start, $end);
|
|
|
|
|
|
|
|
return collect($items)
|
2022-02-16 07:58:19 +00:00
|
|
|
->map(function($id) {
|
2023-11-09 18:27:24 +00:00
|
|
|
return StatusService::get($id, false);
|
2022-02-16 07:58:19 +00:00
|
|
|
})
|
2022-03-09 03:57:18 +00:00
|
|
|
->filter(function($item) {
|
2023-11-09 18:27:24 +00:00
|
|
|
return $item && ($item['visibility'] == 'public' || $item['visibility'] == 'unlisted') && isset($item['account'], $item['media_attachments']);
|
2022-02-16 07:58:19 +00:00
|
|
|
})
|
|
|
|
->values();
|
2019-07-16 05:47:40 +00:00
|
|
|
}
|
2019-07-18 03:10:28 +00:00
|
|
|
|
|
|
|
public function getUserCollections(Request $request, int $id)
|
|
|
|
{
|
2022-02-16 11:42:02 +00:00
|
|
|
$user = $request->user();
|
|
|
|
$pid = $user ? $user->profile_id : null;
|
2022-03-09 03:57:18 +00:00
|
|
|
$follows = false;
|
|
|
|
$visibility = ['public'];
|
2022-02-16 11:42:02 +00:00
|
|
|
|
2022-09-29 05:15:54 +00:00
|
|
|
$profile = AccountService::get($id, true);
|
|
|
|
if(!$profile || !isset($profile['id'])) {
|
|
|
|
return response()->json([], 404);
|
|
|
|
}
|
2019-07-18 03:10:28 +00:00
|
|
|
|
2022-03-09 03:57:18 +00:00
|
|
|
if($pid) {
|
2022-09-29 05:15:54 +00:00
|
|
|
$follows = FollowerService::follows($pid, $profile['id']);
|
2022-03-09 03:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 05:15:54 +00:00
|
|
|
if($profile['locked']) {
|
2022-02-16 11:42:02 +00:00
|
|
|
abort_if(!$pid, 404);
|
2022-03-09 03:57:18 +00:00
|
|
|
if(!$user->is_admin) {
|
2022-09-29 05:15:54 +00:00
|
|
|
abort_if($profile['id'] != $pid && $follows == false, 404);
|
2022-03-09 03:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 05:15:54 +00:00
|
|
|
$owner = $pid ? $pid == $profile['id'] : false;
|
2022-03-09 03:57:18 +00:00
|
|
|
|
|
|
|
if($follows) {
|
|
|
|
$visibility = ['public', 'private'];
|
2019-07-18 03:10:28 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 05:15:54 +00:00
|
|
|
if($pid && $pid == $profile['id']) {
|
2022-03-09 03:57:18 +00:00
|
|
|
$visibility = ['public', 'private', 'draft'];
|
|
|
|
}
|
2022-02-16 11:42:02 +00:00
|
|
|
|
2022-09-29 05:15:54 +00:00
|
|
|
return Collection::whereProfileId($profile['id'])
|
2022-02-16 11:42:02 +00:00
|
|
|
->whereIn('visibility', $visibility)
|
2022-04-03 09:25:08 +00:00
|
|
|
->when(!$owner, function($q, $owner) {
|
|
|
|
return $q->whereNotNull('published_at');
|
|
|
|
})
|
2022-02-16 11:42:02 +00:00
|
|
|
->orderByDesc('id')
|
2019-07-18 03:10:28 +00:00
|
|
|
->paginate(9)
|
|
|
|
->map(function($collection) {
|
2022-03-09 03:57:18 +00:00
|
|
|
return CollectionService::getCollection($collection->id);
|
2019-07-18 03:10:28 +00:00
|
|
|
});
|
|
|
|
}
|
2019-12-08 07:51:14 +00:00
|
|
|
|
|
|
|
public function deleteId(Request $request)
|
|
|
|
{
|
2022-10-01 11:36:07 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2019-12-08 07:51:14 +00:00
|
|
|
$this->validate($request, [
|
|
|
|
'collection_id' => 'required|int|min:1|exists:collections,id',
|
2022-10-01 11:36:07 +00:00
|
|
|
'post_id' => 'required|int|min:1'
|
2019-12-08 07:51:14 +00:00
|
|
|
]);
|
|
|
|
|
2022-10-01 11:36:07 +00:00
|
|
|
$profileId = $request->user()->profile_id;
|
2019-12-08 07:51:14 +00:00
|
|
|
$collectionId = $request->input('collection_id');
|
|
|
|
$postId = $request->input('post_id');
|
|
|
|
|
|
|
|
$collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
|
|
|
|
$count = $collection->items()->count();
|
|
|
|
|
|
|
|
if($count == 1) {
|
|
|
|
abort(400, 'You cannot delete the only post of a collection!');
|
|
|
|
}
|
|
|
|
|
2023-11-09 18:27:24 +00:00
|
|
|
$status = Status::whereIn('scope', ['public', 'unlisted'])
|
2019-12-08 07:51:14 +00:00
|
|
|
->whereIn('type', ['photo', 'photo:album', 'video'])
|
|
|
|
->findOrFail($postId);
|
|
|
|
|
|
|
|
$item = CollectionItem::whereCollectionId($collection->id)
|
|
|
|
->whereObjectType('App\Status')
|
|
|
|
->whereObjectId($status->id)
|
|
|
|
->firstOrFail();
|
|
|
|
|
|
|
|
$item->delete();
|
|
|
|
|
2022-10-03 00:55:16 +00:00
|
|
|
CollectionItem::whereCollectionId($collection->id)
|
|
|
|
->orderBy('created_at')
|
|
|
|
->get()
|
|
|
|
->each(function($item, $index) {
|
|
|
|
$item->order = $index;
|
|
|
|
$item->save();
|
|
|
|
});
|
|
|
|
|
2022-10-01 06:07:28 +00:00
|
|
|
$collection->updated_at = now();
|
|
|
|
$collection->save();
|
2022-10-03 00:55:16 +00:00
|
|
|
CollectionService::deleteCollection($collection->id);
|
2022-10-01 06:07:28 +00:00
|
|
|
|
2019-12-08 07:51:14 +00:00
|
|
|
return 200;
|
|
|
|
}
|
2018-11-09 01:25:22 +00:00
|
|
|
}
|