Support polls

This commit is contained in:
Thomas 2020-05-01 16:40:07 +02:00
parent b8cc61d94b
commit af3a7323ce
4 changed files with 40 additions and 8 deletions

View file

@ -17,7 +17,6 @@ use App\SocialEntity\MastodonAccount;
use App\SocialEntity\PollOption; use App\SocialEntity\PollOption;
use DateTime; use DateTime;
use DateTimeZone; use DateTimeZone;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
@ -204,6 +203,30 @@ class FediPlanController extends AbstractController
} }
$params['sensitive'] = ($data->getSensitive() == null || !$data->getSensitive())?false:true; $params['sensitive'] = ($data->getSensitive() == null || !$data->getSensitive())?false:true;
$pollOptions = $data->getPollOptions();
$pollExpiresAt = $data->getPollExpiresAt();
$isPollMultiple = $data->isPollMultiple();
if( count($pollOptions) > 0){
$count_correct_values = 0;
foreach($pollOptions as $po) {
/** @var $po PollOption */
if( $po->getTitle() != null && strlen(trim($po->getTitle())) > 0){
$count_correct_values++;
}
}
if( $count_correct_values > 1 ){
$params['poll']['options'] = [];
foreach($pollOptions as $po) {
/** @var $po PollOption */
if( $po->getTitle() != null && strlen(trim($po->getTitle())) > 0){
$params['poll']['options'][] = trim($po->getTitle());
}
}
$params['poll']['expires_in'] = $pollExpiresAt;
$params['poll']['multiple'] = $isPollMultiple;
}
}
try { try {
$date = new DateTime( $data->getScheduledAt()->format("Y-m-d H:i"), new DateTimeZone($data->getTimeZone()) ); $date = new DateTime( $data->getScheduledAt()->format("Y-m-d H:i"), new DateTimeZone($data->getTimeZone()) );
$date->setTimezone( new DateTimeZone("UTC")); $date->setTimezone( new DateTimeZone("UTC"));
@ -212,13 +235,14 @@ class FediPlanController extends AbstractController
try { try {
$response = $mastodon_api->post_statuses($params); $response = $mastodon_api->post_statuses($params);
} catch (\ErrorException $e) {} } catch (\ErrorException $e) {}
$session = $request->getSession(); $session = $request->getSession();
if( isset($response['error']) ){ if( isset($response['error']) ){
$session->getFlashBag()->add( $session->getFlashBag()->add(
'Error', 'Error',
$response['error_message'] $response['error_message']
); );
$form->get('content')->addError(new FormError( $response['error_message']));
}else{ }else{
unset($compose); unset($compose);
unset($form); unset($form);

View file

@ -220,7 +220,15 @@ class Mastodon_api {
unset($parameters['body']['media_ids']); unset($parameters['body']['media_ids']);
} }
if(isset($parameters['body']['poll']['options'])) {
$url .= strpos($url, '?') !== false ? '' : '?';
foreach ($parameters['body']['poll']['options'] as $key => $value){
$url .= 'poll[options][]=' . $value . '&';
}
$url = substr($url, 0, -1);
unset($parameters['body']['poll']['options']);
}
//Special treatment for filtering notifications //Special treatment for filtering notifications
if( isset($parameters['body']['exclude_types[]']) && $parameters['method'] == "GET" ){ if( isset($parameters['body']['exclude_types[]']) && $parameters['method'] == "GET" ){
$url .="?"; $url .="?";

View file

@ -14,7 +14,7 @@ class PollOption
/** /**
* @return string * @return string
*/ */
public function getTitle(): string public function getTitle(): ?string
{ {
return $this->title; return $this->title;
} }
@ -22,7 +22,7 @@ class PollOption
/** /**
* @param string $title * @param string $title
*/ */
public function setTitle(string $title): void public function setTitle(?string $title): void
{ {
$this->title = $title; $this->title = $title;
} }
@ -30,7 +30,7 @@ class PollOption
/** /**
* @return int * @return int
*/ */
public function getVotesCount(): int public function getVotesCount(): ?int
{ {
return $this->votes_count; return $this->votes_count;
} }
@ -38,7 +38,7 @@ class PollOption
/** /**
* @param int $votes_count * @param int $votes_count
*/ */
public function setVotesCount(int $votes_count): void public function setVotesCount(?int $votes_count): void
{ {
$this->votes_count = $votes_count; $this->votes_count = $votes_count;
} }

View file

@ -10,13 +10,13 @@
{% for message in messages %} {% for message in messages %}
{% if type == 'Error' %} {% if type == 'Error' %}
<div class="row"> <div class="row">
<div class="alert alert-danger col-md-6" style="text-align: center"> <div class="alert alert-danger col-md-12" style="text-align: center">
{{ message }} {{ message }}
</div> </div>
</div> </div>
{% else %} {% else %}
<div class="row"> <div class="row">
<div class="alert alert-success col-md-6" style="text-align: center"> <div class="alert alert-success col-md-12" style="text-align: center">
{{ message }} {{ message }}
</div> </div>
</div> </div>