diff --git a/src/Controller/FediPlanController.php b/src/Controller/FediPlanController.php index 9a9c5eb..77ae877 100644 --- a/src/Controller/FediPlanController.php +++ b/src/Controller/FediPlanController.php @@ -14,9 +14,12 @@ use App\Services\Mastodon_api; use App\SocialEntity\Client; use App\SocialEntity\Compose; use App\SocialEntity\MastodonAccount; +use DateTime; +use DateTimeZone; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormError; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; @@ -119,34 +122,78 @@ class FediPlanController extends AbstractController /** * @Route("/schedule", name="schedule") */ - public function schedule(Request $request) + public function schedule(Request $request, Mastodon_api $mastodon_api) { $compose = new Compose(); $form = $this->createForm(ComposeType::class, $compose); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $data = $form->getData(); /** @var $data Compose */ - echo "
",print_r($data),"
"; + $data = $form->getData(); + /* @var $user MastodonAccount */ + $user = $this->getUser(); + $mastodon_api->set_url("https://" . $user->getInstance()); + + $token = explode(" " ,$user->getToken())[1]; + $type = explode(" ", $user->getToken())[0]; + $mastodon_api->set_token($token, $type); + $params = []; + //Update media description and store their id foreach ($_POST as $key => $value){ if( $key != "compose"){ + if (strpos($key, 'media_id_') !== false){ + $mediaId = $value; $description = $_POST['media_description_'.$mediaId]; + //update description if needed if( $description != null && trim($description) != ""){ - //TODO: update description + try { + $res = $mastodon_api->update_media($mediaId, ['description' => $description]); + } catch (\ErrorException $e) {} } + $params['media_ids'][] = $mediaId; } } } - $cw = $data->getContentWarning(); - $content = $data->getContent(); - $visibility = $data->getVisibility(); - $scheduled_at = $data->getScheduledAt(); - $sensitive = $data->getSensitive(); - $timezone = $data->getTimeZone(); + //Schedule status + if( $data->getContentWarning() ){ + $params['spoiler_text'] = $data->getContentWarning(); + } + if( $data->getContent() ){ + $params['status'] = $data->getContent(); + } + if( $data->getVisibility() ){ + $params['visibility'] = $data->getVisibility(); + } + $params['sensitive'] = ($data->getSensitive() == null || !$data->getSensitive())?false:true; + try { + $date = new DateTime( $data->getScheduledAt()->format("Y-m-d H:i:s"), new DateTimeZone($data->getTimeZone()) ); + $date->setTimezone( new DateTimeZone("UTC")); + $params['scheduled_at'] = $date->format('c'); + } catch (\Exception $e) {} + try { + $response = $mastodon_api->post_statuses($params); + } catch (\ErrorException $e) {} + $session = $request->getSession(); + if( isset($response['error']) ){ + $session->getFlashBag()->add( + 'Error', + $response['error_message'] + ); + $form->get('content')->addError(new FormError( $response['error_message'])); + }else{ + unset($compose); + unset($form); + $compose = new Compose(); + $session->getFlashBag()->add( + 'Success', + 'The message has been scheduled' + ); + $form = $this->createForm(ComposeType::class, $compose); + } } $user = $this->getUser(); /** @var $user MastodonAccount */ diff --git a/src/Services/Mastodon_api.php b/src/Services/Mastodon_api.php index 41805b1..0df272d 100644 --- a/src/Services/Mastodon_api.php +++ b/src/Services/Mastodon_api.php @@ -73,6 +73,33 @@ class Mastodon_api { return $response; } + + /** + * _put + * + * HTTP API put + * + * @param string $url + * @param array $parameters + * + * @return array $response + * @throws \ErrorException + */ + private function _put ($url, $parameters = array()) { + + $params["method"] = "PUT"; + // set access_token + if (isset($this->token['access_token'])) { + $params['headers'] = array( + 'Authorization' => $this->token['token_type'] . ' ' . $this->token['access_token'] + ); + } + $params['body'] = $parameters; + $url = $this->mastodon_url.$url; + $response = $this->get_content_remote($url,$params); + return $response; + } + /** * _get * @@ -203,10 +230,13 @@ class Mastodon_api { $url .= "&max_id=".$parameters['body']['max_id']; $parameters['body'] = []; } + if( isset($parameters["method"]) && $parameters['method'] == "POST" ) $response = $curl->post($url, $parameters['body'] ); else if( isset($parameters["method"]) && $parameters['method'] == "GET" ) $response = $curl->get($url, $parameters['body'] ); + else if( isset($parameters["method"]) && $parameters['method'] == "PUT" ) + $response = $curl->put($url, $parameters['body'] ); else if( isset($parameters["method"]) && $parameters['method'] == "PATCH" ) $response = $curl->patch($url, $parameters['body'] ); else if( isset($parameters["method"]) && $parameters['method'] == "DELETE" ) @@ -967,6 +997,22 @@ class Mastodon_api { return $response; } + /** + * post_media + * + * @param string $id + * array $parameters + * string $parameters['description'] (optional): A plain-text description of the media for accessibility (max 420 chars) + * array $parameters['focus'] (optional):Two floating points, comma-delimited. See focal points + * + * @return array $response + * @throws \ErrorException + */ + public function update_media ($id, $parameters) { + $response = $this->_put('/api/v1/media/'.$id, $parameters); + return $response; + } + /** * post_statuses * @@ -1102,6 +1148,11 @@ class Mastodon_api { } + /*** + * Custom management added by @tom79 https://github.com/stom79/ + */ + + /** * getInstanceNodeInfo returns the social network type depending of the hostname * @param $host diff --git a/src/SocialEntity/Compose.php b/src/SocialEntity/Compose.php index 56e3ed8..ce4c7e1 100644 --- a/src/SocialEntity/Compose.php +++ b/src/SocialEntity/Compose.php @@ -132,12 +132,12 @@ class Compose return $this; } - public function getScheduledAt(): ?\DateTimeInterface + public function getScheduledAt(): ?\DateTime { return $this->scheduled_at; } - public function setScheduledAt(?\DateTimeInterface $scheduled_at): self + public function setScheduledAt(?\DateTime $scheduled_at): self { $this->scheduled_at = $scheduled_at; diff --git a/templates/fediplan/schedule.html.twig b/templates/fediplan/schedule.html.twig index 4d6310b..3eac515 100644 --- a/templates/fediplan/schedule.html.twig +++ b/templates/fediplan/schedule.html.twig @@ -5,8 +5,28 @@ {% block content %} {% include 'nav.html.twig' %}

Schedule

+ + {% for type, messages in app.session.flashbag.all() %} + {% for message in messages %} + {% if type == 'Error' %} +
+
+ {{ message }} +
+
+ {% else %} +
+
+ {{ message }} +
+
+ {% endif %} + {% endfor %} + {% endfor %} + +
-
+