API: Next bunch of functions transformed

This commit is contained in:
Michael 2021-11-27 09:42:05 +00:00 committed by Hypolite Petovan
parent 9cc675233d
commit b15d3a2523
11 changed files with 387 additions and 292 deletions

View File

@ -38,7 +38,6 @@ use Friendica\Model\Mail;
use Friendica\Model\Photo;
use Friendica\Model\Post;
use Friendica\Model\Profile;
use Friendica\Model\User;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException;
use Friendica\Network\HTTPException\BadRequestException;
@ -50,11 +49,9 @@ use Friendica\Network\HTTPException\UnauthorizedException;
use Friendica\Object\Image;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Images;
use Friendica\Util\Network;
use Friendica\Util\Strings;
require_once __DIR__ . '/../mod/item.php';
require_once __DIR__ . '/../mod/wall_upload.php';
$API = [];
@ -681,7 +678,7 @@ function api_statuses_mediap($type)
}
$txt = HTML::toBBCode($txt);
$picture = wall_upload_post($a, false);
$picture = Photo::upload($uid, $_FILES['media']);
// now that we have the img url in bbcode we can add it to the status and insert the wall item.
$_REQUEST['body'] = $txt . "\n\n" . '[url=' . $picture["albumpage"] . '][img]' . $picture["preview"] . "[/img][/url]";
@ -690,11 +687,10 @@ function api_statuses_mediap($type)
$include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true');
// output the post that we just posted.
$status_info = DI::twitterStatus()->createFromItemId($item_id, $include_entities)->toArray();
$status_info = DI::twitterStatus()->createFromItemId($item_id, $uid, $include_entities)->toArray();
return DI::apiResponse()->formatData('statuses', $type, ['status' => $status_info]);
}
/// @TODO move this to top of file or somewhere better!
api_register_func('api/statuses/mediap', 'api_statuses_mediap', true);
/**
@ -805,7 +801,7 @@ function api_statuses_update($type)
$ids = explode(',', $_REQUEST['media_ids']);
} elseif (!empty($_FILES['media'])) {
// upload the image if we have one
$picture = wall_upload_post($a, false);
$picture = Photo::upload($uid, $_FILES['media']);
if (is_array($picture)) {
$ids[] = $picture['id'];
}
@ -874,108 +870,13 @@ function api_statuses_update($type)
$include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true');
// output the post that we just posted.
$status_info = DI::twitterStatus()->createFromItemId($item_id, $include_entities)->toArray();
$status_info = DI::twitterStatus()->createFromItemId($item_id, $uid, $include_entities)->toArray();
return DI::apiResponse()->formatData('statuses', $type, ['status' => $status_info]);
}
api_register_func('api/statuses/update', 'api_statuses_update', true);
api_register_func('api/statuses/update_with_media', 'api_statuses_update', true);
/**
* Uploads an image to Friendica.
*
* @return array
* @throws BadRequestException
* @throws ForbiddenException
* @throws ImagickException
* @throws InternalServerErrorException
* @throws UnauthorizedException
* @see https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload
*/
function api_media_upload()
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
if (empty($_FILES['media'])) {
// Output error
throw new BadRequestException("No media.");
}
$media = wall_upload_post(DI::app(), false);
if (!$media) {
// Output error
throw new InternalServerErrorException();
}
$returndata = [];
$returndata["media_id"] = $media["id"];
$returndata["media_id_string"] = (string)$media["id"];
$returndata["size"] = $media["size"];
$returndata["image"] = ["w" => $media["width"],
"h" => $media["height"],
"image_type" => $media["type"],
"friendica_preview_url" => $media["preview"]];
Logger::info('Media uploaded', ['return' => $returndata]);
return ["media" => $returndata];
}
api_register_func('api/media/upload', 'api_media_upload', true);
/**
* 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)
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();
$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' => $uid];
$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);
/**
* Repeats a status.
*
@ -1049,81 +950,12 @@ function api_statuses_repeat($type)
$include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true');
// output the post that we just posted.
$status_info = DI::twitterStatus()->createFromItemId($item_id, $include_entities)->toArray();
$status_info = DI::twitterStatus()->createFromItemId($item_id, $uid, $include_entities)->toArray();
return DI::apiResponse()->formatData('statuses', $type, ['status' => $status_info]);
}
api_register_func('api/statuses/retweet', 'api_statuses_repeat', true);
/**
* Star/unstar an item.
* param: id : id of the item
*
* @param string $type Return type (atom, rss, xml, json)
*
* @return array|string
* @throws BadRequestException
* @throws ForbiddenException
* @throws ImagickException
* @throws InternalServerErrorException
* @throws UnauthorizedException
* @see https://web.archive.org/web/20131019055350/https://dev.twitter.com/docs/api/1/post/favorites/create/%3Aid
*/
function api_favorites_create_destroy($type)
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();
// for versioned api.
/// @TODO We need a better global soluton
$action_argv_id = 2;
if (count(DI::args()->getArgv()) > 1 && DI::args()->getArgv()[1] == "1.1") {
$action_argv_id = 3;
}
if (DI::args()->getArgc() <= $action_argv_id) {
throw new BadRequestException("Invalid request.");
}
$action = str_replace("." . $type, "", DI::args()->getArgv()[$action_argv_id]);
if (DI::args()->getArgc() == $action_argv_id + 2) {
$itemid = intval(DI::args()->getArgv()[$action_argv_id + 1] ?? 0);
} else {
$itemid = intval($_REQUEST['id'] ?? 0);
}
$item = Post::selectFirstForUser($uid, [], ['id' => $itemid, 'uid' => $uid]);
if (!DBA::isResult($item)) {
throw new BadRequestException("Invalid item.");
}
switch ($action) {
case "create":
$item['starred'] = 1;
break;
case "destroy":
$item['starred'] = 0;
break;
default:
throw new BadRequestException("Invalid action ".$action);
}
$r = Item::update(['starred' => $item['starred']], ['id' => $itemid]);
if ($r === false) {
throw new InternalServerErrorException("DB error");
}
$include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true');
$ret = DI::twitterStatus()->createFromUriId($item['uri-id'], $item['uid'], $include_entities)->toArray();
return DI::apiResponse()->formatData("status", $type, ['status' => $ret], Contact::getPublicIdByUserId($uid));
}
api_register_func('api/favorites/create', 'api_favorites_create_destroy', true);
api_register_func('api/favorites/destroy', 'api_favorites_create_destroy', true);
/**
* Returns all lists the user subscribes to.
*
@ -1312,83 +1144,6 @@ function api_direct_messages_destroy($type)
api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', true);
/**
* Unfollow Contact
*
* @param string $type Known types are 'atom', 'rss', 'xml' and 'json'
* @return string|array
* @throws HTTPException\BadRequestException
* @throws HTTPException\ExpectationFailedException
* @throws HTTPException\ForbiddenException
* @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException
* @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/post-friendships-destroy.html
*/
function api_friendships_destroy($type)
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();
$owner = User::getOwnerDataById($uid);
if (!$owner) {
Logger::notice(BaseApi::LOG_PREFIX . 'No owner {uid} found', ['module' => 'api', 'action' => 'friendships_destroy', 'uid' => $uid]);
throw new HTTPException\NotFoundException('Error Processing Request');
}
$contact_id = $_REQUEST['user_id'] ?? 0;
if (empty($contact_id)) {
Logger::notice(BaseApi::LOG_PREFIX . 'No user_id specified', ['module' => 'api', 'action' => 'friendships_destroy']);
throw new HTTPException\BadRequestException('no user_id specified');
}
// Get Contact by given id
$contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => 0, 'self' => false]);
if(!DBA::isResult($contact)) {
Logger::notice(BaseApi::LOG_PREFIX . 'No public contact found for ID {contact}', ['module' => 'api', 'action' => 'friendships_destroy', 'contact' => $contact_id]);
throw new HTTPException\NotFoundException('no contact found to given ID');
}
$url = $contact['url'];
$condition = ["`uid` = ? AND (`rel` = ? OR `rel` = ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
$uid, Contact::SHARING, Contact::FRIEND, Strings::normaliseLink($url),
Strings::normaliseLink($url), $url];
$contact = DBA::selectFirst('contact', [], $condition);
if (!DBA::isResult($contact)) {
Logger::notice(BaseApi::LOG_PREFIX . 'Not following contact', ['module' => 'api', 'action' => 'friendships_destroy']);
throw new HTTPException\NotFoundException('Not following Contact');
}
try {
$result = Contact::terminateFriendship($owner, $contact);
if ($result === null) {
Logger::notice(BaseApi::LOG_PREFIX . 'Not supported for {network}', ['module' => 'api', 'action' => 'friendships_destroy', 'network' => $contact['network']]);
throw new HTTPException\ExpectationFailedException('Unfollowing is currently not supported by this contact\'s network.');
}
if ($result === false) {
throw new HTTPException\ServiceUnavailableException('Unable to unfollow this contact, please retry in a few minutes or contact your administrator.');
}
} catch (Exception $e) {
Logger::error(BaseApi::LOG_PREFIX . $e->getMessage(), ['owner' => $owner, 'contact' => $contact]);
throw new HTTPException\InternalServerErrorException('Unable to unfollow this contact, please contact your administrator');
}
// "uid" is only needed for some internal stuff, so remove it from here
unset($contact['uid']);
// Set screen_name since Twidere requests it
$contact['screen_name'] = $contact['nick'];
return DI::apiResponse()->formatData('friendships-destroy', $type, ['user' => $contact]);
}
api_register_func('api/friendships/destroy', 'api_friendships_destroy', true);
/**
*
* @param string $type Return type (atom, rss, xml, json)

View File

@ -74,7 +74,7 @@ class Status extends BaseFactory
* @throws HTTPException\InternalServerErrorException
* @throws ImagickException|HTTPException\NotFoundException
*/
public function createFromItemId(int $id, $include_entities = false): \Friendica\Object\Api\Twitter\Status
public function createFromItemId(int $id, int $uid, bool $include_entities = false): \Friendica\Object\Api\Twitter\Status
{
$fields = ['id', 'parent', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network',
'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'coord'];
@ -82,7 +82,7 @@ class Status extends BaseFactory
if (!$item) {
throw new HTTPException\NotFoundException('Item with ID ' . $id . ' not found.');
}
return $this->createFromArray($item, $include_entities);
return $this->createFromArray($item, $uid, $include_entities);
}
/**
@ -101,7 +101,7 @@ class Status extends BaseFactory
if (!$item) {
throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
}
return $this->createFromArray($item, $include_entities);
return $this->createFromArray($item, $uid, $include_entities);
}
/**
@ -112,10 +112,10 @@ class Status extends BaseFactory
* @throws HTTPException\InternalServerErrorException
* @throws ImagickException|HTTPException\NotFoundException
*/
private function createFromArray(array $item, $include_entities): \Friendica\Object\Api\Twitter\Status
private function createFromArray(array $item, int $uid, bool $include_entities): \Friendica\Object\Api\Twitter\Status
{
$author = $this->twitterUser->createFromContactId($item['author-id'], $item['uid'], true);
$owner = $this->twitterUser->createFromContactId($item['owner-id'], $item['uid'], true);
$author = $this->twitterUser->createFromContactId($item['author-id'], $uid, true);
$owner = $this->twitterUser->createFromContactId($item['owner-id'], $uid, true);
$friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => GRAVITY_COMMENT]);
@ -135,6 +135,15 @@ class Status extends BaseFactory
}
}
$liked = Post::exists([
'thr-parent-id' => $item['uri-id'],
'uid' => $uid,
'origin' => true,
'gravity' => GRAVITY_ACTIVITY,
'vid' => Verb::getID(Activity::LIKE),
'deleted' => false
]);
if ($include_entities) {
$hashtags = $this->hashtag->createFromUriId($item['uri-id'], $text);
$medias = $this->media->createFromUriId($item['uri-id'], $text);
@ -144,7 +153,7 @@ class Status extends BaseFactory
$attachments = $this->attachment->createFromUriId($item['uri-id'], $text);
}
$friendica_activities = $this->activities->createFromUriId($item['uri-id'], $item['uid']);
$friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid);
$shared = BBCode::fetchShareAttributes($item['body']);
if (!empty($shared['guid'])) {
@ -163,11 +172,11 @@ class Status extends BaseFactory
}
if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
$retweeted = $this->createFromUriId($item['thr-parent-id'], $item['uid'])->toArray();
$retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $item['uid']]]);
$retweeted = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
$retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $uid]]);
$item['title'] = $retweeted_item['title'] ?? $item['title'];
$item['body'] = $retweeted_item['body'] ?? $item['body'];
$author = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $item['uid'], true);
$author = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $uid, true);
} else {
$retweeted = [];
}
@ -181,6 +190,6 @@ class Status extends BaseFactory
$entities = [];
}
return new \Friendica\Object\Api\Twitter\Status($text, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $attachments, $friendica_comments);
return new \Friendica\Object\Api\Twitter\Status($text, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $attachments, $friendica_comments, $liked);
}
}

