BaseApiController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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, Storage, URL;
  9. use Carbon\Carbon;
  10. use App\{
  11. Avatar,
  12. Like,
  13. Media,
  14. Notification,
  15. Profile,
  16. Status,
  17. StatusArchived
  18. };
  19. use App\Transformer\Api\{
  20. AccountTransformer,
  21. NotificationTransformer,
  22. MediaTransformer,
  23. MediaDraftTransformer,
  24. StatusTransformer,
  25. StatusStatelessTransformer
  26. };
  27. use League\Fractal;
  28. use App\Util\Media\Filter;
  29. use League\Fractal\Serializer\ArraySerializer;
  30. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  31. use App\Jobs\AvatarPipeline\AvatarOptimize;
  32. use App\Jobs\ImageOptimizePipeline\ImageOptimize;
  33. use App\Jobs\VideoPipeline\{
  34. VideoOptimize,
  35. VideoPostProcess,
  36. VideoThumbnail
  37. };
  38. use App\Services\AccountService;
  39. use App\Services\NotificationService;
  40. use App\Services\MediaPathService;
  41. use App\Services\MediaBlocklistService;
  42. use App\Services\StatusService;
  43. class BaseApiController extends Controller
  44. {
  45. protected $fractal;
  46. public function __construct()
  47. {
  48. // $this->middleware('auth');
  49. $this->fractal = new Fractal\Manager();
  50. $this->fractal->setSerializer(new ArraySerializer());
  51. }
  52. public function notifications(Request $request)
  53. {
  54. abort_if(!$request->user(), 403);
  55. $pid = $request->user()->profile_id;
  56. $limit = $request->input('limit', 20);
  57. $since = $request->input('since_id');
  58. $min = $request->input('min_id');
  59. $max = $request->input('max_id');
  60. if(!$since && !$min && !$max) {
  61. $min = 1;
  62. }
  63. $maxId = null;
  64. $minId = null;
  65. if($max) {
  66. $res = NotificationService::getMax($pid, $max, $limit);
  67. $ids = NotificationService::getRankedMaxId($pid, $max, $limit);
  68. if(!empty($ids)) {
  69. $maxId = max($ids);
  70. $minId = min($ids);
  71. }
  72. } else {
  73. $res = NotificationService::getMin($pid, $min ?? $since, $limit);
  74. $ids = NotificationService::getRankedMinId($pid, $min ?? $since, $limit);
  75. if(!empty($ids)) {
  76. $maxId = max($ids);
  77. $minId = min($ids);
  78. }
  79. }
  80. if(empty($res) && !Cache::has('pf:services:notifications:hasSynced:'.$pid)) {
  81. Cache::put('pf:services:notifications:hasSynced:'.$pid, 1, 1209600);
  82. NotificationService::warmCache($pid, 100, true);
  83. }
  84. return response()->json($res);
  85. }
  86. public function avatarUpdate(Request $request)
  87. {
  88. abort_if(!$request->user(), 403);
  89. $this->validate($request, [
  90. 'upload' => 'required|mimetypes:image/jpeg,image/jpg,image/png|max:'.config('pixelfed.max_avatar_size'),
  91. ]);
  92. try {
  93. $user = Auth::user();
  94. $profile = $user->profile;
  95. $file = $request->file('upload');
  96. $path = (new AvatarController())->getPath($user, $file);
  97. $dir = $path['root'];
  98. $name = $path['name'];
  99. $public = $path['storage'];
  100. $currentAvatar = storage_path('app/'.$profile->avatar->media_path);
  101. $loc = $request->file('upload')->storePubliclyAs($public, $name);
  102. $avatar = Avatar::whereProfileId($profile->id)->firstOrFail();
  103. $opath = $avatar->media_path;
  104. $avatar->media_path = "$public/$name";
  105. $avatar->change_count = ++$avatar->change_count;
  106. $avatar->last_processed_at = null;
  107. $avatar->save();
  108. Cache::forget("avatar:{$profile->id}");
  109. AvatarOptimize::dispatch($user->profile, $currentAvatar);
  110. } catch (Exception $e) {
  111. }
  112. return response()->json([
  113. 'code' => 200,
  114. 'msg' => 'Avatar successfully updated',
  115. ]);
  116. }
  117. public function verifyCredentials(Request $request)
  118. {
  119. abort_if(!$request->user(), 403);
  120. $user = $request->user();
  121. if ($user->status != null) {
  122. Auth::logout();
  123. abort(403);
  124. }
  125. $res = AccountService::get($user->profile_id);
  126. return response()->json($res);
  127. }
  128. public function accountLikes(Request $request)
  129. {
  130. abort_if(!$request->user(), 403);
  131. $this->validate($request, [
  132. 'page' => 'sometimes|int|min:1|max:20',
  133. 'limit' => 'sometimes|int|min:1|max:10'
  134. ]);
  135. $user = $request->user();
  136. $limit = $request->input('limit', 10);
  137. $res = \DB::table('likes')
  138. ->whereProfileId($user->profile_id)
  139. ->latest()
  140. ->simplePaginate($limit)
  141. ->map(function($id) {
  142. $status = StatusService::get($id->status_id, false);
  143. $status['favourited'] = true;
  144. return $status;
  145. })
  146. ->filter(function($post) {
  147. return $post && isset($post['account']);
  148. })
  149. ->values();
  150. return response()->json($res);
  151. }
  152. public function archive(Request $request, $id)
  153. {
  154. abort_if(!$request->user(), 403);
  155. $status = Status::whereNull('in_reply_to_id')
  156. ->whereNull('reblog_of_id')
  157. ->whereProfileId($request->user()->profile_id)
  158. ->findOrFail($id);
  159. if($status->scope === 'archived') {
  160. return [200];
  161. }
  162. $archive = new StatusArchived;
  163. $archive->status_id = $status->id;
  164. $archive->profile_id = $status->profile_id;
  165. $archive->original_scope = $status->scope;
  166. $archive->save();
  167. $status->scope = 'archived';
  168. $status->visibility = 'draft';
  169. $status->save();
  170. StatusService::del($status->id, true);
  171. AccountService::syncPostCount($status->profile_id);
  172. return [200];
  173. }
  174. public function unarchive(Request $request, $id)
  175. {
  176. abort_if(!$request->user(), 403);
  177. $status = Status::whereNull('in_reply_to_id')
  178. ->whereNull('reblog_of_id')
  179. ->whereProfileId($request->user()->profile_id)
  180. ->findOrFail($id);
  181. if($status->scope !== 'archived') {
  182. return [200];
  183. }
  184. $archive = StatusArchived::whereStatusId($status->id)
  185. ->whereProfileId($status->profile_id)
  186. ->firstOrFail();
  187. $status->scope = $archive->original_scope;
  188. $status->visibility = $archive->original_scope;
  189. $status->save();
  190. $archive->delete();
  191. StatusService::del($status->id, true);
  192. AccountService::syncPostCount($status->profile_id);
  193. return [200];
  194. }
  195. public function archivedPosts(Request $request)
  196. {
  197. abort_if(!$request->user(), 403);
  198. $statuses = Status::whereProfileId($request->user()->profile_id)
  199. ->whereScope('archived')
  200. ->orderByDesc('id')
  201. ->simplePaginate(10);
  202. $fractal = new Fractal\Manager();
  203. $fractal->setSerializer(new ArraySerializer());
  204. $resource = new Fractal\Resource\Collection($statuses, new StatusStatelessTransformer());
  205. return $fractal->createData($resource)->toArray();
  206. }
  207. }