API: Added more functions, fixed function names

This commit is contained in:
Michael 2021-11-28 13:34:00 +00:00
parent 8d5c03755d
commit 222b35684d
19 changed files with 322 additions and 296 deletions

View file

@ -40,7 +40,7 @@ use Friendica\Module\BaseApi;
*/
class Activity extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();

View file

@ -30,7 +30,7 @@ use Friendica\Module\BaseApi;
*/
class Setseen extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();

View file

@ -32,7 +32,7 @@ use Friendica\Network\HTTPException\BadRequestException;
*/
class Delete extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();

View file

@ -33,7 +33,7 @@ use Friendica\Network\HTTPException\BadRequestException;
*/
class Update extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();

View file

@ -38,7 +38,7 @@ use Friendica\Network\HTTPException\NotFoundException;
*/
class Seen extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();

View file

@ -33,7 +33,7 @@ use Friendica\Network\HTTPException\InternalServerErrorException;
*/
class Delete extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();

View file

@ -34,7 +34,7 @@ use Friendica\Network\HTTPException\InternalServerErrorException;
*/
class Delete extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();

View file

@ -32,7 +32,7 @@ use Friendica\Network\HTTPException\InternalServerErrorException;
*/
class Update extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();

View file

@ -31,7 +31,7 @@ use Friendica\Network\HTTPException\BadRequestException;
*/
class Create extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();

View file

@ -31,7 +31,7 @@ use Friendica\Network\HTTPException\BadRequestException;
*/
class Destroy extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();

View file

@ -37,7 +37,7 @@ use Friendica\Network\HTTPException;
*/
class Destroy extends ContactEndpoint
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();

View file

@ -34,7 +34,7 @@ use Friendica\Util\Network;
*/
class Create extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();

View file

@ -35,7 +35,7 @@ use Friendica\Network\HTTPException\InternalServerErrorException;
*/
class Upload extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
$uid = BaseApi::getCurrentUserID();

View file

