CollectionController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Auth;
  5. use App\{
  6. Collection,
  7. CollectionItem,
  8. Profile,
  9. Status
  10. };
  11. use League\Fractal;
  12. use App\Transformer\Api\{
  13. AccountTransformer,
  14. StatusTransformer,
  15. };
  16. use League\Fractal\Serializer\ArraySerializer;
  17. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  18. use App\Services\AccountService;
  19. use App\Services\CollectionService;
  20. use App\Services\FollowerService;
  21. use App\Services\StatusService;
  22. class CollectionController extends Controller
  23. {
  24. public function create(Request $request)
  25. {
  26. abort_if(!Auth::check(), 403);
  27. $profile = Auth::user()->profile;
  28. $collection = Collection::firstOrCreate([
  29. 'profile_id' => $profile->id,
  30. 'published_at' => null
  31. ]);
  32. $collection->visibility = 'draft';
  33. $collection->save();
  34. return view('collection.create', compact('collection'));
  35. }
  36. public function show(Request $request, int $id)
  37. {
  38. $user = $request->user();
  39. $collection = CollectionService::getCollection($id);
  40. abort_if(!$collection, 404);
  41. if($collection['published_at'] == null || $collection['visibility'] != 'public') {
  42. abort_if(!$user, 404);
  43. if($user->profile_id != $collection['pid']) {
  44. if(!$user->is_admin) {
  45. abort_if($collection['visibility'] != 'private', 404);
  46. abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404);
  47. }
  48. }
  49. }
  50. return view('collection.show', compact('collection'));
  51. }
  52. public function index(Request $request)
  53. {
  54. abort_if(!Auth::check(), 403);
  55. return $request->all();
  56. }
  57. public function store(Request $request, $id)
  58. {
  59. abort_if(!Auth::check(), 403);
  60. $this->validate($request, [
  61. 'title' => 'nullable',
  62. 'description' => 'nullable',
  63. 'visibility' => 'nullable|string|in:public,private,draft'
  64. ]);
  65. $profile = Auth::user()->profile;
  66. $collection = Collection::whereProfileId($profile->id)->findOrFail($id);
  67. $collection->title = e($request->input('title'));
  68. $collection->description = e($request->input('description'));
  69. $collection->visibility = e($request->input('visibility'));
  70. $collection->save();
  71. return CollectionService::setCollection($collection->id, $collection);
  72. }
  73. public function publish(Request $request, int $id)
  74. {
  75. abort_if(!Auth::check(), 403);
  76. $this->validate($request, [
  77. 'title' => 'nullable',
  78. 'description' => 'nullable',
  79. 'visibility' => 'required|alpha|in:public,private,draft'
  80. ]);
  81. $profile = Auth::user()->profile;
  82. $collection = Collection::whereProfileId($profile->id)->findOrFail($id);
  83. if($collection->items()->count() == 0) {
  84. abort(404);
  85. }
  86. $collection->title = e($request->input('title'));
  87. $collection->description = e($request->input('description'));
  88. $collection->visibility = e($request->input('visibility'));
  89. $collection->published_at = now();
  90. $collection->save();
  91. return CollectionService::setCollection($collection->id, $collection);
  92. }
  93. public function delete(Request $request, int $id)
  94. {
  95. abort_if(!Auth::check(), 403);
  96. $user = Auth::user();
  97. $collection = Collection::whereProfileId($user->profile_id)->findOrFail($id);
  98. $collection->items()->delete();
  99. $collection->delete();
  100. if($request->wantsJson()) {
  101. return 200;
  102. }
  103. CollectionService::deleteCollection($id);
  104. return redirect('/');
  105. }
  106. public function storeId(Request $request)
  107. {
  108. $this->validate($request, [
  109. 'collection_id' => 'required|int|min:1|exists:collections,id',
  110. 'post_id' => 'required|int|min:1|exists:statuses,id'
  111. ]);
  112. $profileId = Auth::user()->profile_id;
  113. $collectionId = $request->input('collection_id');
  114. $postId = $request->input('post_id');
  115. $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
  116. $count = $collection->items()->count();
  117. if($count) {
  118. CollectionItem::whereCollectionId($collection->id)
  119. ->get()
  120. ->filter(function($col) {
  121. return StatusService::get($col->object_id, false) == null;
  122. })
  123. ->each(function($col) use($collectionId) {
  124. CollectionService::removeItem($collectionId, $col->object_id);
  125. $col->delete();
  126. });
  127. }
  128. $max = config('pixelfed.max_collection_length');
  129. if($count >= $max) {
  130. abort(400, 'You can only add '.$max.' posts per collection');
  131. }
  132. $status = Status::whereScope('public')
  133. ->whereIn('type', ['photo', 'photo:album', 'video'])
  134. ->findOrFail($postId);
  135. $item = CollectionItem::firstOrCreate([
  136. 'collection_id' => $collection->id,
  137. 'object_type' => 'App\Status',
  138. 'object_id' => $status->id
  139. ],[
  140. 'order' => $count,
  141. ]);
  142. CollectionService::addItem(
  143. $collection->id,
  144. $status->id,
  145. $count
  146. );
  147. return StatusService::get($status->id);
  148. }
  149. public function getCollection(Request $request, $id)
  150. {
  151. $user = $request->user();
  152. $collection = CollectionService::getCollection($id);
  153. if($collection['published_at'] == null || $collection['visibility'] != 'public') {
  154. abort_unless($user, 404);
  155. if($user->profile_id != $collection['pid']) {
  156. if(!$user->is_admin) {
  157. abort_if($collection['visibility'] != 'private', 404);
  158. abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404);
  159. }
  160. }
  161. }
  162. return $collection;
  163. }
  164. public function getItems(Request $request, int $id)
  165. {
  166. $user = $request->user();
  167. $collection = CollectionService::getCollection($id);
  168. if($collection['published_at'] == null || $collection['visibility'] != 'public') {
  169. abort_unless($user, 404);
  170. if($user->profile_id != $collection['pid']) {
  171. if(!$user->is_admin) {
  172. abort_if($collection['visibility'] != 'private', 404);
  173. abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404);
  174. }
  175. }
  176. }
  177. $page = $request->input('page') ?? 1;
  178. $start = $page == 1 ? 0 : ($page * 10 - 10);
  179. $end = $start + 10;
  180. $items = CollectionService::getItems($id, $start, $end);
  181. return collect($items)
  182. ->map(function($id) {
  183. return StatusService::get($id);
  184. })
  185. ->filter(function($item) {
  186. return $item && isset($item['account'], $item['media_attachments']);
  187. })
  188. ->values();
  189. }
  190. public function getUserCollections(Request $request, int $id)
  191. {
  192. $user = $request->user();
  193. $pid = $user ? $user->profile_id : null;
  194. $follows = false;
  195. $visibility = ['public'];
  196. $profile = AccountService::get($id, true);
  197. if(!$profile || !isset($profile['id'])) {
  198. return response()->json([], 404);
  199. }
  200. if($pid) {
  201. $follows = FollowerService::follows($pid, $profile['id']);
  202. }
  203. if($profile['locked']) {
  204. abort_if(!$pid, 404);
  205. if(!$user->is_admin) {
  206. abort_if($profile['id'] != $pid && $follows == false, 404);
  207. }
  208. }
  209. $owner = $pid ? $pid == $profile['id'] : false;
  210. if($follows) {
  211. $visibility = ['public', 'private'];
  212. }
  213. if($pid && $pid == $profile['id']) {
  214. $visibility = ['public', 'private', 'draft'];
  215. }
  216. return Collection::whereProfileId($profile['id'])
  217. ->whereIn('visibility', $visibility)
  218. ->when(!$owner, function($q, $owner) {
  219. return $q->whereNotNull('published_at');
  220. })
  221. ->orderByDesc('id')
  222. ->paginate(9)
  223. ->map(function($collection) {
  224. return CollectionService::getCollection($collection->id);
  225. });
  226. }
  227. public function deleteId(Request $request)
  228. {
  229. $this->validate($request, [
  230. 'collection_id' => 'required|int|min:1|exists:collections,id',
  231. 'post_id' => 'required|int|min:1|exists:statuses,id'
  232. ]);
  233. $profileId = Auth::user()->profile_id;
  234. $collectionId = $request->input('collection_id');
  235. $postId = $request->input('post_id');
  236. $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
  237. $count = $collection->items()->count();
  238. if($count == 1) {
  239. abort(400, 'You cannot delete the only post of a collection!');
  240. }
  241. $status = Status::whereScope('public')
  242. ->whereIn('type', ['photo', 'photo:album', 'video'])
  243. ->findOrFail($postId);
  244. CollectionService::removeItem(
  245. $collection->id,
  246. $status->id
  247. );
  248. $item = CollectionItem::whereCollectionId($collection->id)
  249. ->whereObjectType('App\Status')
  250. ->whereObjectId($status->id)
  251. ->firstOrFail();
  252. $item->delete();
  253. return 200;
  254. }
  255. }