InternalApiController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\{
  5. DirectMessage,
  6. Hashtag,
  7. Follower,
  8. Like,
  9. Media,
  10. Notification,
  11. Profile,
  12. StatusHashtag,
  13. Status,
  14. UserFilter,
  15. };
  16. use Auth,Cache;
  17. use Carbon\Carbon;
  18. use League\Fractal;
  19. use App\Transformer\Api\{
  20. AccountTransformer,
  21. StatusTransformer,
  22. };
  23. use App\Jobs\StatusPipeline\NewStatusPipeline;
  24. use League\Fractal\Serializer\ArraySerializer;
  25. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  26. class InternalApiController extends Controller
  27. {
  28. protected $fractal;
  29. public function __construct()
  30. {
  31. $this->middleware('auth');
  32. $this->fractal = new Fractal\Manager();
  33. $this->fractal->setSerializer(new ArraySerializer());
  34. }
  35. public function compose(Request $request)
  36. {
  37. $this->validate($request, [
  38. 'caption' => 'nullable|string',
  39. 'media.*' => 'required',
  40. 'media.*.id' => 'required|integer|min:1',
  41. 'media.*.filter' => 'nullable|string|max:30',
  42. 'media.*.license' => 'nullable|string|max:80',
  43. 'visibility' => 'required|string|in:public,private|min:2|max:10'
  44. ]);
  45. $profile = Auth::user()->profile;
  46. $visibility = $request->input('visibility');
  47. $medias = $request->input('media');
  48. $attachments = [];
  49. $status = new Status;
  50. $mimes = [];
  51. $cw = false;
  52. foreach($medias as $k => $media) {
  53. $m = Media::findOrFail($media['id']);
  54. if($m->profile_id !== $profile->id || $m->status_id) {
  55. abort(403, 'Invalid media id');
  56. }
  57. $m->filter_class = $media['filter'];
  58. $m->license = $media['license'];
  59. $m->caption = strip_tags($media['alt']);
  60. $m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
  61. if($media['cw'] == true || $profile->cw == true) {
  62. $cw = true;
  63. $m->is_nsfw = true;
  64. $status->is_nsfw = true;
  65. }
  66. $m->save();
  67. $attachments[] = $m;
  68. array_push($mimes, $m->mime);
  69. }
  70. $status->caption = strip_tags($request->caption);
  71. $status->visibility = 'draft';
  72. $status->scope = 'draft';
  73. $status->profile_id = $profile->id;
  74. $status->save();
  75. foreach($attachments as $media) {
  76. $media->status_id = $status->id;
  77. $media->save();
  78. }
  79. $visibility = $profile->unlisted == true && $visibility == 'public' ? 'unlisted' : $visibility;
  80. $cw = $profile->cw == true ? true : $cw;
  81. $status->is_nsfw = $cw;
  82. $status->visibility = $visibility;
  83. $status->scope = $visibility;
  84. $status->type = StatusController::mimeTypeCheck($mimes);
  85. $status->save();
  86. NewStatusPipeline::dispatch($status);
  87. return $status->url();
  88. }
  89. // deprecated
  90. public function discover(Request $request)
  91. {
  92. $profile = Auth::user()->profile;
  93. $pid = $profile->id;
  94. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  95. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  96. });
  97. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  98. return UserFilter::whereUserId($pid)
  99. ->whereFilterableType('App\Profile')
  100. ->whereIn('filter_type', ['mute', 'block'])
  101. ->pluck('filterable_id')->toArray();
  102. });
  103. $following = array_merge($following, $filters);
  104. $people = Profile::select('id', 'name', 'username')
  105. ->with('avatar')
  106. ->whereNull('status')
  107. ->orderByRaw('rand()')
  108. ->whereHas('statuses')
  109. ->whereNull('domain')
  110. ->whereNotIn('id', $following)
  111. ->whereIsPrivate(false)
  112. ->take(3)
  113. ->get();
  114. $posts = Status::select('id', 'caption', 'profile_id')
  115. ->whereHas('media')
  116. ->whereIsNsfw(false)
  117. ->whereVisibility('public')
  118. ->whereNotIn('profile_id', $following)
  119. ->with('media')
  120. ->orderBy('created_at', 'desc')
  121. ->take(21)
  122. ->get();
  123. $res = [
  124. 'people' => $people->map(function($profile) {
  125. return [
  126. 'id' => $profile->id,
  127. 'avatar' => $profile->avatarUrl(),
  128. 'name' => $profile->name,
  129. 'username' => $profile->username,
  130. 'url' => $profile->url(),
  131. ];
  132. }),
  133. 'posts' => $posts->map(function($post) {
  134. return [
  135. 'url' => $post->url(),
  136. 'thumb' => $post->thumb(),
  137. ];
  138. })
  139. ];
  140. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  141. }
  142. public function discoverPeople(Request $request)
  143. {
  144. $profile = Auth::user()->profile;
  145. $pid = $profile->id;
  146. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  147. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  148. });
  149. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  150. return UserFilter::whereUserId($pid)
  151. ->whereFilterableType('App\Profile')
  152. ->whereIn('filter_type', ['mute', 'block'])
  153. ->pluck('filterable_id')->toArray();
  154. });
  155. $following = array_merge($following, $filters);
  156. $people = Profile::select('id', 'name', 'username')
  157. ->with('avatar')
  158. ->orderByRaw('rand()')
  159. ->whereHas('statuses')
  160. ->whereNull('domain')
  161. ->whereNotIn('id', $following)
  162. ->whereIsPrivate(false)
  163. ->take(3)
  164. ->get();
  165. $res = [
  166. 'people' => $people->map(function($profile) {
  167. return [
  168. 'id' => $profile->id,
  169. 'avatar' => $profile->avatarUrl(),
  170. 'name' => $profile->name,
  171. 'username' => $profile->username,
  172. 'url' => $profile->url(),
  173. ];
  174. })
  175. ];
  176. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  177. }
  178. public function discoverPosts(Request $request)
  179. {
  180. $profile = Auth::user()->profile;
  181. $pid = $profile->id;
  182. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  183. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  184. });
  185. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  186. return UserFilter::whereUserId($pid)
  187. ->whereFilterableType('App\Profile')
  188. ->whereIn('filter_type', ['mute', 'block'])
  189. ->pluck('filterable_id')->toArray();
  190. });
  191. $following = array_merge($following, $filters);
  192. $posts = Status::select('id', 'caption', 'profile_id')
  193. ->whereHas('media')
  194. ->whereHas('profile', function($q) {
  195. return $q->whereNull('status');
  196. })
  197. ->whereIsNsfw(false)
  198. ->whereVisibility('public')
  199. ->whereNotIn('profile_id', $following)
  200. ->with('media')
  201. ->orderBy('created_at', 'desc')
  202. ->take(21)
  203. ->get();
  204. $res = [
  205. 'posts' => $posts->map(function($post) {
  206. return [
  207. 'url' => $post->url(),
  208. 'thumb' => $post->thumb(),
  209. ];
  210. })
  211. ];
  212. return response()->json($res);
  213. }
  214. public function directMessage(Request $request, $profileId, $threadId)
  215. {
  216. $profile = Auth::user()->profile;
  217. if($profileId != $profile->id) {
  218. abort(403);
  219. }
  220. $msg = DirectMessage::whereToId($profile->id)
  221. ->orWhere('from_id',$profile->id)
  222. ->findOrFail($threadId);
  223. $thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
  224. ->whereIn('from_id', [$profile->id,$msg->from_id])
  225. ->orderBy('created_at', 'asc')
  226. ->paginate(30);
  227. return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
  228. }
  229. public function notificationMarkAllRead(Request $request)
  230. {
  231. $profile = Auth::user()->profile;
  232. $notifications = Notification::whereProfileId($profile->id)->get();
  233. foreach($notifications as $n) {
  234. $n->read_at = Carbon::now();
  235. $n->save();
  236. }
  237. return;
  238. }
  239. public function statusReplies(Request $request, int $id)
  240. {
  241. $parent = Status::findOrFail($id);
  242. $children = Status::whereInReplyToId($parent->id)
  243. ->orderBy('created_at', 'desc')
  244. ->take(3)
  245. ->get();
  246. $resource = new Fractal\Resource\Collection($children, new StatusTransformer());
  247. $res = $this->fractal->createData($resource)->toArray();
  248. return response()->json($res);
  249. }
  250. }