2023-05-11 10:06:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2024-07-29 09:53:24 +00:00
|
|
|
use App\Jobs\ImageOptimizePipeline\ImageOptimize;
|
|
|
|
use App\Jobs\MediaPipeline\MediaDeletePipeline;
|
|
|
|
use App\Jobs\VideoPipeline\VideoThumbnail;
|
2023-05-11 10:06:27 +00:00
|
|
|
use App\Media;
|
|
|
|
use App\Services\AccountService;
|
|
|
|
use App\Services\InstanceService;
|
|
|
|
use App\Services\MediaBlocklistService;
|
|
|
|
use App\Services\MediaPathService;
|
|
|
|
use App\Services\SearchApiV2Service;
|
2024-07-29 09:53:24 +00:00
|
|
|
use App\Services\UserRoleService;
|
|
|
|
use App\Services\UserStorageService;
|
|
|
|
use App\Transformer\Api\Mastodon\v1\MediaTransformer;
|
|
|
|
use App\User;
|
|
|
|
use App\UserSetting;
|
2023-05-11 10:06:27 +00:00
|
|
|
use App\Util\Media\Filter;
|
2024-07-29 09:53:24 +00:00
|
|
|
use App\Util\Site\Nodeinfo;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2023-05-11 10:06:27 +00:00
|
|
|
use League\Fractal;
|
|
|
|
use League\Fractal\Serializer\ArraySerializer;
|
|
|
|
|
|
|
|
class ApiV2Controller extends Controller
|
|
|
|
{
|
2024-07-29 09:53:24 +00:00
|
|
|
const PF_API_ENTITY_KEY = '_pe';
|
2023-05-11 10:06:27 +00:00
|
|
|
|
2024-01-11 11:50:11 +00:00
|
|
|
public function json($res, $code = 200, $headers = [])
|
|
|
|
{
|
|
|
|
return response()->json($res, $code, $headers, JSON_UNESCAPED_SLASHES);
|
|
|
|
}
|
2023-05-11 10:06:27 +00:00
|
|
|
|
|
|
|
public function instance(Request $request)
|
|
|
|
{
|
2024-01-11 11:50:11 +00:00
|
|
|
$contact = Cache::remember('api:v1:instance-data:contact', 604800, function () {
|
2024-07-29 09:53:24 +00:00
|
|
|
if (config_cache('instance.admin.pid')) {
|
2024-01-11 11:50:11 +00:00
|
|
|
return AccountService::getMastodon(config_cache('instance.admin.pid'), true);
|
|
|
|
}
|
|
|
|
$admin = User::whereIsAdmin(true)->first();
|
2024-07-29 09:53:24 +00:00
|
|
|
|
2024-01-11 11:50:11 +00:00
|
|
|
return $admin && isset($admin->profile_id) ?
|
|
|
|
AccountService::getMastodon($admin->profile_id, true) :
|
|
|
|
null;
|
|
|
|
});
|
|
|
|
|
|
|
|
$rules = Cache::remember('api:v1:instance-data:rules', 604800, function () {
|
|
|
|
return config_cache('app.rules') ?
|
|
|
|
collect(json_decode(config_cache('app.rules'), true))
|
2024-07-29 09:53:24 +00:00
|
|
|
->map(function ($rule, $key) {
|
|
|
|
$id = $key + 1;
|
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => "{$id}",
|
|
|
|
'text' => $rule,
|
|
|
|
];
|
|
|
|
})
|
|
|
|
->toArray() : [];
|
2024-01-11 11:50:11 +00:00
|
|
|
});
|
|
|
|
|
2024-07-29 09:53:24 +00:00
|
|
|
$res = Cache::remember('api:v2:instance-data-response-v2', 1800, function () use ($contact, $rules) {
|
2024-02-19 05:14:23 +00:00
|
|
|
return [
|
|
|
|
'domain' => config('pixelfed.domain.app'),
|
|
|
|
'title' => config_cache('app.name'),
|
2024-07-29 09:53:24 +00:00
|
|
|
'version' => '3.5.3 (compatible; Pixelfed '.config('pixelfed.version').')',
|
2024-02-19 05:14:23 +00:00
|
|
|
'source_url' => 'https://github.com/pixelfed/pixelfed',
|
|
|
|
'description' => config_cache('app.short_description'),
|
|
|
|
'usage' => [
|
|
|
|
'users' => [
|
2024-07-29 09:53:24 +00:00
|
|
|
'active_month' => (int) Nodeinfo::activeUsersMonthly(),
|
|
|
|
],
|
2024-01-11 11:50:11 +00:00
|
|
|
],
|
2024-02-19 05:14:23 +00:00
|
|
|
'thumbnail' => [
|
|
|
|
'url' => config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg')),
|
|
|
|
'blurhash' => InstanceService::headerBlurhash(),
|
|
|
|
'versions' => [
|
|
|
|
'@1x' => config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg')),
|
2024-07-29 09:53:24 +00:00
|
|
|
'@2x' => config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg')),
|
|
|
|
],
|
2024-02-07 12:59:12 +00:00
|
|
|
],
|
2024-02-19 05:14:23 +00:00
|
|
|
'languages' => [config('app.locale')],
|
|
|
|
'configuration' => [
|
|
|
|
'urls' => [
|
|
|
|
'streaming' => null,
|
2024-07-29 09:53:24 +00:00
|
|
|
'status' => null,
|
2024-02-19 05:14:23 +00:00
|
|
|
],
|
|
|
|
'vapid' => [
|
|
|
|
'public_key' => config('webpush.vapid.public_key'),
|
|
|
|
],
|
|
|
|
'accounts' => [
|
|
|
|
'max_featured_tags' => 0,
|
|
|
|
],
|
|
|
|
'statuses' => [
|
2024-03-10 10:19:44 +00:00
|
|
|
'max_characters' => (int) config_cache('pixelfed.max_caption_length'),
|
2024-02-19 05:14:23 +00:00
|
|
|
'max_media_attachments' => (int) config_cache('pixelfed.max_album_length'),
|
2024-07-29 09:53:24 +00:00
|
|
|
'characters_reserved_per_url' => 23,
|
2024-02-19 05:14:23 +00:00
|
|
|
],
|
|
|
|
'media_attachments' => [
|
|
|
|
'supported_mime_types' => explode(',', config_cache('pixelfed.media_types')),
|
|
|
|
'image_size_limit' => config_cache('pixelfed.max_photo_size') * 1024,
|
|
|
|
'image_matrix_limit' => 3686400,
|
|
|
|
'video_size_limit' => config_cache('pixelfed.max_photo_size') * 1024,
|
|
|
|
'video_frame_rate_limit' => 240,
|
2024-07-29 09:53:24 +00:00
|
|
|
'video_matrix_limit' => 3686400,
|
2024-02-19 05:14:23 +00:00
|
|
|
],
|
|
|
|
'polls' => [
|
|
|
|
'max_options' => 0,
|
|
|
|
'max_characters_per_option' => 0,
|
|
|
|
'min_expiration' => 0,
|
|
|
|
'max_expiration' => 0,
|
|
|
|
],
|
|
|
|
'translation' => [
|
|
|
|
'enabled' => false,
|
|
|
|
],
|
2024-01-11 11:50:11 +00:00
|
|
|
],
|
2024-02-19 05:14:23 +00:00
|
|
|
'registrations' => [
|
|
|
|
'enabled' => null,
|
|
|
|
'approval_required' => false,
|
|
|
|
'message' => null,
|
|
|
|
'url' => null,
|
2024-01-11 11:50:11 +00:00
|
|
|
],
|
2024-02-19 05:14:23 +00:00
|
|
|
'contact' => [
|
|
|
|
'email' => config('instance.email'),
|
2024-07-29 09:53:24 +00:00
|
|
|
'account' => $contact,
|
2024-01-11 11:50:11 +00:00
|
|
|
],
|
2024-07-29 09:53:24 +00:00
|
|
|
'rules' => $rules,
|
2024-02-19 05:14:23 +00:00
|
|
|
];
|
|
|
|
});
|
2024-01-11 11:50:11 +00:00
|
|
|
|
2024-02-19 05:14:23 +00:00
|
|
|
$res['registrations']['enabled'] = (bool) config_cache('pixelfed.open_registration');
|
2024-02-19 11:00:31 +00:00
|
|
|
$res['registrations']['approval_required'] = (bool) config_cache('instance.curated_registration.enabled');
|
2024-07-29 09:53:24 +00:00
|
|
|
|
2024-01-11 11:50:11 +00:00
|
|
|
return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v2/search
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function search(Request $request)
|
|
|
|
{
|
2024-07-29 09:53:24 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2024-01-11 11:50:11 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'q' => 'required|string|min:1|max:100',
|
|
|
|
'account_id' => 'nullable|string',
|
|
|
|
'max_id' => 'nullable|string',
|
|
|
|
'min_id' => 'nullable|string',
|
|
|
|
'type' => 'nullable|in:accounts,hashtags,statuses',
|
|
|
|
'exclude_unreviewed' => 'nullable',
|
|
|
|
'resolve' => 'nullable',
|
|
|
|
'limit' => 'nullable|integer|max:40',
|
|
|
|
'offset' => 'nullable|integer',
|
2024-07-29 09:53:24 +00:00
|
|
|
'following' => 'nullable',
|
2024-01-11 11:50:11 +00:00
|
|
|
]);
|
|
|
|
|
2024-07-29 09:53:24 +00:00
|
|
|
if ($request->user()->has_roles && ! UserRoleService::can('can-view-discover', $request->user()->id)) {
|
2024-01-11 11:50:11 +00:00
|
|
|
return [
|
|
|
|
'accounts' => [],
|
|
|
|
'hashtags' => [],
|
2024-07-29 09:53:24 +00:00
|
|
|
'statuses' => [],
|
2024-01-11 11:50:11 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-07-29 09:53:24 +00:00
|
|
|
$mastodonMode = ! $request->has('_pe');
|
|
|
|
|
2024-01-11 11:50:11 +00:00
|
|
|
return $this->json(SearchApiV2Service::query($request, $mastodonMode));
|
2023-05-11 10:06:27 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 11:50:11 +00:00
|
|
|
/**
|
|
|
|
* GET /api/v2/streaming/config
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function getWebsocketConfig()
|
|
|
|
{
|
|
|
|
return config('broadcasting.default') === 'pusher' ? [
|
|
|
|
'host' => config('broadcasting.connections.pusher.options.host'),
|
|
|
|
'port' => config('broadcasting.connections.pusher.options.port'),
|
|
|
|
'key' => config('broadcasting.connections.pusher.key'),
|
2024-07-29 09:53:24 +00:00
|
|
|
'cluster' => config('broadcasting.connections.pusher.options.cluster'),
|
2024-01-11 11:50:11 +00:00
|
|
|
] : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v2/media
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return MediaTransformer
|
|
|
|
*/
|
|
|
|
public function mediaUploadV2(Request $request)
|
|
|
|
{
|
2024-07-29 09:53:24 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2024-01-11 11:50:11 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'file.*' => [
|
|
|
|
'required_without:file',
|
2024-07-29 09:53:24 +00:00
|
|
|
'mimetypes:'.config_cache('pixelfed.media_types'),
|
|
|
|
'max:'.config_cache('pixelfed.max_photo_size'),
|
2024-01-11 11:50:11 +00:00
|
|
|
],
|
|
|
|
'file' => [
|
|
|
|
'required_without:file.*',
|
2024-07-29 09:53:24 +00:00
|
|
|
'mimetypes:'.config_cache('pixelfed.media_types'),
|
|
|
|
'max:'.config_cache('pixelfed.max_photo_size'),
|
2024-01-11 11:50:11 +00:00
|
|
|
],
|
2024-07-29 09:53:24 +00:00
|
|
|
'filter_name' => 'nullable|string|max:24',
|
|
|
|
'filter_class' => 'nullable|alpha_dash|max:24',
|
|
|
|
'description' => 'nullable|string|max:'.config_cache('pixelfed.max_altext_length'),
|
|
|
|
'replace_id' => 'sometimes',
|
2024-01-11 11:50:11 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $request->user();
|
|
|
|
|
2024-07-29 09:53:24 +00:00
|
|
|
if ($user->last_active_at == null) {
|
2024-01-11 11:50:11 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-07-29 09:53:24 +00:00
|
|
|
if (empty($request->file('file'))) {
|
2024-01-11 11:50:11 +00:00
|
|
|
return response('', 422);
|
|
|
|
}
|
|
|
|
|
2024-07-29 09:53:24 +00:00
|
|
|
$limitKey = 'compose:rate-limit:media-upload:'.$user->id;
|
2024-01-11 11:50:11 +00:00
|
|
|
$limitTtl = now()->addMinutes(15);
|
2024-07-29 09:53:24 +00:00
|
|
|
$limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
|
2024-01-11 11:50:11 +00:00
|
|
|
$dailyLimit = Media::whereUserId($user->id)->where('created_at', '>', now()->subDays(1))->count();
|
|
|
|
|
|
|
|
return $dailyLimit >= 1250;
|
|
|
|
});
|
|
|
|
abort_if($limitReached == true, 429);
|
|
|
|
|
|
|
|
$profile = $user->profile;
|
|
|
|
|
2024-07-29 09:53:24 +00:00
|
|
|
$accountSize = UserStorageService::get($user->id);
|
|
|
|
abort_if($accountSize === -1, 403, 'Invalid request.');
|
|
|
|
$photo = $request->file('file');
|
|
|
|
$fileSize = $photo->getSize();
|
|
|
|
$sizeInKbs = (int) ceil($fileSize / 1000);
|
|
|
|
$updatedAccountSize = (int) $accountSize + (int) $sizeInKbs;
|
|
|
|
|
|
|
|
if ((bool) config_cache('pixelfed.enforce_account_limit') == true) {
|
2024-01-11 11:50:11 +00:00
|
|
|
$limit = (int) config_cache('pixelfed.max_account_size');
|
2024-07-29 09:53:24 +00:00
|
|
|
if ($updatedAccountSize >= $limit) {
|
|
|
|
abort(403, 'Account size limit reached.');
|
2024-01-11 11:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null;
|
|
|
|
$filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null;
|
|
|
|
|
|
|
|
$mimes = explode(',', config_cache('pixelfed.media_types'));
|
2024-07-29 09:53:24 +00:00
|
|
|
if (in_array($photo->getMimeType(), $mimes) == false) {
|
2024-01-11 11:50:11 +00:00
|
|
|
abort(403, 'Invalid or unsupported mime type.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$storagePath = MediaPathService::get($user, 2);
|
|
|
|
$path = $photo->storePublicly($storagePath);
|
|
|
|
$hash = \hash_file('sha256', $photo);
|
|
|
|
$license = null;
|
|
|
|
$mime = $photo->getMimeType();
|
|
|
|
|
|
|
|
$settings = UserSetting::whereUserId($user->id)->first();
|
|
|
|
|
2024-07-29 09:53:24 +00:00
|
|
|
if ($settings && ! empty($settings->compose_settings)) {
|
2024-01-11 11:50:11 +00:00
|
|
|
$compose = $settings->compose_settings;
|
|
|
|
|
2024-07-29 09:53:24 +00:00
|
|
|
if (isset($compose['default_license']) && $compose['default_license'] != 1) {
|
2024-01-11 11:50:11 +00:00
|
|
|
$license = $compose['default_license'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abort_if(MediaBlocklistService::exists($hash) == true, 451);
|
|
|
|
|
2024-07-29 09:53:24 +00:00
|
|
|
if ($request->has('replace_id')) {
|
2024-01-11 11:50:11 +00:00
|
|
|
$rpid = $request->input('replace_id');
|
|
|
|
$removeMedia = Media::whereNull('status_id')
|
|
|
|
->whereUserId($user->id)
|
|
|
|
->whereProfileId($profile->id)
|
|
|
|
->where('created_at', '>', now()->subHours(2))
|
|
|
|
->find($rpid);
|
2024-07-29 09:53:24 +00:00
|
|
|
if ($removeMedia) {
|
2024-01-11 11:50:11 +00:00
|
|
|
MediaDeletePipeline::dispatch($removeMedia)
|
|
|
|
->onQueue('mmo')
|
|
|
|
->delay(now()->addMinutes(15));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
$media->size = $photo->getSize();
|
|
|
|
$media->mime = $mime;
|
|
|
|
$media->caption = $request->input('description');
|
|
|
|
$media->filter_class = $filterClass;
|
|
|
|
$media->filter_name = $filterName;
|
2024-07-29 09:53:24 +00:00
|
|
|
if ($license) {
|
2024-01-11 11:50:11 +00:00
|
|
|
$media->license = $license;
|
|
|
|
}
|
|
|
|
$media->save();
|
|
|
|
|
|
|
|
switch ($media->mime) {
|
|
|
|
case 'image/jpeg':
|
|
|
|
case 'image/png':
|
|
|
|
ImageOptimize::dispatch($media)->onQueue('mmo');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'video/mp4':
|
|
|
|
VideoThumbnail::dispatch($media)->onQueue('mmo');
|
|
|
|
$preview_url = '/storage/no-preview.png';
|
|
|
|
$url = '/storage/no-preview.png';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-07-29 09:53:24 +00:00
|
|
|
$user->storage_used = (int) $updatedAccountSize;
|
|
|
|
$user->storage_used_updated_at = now();
|
|
|
|
$user->save();
|
|
|
|
|
2024-01-11 11:50:11 +00:00
|
|
|
Cache::forget($limitKey);
|
|
|
|
$fractal = new Fractal\Manager();
|
|
|
|
$fractal->setSerializer(new ArraySerializer());
|
|
|
|
$resource = new Fractal\Resource\Item($media, new MediaTransformer());
|
|
|
|
$res = $fractal->createData($resource)->toArray();
|
2024-07-29 09:53:24 +00:00
|
|
|
$res['preview_url'] = $media->url().'?v='.time();
|
2024-01-11 11:50:11 +00:00
|
|
|
$res['url'] = null;
|
2024-07-29 09:53:24 +00:00
|
|
|
|
2024-01-11 11:50:11 +00:00
|
|
|
return $this->json($res, 202);
|
|
|
|
}
|
2023-05-11 10:06:27 +00:00
|
|
|
}
|