CollectionController.php 6.5 KB

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