Fix media description

This commit is contained in:
Thomas 2019-08-10 14:23:06 +02:00
parent f1351d4f88
commit 89bd45107d
4 changed files with 131 additions and 13 deletions

View file

@ -14,9 +14,12 @@ use App\Services\Mastodon_api;
use App\SocialEntity\Client; use App\SocialEntity\Client;
use App\SocialEntity\Compose; use App\SocialEntity\Compose;
use App\SocialEntity\MastodonAccount; use App\SocialEntity\MastodonAccount;
use DateTime;
use DateTimeZone;
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\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
@ -119,34 +122,78 @@ class FediPlanController extends AbstractController
/** /**
* @Route("/schedule", name="schedule") * @Route("/schedule", name="schedule")
*/ */
public function schedule(Request $request) public function schedule(Request $request, Mastodon_api $mastodon_api)
{ {
$compose = new Compose(); $compose = new Compose();
$form = $this->createForm(ComposeType::class, $compose); $form = $this->createForm(ComposeType::class, $compose);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
/** @var $data Compose */ /** @var $data Compose */
echo "<pre>",print_r($data),"</pre>"; $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){ foreach ($_POST as $key => $value){
if( $key != "compose"){ if( $key != "compose"){
if (strpos($key, 'media_id_') !== false){ if (strpos($key, 'media_id_') !== false){
$mediaId = $value; $mediaId = $value;
$description = $_POST['media_description_'.$mediaId]; $description = $_POST['media_description_'.$mediaId];
//update description if needed
if( $description != null && trim($description) != ""){ 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(); //Schedule status
$content = $data->getContent(); if( $data->getContentWarning() ){
$visibility = $data->getVisibility(); $params['spoiler_text'] = $data->getContentWarning();
$scheduled_at = $data->getScheduledAt(); }
$sensitive = $data->getSensitive(); if( $data->getContent() ){
$timezone = $data->getTimeZone(); $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(); $user = $this->getUser();
/** @var $user MastodonAccount */ /** @var $user MastodonAccount */

View file

@ -73,6 +73,33 @@ class Mastodon_api {
return $response; 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 * _get
* *
@ -203,10 +230,13 @@ class Mastodon_api {
$url .= "&max_id=".$parameters['body']['max_id']; $url .= "&max_id=".$parameters['body']['max_id'];
$parameters['body'] = []; $parameters['body'] = [];
} }
if( isset($parameters["method"]) && $parameters['method'] == "POST" ) if( isset($parameters["method"]) && $parameters['method'] == "POST" )
$response = $curl->post($url, $parameters['body'] ); $response = $curl->post($url, $parameters['body'] );
else if( isset($parameters["method"]) && $parameters['method'] == "GET" ) else if( isset($parameters["method"]) && $parameters['method'] == "GET" )
$response = $curl->get($url, $parameters['body'] ); $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" ) else if( isset($parameters["method"]) && $parameters['method'] == "PATCH" )
$response = $curl->patch($url, $parameters['body'] ); $response = $curl->patch($url, $parameters['body'] );
else if( isset($parameters["method"]) && $parameters['method'] == "DELETE" ) else if( isset($parameters["method"]) && $parameters['method'] == "DELETE" )
@ -967,6 +997,22 @@ class Mastodon_api {
return $response; 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 * 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 * getInstanceNodeInfo returns the social network type depending of the hostname
* @param $host * @param $host

View file

@ -132,12 +132,12 @@ class Compose
return $this; return $this;
} }
public function getScheduledAt(): ?\DateTimeInterface public function getScheduledAt(): ?\DateTime
{ {
return $this->scheduled_at; return $this->scheduled_at;
} }
public function setScheduledAt(?\DateTimeInterface $scheduled_at): self public function setScheduledAt(?\DateTime $scheduled_at): self
{ {
$this->scheduled_at = $scheduled_at; $this->scheduled_at = $scheduled_at;

View file

@ -5,8 +5,28 @@
{% block content %} {% block content %}
{% include 'nav.html.twig' %} {% include 'nav.html.twig' %}
<h1>Schedule</h1> <h1>Schedule</h1>
{% for type, messages in app.session.flashbag.all() %}
{% for message in messages %}
{% if type == 'Error' %}
<div class="row">
<div class="alert alert-danger col-md-6" style="text-align: center">
{{ message }}
</div>
</div>
{% else %}
<div class="row">
<div class="alert alert-success col-md-6" style="text-align: center">
{{ message }}
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
<div class="row"> <div class="row">
<div class="col-6 mt-3"> <div class="col-md-6">
<div class="card"> <div class="card">
<div class="card-horizontal" style=" display: flex;flex: 1 1 auto;"> <div class="card-horizontal" style=" display: flex;flex: 1 1 auto;">
<div class="img-square-wrapper"> <div class="img-square-wrapper">