View File

@ -0,0 +1,51 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Twitter\Favorites;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException\BadRequestException;
/**
* @see https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-favorites-create
*/
class Create extends BaseApi
{
protected function rawContent(array $request = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();
$id = $request['id'] ?? 0;
if (empty($id)) {
throw new BadRequestException('Item id not specified');
}
Item::performActivity($id, 'like', $uid);
$status_info = DI::twitterStatus()->createFromItemId($id, $uid)->toArray();
DI::apiResponse()->exit('status', ['status' => $status_info], $this->parameters['extension'] ?? null);
}
}

View File

@ -0,0 +1,51 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Twitter\Favorites;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException\BadRequestException;
/**
* @see https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-favorites-destroy
*/
class Destroy extends BaseApi
{
protected function rawContent(array $request = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();
$id = $request['id'] ?? 0;
if (empty($id)) {
throw new BadRequestException('Item id not specified');
}
Item::performActivity($id, 'unlike', $uid);
$status_info = DI::twitterStatus()->createFromItemId($id, $uid)->toArray();
DI::apiResponse()->exit('status', ['status' => $status_info], $this->parameters['extension'] ?? null);
}
}

View File

@ -0,0 +1,86 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Twitter\Friendships;
use Exception;
use Friendica\Core\Logger;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\User;
use Friendica\Module\Api\Twitter\ContactEndpoint;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException;
/**
* Unfollow Contact
*
* @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/post-friendships-destroy.html
*/
class Destroy extends ContactEndpoint
{
protected function rawContent(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();
$owner = User::getOwnerDataById($uid);
if (!$owner) {
Logger::notice(BaseApi::LOG_PREFIX . 'No owner {uid} found', ['module' => 'api', 'action' => 'friendships_destroy', 'uid' => $uid]);
throw new HTTPException\NotFoundException('Error Processing Request');
}
$contact_id = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $request['user_id'] ?? 0, 0);
if (empty($contact_id)) {
Logger::notice(BaseApi::LOG_PREFIX . 'No user_id specified', ['module' => 'api', 'action' => 'friendships_destroy']);
throw new HTTPException\BadRequestException('no user_id specified');
}
// Get Contact by given id
$cdata = Contact::getPublicAndUserContactID($contact_id, $uid);
if (!empty($cdata['user'])) {
Logger::notice(BaseApi::LOG_PREFIX . 'Not following contact', ['module' => 'api', 'action' => 'friendships_destroy']);
throw new HTTPException\NotFoundException('Not following Contact');
}
$contact = Contact::getById($cdata['user']);
$user = $this->twitterUser->createFromContactId($contact_id, $uid, true)->toArray();
try {
$result = Contact::terminateFriendship($owner, $contact);
if ($result === null) {
Logger::notice(BaseApi::LOG_PREFIX . 'Not supported for {network}', ['module' => 'api', 'action' => 'friendships_destroy', 'network' => $contact['network']]);
throw new HTTPException\ExpectationFailedException('Unfollowing is currently not supported by this contact\'s network.');
}
if ($result === false) {
throw new HTTPException\ServiceUnavailableException('Unable to unfollow this contact, please retry in a few minutes or contact your administrator.');
}
} catch (Exception $e) {
Logger::error(BaseApi::LOG_PREFIX . $e->getMessage(), ['owner' => $owner, 'contact' => $contact]);
throw new HTTPException\InternalServerErrorException('Unable to unfollow this contact, please contact your administrator');
}
DI::apiResponse()->exit('friendships', ['user' => $user], $this->parameters['extension'] ?? null);
}
}

