friendica/src/Module/Api/Twitter/Statuses/Update.php

193 lines
6.6 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* @copyright Copyright (C) 2010-2024, 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\HTML;
use Friendica\Content\Text\Markdown;
2022-03-02 18:17:07 +01:00
use Friendica\Core\Protocol;
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
2021-11-28 14:52:39 +01:00
*/
class Update extends BaseApi
{
2021-12-09 22:08:31 +01:00
public function post(array $request = [])
{
$this->checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID();
$owner = User::getOwnerDataById($uid);
$request = self::getRequest([
'htmlstatus' => '',
'status' => '',
'title' => '',
'in_reply_to_status_id' => 0,
'lat' => 0,
'long' => 0,
2021-11-29 19:03:34 +01:00
'media_ids' => '',
'source' => '',
'include_entities' => false,
'contact_allow' => $owner['allow_cid'],
'circle_allow' => $owner['allow_gid'],
'contact_deny' => $owner['deny_cid'],
'circle_deny' => $owner['deny_gid'],
], $request);
if (!empty($request['htmlstatus'])) {
$body = HTML::toBBCodeVideo($request['htmlstatus']);
$config = HTMLPurifier_Config::createDefault();
$config->set('Cache.DefinitionImpl', null);
$purifier = new HTMLPurifier($config);
2021-11-28 14:52:39 +01:00
$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']);
}
2021-11-28 14:52:39 +01:00
$item = [];
2022-03-02 18:17:07 +01:00
$item['network'] = Protocol::DFRN;
$item['uid'] = $uid;
$item['verb'] = Activity::POST;
$item['contact-id'] = $owner['id'];
2022-03-02 18:17:07 +01:00
$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']);
}
2021-11-28 14:46:30 +01:00
$aclFormatter = DI::aclFormatter();
$item['allow_cid'] = $aclFormatter->toString($request['contact_allow']);
$item['allow_gid'] = $aclFormatter->toString($request['circle_allow']);
$item['deny_cid'] = $aclFormatter->toString($request['contact_deny']);
$item['deny_gid'] = $aclFormatter->toString($request['circle_deny']);
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']) {
2022-03-08 21:46:26 +01:00
$parent = Post::selectFirst(['uri'], ['uri-id' => $request['in_reply_to_status_id'], 'uid' => [0, $uid]]);
2021-11-28 14:52:39 +01:00
$item['thr-parent'] = $parent['uri'];
$item['gravity'] = Item::GRAVITY_COMMENT;
$item['object-type'] = Activity\ObjectType::COMMENT;
} else {
$this->checkThrottleLimit();
$item['gravity'] = Item::GRAVITY_PARENT;
$item['object-type'] = Activity\ObjectType::NOTE;
}
2022-03-02 07:59:07 +01:00
$item = DI::contentItem()->expandTags($item);
2021-12-09 22:08:31 +01:00
if (!empty($request['media_ids'])) {
$ids = explode(',', $request['media_ids']);
2021-11-29 19:03:34 +01:00
} elseif (!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::setPermissionForResource($media[0]['resource-id'], $uid, $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']);
$ext = Images::getExtensionByMimeType($media[0]['type']);
2021-11-28 14:58:26 +01:00
$attachment = [
'type' => Post\Media::IMAGE,
'mimetype' => $media[0]['type'],
'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . $ext,
2021-11-28 14:52:39 +01:00
'size' => $media[0]['datasize'],
'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
'description' => $media[0]['desc'] ?? '',
2021-11-28 14:52:39 +01:00
'width' => $media[0]['width'],
2021-11-28 14:58:26 +01:00
'height' => $media[0]['height']
];
if (count($media) > 1) {
$attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . $ext;
2021-11-28 14:52:39 +01:00
$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()->addFormattedContent('status', ['status' => $status_info], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
2021-11-29 05:38:58 +01:00
return;
}
}
DI::mstdnError()->InternalError();
}
}