BaseApiController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\{
  5. Controller,
  6. AvatarController
  7. };
  8. use Auth, Cache, URL;
  9. use Carbon\Carbon;
  10. use App\{
  11. Avatar,
  12. Notification,
  13. Media,
  14. Profile,
  15. Status
  16. };
  17. use App\Transformer\Api\{
  18. AccountTransformer,
  19. NotificationTransformer,
  20. MediaTransformer,
  21. StatusTransformer
  22. };
  23. use League\Fractal;
  24. use League\Fractal\Serializer\ArraySerializer;
  25. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  26. use App\Jobs\AvatarPipeline\AvatarOptimize;
  27. use App\Jobs\ImageOptimizePipeline\ImageOptimize;
  28. use App\Jobs\VideoPipeline\{
  29. VideoOptimize,
  30. VideoPostProcess,
  31. VideoThumbnail
  32. };
  33. class BaseApiController extends Controller
  34. {
  35. protected $fractal;
  36. public function __construct()
  37. {
  38. $this->middleware('auth');
  39. $this->fractal = new Fractal\Manager();
  40. $this->fractal->setSerializer(new ArraySerializer());
  41. }
  42. public function notification(Request $request, $id)
  43. {
  44. $notification = Notification::findOrFail($id);
  45. $resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
  46. $res = $this->fractal->createData($resource)->toArray();
  47. return response()->json($res);
  48. }
  49. public function notifications(Request $request)
  50. {
  51. $pid = Auth::user()->profile->id;
  52. $timeago = Carbon::now()->subMonths(6);
  53. $notifications = Notification::with('actor')
  54. ->whereProfileId($pid)
  55. ->whereDate('created_at', '>', $timeago)
  56. ->orderBy('created_at','desc')
  57. ->paginate(10);
  58. $resource = new Fractal\Resource\Collection($notifications, new NotificationTransformer());
  59. $res = $this->fractal->createData($resource)->toArray();
  60. return response()->json($res);
  61. }
  62. public function accounts(Request $request, $id)
  63. {
  64. $profile = Profile::findOrFail($id);
  65. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  66. $res = $this->fractal->createData($resource)->toArray();
  67. return response()->json($res);
  68. }
  69. public function accountFollowers(Request $request, $id)
  70. {
  71. $profile = Profile::findOrFail($id);
  72. $followers = $profile->followers;
  73. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  74. $res = $this->fractal->createData($resource)->toArray();
  75. return response()->json($res);
  76. }
  77. public function accountFollowing(Request $request, $id)
  78. {
  79. $profile = Profile::findOrFail($id);
  80. $following = $profile->following;
  81. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  82. $res = $this->fractal->createData($resource)->toArray();
  83. return response()->json($res);
  84. }
  85. public function accountStatuses(Request $request, $id)
  86. {
  87. $this->validate($request, [
  88. 'only_media' => 'nullable',
  89. 'pinned' => 'nullable',
  90. 'exclude_replies' => 'nullable',
  91. 'max_id' => 'nullable|integer|min:1',
  92. 'since_id' => 'nullable|integer|min:1',
  93. 'min_id' => 'nullable|integer|min:1',
  94. 'limit' => 'nullable|integer|min:1|max:24'
  95. ]);
  96. $limit = $request->limit ?? 20;
  97. $max_id = $request->max_id ?? false;
  98. $min_id = $request->min_id ?? false;
  99. $since_id = $request->since_id ?? false;
  100. $only_media = $request->only_media ?? false;
  101. $user = Auth::user();
  102. $account = Profile::findOrFail($id);
  103. $statuses = $account->statuses()->getQuery();
  104. if($only_media == true) {
  105. $statuses = $statuses
  106. ->whereHas('media')
  107. ->whereNull('in_reply_to_id')
  108. ->whereNull('reblog_of_id');
  109. }
  110. if($id == $account->id && !$max_id && !$min_id && !$since_id) {
  111. $statuses = $statuses->orderBy('id', 'desc')
  112. ->paginate($limit);
  113. } else if($since_id) {
  114. $statuses = $statuses->where('id', '>', $since_id)
  115. ->orderBy('id', 'DESC')
  116. ->paginate($limit);
  117. } else if($min_id) {
  118. $statuses = $statuses->where('id', '>', $min_id)
  119. ->orderBy('id', 'ASC')
  120. ->paginate($limit);
  121. } else if($max_id) {
  122. $statuses = $statuses->where('id', '<', $max_id)
  123. ->orderBy('id', 'DESC')
  124. ->paginate($limit);
  125. } else {
  126. $statuses = $statuses->whereVisibility('public')->orderBy('id', 'desc')->paginate($limit);
  127. }
  128. $resource = new Fractal\Resource\Collection($statuses, new StatusTransformer());
  129. $res = $this->fractal->createData($resource)->toArray();
  130. return response()->json($res);
  131. }
  132. public function followSuggestions(Request $request)
  133. {
  134. $followers = Auth::user()->profile->recommendFollowers();
  135. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  136. $res = $this->fractal->createData($resource)->toArray();
  137. return response()->json($res);
  138. }
  139. public function avatarUpdate(Request $request)
  140. {
  141. $this->validate($request, [
  142. 'upload' => 'required|mimes:jpeg,png,gif|max:'.config('pixelfed.max_avatar_size'),
  143. ]);
  144. try {
  145. $user = Auth::user();
  146. $profile = $user->profile;
  147. $file = $request->file('upload');
  148. $path = (new AvatarController())->getPath($user, $file);
  149. $dir = $path['root'];
  150. $name = $path['name'];
  151. $public = $path['storage'];
  152. $currentAvatar = storage_path('app/'.$profile->avatar->media_path);
  153. $loc = $request->file('upload')->storeAs($public, $name);
  154. $avatar = Avatar::whereProfileId($profile->id)->firstOrFail();
  155. $opath = $avatar->media_path;
  156. $avatar->media_path = "$public/$name";
  157. $avatar->thumb_path = null;
  158. $avatar->change_count = ++$avatar->change_count;
  159. $avatar->last_processed_at = null;
  160. $avatar->save();
  161. Cache::forget("avatar:{$profile->id}");
  162. AvatarOptimize::dispatch($user->profile, $currentAvatar);
  163. } catch (Exception $e) {
  164. }
  165. return response()->json([
  166. 'code' => 200,
  167. 'msg' => 'Avatar successfully updated',
  168. ]);
  169. }
  170. public function showTempMedia(Request $request, int $profileId, $mediaId)
  171. {
  172. if (!$request->hasValidSignature()) {
  173. abort(401);
  174. }
  175. $profile = Auth::user()->profile;
  176. if($profile->id !== $profileId) {
  177. abort(403);
  178. }
  179. $media = Media::whereProfileId($profile->id)->findOrFail($mediaId);
  180. $path = storage_path('app/'.$media->media_path);
  181. return response()->file($path);
  182. }
  183. public function uploadMedia(Request $request)
  184. {
  185. $this->validate($request, [
  186. 'file.*' => function() {
  187. return [
  188. 'required',
  189. 'mimes:' . config('pixelfed.media_types'),
  190. 'max:' . config('pixelfed.max_photo_size'),
  191. ];
  192. },
  193. ]);
  194. $user = Auth::user();
  195. $profile = $user->profile;
  196. if(config('pixelfed.enforce_account_limit') == true) {
  197. $size = Media::whereUserId($user->id)->sum('size') / 1000;
  198. $limit = (int) config('pixelfed.max_account_size');
  199. if ($size >= $limit) {
  200. abort(403, 'Account size limit reached.');
  201. }
  202. }
  203. $recent = Media::whereProfileId($profile->id)->whereNull('status_id')->count();
  204. if($recent > 50) {
  205. abort(403);
  206. }
  207. $monthHash = hash('sha1', date('Y').date('m'));
  208. $userHash = hash('sha1', $user->id.(string) $user->created_at);
  209. $photo = $request->file('file');
  210. $mimes = explode(',', config('pixelfed.media_types'));
  211. if(in_array($photo->getMimeType(), $mimes) == false) {
  212. return;
  213. }
  214. $storagePath = "public/m/{$monthHash}/{$userHash}";
  215. $path = $photo->store($storagePath);
  216. $hash = \hash_file('sha256', $photo);
  217. $media = new Media();
  218. $media->status_id = null;
  219. $media->profile_id = $profile->id;
  220. $media->user_id = $user->id;
  221. $media->media_path = $path;
  222. $media->original_sha256 = $hash;
  223. $media->size = $photo->getSize();
  224. $media->mime = $photo->getMimeType();
  225. $media->filter_class = null;
  226. $media->filter_name = null;
  227. $media->save();
  228. $url = URL::temporarySignedRoute(
  229. 'temp-media', now()->addHours(1), ['profileId' => $profile->id, 'mediaId' => $media->id]
  230. );
  231. switch ($media->mime) {
  232. case 'image/jpeg':
  233. case 'image/png':
  234. ImageOptimize::dispatch($media);
  235. break;
  236. case 'video/mp4':
  237. VideoThumbnail::dispatch($media);
  238. break;
  239. default:
  240. break;
  241. }
  242. $res = [
  243. 'id' => $media->id,
  244. 'type' => $media->activityVerb(),
  245. 'url' => $url,
  246. 'remote_url' => null,
  247. 'preview_url' => $url,
  248. 'text_url' => null,
  249. 'meta' => $media->metadata,
  250. 'description' => null,
  251. ];
  252. return response()->json($res);
  253. }
  254. public function verifyCredentials(Request $request)
  255. {
  256. $profile = Auth::user()->profile;
  257. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  258. $res = $this->fractal->createData($resource)->toArray();
  259. return response()->json($res);
  260. }
  261. }