From 8e76d13a968a9b1cb03fc559754275d8be98eb2c Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 6 Jan 2025 22:04:45 +0000 Subject: [PATCH 1/2] New function to handle "get" requests --- src/BaseModule.php | 18 +++++++++++++++++- src/Module/Api/Mastodon/Lists.php | 2 +- src/Module/Api/Mastodon/Lists/Accounts.php | 2 +- src/Module/Api/Mastodon/Markers.php | 2 +- src/Module/Api/Mastodon/Media.php | 2 +- src/Module/Api/Mastodon/PushSubscription.php | 2 +- src/Module/Api/Mastodon/ScheduledStatuses.php | 2 +- src/Module/Api/Mastodon/Statuses.php | 2 +- .../Api/Twitter/DirectMessages/Inbox.php | 2 +- 9 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/BaseModule.php b/src/BaseModule.php index a1f4c033c6..aa72249c73 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -164,6 +164,19 @@ abstract class BaseModule implements ICanHandleRequests { } + /** + * Module GET method to process submitted data + * + * Extend this method if the module is supposed to process GET requests. + * Doesn't display any content + * + * @param string[] $request The $_REQUEST content + * @return void + */ + protected function get(array $request = []) + { + } + /** * {@inheritDoc} */ @@ -221,7 +234,10 @@ abstract class BaseModule implements ICanHandleRequests case Router::PUT: $this->put($request); return $this->response->generate(); - } + case Router::GET: + $this->get($request); + break; + } $timestamp = microtime(true); // "rawContent" is especially meant for technical endpoints. diff --git a/src/Module/Api/Mastodon/Lists.php b/src/Module/Api/Mastodon/Lists.php index b2f3292d9e..2fe4e9df7f 100644 --- a/src/Module/Api/Mastodon/Lists.php +++ b/src/Module/Api/Mastodon/Lists.php @@ -99,7 +99,7 @@ class Lists extends BaseApi /** * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - protected function rawContent(array $request = []) + protected function get(array $request = []) { $this->checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); diff --git a/src/Module/Api/Mastodon/Lists/Accounts.php b/src/Module/Api/Mastodon/Lists/Accounts.php index a220301e7a..5d3798946c 100644 --- a/src/Module/Api/Mastodon/Lists/Accounts.php +++ b/src/Module/Api/Mastodon/Lists/Accounts.php @@ -53,7 +53,7 @@ class Accounts extends BaseApi /** * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - protected function rawContent(array $request = []) + protected function get(array $request = []) { $this->checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); diff --git a/src/Module/Api/Mastodon/Markers.php b/src/Module/Api/Mastodon/Markers.php index 11ae2e9324..5c9b6476a1 100644 --- a/src/Module/Api/Mastodon/Markers.php +++ b/src/Module/Api/Mastodon/Markers.php @@ -51,7 +51,7 @@ class Markers extends BaseApi /** * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - protected function rawContent(array $request = []) + protected function get(array $request = []) { $this->checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); diff --git a/src/Module/Api/Mastodon/Media.php b/src/Module/Api/Mastodon/Media.php index cc69d5a546..b15f1cc81c 100644 --- a/src/Module/Api/Mastodon/Media.php +++ b/src/Module/Api/Mastodon/Media.php @@ -116,7 +116,7 @@ class Media extends BaseApi /** * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - protected function rawContent(array $request = []) + protected function get(array $request = []) { $this->checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); diff --git a/src/Module/Api/Mastodon/PushSubscription.php b/src/Module/Api/Mastodon/PushSubscription.php index a83263bb24..835a8273d5 100644 --- a/src/Module/Api/Mastodon/PushSubscription.php +++ b/src/Module/Api/Mastodon/PushSubscription.php @@ -133,7 +133,7 @@ class PushSubscription extends BaseApi $this->response->addJsonContent([]); } - protected function rawContent(array $request = []): void + protected function get(array $request = []): void { $this->checkAllowedScope(self::SCOPE_PUSH); $uid = self::getCurrentUserID(); diff --git a/src/Module/Api/Mastodon/ScheduledStatuses.php b/src/Module/Api/Mastodon/ScheduledStatuses.php index e63d81522d..3cb8568639 100644 --- a/src/Module/Api/Mastodon/ScheduledStatuses.php +++ b/src/Module/Api/Mastodon/ScheduledStatuses.php @@ -48,7 +48,7 @@ class ScheduledStatuses extends BaseApi /** * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - protected function rawContent(array $request = []) + protected function get(array $request = []) { $this->checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); diff --git a/src/Module/Api/Mastodon/Statuses.php b/src/Module/Api/Mastodon/Statuses.php index 2a3da0288a..8d0fcdd209 100644 --- a/src/Module/Api/Mastodon/Statuses.php +++ b/src/Module/Api/Mastodon/Statuses.php @@ -350,7 +350,7 @@ class Statuses extends BaseApi /** * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - protected function rawContent(array $request = []) + protected function get(array $request = []) { $uid = self::getCurrentUserID(); diff --git a/src/Module/Api/Twitter/DirectMessages/Inbox.php b/src/Module/Api/Twitter/DirectMessages/Inbox.php index fbf5ac9096..fe017b16a6 100644 --- a/src/Module/Api/Twitter/DirectMessages/Inbox.php +++ b/src/Module/Api/Twitter/DirectMessages/Inbox.php @@ -18,7 +18,7 @@ use Friendica\Module\BaseApi; */ class Inbox extends DirectMessagesEndpoint { - protected function rawContent(array $request = []) + protected function get(array $request = []) { $this->checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); From e090fd44953d76e915b1a9bdb6e83e20f4e1f2d0 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 6 Jan 2025 22:15:41 +0000 Subject: [PATCH 2/2] Improved code style --- src/BaseModule.php | 2 +- src/Module/Api/Mastodon/Lists/Accounts.php | 3 +- src/Module/Api/Mastodon/Markers.php | 4 +-- src/Module/Api/Mastodon/PushSubscription.php | 4 +-- src/Module/Api/Mastodon/ScheduledStatuses.php | 11 +++--- src/Module/Api/Mastodon/Statuses.php | 35 ++++++++++--------- 6 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/BaseModule.php b/src/BaseModule.php index aa72249c73..bdfba74ce9 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -237,7 +237,7 @@ abstract class BaseModule implements ICanHandleRequests case Router::GET: $this->get($request); break; - } + } $timestamp = microtime(true); // "rawContent" is especially meant for technical endpoints. diff --git a/src/Module/Api/Mastodon/Lists/Accounts.php b/src/Module/Api/Mastodon/Lists/Accounts.php index 5d3798946c..796ced67b8 100644 --- a/src/Module/Api/Mastodon/Lists/Accounts.php +++ b/src/Module/Api/Mastodon/Lists/Accounts.php @@ -7,7 +7,6 @@ namespace Friendica\Module\Api\Mastodon\Lists; -use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Circle; @@ -40,7 +39,7 @@ class Accounts extends BaseApi $this->checkAllowedScope(self::SCOPE_WRITE); $request = $this->getRequest([ - 'account_ids' => [], // Array of account IDs to add to the list + 'account_ids' => [], // Array of account IDs to add to the list ], $request); if (empty($request['account_ids']) || empty($this->parameters['id'])) { diff --git a/src/Module/Api/Mastodon/Markers.php b/src/Module/Api/Mastodon/Markers.php index 5c9b6476a1..358dc6c01a 100644 --- a/src/Module/Api/Mastodon/Markers.php +++ b/src/Module/Api/Mastodon/Markers.php @@ -36,7 +36,7 @@ class Markers extends BaseApi } $condition = ['application-id' => $application['id'], 'uid' => $uid, 'timeline' => $timeline]; - $marker = DBA::selectFirst('application-marker', [], $condition); + $marker = DBA::selectFirst('application-marker', [], $condition); if (!empty($marker['version'])) { $version = $marker['version'] + 1; } else { @@ -62,7 +62,7 @@ class Markers extends BaseApi private function fetchTimelines(int $application_id, int $uid): \stdClass { - $values = new \stdClass(); + $values = new \stdClass(); $markers = DBA::select('application-marker', [], ['application-id' => $application_id, 'uid' => $uid]); while ($marker = DBA::fetch($markers)) { $values->{$marker['timeline']} = [ diff --git a/src/Module/Api/Mastodon/PushSubscription.php b/src/Module/Api/Mastodon/PushSubscription.php index 835a8273d5..0c8e1d5257 100644 --- a/src/Module/Api/Mastodon/PushSubscription.php +++ b/src/Module/Api/Mastodon/PushSubscription.php @@ -49,9 +49,9 @@ class PushSubscription extends BaseApi $subscription = [ 'application-id' => $application['id'], 'uid' => $uid, - 'endpoint' => $request['subscription']['endpoint'] ?? '', + 'endpoint' => $request['subscription']['endpoint'] ?? '', 'pubkey' => $request['subscription']['keys']['p256dh'] ?? '', - 'secret' => $request['subscription']['keys']['auth'] ?? '', + 'secret' => $request['subscription']['keys']['auth'] ?? '', Notification::TYPE_FOLLOW => filter_var($request['data']['alerts'][Notification::TYPE_FOLLOW] ?? false, FILTER_VALIDATE_BOOLEAN), Notification::TYPE_LIKE => filter_var($request['data']['alerts'][Notification::TYPE_LIKE] ?? false, FILTER_VALIDATE_BOOLEAN), Notification::TYPE_RESHARE => filter_var($request['data']['alerts'][Notification::TYPE_RESHARE] ?? false, FILTER_VALIDATE_BOOLEAN), diff --git a/src/Module/Api/Mastodon/ScheduledStatuses.php b/src/Module/Api/Mastodon/ScheduledStatuses.php index 3cb8568639..9203380432 100644 --- a/src/Module/Api/Mastodon/ScheduledStatuses.php +++ b/src/Module/Api/Mastodon/ScheduledStatuses.php @@ -8,7 +8,6 @@ namespace Friendica\Module\Api\Mastodon; use Friendica\App\Router; -use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Post; @@ -58,10 +57,10 @@ class ScheduledStatuses extends BaseApi } $request = $this->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 + '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 ], $request); $params = ['order' => ['id' => true], 'limit' => $request['limit']]; @@ -77,7 +76,7 @@ class ScheduledStatuses extends BaseApi } if (!empty($request['min_id'])) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); $params['order'] = ['uri-id']; } diff --git a/src/Module/Api/Mastodon/Statuses.php b/src/Module/Api/Mastodon/Statuses.php index 8d0fcdd209..9bf6ca382e 100644 --- a/src/Module/Api/Mastodon/Statuses.php +++ b/src/Module/Api/Mastodon/Statuses.php @@ -64,18 +64,18 @@ class Statuses extends BaseApi throw new HTTPException\NotFoundException('Item with URI ID ' . $this->parameters['id'] . ' not found for user ' . $uid . '.'); } - $item['title'] = ''; - $item['uid'] = $post['uid']; - $item['body'] = $this->formatStatus($request['status'], $uid); - $item['network'] = $post['network']; - $item['gravity'] = $post['gravity']; - $item['verb'] = $post['verb']; - $item['allow_cid'] = $post['allow_cid']; - $item['allow_gid'] = $post['allow_gid']; - $item['deny_cid'] = $post['deny_cid']; - $item['deny_gid'] = $post['deny_gid']; - $item['app'] = $this->getApp(); - $item['sensitive'] = $request['sensitive']; + $item['title'] = ''; + $item['uid'] = $post['uid']; + $item['body'] = $this->formatStatus($request['status'], $uid); + $item['network'] = $post['network']; + $item['gravity'] = $post['gravity']; + $item['verb'] = $post['verb']; + $item['allow_cid'] = $post['allow_cid']; + $item['allow_gid'] = $post['allow_gid']; + $item['deny_cid'] = $post['deny_cid']; + $item['deny_gid'] = $post['deny_gid']; + $item['app'] = $this->getApp(); + $item['sensitive'] = $request['sensitive']; if (!empty($request['language'])) { $item['language'] = json_encode([$request['language'] => 1]); @@ -91,7 +91,7 @@ class Statuses extends BaseApi if (!isset($request['friendica']['title']) && $post['gravity'] == Item::GRAVITY_PARENT && DI::pConfig()->get($uid, 'system', 'api_spoiler_title', true)) { $item['title'] = $spoiler_text; } else { - $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $spoiler_text . "[/abstract]\n" . $item['body']; + $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $spoiler_text . "[/abstract]\n" . $item['body']; $item['content-warning'] = BBCode::toPlaintext($spoiler_text); } } @@ -307,7 +307,8 @@ class Statuses extends BaseApi if (!empty($request['scheduled_at'])) { $item['guid'] = Item::guid($item, true); - $item['uri'] = Item::newURI($item['guid']); + $item['uri'] = Item::newURI($item['guid']); + $id = Post\Delayed::add($item['uri'], $item, Worker::PRIORITY_HIGH, Post\Delayed::PREPARED, DateTimeFormat::utc($request['scheduled_at'])); if (empty($id)) { $this->logAndJsonError(500, $this->errorFactory->InternalError()); @@ -385,7 +386,7 @@ class Statuses extends BaseApi foreach ($media_ids as $id) { if (DI::mstdnAttachment()->isAttach($id) && Attach::exists(['id' => substr($id, 7)])) { - $attach = Attach::selectFirst([], ['id' => substr($id, 7)]); + $attach = Attach::selectFirst([], ['id' => substr($id, 7)]); $attachment = [ 'type' => Post\Media::getType($attach['filetype']), 'mimetype' => $attach['filetype'], @@ -422,8 +423,8 @@ class Statuses extends BaseApi ]; 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'] = 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;