1
0

CollectionController.php 11 KB

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