diff --git a/doc/api.md b/doc/api.md index ccd2e7a7e..d522767fd 100644 --- a/doc/api.md +++ b/doc/api.md @@ -393,6 +393,27 @@ Object of: --- +### media/metadata/create (POST,PUT; AUTH) + +#### Parameters + +Parameters are sent as JSON object: + +``` +{ + "media_id":"1234", + "alt_text": { + "text":"Here comes the description" + } +} +``` + +#### Return values + +None + +--- + ### oauth/request_token (*) #### Parameters diff --git a/include/api.php b/include/api.php index 39a61c983..5665a7fbd 100644 --- a/include/api.php +++ b/include/api.php @@ -1158,15 +1158,16 @@ function api_statuses_update($type) // To-Do: Multiple IDs if (requestdata('media_ids')) { $r = q( - "SELECT `resource-id`, `scale`, `nickname`, `type` FROM `photo` INNER JOIN `user` ON `user`.`uid` = `photo`.`uid` WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = %d) AND `scale` > 0 AND `photo`.`uid` = %d ORDER BY `photo`.`width` DESC LIMIT 1", + "SELECT `resource-id`, `scale`, `nickname`, `type`, `desc` FROM `photo` INNER JOIN `user` ON `user`.`uid` = `photo`.`uid` WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = %d) AND `scale` > 0 AND `photo`.`uid` = %d ORDER BY `photo`.`width` DESC LIMIT 1", intval(requestdata('media_ids')), api_user() ); if (DBA::isResult($r)) { $phototypes = Image::supportedTypes(); $ext = $phototypes[$r[0]['type']]; + $description = $r[0]['desc'] ?? ''; $_REQUEST['body'] .= "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $r[0]['nickname'] . '/image/' . $r[0]['resource-id'] . ']'; - $_REQUEST['body'] .= '[img]' . System::baseUrl() . '/photo/' . $r[0]['resource-id'] . '-' . $r[0]['scale'] . '.' . $ext . '[/img][/url]'; + $_REQUEST['body'] .= '[img=' . System::baseUrl() . '/photo/' . $r[0]['resource-id'] . '-' . $r[0]['scale'] . '.' . $ext . ']' . $description . '[/img][/url]'; } } @@ -1239,6 +1240,65 @@ function api_media_upload() /// @TODO move to top of file or somewhere better api_register_func('api/media/upload', 'api_media_upload', true, API_METHOD_POST); +/** + * Updates media meta data (picture descriptions) + * + * @param string $type Return type (atom, rss, xml, json) + * + * @return array|string + * @throws BadRequestException + * @throws ForbiddenException + * @throws ImagickException + * @throws InternalServerErrorException + * @throws TooManyRequestsException + * @throws UnauthorizedException + * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update + * + * @todo Compare the corresponding Twitter function for correct return values + */ +function api_media_metadata_create($type) +{ + $a = \get_app(); + + if (api_user() === false) { + Logger::info('no user'); + throw new ForbiddenException(); + } + + api_get_user($a); + + $postdata = Network::postdata(); + + if (empty($postdata)) { + throw new BadRequestException("No post data"); + } + + $data = json_decode($postdata, true); + if (empty($data)) { + throw new BadRequestException("Invalid post data"); + } + + if (empty($data['media_id']) || empty($data['alt_text'])) { + throw new BadRequestException("Missing post data values"); + } + + if (empty($data['alt_text']['text'])) { + throw new BadRequestException("No alt text."); + } + + Logger::info('Updating metadata', ['media_id' => $data['media_id']]); + + $condition = ['id' => $data['media_id'], 'uid' => api_user()]; + $photo = DBA::selectFirst('photo', ['resource-id'], $condition); + if (!DBA::isResult($photo)) { + throw new BadRequestException("Metadata not found."); + } + + DBA::update('photo', ['desc' => $data['alt_text']['text']], ['resource-id' => $photo['resource-id']]); +} + +api_register_func('api/media/metadata/create', 'api_media_metadata_create', true, API_METHOD_POST); + /** * @param string $type Return format (atom, rss, xml, json) * @param int $item_id diff --git a/mod/dfrn_notify.php b/mod/dfrn_notify.php index e75d975a8..3f0ecba00 100644 --- a/mod/dfrn_notify.php +++ b/mod/dfrn_notify.php @@ -16,11 +16,12 @@ use Friendica\Model\User; use Friendica\Protocol\DFRN; use Friendica\Protocol\Diaspora; use Friendica\Util\Strings; +use Friendica\Util\Network; function dfrn_notify_post(App $a) { Logger::log(__function__, Logger::TRACE); - $postdata = file_get_contents('php://input'); + $postdata = Network::postdata(); if (empty($_POST) || !empty($postdata)) { $data = json_decode($postdata); diff --git a/mod/pubsub.php b/mod/pubsub.php index e5ede6c80..d10d7031d 100644 --- a/mod/pubsub.php +++ b/mod/pubsub.php @@ -7,6 +7,7 @@ use Friendica\Database\DBA; use Friendica\Model\Contact; use Friendica\Protocol\OStatus; use Friendica\Util\Strings; +use Friendica\Util\Network; use Friendica\Core\System; function hub_return($valid, $body) @@ -83,7 +84,7 @@ function pubsub_init(App $a) function pubsub_post(App $a) { - $xml = file_get_contents('php://input'); + $xml = Network::postdata(); Logger::log('Feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $a->cmd . ' with user-agent: ' . $_SERVER['HTTP_USER_AGENT']); Logger::log('Data: ' . $xml, Logger::DATA); diff --git a/mod/receive.php b/mod/receive.php index db1287ea6..182a1df8c 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -10,6 +10,7 @@ use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Protocol\Diaspora; +use Friendica\Util\Network; /** * @param App $a App @@ -47,7 +48,7 @@ function receive_post(App $a) Logger::log('mod-diaspora: receiving post', Logger::DEBUG); if (empty($_POST['xml'])) { - $postdata = file_get_contents("php://input"); + $postdata = Network::postdata(); if ($postdata == '') { throw new \Friendica\Network\HTTPException\InternalServerErrorException(); } diff --git a/mod/salmon.php b/mod/salmon.php index ba1bc8d46..67e467a73 100644 --- a/mod/salmon.php +++ b/mod/salmon.php @@ -13,11 +13,12 @@ use Friendica\Protocol\OStatus; use Friendica\Protocol\Salmon; use Friendica\Util\Crypto; use Friendica\Util\Strings; +use Friendica\Util\Network; function salmon_post(App $a, $xml = '') { if (empty($xml)) { - $xml = file_get_contents('php://input'); + $xml = Network::postdata(); } Logger::log('new salmon ' . $xml, Logger::DATA); diff --git a/src/Module/Inbox.php b/src/Module/Inbox.php index 5367adb7e..2cc273b13 100644 --- a/src/Module/Inbox.php +++ b/src/Module/Inbox.php @@ -12,6 +12,7 @@ use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Protocol\ActivityPub; use Friendica\Util\HTTPSignature; +use Friendica\Util\Network; /** * ActivityPub Inbox @@ -22,7 +23,7 @@ class Inbox extends BaseModule { $a = self::getApp(); - $postdata = file_get_contents('php://input'); + $postdata = Network::postdata(); if (empty($postdata)) { throw new \Friendica\Network\HTTPException\BadRequestException(); diff --git a/src/Util/Network.php b/src/Util/Network.php index 75b928bea..cd66abe0b 100644 --- a/src/Util/Network.php +++ b/src/Util/Network.php @@ -342,6 +342,16 @@ class Network return $curlResponse; } + /** + * Return raw post data from a post request + * + * @return string post data + */ + public static function postdata() + { + return file_get_contents('php://input'); + } + /** * @brief Check URL to see if it's real *