pixelfed/app/Http/Controllers/Api/BaseApiController.php

247 lines
6.9 KiB
PHP
Raw Normal View History

2018-08-05 19:17:28 +00:00
<?php
namespace App\Http\Controllers\Api;
2018-08-28 03:07:36 +00:00
use Illuminate\Http\Request;
2018-11-05 00:09:45 +00:00
use App\Http\Controllers\{
Controller,
AvatarController
};
2019-02-28 04:11:56 +00:00
use Auth, Cache, Storage, URL;
2018-12-11 03:36:53 +00:00
use Carbon\Carbon;
2018-12-03 01:42:55 +00:00
use App\{
Avatar,
Like,
2018-12-03 01:42:55 +00:00
Media,
Notification,
2019-01-21 20:01:36 +00:00
Profile,
2021-07-27 04:49:46 +00:00
Status,
StatusArchived
2018-12-03 01:42:55 +00:00
};
2018-11-05 00:09:45 +00:00
use App\Transformer\Api\{
AccountTransformer,
2018-12-03 01:42:55 +00:00
NotificationTransformer,
2018-11-05 00:09:45 +00:00
MediaTransformer,
MediaDraftTransformer,
StatusTransformer,
StatusStatelessTransformer
2018-11-05 00:09:45 +00:00
};
2018-08-28 03:07:36 +00:00
use League\Fractal;
2020-01-30 06:04:25 +00:00
use App\Util\Media\Filter;
2018-08-05 19:17:28 +00:00
use League\Fractal\Serializer\ArraySerializer;
2019-01-21 20:01:36 +00:00
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
2018-11-05 00:09:45 +00:00
use App\Jobs\AvatarPipeline\AvatarOptimize;
use App\Jobs\ImageOptimizePipeline\ImageOptimize;
use App\Jobs\VideoPipeline\{
VideoOptimize,
VideoPostProcess,
VideoThumbnail
};
use App\Services\AccountService;
2019-05-01 04:51:50 +00:00
use App\Services\NotificationService;
use App\Services\MediaPathService;
use App\Services\MediaBlocklistService;
2021-07-27 04:49:46 +00:00
use App\Services\StatusService;
2018-08-05 19:17:28 +00:00
class BaseApiController extends Controller
{
protected $fractal;
public function __construct()
{
2019-09-21 04:27:00 +00:00
// $this->middleware('auth');
2018-08-05 19:17:28 +00:00
$this->fractal = new Fractal\Manager();
$this->fractal->setSerializer(new ArraySerializer());
}
2018-12-11 03:36:53 +00:00
public function notifications(Request $request)
{
2024-02-10 03:41:12 +00:00
abort_if(!$request->user(), 403);
$pid = $request->user()->profile_id;
$limit = $request->input('limit', 20);
$since = $request->input('since_id');
$min = $request->input('min_id');
$max = $request->input('max_id');
if(!$since && !$min && !$max) {
$min = 1;
}
$maxId = null;
$minId = null;
if($max) {
$res = NotificationService::getMax($pid, $max, $limit);
$ids = NotificationService::getRankedMaxId($pid, $max, $limit);
if(!empty($ids)) {
$maxId = max($ids);
$minId = min($ids);
}
} else {
$res = NotificationService::getMin($pid, $min ?? $since, $limit);
$ids = NotificationService::getRankedMinId($pid, $min ?? $since, $limit);
if(!empty($ids)) {
$maxId = max($ids);
$minId = min($ids);
}
}
if(empty($res) && !Cache::has('pf:services:notifications:hasSynced:'.$pid)) {
Cache::put('pf:services:notifications:hasSynced:'.$pid, 1, 1209600);
NotificationService::warmCache($pid, 100, true);
2019-10-01 02:53:14 +00:00
}
2018-12-11 03:36:53 +00:00
return response()->json($res);
}
public function avatarUpdate(Request $request)
{
2024-02-10 03:41:12 +00:00
abort_if(!$request->user(), 403);
$this->validate($request, [
2022-02-10 02:57:12 +00:00
'upload' => 'required|mimetypes:image/jpeg,image/jpg,image/png|max:'.config('pixelfed.max_avatar_size'),
]);
2018-08-28 03:07:36 +00:00
try {
2018-08-28 03:07:36 +00:00
$user = Auth::user();
$profile = $user->profile;
$file = $request->file('upload');
$path = (new AvatarController())->getPath($user, $file);
$dir = $path['root'];
$name = $path['name'];
$public = $path['storage'];
$currentAvatar = storage_path('app/'.$profile->avatar->media_path);
$loc = $request->file('upload')->storePubliclyAs($public, $name);
2018-08-28 03:07:36 +00:00
$avatar = Avatar::whereProfileId($profile->id)->firstOrFail();
$opath = $avatar->media_path;
$avatar->media_path = "$public/$name";
$avatar->change_count = ++$avatar->change_count;
$avatar->last_processed_at = null;
$avatar->save();
Cache::forget("avatar:{$profile->id}");
AvatarOptimize::dispatch($user->profile, $currentAvatar);
} catch (Exception $e) {
}
return response()->json([
'code' => 200,
2018-08-28 03:07:36 +00:00
'msg' => 'Avatar successfully updated',
]);
}
2018-10-17 18:22:35 +00:00
2018-12-09 23:02:01 +00:00
public function verifyCredentials(Request $request)
{
2024-02-10 03:41:12 +00:00
abort_if(!$request->user(), 403);
$user = $request->user();
if ($user->status != null) {
Auth::logout();
abort(403);
}
$res = AccountService::get($user->profile_id);
2018-12-09 23:02:01 +00:00
return response()->json($res);
}
public function accountLikes(Request $request)
{
2024-02-10 03:41:12 +00:00
abort_if(!$request->user(), 403);
$this->validate($request, [
'page' => 'sometimes|int|min:1|max:20',
'limit' => 'sometimes|int|min:1|max:10'
]);
$user = $request->user();
$limit = $request->input('limit', 10);
$res = \DB::table('likes')
->whereProfileId($user->profile_id)
->latest()
->simplePaginate($limit)
->map(function($id) {
$status = StatusService::get($id->status_id, false);
$status['favourited'] = true;
return $status;
})
->filter(function($post) {
return $post && isset($post['account']);
})
->values();
return response()->json($res);
}
2021-07-27 04:49:46 +00:00
public function archive(Request $request, $id)
{
2024-02-10 03:41:12 +00:00
abort_if(!$request->user(), 403);
2021-07-27 04:49:46 +00:00
$status = Status::whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereProfileId($request->user()->profile_id)
->findOrFail($id);
if($status->scope === 'archived') {
return [200];
}
$archive = new StatusArchived;
$archive->status_id = $status->id;
$archive->profile_id = $status->profile_id;
$archive->original_scope = $status->scope;
$archive->save();
$status->scope = 'archived';
$status->visibility = 'draft';
$status->save();
StatusService::del($status->id, true);
AccountService::syncPostCount($status->profile_id);
2021-07-27 04:49:46 +00:00
return [200];
}
public function unarchive(Request $request, $id)
{
2024-02-10 03:41:12 +00:00
abort_if(!$request->user(), 403);
2021-07-27 04:49:46 +00:00
$status = Status::whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereProfileId($request->user()->profile_id)
->findOrFail($id);
if($status->scope !== 'archived') {
return [200];
}
$archive = StatusArchived::whereStatusId($status->id)
->whereProfileId($status->profile_id)
->firstOrFail();
$status->scope = $archive->original_scope;
$status->visibility = $archive->original_scope;
$status->save();
$archive->delete();
StatusService::del($status->id, true);
AccountService::syncPostCount($status->profile_id);
2021-07-27 04:49:46 +00:00
return [200];
}
public function archivedPosts(Request $request)
{
2024-02-10 03:41:12 +00:00
abort_if(!$request->user(), 403);
2021-07-27 04:49:46 +00:00
$statuses = Status::whereProfileId($request->user()->profile_id)
->whereScope('archived')
->orderByDesc('id')
->simplePaginate(10);
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Collection($statuses, new StatusStatelessTransformer());
return $fractal->createData($resource)->toArray();
}
2018-08-28 03:07:36 +00:00
}