InternalApiController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\{
  5. DirectMessage,
  6. Hashtag,
  7. Like,
  8. Media,
  9. Notification,
  10. Profile,
  11. StatusHashtag,
  12. Status,
  13. };
  14. use Auth,Cache;
  15. use Carbon\Carbon;
  16. use League\Fractal;
  17. use App\Transformer\Api\{
  18. AccountTransformer,
  19. StatusTransformer,
  20. };
  21. use App\Jobs\StatusPipeline\NewStatusPipeline;
  22. use League\Fractal\Serializer\ArraySerializer;
  23. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  24. class InternalApiController extends Controller
  25. {
  26. protected $fractal;
  27. public function __construct()
  28. {
  29. $this->middleware('auth');
  30. $this->fractal = new Fractal\Manager();
  31. $this->fractal->setSerializer(new ArraySerializer());
  32. }
  33. public function compose(Request $request)
  34. {
  35. $this->validate($request, [
  36. 'caption' => 'nullable|string',
  37. 'media.*' => 'required',
  38. 'media.*.id' => 'required|integer|min:1',
  39. 'media.*.filter' => 'nullable|string|max:30',
  40. 'media.*.license' => 'nullable|string|max:80',
  41. 'visibility' => 'required|string|in:public,private|min:2|max:10'
  42. ]);
  43. $profile = Auth::user()->profile;
  44. $visibility = $request->input('visibility');
  45. $medias = $request->input('media');
  46. $attachments = [];
  47. $status = new Status;
  48. foreach($medias as $k => $media) {
  49. $m = Media::findOrFail($media['id']);
  50. if($m->profile_id !== $profile->id || $m->status_id) {
  51. abort(403, 'Invalid media id');
  52. }
  53. $m->filter_class = $media['filter'];
  54. $m->license = $media['license'];
  55. $m->caption = strip_tags($media['alt']);
  56. $m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
  57. if($media['cw'] == true) {
  58. $m->is_nsfw = true;
  59. $status->is_nsfw = true;
  60. }
  61. $m->save();
  62. $attachments[] = $m;
  63. }
  64. $status->caption = strip_tags($request->caption);
  65. $status->visibility = 'draft';
  66. $status->scope = 'draft';
  67. $status->profile_id = $profile->id;
  68. $status->save();
  69. foreach($attachments as $media) {
  70. $media->status_id = $status->id;
  71. $media->save();
  72. }
  73. $status->visibility = $visibility;
  74. $status->scope = $visibility;
  75. $status->save();
  76. NewStatusPipeline::dispatch($status);
  77. return $status->url();
  78. }
  79. public function notifications(Request $request)
  80. {
  81. $this->validate($request, [
  82. 'page' => 'nullable|min:1|max:3',
  83. ]);
  84. $profile = Auth::user()->profile;
  85. $timeago = Carbon::now()->subMonths(6);
  86. $notifications = Notification::with('actor')
  87. ->whereProfileId($profile->id)
  88. ->whereDate('created_at', '>', $timeago)
  89. ->orderBy('id', 'desc')
  90. ->simplePaginate(30);
  91. $notifications = $notifications->map(function($k, $v) {
  92. return [
  93. 'id' => $k->id,
  94. 'action' => $k->action,
  95. 'message' => $k->message,
  96. 'rendered' => $k->rendered,
  97. 'actor' => [
  98. 'avatar' => $k->actor->avatarUrl(),
  99. 'username' => $k->actor->username,
  100. 'url' => $k->actor->url(),
  101. ],
  102. 'url' => $k->item->url(),
  103. 'read_at' => $k->read_at,
  104. ];
  105. });
  106. return response()->json($notifications, 200, [], JSON_PRETTY_PRINT);
  107. }
  108. public function discover(Request $request)
  109. {
  110. $profile = Auth::user()->profile;
  111. $following = Cache::get('feature:discover:following:'.$profile->id, []);
  112. $people = Profile::select('id', 'name', 'username')
  113. ->with('avatar')
  114. ->inRandomOrder()
  115. ->whereHas('statuses')
  116. ->whereNull('domain')
  117. ->whereNotIn('id', $following)
  118. ->whereIsPrivate(false)
  119. ->take(3)
  120. ->get();
  121. $posts = Status::select('id', 'caption', 'profile_id')
  122. ->whereHas('media')
  123. ->whereHas('profile', function($q) {
  124. $q->where('is_private', false);
  125. })
  126. ->whereIsNsfw(false)
  127. ->whereVisibility('public')
  128. ->where('profile_id', '<>', $profile->id)
  129. ->whereNotIn('profile_id', $following)
  130. ->withCount(['comments', 'likes'])
  131. ->orderBy('created_at', 'desc')
  132. ->take(21)
  133. ->get();
  134. $res = [
  135. 'people' => $people->map(function($profile) {
  136. return [
  137. 'avatar' => $profile->avatarUrl(),
  138. 'name' => $profile->name,
  139. 'username' => $profile->username,
  140. 'url' => $profile->url(),
  141. ];
  142. }),
  143. 'posts' => $posts->map(function($post) {
  144. return [
  145. 'url' => $post->url(),
  146. 'thumb' => $post->thumb(),
  147. 'comments_count' => $post->comments_count,
  148. 'likes_count' => $post->likes_count,
  149. ];
  150. })
  151. ];
  152. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  153. }
  154. public function directMessage(Request $request, $profileId, $threadId)
  155. {
  156. $profile = Auth::user()->profile;
  157. if($profileId != $profile->id) {
  158. abort(403);
  159. }
  160. $msg = DirectMessage::whereToId($profile->id)
  161. ->orWhere('from_id',$profile->id)
  162. ->findOrFail($threadId);
  163. $thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
  164. ->whereIn('from_id', [$profile->id,$msg->from_id])
  165. ->orderBy('created_at', 'asc')
  166. ->paginate(30);
  167. return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
  168. }
  169. public function notificationMarkAllRead(Request $request)
  170. {
  171. $profile = Auth::user()->profile;
  172. $notifications = Notification::whereProfileId($profile->id)->get();
  173. foreach($notifications as $n) {
  174. $n->read_at = Carbon::now();
  175. $n->save();
  176. }
  177. return;
  178. }
  179. }