From bb37c41bd7c481de88ec75ee2b93f85f4b6c5cba Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 30 Jul 2021 06:22:32 +0000 Subject: [PATCH] Scheduled posts can now be created --- src/Module/Api/Mastodon/ScheduledStatuses.php | 65 ++++++++++++++++++- src/Module/Api/Mastodon/Statuses.php | 12 +++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/Module/Api/Mastodon/ScheduledStatuses.php b/src/Module/Api/Mastodon/ScheduledStatuses.php index 1c8a22282..5e7f4b954 100644 --- a/src/Module/Api/Mastodon/ScheduledStatuses.php +++ b/src/Module/Api/Mastodon/ScheduledStatuses.php @@ -21,7 +21,9 @@ namespace Friendica\Module\Api\Mastodon; +use Friendica\App\Router; use Friendica\Core\System; +use Friendica\Database\DBA; use Friendica\DI; use Friendica\Module\BaseApi; @@ -30,6 +32,36 @@ use Friendica\Module\BaseApi; */ class ScheduledStatuses extends BaseApi { + public static function put(array $parameters = []) + { + self::checkAllowedScope(self::SCOPE_WRITE); + $uid = self::getCurrentUserID(); + + self::unsupported(Router::PUT); + } + + public static function delete(array $parameters = []) + { + self::checkAllowedScope(self::SCOPE_WRITE); + $uid = self::getCurrentUserID(); + + if (empty($parameters['id'])) { + DI::mstdnError()->UnprocessableEntity(); + } + + $condtion = ['id' => $parameters['id'], 'uid' => $uid]; + $post = DBA::selectFirst('delayed-post', ['id'], $condtion); + if (empty($post['id'])) { + DI::mstdnError()->RecordNotFound(); + } + + if (!DBA::delete('delayed-post', $condtion)) { + DI::mstdnError()->RecordNotFound(); + } + + System::jsonExit([]); + } + /** * @param array $parameters * @throws \Friendica\Network\HTTPException\InternalServerErrorException @@ -50,6 +82,37 @@ class ScheduledStatuses extends BaseApi 'min_id' => 0, // Return results immediately newer than ID ]); - System::jsonExit([]); + $params = ['order' => ['id' => true], 'limit' => $request['limit']]; + + $condition = ["`uid` = ? AND NOT `wid` IS NULL", $uid]; + + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]); + } + + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]); + } + + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); + $params['order'] = ['uri-id']; + } + + $posts = DBA::select('delayed-post', ['id'], $condition, $params); + + $statuses = []; + while ($post = DBA::fetch($posts)) { + self::setBoundaries($post['id']); + $statuses[] = DI::mstdnScheduledStatus()->createFromId($post['id'], $uid); + } + DBA::close($posts); + + if (!empty($request['min_id'])) { + array_reverse($statuses); + } + + self::setLinkHeader(); + System::jsonExit($statuses); } } diff --git a/src/Module/Api/Mastodon/Statuses.php b/src/Module/Api/Mastodon/Statuses.php index 1007bea0b..40bbca0cf 100644 --- a/src/Module/Api/Mastodon/Statuses.php +++ b/src/Module/Api/Mastodon/Statuses.php @@ -104,7 +104,7 @@ class Statuses extends BaseApi $item['deny_gid'] = $owner['deny_gid']; } else { $item['allow_cid'] = ''; - $item['allow_gid'] = [Group::FOLLOWERS]; + $item['allow_gid'] = '<' . Group::FOLLOWERS . '>'; $item['deny_cid'] = ''; $item['deny_gid'] = ''; } @@ -183,6 +183,16 @@ class Statuses extends BaseApi } } + if (!empty($request['scheduled_at'])) { + $item['guid'] = Item::guid($item, true); + $item['uri'] = Item::newURI($item['uid'], $item['guid']); + $id = Post\Delayed::add($item['uri'], $item, PRIORITY_HIGH, false, $request['scheduled_at']); + if (empty($id)) { + DI::mstdnError()->InternalError(); + } + System::jsonExit(DI::mstdnScheduledStatus()->createFromId($id, $uid)->toArray()); + } + $id = Item::insert($item, true); if (!empty($id)) { $item = Post::selectFirst(['uri-id'], ['id' => $id]);