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,
|
|
|
|
Notification,
|
|
|
|
Media,
|
2019-01-21 20:01:36 +00:00
|
|
|
Profile,
|
|
|
|
Status
|
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,
|
2019-10-15 03:33:42 +00:00
|
|
|
MediaDraftTransformer,
|
2018-11-05 00:09:45 +00:00
|
|
|
StatusTransformer
|
|
|
|
};
|
2018-08-28 03:07:36 +00:00
|
|
|
use League\Fractal;
|
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
|
|
|
|
};
|
2019-05-01 04:51:50 +00:00
|
|
|
use App\Services\NotificationService;
|
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)
|
|
|
|
{
|
2019-09-21 04:27:00 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2019-09-21 04:31:34 +00:00
|
|
|
$pid = $request->user()->profile_id;
|
2019-10-01 02:53:14 +00:00
|
|
|
$pg = $request->input('pg');
|
|
|
|
if($pg == true) {
|
|
|
|
$timeago = Carbon::now()->subMonths(6);
|
|
|
|
$notifications = Notification::whereProfileId($pid)
|
|
|
|
->whereDate('created_at', '>', $timeago)
|
|
|
|
->latest()
|
|
|
|
->simplePaginate(10);
|
|
|
|
$resource = new Fractal\Resource\Collection($notifications, new NotificationTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
|
|
|
} else {
|
|
|
|
$this->validate($request, [
|
|
|
|
'page' => 'nullable|integer|min:1|max:10',
|
|
|
|
'limit' => 'nullable|integer|min:1|max:40'
|
|
|
|
]);
|
|
|
|
$limit = $request->input('limit') ?? 10;
|
|
|
|
$page = $request->input('page') ?? 1;
|
|
|
|
$end = (int) $page * $limit;
|
|
|
|
$start = (int) $end - $limit;
|
|
|
|
$res = NotificationService::get($pid, $start, $end);
|
|
|
|
}
|
2018-12-11 03:36:53 +00:00
|
|
|
|
|
|
|
return response()->json($res);
|
|
|
|
}
|
|
|
|
|
2018-08-05 19:17:28 +00:00
|
|
|
public function accounts(Request $request, $id)
|
|
|
|
{
|
2019-09-21 04:27:00 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2018-08-05 19:17:28 +00:00
|
|
|
$profile = Profile::findOrFail($id);
|
2018-08-28 03:07:36 +00:00
|
|
|
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
|
2018-08-05 19:17:28 +00:00
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2018-12-09 23:02:01 +00:00
|
|
|
return response()->json($res);
|
2018-08-05 19:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function accountFollowers(Request $request, $id)
|
|
|
|
{
|
2019-09-21 04:27:00 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2018-08-05 19:17:28 +00:00
|
|
|
$profile = Profile::findOrFail($id);
|
|
|
|
$followers = $profile->followers;
|
2018-08-28 03:07:36 +00:00
|
|
|
$resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
|
2018-08-05 19:17:28 +00:00
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2018-12-09 23:02:01 +00:00
|
|
|
return response()->json($res);
|
2018-08-05 19:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function accountFollowing(Request $request, $id)
|
|
|
|
{
|
2019-09-21 04:27:00 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2018-08-05 19:17:28 +00:00
|
|
|
$profile = Profile::findOrFail($id);
|
|
|
|
$following = $profile->following;
|
2018-08-28 03:07:36 +00:00
|
|
|
$resource = new Fractal\Resource\Collection($following, new AccountTransformer());
|
2018-08-05 19:17:28 +00:00
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2018-12-09 23:02:01 +00:00
|
|
|
return response()->json($res);
|
2018-08-05 19:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function accountStatuses(Request $request, $id)
|
|
|
|
{
|
2019-09-21 04:27:00 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2019-01-21 20:01:36 +00:00
|
|
|
$this->validate($request, [
|
|
|
|
'only_media' => 'nullable',
|
|
|
|
'pinned' => 'nullable',
|
|
|
|
'exclude_replies' => 'nullable',
|
|
|
|
'max_id' => 'nullable|integer|min:1',
|
|
|
|
'since_id' => 'nullable|integer|min:1',
|
|
|
|
'min_id' => 'nullable|integer|min:1',
|
|
|
|
'limit' => 'nullable|integer|min:1|max:24'
|
|
|
|
]);
|
|
|
|
$limit = $request->limit ?? 20;
|
|
|
|
$max_id = $request->max_id ?? false;
|
|
|
|
$min_id = $request->min_id ?? false;
|
|
|
|
$since_id = $request->since_id ?? false;
|
|
|
|
$only_media = $request->only_media ?? false;
|
|
|
|
$user = Auth::user();
|
2019-08-06 03:02:39 +00:00
|
|
|
$account = Profile::whereNull('status')->findOrFail($id);
|
2019-01-21 20:01:36 +00:00
|
|
|
$statuses = $account->statuses()->getQuery();
|
|
|
|
if($only_media == true) {
|
|
|
|
$statuses = $statuses
|
|
|
|
->whereHas('media')
|
|
|
|
->whereNull('in_reply_to_id')
|
|
|
|
->whereNull('reblog_of_id');
|
|
|
|
}
|
|
|
|
if($id == $account->id && !$max_id && !$min_id && !$since_id) {
|
|
|
|
$statuses = $statuses->orderBy('id', 'desc')
|
|
|
|
->paginate($limit);
|
|
|
|
} else if($since_id) {
|
|
|
|
$statuses = $statuses->where('id', '>', $since_id)
|
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
->paginate($limit);
|
|
|
|
} else if($min_id) {
|
|
|
|
$statuses = $statuses->where('id', '>', $min_id)
|
|
|
|
->orderBy('id', 'ASC')
|
|
|
|
->paginate($limit);
|
|
|
|
} else if($max_id) {
|
|
|
|
$statuses = $statuses->where('id', '<', $max_id)
|
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
->paginate($limit);
|
2018-09-01 00:54:50 +00:00
|
|
|
} else {
|
2019-01-21 20:01:36 +00:00
|
|
|
$statuses = $statuses->whereVisibility('public')->orderBy('id', 'desc')->paginate($limit);
|
2018-09-01 00:54:50 +00:00
|
|
|
}
|
2018-08-28 03:07:36 +00:00
|
|
|
$resource = new Fractal\Resource\Collection($statuses, new StatusTransformer());
|
2018-08-05 19:17:28 +00:00
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2019-02-11 04:49:54 +00:00
|
|
|
return response()->json($res);
|
2018-08-05 19:17:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 02:38:47 +00:00
|
|
|
public function avatarUpdate(Request $request)
|
|
|
|
{
|
2019-09-21 04:27:00 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2018-08-13 02:38:47 +00:00
|
|
|
$this->validate($request, [
|
2018-12-18 07:09:36 +00:00
|
|
|
'upload' => 'required|mimes:jpeg,png,gif|max:'.config('pixelfed.max_avatar_size'),
|
2018-08-13 02:38:47 +00:00
|
|
|
]);
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2018-08-13 02:38:47 +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')->storeAs($public, $name);
|
|
|
|
|
|
|
|
$avatar = Avatar::whereProfileId($profile->id)->firstOrFail();
|
|
|
|
$opath = $avatar->media_path;
|
|
|
|
$avatar->media_path = "$public/$name";
|
|
|
|
$avatar->thumb_path = null;
|
|
|
|
$avatar->change_count = ++$avatar->change_count;
|
|
|
|
$avatar->last_processed_at = null;
|
|
|
|
$avatar->save();
|
|
|
|
|
|
|
|
Cache::forget("avatar:{$profile->id}");
|
|
|
|
AvatarOptimize::dispatch($user->profile, $currentAvatar);
|
2018-08-13 02:38:47 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'code' => 200,
|
2018-08-28 03:07:36 +00:00
|
|
|
'msg' => 'Avatar successfully updated',
|
2018-08-13 02:38:47 +00:00
|
|
|
]);
|
|
|
|
}
|
2018-10-17 18:22:35 +00:00
|
|
|
|
2019-10-12 05:24:56 +00:00
|
|
|
public function showTempMedia(Request $request, int $profileId, $mediaId, $timestamp)
|
2018-10-21 04:27:27 +00:00
|
|
|
{
|
2019-09-21 04:27:00 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2019-08-06 03:02:39 +00:00
|
|
|
abort_if(!$request->hasValidSignature(), 404);
|
|
|
|
abort_if(Auth::user()->profile_id !== $profileId, 404);
|
|
|
|
$media = Media::whereProfileId(Auth::user()->profile_id)->findOrFail($mediaId);
|
2018-10-21 04:27:27 +00:00
|
|
|
$path = storage_path('app/'.$media->media_path);
|
|
|
|
return response()->file($path);
|
|
|
|
}
|
|
|
|
|
2018-10-17 18:22:35 +00:00
|
|
|
public function uploadMedia(Request $request)
|
|
|
|
{
|
2019-09-21 04:27:00 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2018-10-17 18:22:35 +00:00
|
|
|
$this->validate($request, [
|
|
|
|
'file.*' => function() {
|
|
|
|
return [
|
|
|
|
'required',
|
|
|
|
'mimes:' . config('pixelfed.media_types'),
|
|
|
|
'max:' . config('pixelfed.max_photo_size'),
|
|
|
|
];
|
|
|
|
},
|
2019-02-25 07:03:51 +00:00
|
|
|
'filter_name' => 'nullable|string|max:24',
|
|
|
|
'filter_class' => 'nullable|alpha_dash|max:24'
|
2018-10-17 18:22:35 +00:00
|
|
|
]);
|
2018-10-21 04:27:27 +00:00
|
|
|
|
2018-10-17 18:22:35 +00:00
|
|
|
$user = Auth::user();
|
|
|
|
$profile = $user->profile;
|
2018-10-21 04:27:27 +00:00
|
|
|
|
|
|
|
if(config('pixelfed.enforce_account_limit') == true) {
|
2019-09-06 05:09:17 +00:00
|
|
|
$size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) {
|
|
|
|
return Media::whereUserId($user->id)->sum('size') / 1000;
|
|
|
|
});
|
2018-10-21 04:27:27 +00:00
|
|
|
$limit = (int) config('pixelfed.max_account_size');
|
|
|
|
if ($size >= $limit) {
|
|
|
|
abort(403, 'Account size limit reached.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 18:22:35 +00:00
|
|
|
$monthHash = hash('sha1', date('Y').date('m'));
|
2019-08-20 01:06:22 +00:00
|
|
|
$userHash = hash('sha1', $user->id . (string) $user->created_at);
|
2018-10-21 04:27:27 +00:00
|
|
|
|
2018-10-17 18:22:35 +00:00
|
|
|
$photo = $request->file('file');
|
|
|
|
|
2018-12-03 01:42:55 +00:00
|
|
|
$mimes = explode(',', config('pixelfed.media_types'));
|
|
|
|
if(in_array($photo->getMimeType(), $mimes) == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-17 18:22:35 +00:00
|
|
|
$storagePath = "public/m/{$monthHash}/{$userHash}";
|
|
|
|
$path = $photo->store($storagePath);
|
|
|
|
$hash = \hash_file('sha256', $photo);
|
|
|
|
|
|
|
|
$media = new Media();
|
|
|
|
$media->status_id = null;
|
|
|
|
$media->profile_id = $profile->id;
|
|
|
|
$media->user_id = $user->id;
|
|
|
|
$media->media_path = $path;
|
|
|
|
$media->original_sha256 = $hash;
|
2018-12-03 01:42:55 +00:00
|
|
|
$media->size = $photo->getSize();
|
|
|
|
$media->mime = $photo->getMimeType();
|
2019-02-26 02:04:59 +00:00
|
|
|
$media->filter_class = $request->input('filter_class');
|
2019-02-25 07:03:51 +00:00
|
|
|
$media->filter_name = $request->input('filter_name');
|
2018-10-17 18:22:35 +00:00
|
|
|
$media->save();
|
|
|
|
|
2018-10-21 04:27:27 +00:00
|
|
|
$url = URL::temporarySignedRoute(
|
2019-10-12 05:24:56 +00:00
|
|
|
'temp-media', now()->addHours(1), ['profileId' => $profile->id, 'mediaId' => $media->id, 'timestamp' => time()]
|
2018-10-21 04:27:27 +00:00
|
|
|
);
|
2018-11-05 00:09:45 +00:00
|
|
|
|
|
|
|
switch ($media->mime) {
|
|
|
|
case 'image/jpeg':
|
|
|
|
case 'image/png':
|
|
|
|
ImageOptimize::dispatch($media);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'video/mp4':
|
|
|
|
VideoThumbnail::dispatch($media);
|
2019-05-27 00:40:09 +00:00
|
|
|
$preview_url = '/storage/no-preview.png';
|
|
|
|
$url = '/storage/no-preview.png';
|
2018-11-05 00:09:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-10-21 04:27:27 +00:00
|
|
|
|
2019-02-25 07:03:51 +00:00
|
|
|
$resource = new Fractal\Resource\Item($media, new MediaTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
2019-10-12 05:24:56 +00:00
|
|
|
$res['preview_url'] = $url;
|
2019-02-25 07:03:51 +00:00
|
|
|
$res['url'] = $url;
|
2018-10-17 18:22:35 +00:00
|
|
|
return response()->json($res);
|
|
|
|
}
|
2018-12-09 23:02:01 +00:00
|
|
|
|
2019-02-28 04:11:56 +00:00
|
|
|
public function deleteMedia(Request $request)
|
|
|
|
{
|
2019-09-21 04:27:00 +00:00
|
|
|
abort_if(!$request->user(), 403);
|
2019-02-28 04:11:56 +00:00
|
|
|
$this->validate($request, [
|
|
|
|
'id' => 'required|integer|min:1|exists:media,id'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$media = Media::whereNull('status_id')
|
|
|
|
->whereUserId(Auth::id())
|
|
|
|
->findOrFail($request->input('id'));
|
|
|
|
|
|
|
|
Storage::delete($media->media_path);
|
|
|
|
Storage::delete($media->thumbnail_path);
|
|
|
|
|
|
|
|
$media->forceDelete();
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'msg' => 'Successfully deleted',
|
|
|
|
'code' => 200
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-12-09 23:02:01 +00:00
|
|
|
public function verifyCredentials(Request $request)
|
|
|
|
{
|
2019-10-15 03:33:42 +00:00
|
|
|
$user = $request->user();
|
|
|
|
abort_if(!$user, 403);
|
|
|
|
$id = $user->id;
|
2019-04-30 02:37:27 +00:00
|
|
|
|
|
|
|
$res = Cache::remember('user:account:id:'.$id, now()->addHours(6), function() use($id) {
|
|
|
|
$profile = Profile::whereNull('status')->whereUserId($id)->firstOrFail();
|
|
|
|
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
|
2019-09-24 01:08:45 +00:00
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
|
|
|
return $res;
|
2019-04-30 02:37:27 +00:00
|
|
|
});
|
2018-12-09 23:02:01 +00:00
|
|
|
|
|
|
|
return response()->json($res);
|
|
|
|
}
|
2019-10-15 03:33:42 +00:00
|
|
|
|
|
|
|
public function drafts(Request $request)
|
|
|
|
{
|
|
|
|
$user = $request->user();
|
|
|
|
abort_if(!$request->user(), 403);
|
|
|
|
|
|
|
|
$medias = Media::whereUserId($user->id)
|
|
|
|
->whereNull('status_id')
|
|
|
|
->latest()
|
|
|
|
->take(13)
|
|
|
|
->get();
|
|
|
|
$resource = new Fractal\Resource\Collection($medias, new MediaDraftTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
|
|
|
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
|
|
|
|
}
|
2018-08-28 03:07:36 +00:00
|
|
|
}
|