pixelfed/app/Http/Controllers/ComposeController.php

836 lines
29 KiB
PHP
Raw Normal View History

2021-01-24 21:11:58 +00:00
<?php
namespace App\Http\Controllers;
2024-03-10 10:19:44 +00:00
use App\Collection;
use App\CollectionItem;
use App\Hashtag;
2021-01-24 21:11:58 +00:00
use App\Jobs\ImageOptimizePipeline\ImageOptimize;
use App\Jobs\StatusPipeline\NewStatusPipeline;
2024-03-10 10:19:44 +00:00
use App\Jobs\VideoPipeline\VideoThumbnail;
use App\Media;
use App\MediaTag;
use App\Models\Poll;
use App\Notification;
use App\Profile;
use App\Services\AccountService;
use App\Services\CollectionService;
2021-01-24 21:11:58 +00:00
use App\Services\MediaBlocklistService;
2024-03-10 10:19:44 +00:00
use App\Services\MediaPathService;
use App\Services\MediaStorageService;
2021-01-24 21:11:58 +00:00
use App\Services\MediaTagService;
use App\Services\SnowflakeService;
2024-03-10 10:19:44 +00:00
use App\Services\UserRoleService;
2024-07-29 09:52:07 +00:00
use App\Services\UserStorageService;
2024-03-10 10:19:44 +00:00
use App\Status;
use App\Transformer\Api\MediaTransformer;
use App\UserFilter;
use App\Util\Media\Filter;
2021-04-01 04:08:03 +00:00
use App\Util\Media\License;
2024-03-10 10:19:44 +00:00
use Auth;
use Cache;
use DB;
2024-12-23 08:50:51 +00:00
use Purify;
2024-03-10 10:19:44 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
2021-01-24 21:11:58 +00:00
class ComposeController extends Controller
{
protected $fractal;
public function __construct()
{
$this->middleware('auth');
$this->fractal = new Fractal\Manager;
$this->fractal->setSerializer(new ArraySerializer);
}
public function show(Request $request)
{
return view('status.compose');
}
public function mediaUpload(Request $request)
{
2024-03-10 10:19:44 +00:00
abort_if(! $request->user(), 403);
$this->validate($request, [
'file.*' => [
'required_without:file',
2024-03-10 10:19:44 +00:00
'mimetypes:'.config_cache('pixelfed.media_types'),
'max:'.config_cache('pixelfed.max_photo_size'),
],
'file' => [
'required_without:file.*',
2024-03-10 10:19:44 +00:00
'mimetypes:'.config_cache('pixelfed.media_types'),
'max:'.config_cache('pixelfed.max_photo_size'),
],
'filter_name' => 'nullable|string|max:24',
2024-03-10 10:19:44 +00:00
'filter_class' => 'nullable|alpha_dash|max:24',
]);
2024-07-29 09:52:07 +00:00
$user = $request->user();
$profile = $user->profile;
2024-03-10 10:19:44 +00:00
abort_if($user->has_roles && ! UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action');
2024-03-10 10:19:44 +00:00
$limitKey = 'compose:rate-limit:media-upload:'.$user->id;
$limitTtl = now()->addMinutes(15);
2024-03-10 10:19:44 +00:00
$limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
$dailyLimit = Media::whereUserId($user->id)->where('created_at', '>', now()->subDays(1))->count();
return $dailyLimit >= 1250;
});
abort_if($limitReached == true, 429);
2024-07-29 09:52:07 +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;
$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) {
$limit = (int) config_cache('pixelfed.max_account_size');
2024-07-29 09:52:07 +00:00
if ($updatedAccountSize >= $limit) {
abort(403, 'Account size limit reached.');
}
}
$mimes = explode(',', config_cache('pixelfed.media_types'));
abort_if(in_array($photo->getMimeType(), $mimes) == false, 400, 'Invalid media format');
$storagePath = MediaPathService::get($user, 2);
$path = $photo->storePublicly($storagePath);
$hash = \hash_file('sha256', $photo);
$mime = $photo->getMimeType();
abort_if(MediaBlocklistService::exists($hash) == true, 451);
$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->caption = '';
$media->mime = $mime;
$media->filter_class = $filterClass;
$media->filter_name = $filterName;
$media->version = '3';
$media->save();
2024-03-10 10:19:44 +00:00
$preview_url = $media->url().'?v='.time();
$url = $media->url().'?v='.time();
switch ($media->mime) {
case 'image/jpeg':
case 'image/png':
case 'image/webp':
2024-03-10 10:19:44 +00:00
ImageOptimize::dispatch($media)->onQueue('mmo');
break;
case 'video/mp4':
2024-03-10 10:19:44 +00:00
VideoThumbnail::dispatch($media)->onQueue('mmo');
$preview_url = '/storage/no-preview.png';
$url = '/storage/no-preview.png';
break;
default:
2024-03-10 10:19:44 +00:00
break;
}
2024-07-29 09:52:07 +00:00
$user->storage_used = (int) $updatedAccountSize;
$user->storage_used_updated_at = now();
$user->save();
Cache::forget($limitKey);
$resource = new Fractal\Resource\Item($media, new MediaTransformer);
$res = $this->fractal->createData($resource)->toArray();
$res['preview_url'] = $preview_url;
$res['url'] = $url;
2024-03-10 10:19:44 +00:00
return response()->json($res);
}
public function mediaUpdate(Request $request)
{
$this->validate($request, [
'id' => 'required',
2024-03-10 10:19:44 +00:00
'file' => function () {
return [
'required',
2024-03-10 10:19:44 +00:00
'mimetypes:'.config_cache('pixelfed.media_types'),
'max:'.config_cache('pixelfed.max_photo_size'),
];
},
]);
$user = Auth::user();
2024-03-10 10:19:44 +00:00
abort_if($user->has_roles && ! UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action');
2024-03-10 10:19:44 +00:00
$limitKey = 'compose:rate-limit:media-updates:'.$user->id;
$limitTtl = now()->addMinutes(15);
2024-03-10 10:19:44 +00:00
$limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
$dailyLimit = Media::whereUserId($user->id)->where('created_at', '>', now()->subDays(1))->count();
return $dailyLimit >= 1500;
});
abort_if($limitReached == true, 429);
$photo = $request->file('file');
$id = $request->input('id');
$media = Media::whereUserId($user->id)
2024-03-10 10:19:44 +00:00
->whereProfileId($user->profile_id)
->whereNull('status_id')
->findOrFail($id);
$media->save();
$fragments = explode('/', $media->media_path);
$name = last($fragments);
array_pop($fragments);
$dir = implode('/', $fragments);
$path = $photo->storePubliclyAs($dir, $name);
$res = [
2024-03-10 10:19:44 +00:00
'url' => $media->url().'?v='.time(),
];
ImageOptimize::dispatch($media)->onQueue('mmo');
Cache::forget($limitKey);
2024-07-29 09:52:07 +00:00
UserStorageService::recalculateUpdateStorageUsed($request->user()->id);
2024-03-10 10:19:44 +00:00
return $res;
}
public function mediaDelete(Request $request)
{
2024-03-10 10:19:44 +00:00
abort_if(! $request->user(), 403);
$this->validate($request, [
2024-03-10 10:19:44 +00:00
'id' => 'required|integer|min:1|exists:media,id',
]);
2024-03-10 10:19:44 +00:00
abort_if($request->user()->has_roles && ! UserRoleService::can('can-post', $request->user()->id), 403, 'Invalid permissions for this action');
$media = Media::whereNull('status_id')
2024-03-10 10:19:44 +00:00
->whereUserId(Auth::id())
->findOrFail($request->input('id'));
MediaStorageService::delete($media, true);
2024-07-29 09:52:07 +00:00
UserStorageService::recalculateUpdateStorageUsed($request->user()->id);
return response()->json([
'msg' => 'Successfully deleted',
2024-03-10 10:19:44 +00:00
'code' => 200,
]);
}
public function searchTag(Request $request)
{
2024-03-10 10:19:44 +00:00
abort_if(! $request->user(), 403);
$this->validate($request, [
2024-03-10 10:19:44 +00:00
'q' => 'required|string|min:1|max:50',
]);
$q = $request->input('q');
2024-03-10 10:19:44 +00:00
if (Str::of($q)->startsWith('@')) {
if (strlen($q) < 3) {
return [];
}
$q = mb_substr($q, 1);
}
2024-02-10 03:51:37 +00:00
$user = $request->user();
2024-03-10 10:19:44 +00:00
abort_if($user->has_roles && ! UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action');
$blocked = UserFilter::whereFilterableType('App\Profile')
->whereFilterType('block')
->whereFilterableId($request->user()->profile_id)
->pluck('user_id');
$blocked->push($request->user()->profile_id);
2024-03-10 10:19:44 +00:00
$results = Profile::select('id', 'domain', 'username')
->whereNotIn('id', $blocked)
->whereNull('domain')
2024-03-10 10:19:44 +00:00
->where('username', 'like', '%'.$q.'%')
->limit(15)
->get()
2024-03-10 10:19:44 +00:00
->map(function ($r) {
return [
'id' => (string) $r->id,
'name' => $r->username,
'privacy' => true,
2024-03-10 10:19:44 +00:00
'avatar' => $r->avatarUrl(),
];
2024-03-10 10:19:44 +00:00
});
return $results;
}
2021-01-24 21:11:58 +00:00
public function searchUntag(Request $request)
{
2024-03-10 10:19:44 +00:00
abort_if(! $request->user(), 403);
2021-01-24 21:11:58 +00:00
$this->validate($request, [
'status_id' => 'required',
2024-03-10 10:19:44 +00:00
'profile_id' => 'required',
2021-01-24 21:11:58 +00:00
]);
2024-03-10 10:19:44 +00:00
abort_if($request->user()->has_roles && ! UserRoleService::can('can-post', $request->user()->id), 403, 'Invalid permissions for this action');
2021-01-24 21:11:58 +00:00
$user = $request->user();
$status_id = $request->input('status_id');
$profile_id = (int) $request->input('profile_id');
abort_if((int) $user->profile_id !== $profile_id, 400);
$tag = MediaTag::whereStatusId($status_id)
->whereProfileId($profile_id)
->first();
2024-03-10 10:19:44 +00:00
if (! $tag) {
2021-01-24 21:11:58 +00:00
return [];
}
Notification::whereItemType('App\MediaTag')
->whereItemId($tag->id)
->whereProfileId($profile_id)
->whereAction('tagged')
->delete();
MediaTagService::untag($status_id, $profile_id);
return [200];
}
public function searchLocation(Request $request)
{
2024-03-10 10:19:44 +00:00
abort_if(! $request->user(), 403);
$this->validate($request, [
2024-03-10 10:19:44 +00:00
'q' => 'required|string|max:100',
]);
2024-03-10 10:19:44 +00:00
abort_if($request->user()->has_roles && ! UserRoleService::can('can-post', $request->user()->id), 403, 'Invalid permissions for this action');
$pid = $request->user()->profile_id;
2024-03-10 10:19:44 +00:00
abort_if(! $pid, 400);
$q = e($request->input('q'));
2024-03-10 10:19:44 +00:00
$popular = Cache::remember('pf:search:location:v1:popular', 1209600, function () {
$minId = SnowflakeService::byDate(now()->subDays(290));
2024-03-10 10:19:44 +00:00
if (config('database.default') == 'pgsql') {
return Status::selectRaw('id, place_id, count(place_id) as pc')
2024-03-10 10:19:44 +00:00
->whereNotNull('place_id')
->where('id', '>', $minId)
->orderByDesc('pc')
->groupBy(['place_id', 'id'])
->limit(400)
->get()
->filter(function ($post) {
return $post;
})
->map(function ($place) {
return [
'id' => $place->place_id,
'count' => $place->pc,
];
})
->unique('id')
->values();
}
2024-03-10 10:19:44 +00:00
return Status::selectRaw('id, place_id, count(place_id) as pc')
->whereNotNull('place_id')
->where('id', '>', $minId)
->groupBy('place_id')
->orderByDesc('pc')
->limit(400)
->get()
2024-03-10 10:19:44 +00:00
->filter(function ($post) {
return $post;
})
2024-03-10 10:19:44 +00:00
->map(function ($place) {
return [
'id' => $place->place_id,
2024-03-10 10:19:44 +00:00
'count' => $place->pc,
];
});
});
2024-03-10 10:19:44 +00:00
$q = '%'.$q.'%';
$wildcard = config('database.default') === 'pgsql' ? 'ilike' : 'like';
$places = DB::table('places')
2024-03-10 10:19:44 +00:00
->where('name', $wildcard, $q)
->limit((strlen($q) > 5 ? 360 : 30))
->get()
->sortByDesc(function ($place, $key) use ($popular) {
return $popular->filter(function ($p) use ($place) {
return $p['id'] == $place->id;
})->map(function ($p) use ($place) {
return in_array($place->country, ['Canada', 'USA', 'France', 'Germany', 'United Kingdom']) ? $p['count'] : 1;
})->values();
})
->map(function ($r) {
return [
'id' => $r->id,
'name' => $r->name,
'country' => $r->country,
'url' => url('/discover/places/'.$r->id.'/'.$r->slug),
];
})
->values()
->all();
return $places;
}
public function searchMentionAutocomplete(Request $request)
{
2024-03-10 10:19:44 +00:00
abort_if(! $request->user(), 403);
$this->validate($request, [
2024-03-10 10:19:44 +00:00
'q' => 'required|string|min:2|max:50',
]);
2024-03-10 10:19:44 +00:00
abort_if($request->user()->has_roles && ! UserRoleService::can('can-post', $request->user()->id), 403, 'Invalid permissions for this action');
$q = $request->input('q');
2024-03-10 10:19:44 +00:00
if (Str::of($q)->startsWith('@')) {
if (strlen($q) < 3) {
return [];
}
}
$blocked = UserFilter::whereFilterableType('App\Profile')
->whereFilterType('block')
->whereFilterableId($request->user()->profile_id)
->pluck('user_id');
$blocked->push($request->user()->profile_id);
2024-03-10 10:19:44 +00:00
$results = Profile::select('id', 'domain', 'username')
->whereNotIn('id', $blocked)
2024-03-10 10:19:44 +00:00
->where('username', 'like', '%'.$q.'%')
->groupBy('id', 'domain')
->limit(15)
->get()
2024-03-10 10:19:44 +00:00
->map(function ($profile) {
$username = $profile->domain ? substr($profile->username, 1) : $profile->username;
2024-03-10 10:19:44 +00:00
return [
2024-03-10 10:19:44 +00:00
'key' => '@'.str_limit($username, 30),
'value' => $username,
];
2024-03-10 10:19:44 +00:00
});
return $results;
}
public function searchHashtagAutocomplete(Request $request)
{
2024-03-10 10:19:44 +00:00
abort_if(! $request->user(), 403);
$this->validate($request, [
2024-03-10 10:19:44 +00:00
'q' => 'required|string|min:2|max:50',
]);
2024-03-10 10:19:44 +00:00
abort_if($request->user()->has_roles && ! UserRoleService::can('can-post', $request->user()->id), 403, 'Invalid permissions for this action');
$q = $request->input('q');
$results = Hashtag::select('slug')
->where('slug', 'like', '%'.$q.'%')
->whereIsNsfw(false)
->whereIsBanned(false)
->limit(5)
->get()
2024-03-10 10:19:44 +00:00
->map(function ($tag) {
return [
2024-03-10 10:19:44 +00:00
'key' => '#'.$tag->slug,
'value' => $tag->slug,
];
2024-03-10 10:19:44 +00:00
});
return $results;
}
public function store(Request $request)
{
$this->validate($request, [
2024-03-10 10:19:44 +00:00
'caption' => 'nullable|string|max:'.config_cache('pixelfed.max_caption_length', 500),
'media.*' => 'required',
'media.*.id' => 'required|integer|min:1',
'media.*.filter_class' => 'nullable|alpha_dash|max:30',
'media.*.license' => 'nullable|string|max:140',
'media.*.alt' => 'nullable|string|max:'.config_cache('pixelfed.max_altext_length'),
'cw' => 'nullable|boolean',
'visibility' => 'required|string|in:public,private,unlisted|min:2|max:10',
'place' => 'nullable',
'comments_disabled' => 'nullable',
'tagged' => 'nullable',
'license' => 'nullable|integer|min:1|max:16',
'collections' => 'sometimes|array|min:1|max:5',
'spoiler_text' => 'nullable|string|max:140',
// 'optimize_media' => 'nullable'
]);
2024-03-10 10:19:44 +00:00
abort_if($request->user()->has_roles && ! UserRoleService::can('can-post', $request->user()->id), 403, 'Invalid permissions for this action');
2024-03-10 10:19:44 +00:00
if (config('costar.enabled') == true) {
$blockedKeywords = config('costar.keyword.block');
2024-03-10 10:19:44 +00:00
if ($blockedKeywords !== null && $request->caption) {
$keywords = config('costar.keyword.block');
2024-03-10 10:19:44 +00:00
foreach ($keywords as $kw) {
if (Str::contains($request->caption, $kw) == true) {
abort(400, 'Invalid object');
}
}
}
}
$user = $request->user();
$profile = $user->profile;
2024-03-10 10:19:44 +00:00
$limitKey = 'compose:rate-limit:store:'.$user->id;
$limitTtl = now()->addMinutes(15);
2024-07-29 09:52:07 +00:00
// $limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
// $dailyLimit = Status::whereProfileId($user->profile_id)
// ->whereNull('in_reply_to_id')
// ->whereNull('reblog_of_id')
// ->where('created_at', '>', now()->subDays(1))
// ->count();
2024-07-29 09:52:07 +00:00
// return $dailyLimit >= 1000;
// });
2024-07-29 09:52:07 +00:00
// abort_if($limitReached == true, 429);
$license = in_array($request->input('license'), License::keys()) ? $request->input('license') : null;
$visibility = $request->input('visibility');
$medias = $request->input('media');
$attachments = [];
$status = new Status;
$mimes = [];
$place = $request->input('place');
$cw = $request->input('cw');
$tagged = $request->input('tagged');
$optimize_media = (bool) $request->input('optimize_media');
2024-03-10 10:19:44 +00:00
foreach ($medias as $k => $media) {
if ($k + 1 > config_cache('pixelfed.max_album_length')) {
continue;
}
$m = Media::findOrFail($media['id']);
2024-03-10 10:19:44 +00:00
if ($m->profile_id !== $profile->id || $m->status_id) {
abort(403, 'Invalid media id');
}
$m->filter_class = in_array($media['filter_class'], Filter::classes()) ? $media['filter_class'] : null;
$m->license = $license;
$m->caption = isset($media['alt']) ? strip_tags($media['alt']) : null;
$m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
2024-03-10 10:19:44 +00:00
if ($cw == true || $profile->cw == true) {
$m->is_nsfw = $cw;
$status->is_nsfw = $cw;
}
$m->save();
$attachments[] = $m;
array_push($mimes, $m->mime);
}
abort_if(empty($attachments), 422);
$mediaType = StatusController::mimeTypeCheck($mimes);
2024-03-10 10:19:44 +00:00
if (in_array($mediaType, ['photo', 'video', 'photo:album']) == false) {
abort(400, __('exception.compose.invalid.album'));
}
2024-03-10 10:19:44 +00:00
if ($place && is_array($place)) {
$status->place_id = $place['id'];
}
2024-03-10 10:19:44 +00:00
if ($request->filled('comments_disabled')) {
$status->comments_disabled = (bool) $request->input('comments_disabled');
}
2024-03-10 10:19:44 +00:00
if ($request->filled('spoiler_text') && $cw) {
$status->cw_summary = $request->input('spoiler_text');
}
2024-12-23 08:50:51 +00:00
$defaultCaption = config_cache('database.default') === 'mysql' ? null : "";
$status->caption = strip_tags($request->input('caption')) ?? $defaultCaption;
$status->rendered = $defaultCaption;
$status->scope = 'draft';
$status->visibility = 'draft';
$status->profile_id = $profile->id;
$status->save();
2024-03-10 10:19:44 +00:00
foreach ($attachments as $media) {
$media->status_id = $status->id;
$media->save();
}
$visibility = $profile->unlisted == true && $visibility == 'public' ? 'unlisted' : $visibility;
$visibility = $profile->is_private ? 'private' : $visibility;
$cw = $profile->cw == true ? true : $cw;
$status->is_nsfw = $cw;
$status->visibility = $visibility;
$status->scope = $visibility;
$status->type = $mediaType;
$status->save();
2024-03-10 10:19:44 +00:00
foreach ($tagged as $tg) {
$mt = new MediaTag;
$mt->status_id = $status->id;
$mt->media_id = $status->media->first()->id;
$mt->profile_id = $tg['id'];
$mt->tagged_username = $tg['name'];
$mt->is_public = true;
$mt->metadata = json_encode([
'_v' => 1,
]);
$mt->save();
MediaTagService::set($mt->status_id, $mt->profile_id);
MediaTagService::sendNotification($mt);
}
2024-03-10 10:19:44 +00:00
if ($request->filled('collections')) {
$collections = Collection::whereProfileId($profile->id)
->find($request->input('collections'))
2024-03-10 10:19:44 +00:00
->each(function ($collection) use ($status) {
$count = $collection->items()->count();
CollectionItem::firstOrCreate([
'collection_id' => $collection->id,
'object_type' => 'App\Status',
2024-03-10 10:19:44 +00:00
'object_id' => $status->id,
], [
2024-03-10 10:19:44 +00:00
'order' => $count,
]);
CollectionService::addItem(
$collection->id,
$status->id,
$count
);
$collection->updated_at = now();
$collection->save();
CollectionService::setCollection($collection->id, $collection);
});
}
NewStatusPipeline::dispatch($status);
Cache::forget('user:account:id:'.$profile->user_id);
Cache::forget('_api:statuses:recent_9:'.$profile->id);
Cache::forget('profile:status_count:'.$profile->id);
Cache::forget('status:transformer:media:attachments:'.$status->id);
2024-03-10 10:19:44 +00:00
Cache::forget('profile:embed:'.$status->profile_id);
Cache::forget($limitKey);
return $status->url();
}
public function storeText(Request $request)
{
abort_unless(config('exp.top'), 404);
$this->validate($request, [
2024-03-10 10:19:44 +00:00
'caption' => 'nullable|string|max:'.config_cache('pixelfed.max_caption_length', 500),
'cw' => 'nullable|boolean',
'visibility' => 'required|string|in:public,private,unlisted|min:2|max:10',
'place' => 'nullable',
'comments_disabled' => 'nullable',
'tagged' => 'nullable',
]);
2024-03-10 10:19:44 +00:00
abort_if($request->user()->has_roles && ! UserRoleService::can('can-post', $request->user()->id), 403, 'Invalid permissions for this action');
2024-03-10 10:19:44 +00:00
if (config('costar.enabled') == true) {
$blockedKeywords = config('costar.keyword.block');
2024-03-10 10:19:44 +00:00
if ($blockedKeywords !== null && $request->caption) {
$keywords = config('costar.keyword.block');
2024-03-10 10:19:44 +00:00
foreach ($keywords as $kw) {
if (Str::contains($request->caption, $kw) == true) {
abort(400, 'Invalid object');
}
}
}
}
$user = $request->user();
$profile = $user->profile;
$visibility = $request->input('visibility');
$status = new Status;
$place = $request->input('place');
$cw = $request->input('cw');
$tagged = $request->input('tagged');
2024-12-23 08:50:51 +00:00
$defaultCaption = config_cache('database.default') === 'mysql' ? null : "";
2024-03-10 10:19:44 +00:00
if ($place && is_array($place)) {
$status->place_id = $place['id'];
}
2024-03-10 10:19:44 +00:00
if ($request->filled('comments_disabled')) {
$status->comments_disabled = (bool) $request->input('comments_disabled');
}
2024-12-23 08:50:51 +00:00
$status->caption = $request->filled('caption') ? strip_tags($request->caption) : $defaultCaption;
$status->rendered = $defaultCaption;
$status->profile_id = $profile->id;
$entities = [];
$visibility = $profile->unlisted == true && $visibility == 'public' ? 'unlisted' : $visibility;
$cw = $profile->cw == true ? true : $cw;
$status->is_nsfw = $cw;
$status->visibility = $visibility;
$status->scope = $visibility;
$status->type = 'text';
$status->entities = json_encode(array_merge([
'timg' => [
'version' => 0,
'bg_id' => 1,
'font_size' => strlen($status->caption) <= 140 ? 'h1' : 'h3',
'length' => strlen($status->caption),
2024-03-10 10:19:44 +00:00
],
], $entities), JSON_UNESCAPED_SLASHES);
$status->save();
2024-03-10 10:19:44 +00:00
foreach ($tagged as $tg) {
$mt = new MediaTag;
$mt->status_id = $status->id;
$mt->media_id = $status->media->first()->id;
$mt->profile_id = $tg['id'];
$mt->tagged_username = $tg['name'];
$mt->is_public = true;
$mt->metadata = json_encode([
'_v' => 1,
]);
$mt->save();
MediaTagService::set($mt->status_id, $mt->profile_id);
MediaTagService::sendNotification($mt);
}
Cache::forget('user:account:id:'.$profile->user_id);
Cache::forget('_api:statuses:recent_9:'.$profile->id);
Cache::forget('profile:status_count:'.$profile->id);
return $status->url();
}
public function mediaProcessingCheck(Request $request)
{
$this->validate($request, [
2024-03-10 10:19:44 +00:00
'id' => 'required|integer|min:1',
]);
2024-03-10 10:19:44 +00:00
abort_if($request->user()->has_roles && ! UserRoleService::can('can-post', $request->user()->id), 403, 'Invalid permissions for this action');
$media = Media::whereUserId($request->user()->id)
->whereNull('status_id')
->findOrFail($request->input('id'));
2024-03-10 10:19:44 +00:00
if (config('pixelfed.media_fast_process')) {
return [
2024-03-10 10:19:44 +00:00
'finished' => true,
];
}
$finished = false;
switch ($media->mime) {
case 'image/jpeg':
case 'image/png':
case 'video/mp4':
2024-03-12 07:03:33 +00:00
$finished = (bool) config_cache('pixelfed.cloud_storage') ? (bool) $media->cdn_url : (bool) $media->processed_at;
break;
default:
2024-03-10 10:19:44 +00:00
// code...
break;
}
return [
2024-03-10 10:19:44 +00:00
'finished' => $finished,
];
}
public function composeSettings(Request $request)
{
$uid = $request->user()->id;
2024-03-10 10:19:44 +00:00
abort_if($request->user()->has_roles && ! UserRoleService::can('can-post', $request->user()->id), 403, 'Invalid permissions for this action');
$default = [
'default_license' => 1,
'media_descriptions' => false,
2024-03-10 10:19:44 +00:00
'max_altext_length' => config_cache('pixelfed.max_altext_length'),
];
$settings = AccountService::settings($uid);
2024-03-10 10:19:44 +00:00
if (isset($settings['other']) && isset($settings['other']['scope'])) {
$s = $settings['compose_settings'];
$s['default_scope'] = $settings['other']['scope'];
$settings['compose_settings'] = $s;
}
return array_merge($default, $settings['compose_settings']);
}
public function createPoll(Request $request)
{
$this->validate($request, [
2024-03-10 10:19:44 +00:00
'caption' => 'nullable|string|max:'.config_cache('pixelfed.max_caption_length', 500),
'cw' => 'nullable|boolean',
'visibility' => 'required|string|in:public,private',
'comments_disabled' => 'nullable',
'expiry' => 'required|in:60,360,1440,10080',
2024-03-10 10:19:44 +00:00
'pollOptions' => 'required|array|min:1|max:4',
]);
abort(404);
abort_if(config('instance.polls.enabled') == false, 404, 'Polls not enabled');
2024-03-10 10:19:44 +00:00
abort_if($request->user()->has_roles && ! UserRoleService::can('can-post', $request->user()->id), 403, 'Invalid permissions for this action');
abort_if(Status::whereType('poll')
->whereProfileId($request->user()->profile_id)
->whereCaption($request->input('caption'))
->where('created_at', '>', now()->subDays(2))
2024-03-10 10:19:44 +00:00
->exists(), 422, 'Duplicate detected.');
$status = new Status;
$status->profile_id = $request->user()->profile_id;
$status->caption = $request->input('caption');
$status->visibility = 'draft';
$status->scope = 'draft';
$status->type = 'poll';
$status->local = true;
$status->save();
$poll = new Poll;
$poll->status_id = $status->id;
$poll->profile_id = $status->profile_id;
$poll->poll_options = $request->input('pollOptions');
$poll->expires_at = now()->addMinutes($request->input('expiry'));
2024-03-10 10:19:44 +00:00
$poll->cached_tallies = collect($poll->poll_options)->map(function ($o) {
return 0;
})->toArray();
$poll->save();
$status->visibility = $request->input('visibility');
$status->scope = $request->input('visibility');
$status->save();
NewStatusPipeline::dispatch($status);
return ['url' => $status->url()];
}
2021-01-24 21:11:58 +00:00
}