@ -34,7 +34,7 @@ use Friendica\Model\Item;
*/
class Destroy extends BaseApi
{
protected function rawContent(array $request = [])
protected function post(array $request = [], array $post = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
$uid = BaseApi::getCurrentUserID();

View file

@ -0,0 +1,97 @@
<?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\Statuses;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Protocol;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Network\HTTPException\ForbiddenException;
use Friendica\Network\HTTPException\InternalServerErrorException;
/**
* Repeats a status.
*
* @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-retweet-id
*/
class Retweet extends BaseApi
{
protected function post(array $request = [], array $post = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();
$id = $request['id'] ?? 0;
if (empty($id)) {
throw new BadRequestException('Item id not specified');
}
$fields = ['uri-id', 'network', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink'];
$item = Post::selectFirst($fields, ['id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
if (DBA::isResult($item) && !empty($item['body'])) {
if (in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::TWITTER])) {
if (!Item::performActivity($id, 'announce', $uid)) {
throw new InternalServerErrorException();
}
$item_id = $id;
} else {
if (strpos($item['body'], "[/share]") !== false) {
$pos = strpos($item['body'], "[share");
$post = substr($item['body'], $pos);
} else {
$post = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'], $item['plink'], $item['created'], $item['guid']);
if (!empty($item['title'])) {
$post .= '[h3]' . $item['title'] . "[/h3]\n";
}
$post .= $item['body'];
$post .= "[/share]";
}
$item = [
'uid' => $uid,
'body' => $post,
'app' => $request['source'] ?? '',
];
if (empty($item['app']) && !empty(self::getCurrentApplication()['name'])) {
$item['app'] = self::getCurrentApplication()['name'];
}
$item_id = Item::insert($item, true);
}
} else {
throw new ForbiddenException();
}
$status_info = DI::twitterStatus()->createFromItemId($item_id, $uid)->toArray();
DI::apiResponse()->exit('status', ['status' => $status_info], $this->parameters['extension'] ?? null);
}
}

View file

@ -0,0 +1,188 @@
<?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\Statuses;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
use Friendica\Content\Text\Markdown;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\Item;
use Friendica\Model\Photo;
use Friendica\Model\Post;
use Friendica\Model\User;
use Friendica\Module\BaseApi;
use Friendica\Protocol\Activity;
use Friendica\Util\Images;
use HTMLPurifier;
use HTMLPurifier_Config;
/**
* Updates the users current status.
*
* @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update
*/
class Update extends BaseApi
{
public function post(array $request = [], array $post = [])
{
self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();
$request = self::getRequest([
'htmlstatus' => '',
'status' => '',
'title' => '',
'in_reply_to_status_id' => 0,
'lat' => 0,
'long' => 0,
'media_ids' => [],
'source' => '',
'include_entities' => false,
], $request);
$owner = User::getOwnerDataById($uid);
if (!empty($request['htmlstatus'])) {
$body = HTML::toBBCodeVideo($request['htmlstatus']);
$config = HTMLPurifier_Config::createDefault();
$config->set('Cache.DefinitionImpl', null);
$purifier = new HTMLPurifier($config);
$body = $purifier->purify($body);
$body = HTML::toBBCode($request['htmlstatus']);
} else {
// The imput is defined as text. So we can use Markdown for some enhancements
$body = Markdown::toBBCode($request['status']);
}
// Avoids potential double expansion of existing links
$body = BBCode::performWithEscapedTags($body, ['url'], function ($body) {
return BBCode::expandTags($body);
});
$item = [];
$item['uid'] = $uid;
$item['verb'] = Activity::POST;
$item['contact-id'] = $owner['id'];
$item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
$item['title'] = $request['title'];
$item['body'] = $body;
$item['app'] = $request['source'];
if (empty($item['app']) && !empty(self::getCurrentApplication()['name'])) {
$item['app'] = self::getCurrentApplication()['name'];
}
if (!empty($request['lat']) && !empty($request['long'])) {
$item['coord'] = sprintf("%s %s", $request['lat'], $request['long']);
}
$item['allow_cid'] = $owner['allow_cid'];
$item['allow_gid'] = $owner['allow_gid'];
$item['deny_cid'] = $owner['deny_cid'];
$item['deny_gid'] = $owner['deny_gid'];
if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
$item['private'] = Item::PRIVATE;
} elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
$item['private'] = Item::UNLISTED;
} else {
$item['private'] = Item::PUBLIC;
}
if ($request['in_reply_to_status_id']) {
$parent = Post::selectFirst(['uri'], ['id' => $request['in_reply_to_status_id'], 'uid' => [0, $uid]]);
$item['thr-parent'] = $parent['uri'];
$item['gravity'] = GRAVITY_COMMENT;
$item['object-type'] = Activity\ObjectType::COMMENT;
} else {
self::checkThrottleLimit();
$item['gravity'] = GRAVITY_PARENT;
$item['object-type'] = Activity\ObjectType::NOTE;
}
$ids = $request['media_ids'];
if (!empty($_FILES['media'])) {
// upload the image if we have one
$picture = Photo::upload($uid, $_FILES['media']);
if (!empty($picture)) {
$ids[] = $picture['id'];
}
}
if (!empty($ids)) {
$item['object-type'] = Activity\ObjectType::IMAGE;
$item['post-type'] = Item::PT_IMAGE;
$item['attachments'] = [];
foreach ($ids as $id) {
$media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo`
WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
ORDER BY `photo`.`width` DESC LIMIT 2", $id, $uid));
if (empty($media)) {
continue;
}
Photo::setPermissionForRessource($media[0]['resource-id'], $uid, $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']);
$ressources[] = $media[0]['resource-id'];
$phototypes = Images::supportedTypes();
$ext = $phototypes[$media[0]['type']];
$attachment = ['type' => Post\Media::IMAGE, 'mimetype' => $media[0]['type'],
'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext,
'size' => $media[0]['datasize'],
'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
'description' => $media[0]['desc'] ?? '',
'width' => $media[0]['width'],
'height' => $media[0]['height']];
if (count($media) > 1) {
$attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext;
$attachment['preview-width'] = $media[1]['width'];
$attachment['preview-height'] = $media[1]['height'];
}
$item['attachments'][] = $attachment;
}
}
$id = Item::insert($item, true);
if (!empty($id)) {
$item = Post::selectFirst(['uri-id'], ['id' => $id]);
if (!empty($item['uri-id'])) {
// output the post that we just posted.
$status_info = DI::twitterStatus()->createFromUriId($item['uri-id'], $uid, $request['include_entities'])->toArray();
DI::apiResponse()->exit('status', ['status' => $status_info], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
}
}
DI::mstdnError()->InternalError();
}
}