View File

@ -0,0 +1,72 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Twitter\Media\Metadata;
use Friendica\Core\Logger;
use Friendica\Model\Photo;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Util\Network;
/**
* Updates media meta data (picture descriptions)
*
* @see https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-metadata-create
*/
class Create extends BaseApi
{
protected function rawContent(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();
$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' => $uid];
$photo = Photo::selectFirst(['resource-id'], $condition);
if (empty($photo['resource-id'])) {
throw new BadRequestException('Metadata not found.');
}
Photo::update(['desc' => $data['alt_text']['text']], ['resource-id' => $photo['resource-id']]);
}
}

View File

@ -0,0 +1,70 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Twitter\Media;
use Friendica\Core\Logger;
use Friendica\DI;
use Friendica\Model\Photo;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Network\HTTPException\InternalServerErrorException;
/**
* Uploads an image to Friendica.
*
* @see https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload
*/
class Upload extends BaseApi
{
protected function rawContent(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();
if (empty($_FILES['media'])) {
// Output error
throw new BadRequestException("No media.");
}
$media = Photo::upload($uid, $_FILES['media']);
if (!$media) {
// Output error
throw new InternalServerErrorException();
}
$returndata = [];
$returndata["media_id"] = $media["id"];
$returndata["media_id_string"] = (string)$media["id"];
$returndata["size"] = $media["size"];
$returndata["image"] = [
"w" => $media["width"],
"h" => $media["height"],
"image_type" => $media["type"],
"friendica_preview_url" => $media["preview"]
];
Logger::info('Media uploaded', ['return' => $returndata]);
DI::apiResponse()->exit('media', ['media' => $returndata], $this->parameters['extension'] ?? null);
}
}

