InternalApiController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. public function discover(Request $request)
  111. {
  112. $profile = Auth::user()->profile;
  113. $pid = $profile->id;
  114. //$following = Cache::get('feature:discover:following:'.$profile->id, []);
  115. $following = Follower::whereProfileId($pid)->pluck('following_id');
  116. $filtered = UserFilter::whereUserId($pid)
  117. ->whereFilterableType('App\Profile')
  118. ->whereIn('filter_type', ['mute', 'block'])
  119. ->pluck('filterable_id')->toArray();
  120. $following = array_merge($following->push($pid)->toArray(), $filtered);
  121. $people = Profile::select('id', 'name', 'username')
  122. ->with('avatar')
  123. ->inRandomOrder()
  124. ->whereHas('statuses')
  125. ->whereNull('domain')
  126. ->whereNotIn('id', $following)
  127. ->whereIsPrivate(false)
  128. ->take(3)
  129. ->get();
  130. $posts = Status::select('id', 'caption', 'profile_id')
  131. ->whereHas('media')
  132. ->whereHas('profile', function($q) {
  133. $q->where('is_private', false);
  134. })
  135. ->whereIsNsfw(false)
  136. ->whereVisibility('public')
  137. ->whereNotIn('profile_id', $following)
  138. ->withCount(['comments', 'likes'])
  139. ->orderBy('created_at', 'desc')
  140. ->take(21)
  141. ->get();
  142. $res = [
  143. 'people' => $people->map(function($profile) {
  144. return [
  145. 'avatar' => $profile->avatarUrl(),
  146. 'name' => $profile->name,
  147. 'username' => $profile->username,
  148. 'url' => $profile->url(),
  149. ];
  150. }),
  151. 'posts' => $posts->map(function($post) {
  152. return [
  153. 'url' => $post->url(),
  154. 'thumb' => $post->thumb(),
  155. 'comments_count' => $post->comments_count,
  156. 'likes_count' => $post->likes_count,
  157. ];
  158. })
  159. ];
  160. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  161. }
  162. public function directMessage(Request $request, $profileId, $threadId)
  163. {
  164. $profile = Auth::user()->profile;
  165. if($profileId != $profile->id) {
  166. abort(403);
  167. }
  168. $msg = DirectMessage::whereToId($profile->id)
  169. ->orWhere('from_id',$profile->id)
  170. ->findOrFail($threadId);
  171. $thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
  172. ->whereIn('from_id', [$profile->id,$msg->from_id])
  173. ->orderBy('created_at', 'asc')
  174. ->paginate(30);
  175. return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
  176. }
  177. public function notificationMarkAllRead(Request $request)
  178. {
  179. $profile = Auth::user()->profile;
  180. $notifications = Notification::whereProfileId($profile->id)->get();
  181. foreach($notifications as $n) {
  182. $n->read_at = Carbon::now();
  183. $n->save();
  184. }
  185. return;
  186. }
  187. }