1
0

BaseApiController.php 10.0 KB

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