pixelfed/app/Http/Controllers/CollectionController.php

301 lines
9.2 KiB
PHP
Raw Normal View History

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;
use App\Services\CollectionService;
use App\Services\FollowerService;
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
]);
$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();
$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();
}
public function store(Request $request, $id)
2019-07-16 05:47:40 +00:00
{
abort_if(!Auth::check(), 403);
$this->validate($request, [
2019-07-18 02:24:53 +00:00
'title' => 'nullable',
'description' => 'nullable',
'visibility' => 'nullable|string|in:public,private,draft'
2019-07-16 05:47:40 +00:00
]);
$profile = Auth::user()->profile;
$collection = Collection::whereProfileId($profile->id)->findOrFail($id);
$collection->title = e($request->input('title'));
$collection->description = e($request->input('description'));
$collection->visibility = e($request->input('visibility'));
$collection->save();
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
{
abort_if(!Auth::check(), 403);
2019-07-18 02:24:53 +00:00
$this->validate($request, [
'title' => 'nullable',
'description' => 'nullable',
'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);
}
2019-07-18 02:24:53 +00:00
$collection->title = e($request->input('title'));
$collection->description = e($request->input('description'));
$collection->visibility = e($request->input('visibility'));
2019-07-16 05:47:40 +00:00
$collection->published_at = now();
$collection->save();
return CollectionService::setCollection($collection->id, $collection);
2019-07-16 05:47:40 +00:00
}
public function delete(Request $request, int $id)
{
abort_if(!Auth::check(), 403);
$user = Auth::user();
$collection = Collection::whereProfileId($user->profile_id)->findOrFail($id);
$collection->items()->delete();
$collection->delete();
2019-08-02 04:34:39 +00:00
if($request->wantsJson()) {
return 200;
}
CollectionService::deleteCollection($id);
2019-08-02 04:34:39 +00:00
return redirect('/');
2019-07-16 05:47:40 +00:00
}
public function storeId(Request $request)
{
$this->validate($request, [
'collection_id' => 'required|int|min:1|exists:collections,id',
'post_id' => 'required|int|min:1|exists:statuses,id'
]);
$profileId = Auth::user()->profile_id;
$collectionId = $request->input('collection_id');
$postId = $request->input('post_id');
$collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
$count = $collection->items()->count();
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
}
$status = Status::whereScope('public')
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,
]);
CollectionService::addItem(
$collection->id,
$status->id,
$count
);
return StatusService::get($status->id);
2019-07-16 05:47:40 +00:00
}
public function getCollection(Request $request, $id)
2019-07-16 05:47:40 +00:00
{
$user = $request->user();
$collection = CollectionService::getCollection($id);
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)
{
$user = $request->user();
$collection = CollectionService::getCollection($id);
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)
->map(function($id) {
return StatusService::get($id);
})
->filter(function($item) {
return $item && isset($item['account'], $item['media_attachments']);
})
->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;
$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
if($pid) {
2022-09-29 05:15:54 +00:00
$follows = FollowerService::follows($pid, $profile['id']);
}
2022-09-29 05:15:54 +00:00
if($profile['locked']) {
2022-02-16 11:42:02 +00:00
abort_if(!$pid, 404);
if(!$user->is_admin) {
2022-09-29 05:15:54 +00:00
abort_if($profile['id'] != $pid && $follows == false, 404);
}
}
2022-09-29 05:15:54 +00:00
$owner = $pid ? $pid == $profile['id'] : false;
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']) {
$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)
->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) {
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)
{
$this->validate($request, [
'collection_id' => 'required|int|min:1|exists:collections,id',
'post_id' => 'required|int|min:1|exists:statuses,id'
]);
$profileId = Auth::user()->profile_id;
$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!');
}
$status = Status::whereScope('public')
->whereIn('type', ['photo', 'photo:album', 'video'])
->findOrFail($postId);
CollectionService::removeItem(
$collection->id,
$status->id
);
2019-12-08 07:51:14 +00:00
$item = CollectionItem::whereCollectionId($collection->id)
->whereObjectType('App\Status')
->whereObjectId($status->id)
->firstOrFail();
$item->delete();
return 200;
}
2018-11-09 01:25:22 +00:00
}