InternalApiController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. public function notifications(Request $request)
  85. {
  86. $this->validate($request, [
  87. 'page' => 'nullable|min:1|max:3',
  88. ]);
  89. $profile = Auth::user()->profile;
  90. $timeago = Carbon::now()->subMonths(6);
  91. $notifications = Notification::with('actor')
  92. ->whereProfileId($profile->id)
  93. ->whereDate('created_at', '>', $timeago)
  94. ->orderBy('id', 'desc')
  95. ->simplePaginate(30);
  96. $notifications = $notifications->map(function($k, $v) {
  97. return [
  98. 'id' => $k->id,
  99. 'action' => $k->action,
  100. 'message' => $k->message,
  101. 'rendered' => $k->rendered,
  102. 'actor' => [
  103. 'avatar' => $k->actor->avatarUrl(),
  104. 'username' => $k->actor->username,
  105. 'url' => $k->actor->url(),
  106. ],
  107. 'url' => $k->item->url(),
  108. 'read_at' => $k->read_at,
  109. ];
  110. });
  111. return response()->json($notifications, 200, [], JSON_PRETTY_PRINT);
  112. }
  113. // deprecated
  114. public function discover(Request $request)
  115. {
  116. $profile = Auth::user()->profile;
  117. $pid = $profile->id;
  118. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  119. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  120. });
  121. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  122. return UserFilter::whereUserId($pid)
  123. ->whereFilterableType('App\Profile')
  124. ->whereIn('filter_type', ['mute', 'block'])
  125. ->pluck('filterable_id')->toArray();
  126. });
  127. $following = array_merge($following, $filters);
  128. $people = Profile::select('id', 'name', 'username')
  129. ->with('avatar')
  130. ->orderByRaw('rand()')
  131. ->whereHas('statuses')
  132. ->whereNull('domain')
  133. ->whereNotIn('id', $following)
  134. ->whereIsPrivate(false)
  135. ->take(3)
  136. ->get();
  137. $posts = Status::select('id', 'caption', 'profile_id')
  138. ->whereHas('media')
  139. ->whereIsNsfw(false)
  140. ->whereVisibility('public')
  141. ->whereNotIn('profile_id', $following)
  142. ->with('media')
  143. ->orderBy('created_at', 'desc')
  144. ->take(21)
  145. ->get();
  146. $res = [
  147. 'people' => $people->map(function($profile) {
  148. return [
  149. 'id' => $profile->id,
  150. 'avatar' => $profile->avatarUrl(),
  151. 'name' => $profile->name,
  152. 'username' => $profile->username,
  153. 'url' => $profile->url(),
  154. ];
  155. }),
  156. 'posts' => $posts->map(function($post) {
  157. return [
  158. 'url' => $post->url(),
  159. 'thumb' => $post->thumb(),
  160. ];
  161. })
  162. ];
  163. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  164. }
  165. public function discoverPeople(Request $request)
  166. {
  167. $profile = Auth::user()->profile;
  168. $pid = $profile->id;
  169. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  170. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  171. });
  172. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  173. return UserFilter::whereUserId($pid)
  174. ->whereFilterableType('App\Profile')
  175. ->whereIn('filter_type', ['mute', 'block'])
  176. ->pluck('filterable_id')->toArray();
  177. });
  178. $following = array_merge($following, $filters);
  179. $people = Profile::select('id', 'name', 'username')
  180. ->with('avatar')
  181. ->orderByRaw('rand()')
  182. ->whereHas('statuses')
  183. ->whereNull('domain')
  184. ->whereNotIn('id', $following)
  185. ->whereIsPrivate(false)
  186. ->take(3)
  187. ->get();
  188. $res = [
  189. 'people' => $people->map(function($profile) {
  190. return [
  191. 'id' => $profile->id,
  192. 'avatar' => $profile->avatarUrl(),
  193. 'name' => $profile->name,
  194. 'username' => $profile->username,
  195. 'url' => $profile->url(),
  196. ];
  197. })
  198. ];
  199. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  200. }
  201. public function discoverPosts(Request $request)
  202. {
  203. $profile = Auth::user()->profile;
  204. $pid = $profile->id;
  205. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  206. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  207. });
  208. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  209. return UserFilter::whereUserId($pid)
  210. ->whereFilterableType('App\Profile')
  211. ->whereIn('filter_type', ['mute', 'block'])
  212. ->pluck('filterable_id')->toArray();
  213. });
  214. $following = array_merge($following, $filters);
  215. $posts = Status::select('id', 'caption', 'profile_id')
  216. ->whereHas('media')
  217. ->whereIsNsfw(false)
  218. ->whereVisibility('public')
  219. ->whereNotIn('profile_id', $following)
  220. ->with('media')
  221. ->orderBy('created_at', 'desc')
  222. ->take(21)
  223. ->get();
  224. $res = [
  225. 'posts' => $posts->map(function($post) {
  226. return [
  227. 'url' => $post->url(),
  228. 'thumb' => $post->thumb(),
  229. ];
  230. })
  231. ];
  232. return response()->json($res);
  233. }
  234. public function directMessage(Request $request, $profileId, $threadId)
  235. {
  236. $profile = Auth::user()->profile;
  237. if($profileId != $profile->id) {
  238. abort(403);
  239. }
  240. $msg = DirectMessage::whereToId($profile->id)
  241. ->orWhere('from_id',$profile->id)
  242. ->findOrFail($threadId);
  243. $thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
  244. ->whereIn('from_id', [$profile->id,$msg->from_id])
  245. ->orderBy('created_at', 'asc')
  246. ->paginate(30);
  247. return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
  248. }
  249. public function notificationMarkAllRead(Request $request)
  250. {
  251. $profile = Auth::user()->profile;
  252. $notifications = Notification::whereProfileId($profile->id)->get();
  253. foreach($notifications as $n) {
  254. $n->read_at = Carbon::now();
  255. $n->save();
  256. }
  257. return;
  258. }
  259. }