From fee2857deb3f6173ccae4ac273df0466b4fbc78e Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 31 Aug 2021 00:22:08 -0600 Subject: [PATCH] Update ComposeController --- app/Http/Controllers/ComposeController.php | 52 +++++++++++++++++++++- config/instance.php | 4 ++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/ComposeController.php b/app/Http/Controllers/ComposeController.php index 50e8bf5fa..700bae2d9 100644 --- a/app/Http/Controllers/ComposeController.php +++ b/app/Http/Controllers/ComposeController.php @@ -18,6 +18,7 @@ use App\{ UserFilter, UserSetting }; +use App\Models\Poll; use App\Transformer\Api\{ MediaTransformer, MediaDraftTransformer, @@ -42,7 +43,7 @@ use App\Services\MediaPathService; use App\Services\MediaBlocklistService; use App\Services\MediaStorageService; use App\Services\MediaTagService; -use App\Services\ServiceService; +use App\Services\StatusService; use Illuminate\Support\Str; use App\Util\Lexer\Autolink; use App\Util\Lexer\Extractor; @@ -682,4 +683,53 @@ class ComposeController extends Controller return json_decode($res->compose_settings, true); })); } + + public function createPoll(Request $request) + { + $this->validate($request, [ + 'caption' => 'nullable|string|max:'.config('pixelfed.max_caption_length', 500), + 'cw' => 'nullable|boolean', + 'visibility' => 'required|string|in:public,private', + 'comments_disabled' => 'nullable', + 'expiry' => 'required|in:60,360,1440,10080', + 'pollOptions' => 'required|array|min:1|max:4' + ]); + + abort_if(config('instance.polls.enabled') == false, 404, 'Polls not enabled'); + + abort_if(Status::whereType('poll') + ->whereProfileId($request->user()->profile_id) + ->whereCaption($request->input('caption')) + ->where('created_at', '>', now()->subDays(2)) + ->exists() + , 422, 'Duplicate detected.'); + + $status = new Status; + $status->profile_id = $request->user()->profile_id; + $status->caption = $request->input('caption'); + $status->rendered = Autolink::create()->autolink($status->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')); + $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()]; + } } diff --git a/config/instance.php b/config/instance.php index a4cc534b9..f0b99e60e 100644 --- a/config/instance.php +++ b/config/instance.php @@ -47,6 +47,10 @@ return [ ] ], + 'polls' => [ + 'enabled' => false + ], + 'stories' => [ 'enabled' => env('STORIES_ENABLED', false), ],