Added API calls, removed fields

This commit is contained in:
Michael 2021-07-29 10:34:31 +00:00
parent fbbe9a3c1f
commit 6ea3d4aa61
9 changed files with 207 additions and 20 deletions

View file

@ -311,6 +311,14 @@ abstract class DI
return self::$dice->create(Factory\Api\Mastodon\Status::class);
}
/**
* @return Factory\Api\Mastodon\ScheduledStatus
*/
public static function mstdnScheduledStatus()
{
return self::$dice->create(Factory\Api\Mastodon\ScheduledStatus::class);
}
/**
* @return Factory\Api\Mastodon\ListEntity
*/

View file

@ -0,0 +1,59 @@
<?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\Factory\Api\Mastodon;
use Friendica\BaseFactory;
use Friendica\Database\Database;
use Friendica\Model\Post;
use Friendica\Network\HTTPException;
use Psr\Log\LoggerInterface;
class ScheduledStatus extends BaseFactory
{
/** @var Database */
private $dba;
public function __construct(LoggerInterface $logger, Database $dba)
{
parent::__construct($logger);
$this->dba = $dba;
}
/**
* @param int $id Id of the delayed post
* @param int $uid Post user
*
* @return \Friendica\Object\Api\Mastodon\ScheduledStatus
* @throws HTTPException\InternalServerErrorException
*/
public function createFromId(int $id, int $uid): \Friendica\Object\Api\Mastodon\ScheduledStatus
{
$delayed_post = $this->dba->selectFirst('delayed-post', [], ['id' => $id, 'uid' => $uid]);
if (empty($delayed_post)) {
throw new HTTPException\NotFoundException('Scheduled status with ID ' . $id . ' not found for user ' . $uid . '.');
}
$parameters = Post\Delayed::getParametersForid($delayed_post['id']);
return new \Friendica\Object\Api\Mastodon\ScheduledStatus($delayed_post, $parameters);
}
}

View file

@ -73,12 +73,9 @@ class Delayed
$delayed_post = [
'uri' => $uri,
'title' => $item['title'],
'body' => $item['body'],
'private' => $item['private'],
'wid' => $wid,
'uid' => $item['uid'],
'delayed' => $delayed,
'wid' => $wid,
];
return DBA::insert('delayed-post', $delayed_post, Database::INSERT_IGNORE);
@ -110,6 +107,40 @@ class Delayed
return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]);
}
/**
* Fetch parameters for delayed posts
*
* @param integer $id
* @return array
*/
public static function getParametersForid(int $id)
{
$delayed = DBA::selectFirst('delayed-post', ['id', 'wid', 'delayed'], ['id' => $id]);
if (empty($delayed['wid'])) {
return [];
}
$worker = DBA::selectFirst('workerqueue', ['parameter'], ['id' => $delayed['wid'], 'command' => 'DelayedPublish']);
if (empty($worker)) {
return [];
}
$parameters = json_decode($worker['parameter']);
if (empty($parameters)) {
return [];
}
return [
'parameters' => $delayed,
'item' => $parameters[0],
'notify' => $parameters[1],
'taglist' => $parameters[2],
'attachments' => $parameters[3],
'unprepared' => $parameters[4],
'uri' => $parameters[5],
];
}
/**
* Publish a delayed post
*

View file

@ -22,6 +22,7 @@
namespace Friendica\Module\Api\Mastodon;
use Friendica\Core\System;
use Friendica\DI;
use Friendica\Module\BaseApi;
/**
@ -35,6 +36,20 @@ class ScheduledStatuses extends BaseApi
*/
public static function rawContent(array $parameters = [])
{
self::checkAllowedScope(self::SCOPE_READ);
$uid = self::getCurrentUserID();
if (isset($parameters['id'])) {
System::jsonExit(DI::mstdnScheduledStatus()->createFromId($parameters['id'], $uid));
}
$request = self::getRequest([
'limit' => 20, // Max number of results to return. Defaults to 20.
'max_id' => 0, // Return results older than ID
'since_id' => 0, // Return results newer than ID
'min_id' => 0, // Return results immediately newer than ID
]);
System::jsonExit([]);
}
}

View file

@ -0,0 +1,83 @@
<?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\Object\Api\Mastodon;
use Friendica\BaseDataTransferObject;
use Friendica\Content\Text\BBCode;
use Friendica\Util\DateTimeFormat;
/**
* Class ScheduledStatus
*
* @see https://docs.joinmastodon.org/entities/scheduledstatus
*/
class ScheduledStatus extends BaseDataTransferObject
{
/** @var string */
protected $id;
/** @var string (Datetime) */
protected $scheduled_at;
/** @var array */
protected $params = [
'text' => '',
'media_ids' => null,
'sensitive' => null,
'spoiler_text' => null,
'visibility' => '',
'scheduled_at' => null,
'poll' => null,
'idempotency' => null,
'in_reply_to_id' => null,
'application_id' => ''
];
/** @var Attachment */
protected $media_attachments = [];
/**
* Creates a status record from a delayed-post record.
*
* @param array $delayed_post
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(array $delayed_post, array $parameters)
{
$visibility = ['public', 'private', 'unlisted'];
$this->id = (string)$delayed_post['uri-id'];
$this->scheduled_at = DateTimeFormat::utc($delayed_post['scheduled_at'], DateTimeFormat::JSON);
$this->params = [
'text' => BBCode::convert(BBCode::setMentionsToNicknames($parameters['item']['body'] ?? ''), false, BBCode::API),
'media_ids' => null,
'sensitive' => null,
'spoiler_text' => $parameters['item']['title'] ?? '',
'visibility' => $visibility[$parameters['item']['private']],
'scheduled_at' => $this->scheduled_at,
'poll' => null,
'idempotency' => null,
'in_reply_to_id' => null,
'application_id' => ''
];
$this->media_attachments = [];
}
}