View File

@ -40,16 +40,16 @@ class Destroy extends BaseApi
$uid = BaseApi::getCurrentUserID();
if (empty($this->parameters['id'])) {
$id = intval($_REQUEST['id'] ?? 0);
$id = intval($request['id'] ?? 0);
} else {
$id = (int)$this->parameters['id'];
}
logger::notice('API: api_statuses_destroy: ' . $id);
$this->logger->notice('API: api_statuses_destroy: ' . $id);
$include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true');
$include_entities = strtolower(($request['include_entities'] ?? 'false') == 'true');
$ret = DI::twitterStatus()->createFromItemId($$id, $uid, $include_entities)->toArray();
$ret = DI::twitterStatus()->createFromItemId($id, $uid, $include_entities)->toArray();
Item::deleteForUser(['id' => $id], $uid);

View File

@ -99,7 +99,7 @@ class Status extends BaseDataTransferObject
* @param array $item
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(string $text, array $item, User $author, User $owner, array $retweeted, array $quoted, array $geo, array $friendica_activities, array $entities, array $attachments, int $friendica_comments)
public function __construct(string $text, array $item, User $author, User $owner, array $retweeted, array $quoted, array $geo, array $friendica_activities, array $entities, array $attachments, int $friendica_comments, bool $liked)
{
$this->id = (int)$item['id'];
$this->id_str = (string)$item['id'];
@ -127,7 +127,7 @@ class Status extends BaseDataTransferObject
$this->retweeted_status = $retweeted;
$this->quoted_status = $quoted;
$this->external_url = $item['plink'];
$this->favorited = (bool)$item['starred'];
$this->favorited = $liked;
$this->friendica_comments = $friendica_comments;
$this->source = $item['app'];
$this->geo = $geo;

View File

@ -54,21 +54,21 @@ $apiRoutes = [
'/direct_messages' => [
'/all[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]],
'/conversation[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]],
'/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::DELETE, R::POST]],
'/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/new[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/sent[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]],
],
'/direct_messages[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET, R::POST]],
'/externalprofile/show[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Users\Show::class, [R::GET ]],
'/favorites/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/favorites/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::DELETE, R::POST]],
'/favorites/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Favorites\Create::class, [ R::POST]],
'/favorites/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Favorites\Destroy::class, [ R::POST]],
'/favorites[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Favorites::class, [R::GET ]],
'/followers/ids[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Followers\Ids::class, [R::GET ]],
'/followers/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Followers\Lists::class, [R::GET ]],
'/friends/ids[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Friends\Ids::class, [R::GET ]],
'/friends/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Friends\Lists::class, [R::GET ]],
'/friendships/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/friendships/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Friendships\Destroy::class, [ R::POST]],
'/friendships/incoming[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Friendships\Incoming::class, [R::GET ]],
'/friendica' => [
@ -81,14 +81,14 @@ $apiRoutes = [
'/events[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Events\Index::class, [R::GET ]],
'/group_show[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]],
'/group_create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/group_delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Delete::class, [R::DELETE, R::POST]],
'/group_delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Delete::class, [ R::POST]],
'/group_update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/profile/show[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Profile\Show::class, [R::GET ]],
'/photoalbum/delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photoalbum\Delete::class, [R::DELETE, R::POST]],
'/photoalbum/delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photoalbum\Delete::class, [ R::POST]],
'/photoalbum/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photoalbum\Update::class, [ R::POST]],
'/photos/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]],
'/photo/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/photo/delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photo\Delete::class, [R::DELETE, R::POST]],
'/photo/delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photo\Delete::class, [ R::POST]],
'/photo/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/photo[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]],
],
@ -99,7 +99,7 @@ $apiRoutes = [
'/lists' => [
'/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::DELETE, R::POST]],
'/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]],
'/ownerships[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]],
'/statuses[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Lists\Statuses::class, [R::GET ]],
@ -107,8 +107,8 @@ $apiRoutes = [
'/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
],
'/media/upload[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/media/metadata/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/media/upload[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Media\Upload::class, [ R::POST]],
'/media/metadata/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Media\Metadata\Create::class, [ R::POST]],
'/saved_searches/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\SavedSearches::class, [R::GET ]],
'/search/tweets[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Search\Tweets::class, [R::GET ]],
'/search[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Search\Tweets::class, [R::GET ]],
@ -118,7 +118,7 @@ $apiRoutes = [
'/statusnet/version[.{extension:json|xml|rss|atom}]' => [Module\Api\GNUSocial\GNUSocial\Version::class, [R::GET ]],
'/statuses' => [
'/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::DELETE, R::POST]],
'/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]],
'/followers[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Followers\Lists::class, [R::GET ]],
'/friends[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Friends\Lists::class, [R::GET ]],
'/friends_timeline[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Statuses\HomeTimeline::class, [R::GET ]],

View File

@ -12,6 +12,7 @@ use Friendica\Core\Protocol;
use Friendica\DI;
use Friendica\Model\Post;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\Api\Twitter\Media\Upload;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException;
use Friendica\Security\BasicAuth;
@ -990,18 +991,18 @@ class ApiTest extends FixtureTest
}
/**
* Test the api_media_upload() function.
* Test the \Friendica\Module\Api\Twitter\Media\Upload module.
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testApiMediaUpload()
{
$this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
api_media_upload();
(new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
}
/**
* Test the api_media_upload() function without an authenticated user.
* Test the \Friendica\Module\Api\Twitter\Media\Upload module without an authenticated user.
*
* @return void
*/
@ -1010,11 +1011,11 @@ class ApiTest extends FixtureTest
$this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
BasicAuth::setCurrentUserID();
$_SESSION['authenticated'] = false;
api_media_upload();
(new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
}
/**
* Test the api_media_upload() function with an invalid uploaded media.
* Test the \Friendica\Module\Api\Twitter\Media\Upload module with an invalid uploaded media.
*
* @return void
*/
@ -1027,11 +1028,11 @@ class ApiTest extends FixtureTest
'tmp_name' => 'tmp_name'
]
];
api_media_upload();
(new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
}
/**
* Test the api_media_upload() function with an valid uploaded media.
* Test the \Friendica\Module\Api\Twitter\Media\Upload module with an valid uploaded media.
*
* @return void
*/
@ -1048,14 +1049,14 @@ class ApiTest extends FixtureTest
'type' => 'image/png'
]
];
$app = DI::app();
DI::args()->setArgc(2);
$result = api_media_upload();
self::assertEquals('image/png', $result['media']['image']['image_type']);
self::assertEquals(1, $result['media']['image']['w']);
self::assertEquals(1, $result['media']['image']['h']);
self::assertNotEmpty($result['media']['image']['friendica_preview_url']);
$response = (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
$media = json_decode($response->getBody(), true);
self::assertEquals('image/png', $media['image']['image_type']);
self::assertEquals(1, $media['image']['w']);
self::assertEquals(1, $media['image']['h']);
self::assertNotEmpty($media['image']['friendica_preview_url']);
}
/**