InternalApiController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. foreach($medias as $k => $media) {
  52. $m = Media::findOrFail($media['id']);
  53. if($m->profile_id !== $profile->id || $m->status_id) {
  54. abort(403, 'Invalid media id');
  55. }
  56. $m->filter_class = $media['filter'];
  57. $m->license = $media['license'];
  58. $m->caption = strip_tags($media['alt']);
  59. $m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
  60. if($media['cw'] == true) {
  61. $m->is_nsfw = true;
  62. $status->is_nsfw = true;
  63. }
  64. $m->save();
  65. $attachments[] = $m;
  66. array_push($mimes, $m->mime);
  67. }
  68. $status->caption = strip_tags($request->caption);
  69. $status->visibility = 'draft';
  70. $status->scope = 'draft';
  71. $status->profile_id = $profile->id;
  72. $status->save();
  73. foreach($attachments as $media) {
  74. $media->status_id = $status->id;
  75. $media->save();
  76. }
  77. $status->visibility = $visibility;
  78. $status->scope = $visibility;
  79. $status->type = StatusController::mimeTypeCheck($mimes);
  80. $status->save();
  81. NewStatusPipeline::dispatch($status);
  82. return $status->url();
  83. }
  84. // deprecated
  85. public function discover(Request $request)
  86. {
  87. $profile = Auth::user()->profile;
  88. $pid = $profile->id;
  89. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  90. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  91. });
  92. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  93. return UserFilter::whereUserId($pid)
  94. ->whereFilterableType('App\Profile')
  95. ->whereIn('filter_type', ['mute', 'block'])
  96. ->pluck('filterable_id')->toArray();
  97. });
  98. $following = array_merge($following, $filters);
  99. $people = Profile::select('id', 'name', 'username')
  100. ->with('avatar')
  101. ->orderByRaw('rand()')
  102. ->whereHas('statuses')
  103. ->whereNull('domain')
  104. ->whereNotIn('id', $following)
  105. ->whereIsPrivate(false)
  106. ->take(3)
  107. ->get();
  108. $posts = Status::select('id', 'caption', 'profile_id')
  109. ->whereHas('media')
  110. ->whereIsNsfw(false)
  111. ->whereVisibility('public')
  112. ->whereNotIn('profile_id', $following)
  113. ->with('media')
  114. ->orderBy('created_at', 'desc')
  115. ->take(21)
  116. ->get();
  117. $res = [
  118. 'people' => $people->map(function($profile) {
  119. return [
  120. 'id' => $profile->id,
  121. 'avatar' => $profile->avatarUrl(),
  122. 'name' => $profile->name,
  123. 'username' => $profile->username,
  124. 'url' => $profile->url(),
  125. ];
  126. }),
  127. 'posts' => $posts->map(function($post) {
  128. return [
  129. 'url' => $post->url(),
  130. 'thumb' => $post->thumb(),
  131. ];
  132. })
  133. ];
  134. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  135. }
  136. public function discoverPeople(Request $request)
  137. {
  138. $profile = Auth::user()->profile;
  139. $pid = $profile->id;
  140. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  141. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  142. });
  143. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  144. return UserFilter::whereUserId($pid)
  145. ->whereFilterableType('App\Profile')
  146. ->whereIn('filter_type', ['mute', 'block'])
  147. ->pluck('filterable_id')->toArray();
  148. });
  149. $following = array_merge($following, $filters);
  150. $people = Profile::select('id', 'name', 'username')
  151. ->with('avatar')
  152. ->orderByRaw('rand()')
  153. ->whereHas('statuses')
  154. ->whereNull('domain')
  155. ->whereNotIn('id', $following)
  156. ->whereIsPrivate(false)
  157. ->take(3)
  158. ->get();
  159. $res = [
  160. 'people' => $people->map(function($profile) {
  161. return [
  162. 'id' => $profile->id,
  163. 'avatar' => $profile->avatarUrl(),
  164. 'name' => $profile->name,
  165. 'username' => $profile->username,
  166. 'url' => $profile->url(),
  167. ];
  168. })
  169. ];
  170. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  171. }
  172. public function discoverPosts(Request $request)
  173. {
  174. $profile = Auth::user()->profile;
  175. $pid = $profile->id;
  176. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  177. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  178. });
  179. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  180. return UserFilter::whereUserId($pid)
  181. ->whereFilterableType('App\Profile')
  182. ->whereIn('filter_type', ['mute', 'block'])
  183. ->pluck('filterable_id')->toArray();
  184. });
  185. $following = array_merge($following, $filters);
  186. $posts = Status::select('id', 'caption', 'profile_id')
  187. ->whereHas('media')
  188. ->whereIsNsfw(false)
  189. ->whereVisibility('public')
  190. ->whereNotIn('profile_id', $following)
  191. ->with('media')
  192. ->orderBy('created_at', 'desc')
  193. ->take(21)
  194. ->get();
  195. $res = [
  196. 'posts' => $posts->map(function($post) {
  197. return [
  198. 'url' => $post->url(),
  199. 'thumb' => $post->thumb(),
  200. ];
  201. })
  202. ];
  203. return response()->json($res);
  204. }
  205. public function directMessage(Request $request, $profileId, $threadId)
  206. {
  207. $profile = Auth::user()->profile;
  208. if($profileId != $profile->id) {
  209. abort(403);
  210. }
  211. $msg = DirectMessage::whereToId($profile->id)
  212. ->orWhere('from_id',$profile->id)
  213. ->findOrFail($threadId);
  214. $thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
  215. ->whereIn('from_id', [$profile->id,$msg->from_id])
  216. ->orderBy('created_at', 'asc')
  217. ->paginate(30);
  218. return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
  219. }
  220. public function notificationMarkAllRead(Request $request)
  221. {
  222. $profile = Auth::user()->profile;
  223. $notifications = Notification::whereProfileId($profile->id)->get();
  224. foreach($notifications as $n) {
  225. $n->read_at = Carbon::now();
  226. $n->save();
  227. }
  228. return;
  229. }
  230. public function statusReplies(Request $request, int $id)
  231. {
  232. $parent = Status::findOrFail($id);
  233. $children = Status::whereInReplyToId($parent->id)
  234. ->orderBy('created_at', 'desc')
  235. ->take(3)
  236. ->get();
  237. $resource = new Fractal\Resource\Collection($children, new StatusTransformer());
  238. $res = $this->fractal->createData($resource)->toArray();
  239. return response()->json($res);
  240. }
  241. }