InternalApiController.php 9.4 KB

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