CollectionController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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\StatusService;
  19. class CollectionController extends Controller
  20. {
  21. public function create(Request $request)
  22. {
  23. abort_if(!Auth::check(), 403);
  24. $profile = Auth::user()->profile;
  25. $collection = Collection::firstOrCreate([
  26. 'profile_id' => $profile->id,
  27. 'published_at' => null
  28. ]);
  29. return view('collection.create', compact('collection'));
  30. }
  31. public function show(Request $request, int $collection)
  32. {
  33. $collection = Collection::with('profile')->whereNotNull('published_at')->findOrFail($collection);
  34. if($collection->profile->status != null) {
  35. abort(404);
  36. }
  37. if($collection->visibility !== 'public') {
  38. abort_if(!Auth::check() || Auth::user()->profile_id != $collection->profile_id, 404);
  39. }
  40. return view('collection.show', compact('collection'));
  41. }
  42. public function index(Request $request)
  43. {
  44. abort_if(!Auth::check(), 403);
  45. return $request->all();
  46. }
  47. public function store(Request $request, $id)
  48. {
  49. abort_if(!Auth::check(), 403);
  50. $this->validate($request, [
  51. 'title' => 'nullable',
  52. 'description' => 'nullable',
  53. 'visibility' => 'nullable|string|in:public,private'
  54. ]);
  55. $profile = Auth::user()->profile;
  56. $collection = Collection::whereProfileId($profile->id)->findOrFail($id);
  57. $collection->title = e($request->input('title'));
  58. $collection->description = e($request->input('description'));
  59. $collection->visibility = e($request->input('visibility'));
  60. $collection->save();
  61. return 200;
  62. }
  63. public function publish(Request $request, int $id)
  64. {
  65. abort_if(!Auth::check(), 403);
  66. $this->validate($request, [
  67. 'title' => 'nullable',
  68. 'description' => 'nullable',
  69. 'visibility' => 'required|alpha|in:public,private'
  70. ]);
  71. $profile = Auth::user()->profile;
  72. $collection = Collection::whereProfileId($profile->id)->findOrFail($id);
  73. if($collection->items()->count() == 0) {
  74. abort(404);
  75. }
  76. $collection->title = e($request->input('title'));
  77. $collection->description = e($request->input('description'));
  78. $collection->visibility = e($request->input('visibility'));
  79. $collection->published_at = now();
  80. $collection->save();
  81. return $collection->url();
  82. }
  83. public function delete(Request $request, int $id)
  84. {
  85. abort_if(!Auth::check(), 403);
  86. $user = Auth::user();
  87. $collection = Collection::whereProfileId($user->profile_id)->findOrFail($id);
  88. $collection->items()->delete();
  89. $collection->delete();
  90. if($request->wantsJson()) {
  91. return 200;
  92. }
  93. return redirect('/');
  94. }
  95. public function storeId(Request $request)
  96. {
  97. $this->validate($request, [
  98. 'collection_id' => 'required|int|min:1|exists:collections,id',
  99. 'post_id' => 'required|int|min:1|exists:statuses,id'
  100. ]);
  101. $profileId = Auth::user()->profile_id;
  102. $collectionId = $request->input('collection_id');
  103. $postId = $request->input('post_id');
  104. $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
  105. $count = $collection->items()->count();
  106. $max = config('pixelfed.max_collection_length');
  107. if($count >= $max) {
  108. abort(400, 'You can only add '.$max.' posts per collection');
  109. }
  110. $status = Status::whereScope('public')
  111. ->whereIn('type', ['photo', 'photo:album', 'video'])
  112. ->findOrFail($postId);
  113. $item = CollectionItem::firstOrCreate([
  114. 'collection_id' => $collection->id,
  115. 'object_type' => 'App\Status',
  116. 'object_id' => $status->id
  117. ],[
  118. 'order' => $count,
  119. ]);
  120. return 200;
  121. }
  122. public function get(Request $request, $id)
  123. {
  124. $profile = Auth::check() ? Auth::user()->profile : [];
  125. $collection = Collection::whereVisibility('public')->findOrFail($id);
  126. if($collection->published_at == null) {
  127. if(!Auth::check() || $profile->id !== $collection->profile_id) {
  128. abort(404);
  129. }
  130. }
  131. return [
  132. 'id' => $collection->id,
  133. 'title' => $collection->title,
  134. 'description' => $collection->description,
  135. 'visibility' => $collection->visibility
  136. ];
  137. }
  138. public function getItems(Request $request, int $id)
  139. {
  140. $collection = Collection::findOrFail($id);
  141. if($collection->visibility !== 'public') {
  142. abort_if(!Auth::check() || Auth::user()->profile_id != $collection->profile_id, 404);
  143. }
  144. $res = CollectionItem::whereCollectionId($id)
  145. ->pluck('object_id')
  146. ->map(function($id) {
  147. return StatusService::get($id);
  148. })
  149. ->filter(function($post) {
  150. return $post && isset($post['account']);
  151. })
  152. ->values();
  153. return response()->json($res);
  154. }
  155. public function getUserCollections(Request $request, int $id)
  156. {
  157. $profile = Profile::whereNull('status')
  158. ->whereNull('domain')
  159. ->findOrFail($id);
  160. if($profile->is_private) {
  161. abort_if(!Auth::check(), 404);
  162. abort_if(!$profile->followedBy(Auth::user()->profile) && $profile->id != Auth::user()->profile_id, 404);
  163. }
  164. return $profile
  165. ->collections()
  166. ->has('posts')
  167. ->with('posts')
  168. ->whereVisibility('public')
  169. ->whereNotNull('published_at')
  170. ->orderByDesc('published_at')
  171. ->paginate(9)
  172. ->map(function($collection) {
  173. return [
  174. 'id' => (string) $collection->id,
  175. 'title' => $collection->title,
  176. 'description' => $collection->description,
  177. 'thumb' => $collection->posts()->first()->thumb(),
  178. 'url' => $collection->url(),
  179. 'post_count' => $collection->posts()->count(),
  180. 'published_at' => $collection->published_at
  181. ];
  182. });
  183. }
  184. public function deleteId(Request $request)
  185. {
  186. $this->validate($request, [
  187. 'collection_id' => 'required|int|min:1|exists:collections,id',
  188. 'post_id' => 'required|int|min:1|exists:statuses,id'
  189. ]);
  190. $profileId = Auth::user()->profile_id;
  191. $collectionId = $request->input('collection_id');
  192. $postId = $request->input('post_id');
  193. $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
  194. $count = $collection->items()->count();
  195. if($count == 1) {
  196. abort(400, 'You cannot delete the only post of a collection!');
  197. }
  198. $status = Status::whereScope('public')
  199. ->whereIn('type', ['photo', 'photo:album', 'video'])
  200. ->findOrFail($postId);
  201. $item = CollectionItem::whereCollectionId($collection->id)
  202. ->whereObjectType('App\Status')
  203. ->whereObjectId($status->id)
  204. ->firstOrFail();
  205. $item->delete();
  206. return 200;
  207. }
  208. }