From 70259965dfcd278d7f4e91a30bc08b1d73bd12bf Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 19 May 2021 21:56:50 +0000 Subject: [PATCH 01/10] API: Support for private mails --- doc/API-Mastodon.md | 6 +- src/DI.php | 8 ++ src/Factory/Api/Mastodon/Conversation.php | 60 +++++++++++++++ src/Factory/Api/Mastodon/Status.php | 50 ++++++++++++ src/Module/Api/Mastodon/Conversation.php | 76 +++++++++++++++++++ .../Api/Mastodon/Conversations/Read.php | 47 ++++++++++++ src/Object/Api/Mastodon/Conversation.php | 54 +++++++++++++ static/routes.config.php | 6 +- 8 files changed, 301 insertions(+), 6 deletions(-) create mode 100644 src/Factory/Api/Mastodon/Conversation.php create mode 100644 src/Module/Api/Mastodon/Conversation.php create mode 100644 src/Module/Api/Mastodon/Conversations/Read.php create mode 100644 src/Object/Api/Mastodon/Conversation.php diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index e94d3c04ed..5cb4535547 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -51,6 +51,9 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en - [`GET /api/v1/apps/verify_credentials`](https://docs.joinmastodon.org/methods/apps/) - [`GET /api/v1/blocks`](https://docs.joinmastodon.org/methods/accounts/blocks/) - [`GET /api/v1/bookmarks`](https://docs.joinmastodon.org/methods/accounts/bookmarks/) +- [`GET /api/v1/conversations`](https://docs.joinmastodon.org/methods/timelines/conversations/) +- [`DELETE /api/v1/conversations/:id`](https://docs.joinmastodon.org/methods/timelines/conversations/) +- [`POST /api/v1/conversations/:id/read`](https://docs.joinmastodon.org/methods/timelines/conversations/) - [`GET /api/v1/custom_emojis`](https://docs.joinmastodon.org/methods/instance/custom_emojis/) - Doesn't return unicode emojis since they aren't using an image URL @@ -118,9 +121,6 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en These emdpoints are planned to be implemented - [`PATCH /api/v1/accounts/update_credentials`](https://docs.joinmastodon.org/methods/accounts/) -- [`GET /api/v1/conversations`](https://docs.joinmastodon.org/methods/timelines/conversations/) -- [`DELETE /api/v1/conversations/:id`](https://docs.joinmastodon.org/methods/timelines/conversations/) -- [`POST /api/v1/conversations/:id/read`](https://docs.joinmastodon.org/methods/timelines/conversations/) - [`GET /api/v1/instance/activity`](https://docs.joinmastodon.org/methods/instance#weekly-activity) - [`GET /api/v1/timelines/direct`](https://docs.joinmastodon.org/methods/timelines/) diff --git a/src/DI.php b/src/DI.php index 9e77943bda..944fad805d 100644 --- a/src/DI.php +++ b/src/DI.php @@ -263,6 +263,14 @@ abstract class DI return self::$dice->create(Factory\Api\Mastodon\Card::class); } + /** + * @return Factory\Api\Mastodon\Conversation + */ + public static function mstdnConversation() + { + return self::$dice->create(Factory\Api\Mastodon\Conversation::class); + } + /** * @return Factory\Api\Mastodon\Emoji */ diff --git a/src/Factory/Api/Mastodon/Conversation.php b/src/Factory/Api/Mastodon/Conversation.php new file mode 100644 index 0000000000..f30ccf4394 --- /dev/null +++ b/src/Factory/Api/Mastodon/Conversation.php @@ -0,0 +1,60 @@ +. + * + */ + +namespace Friendica\Factory\Api\Mastodon; + +use Friendica\BaseFactory; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\Contact; + +class Conversation extends BaseFactory +{ + public function CreateFromConvId(int $id) + { + $accounts = []; + $unread = false; + $last_status = null; + + $ids = []; + $mails = DBA::select('mail', ['id', 'from-url', 'uid', 'seen'], ['convid' => $id], ['order' => ['id' => true]]); + while ($mail = DBA::fetch($mails)) { + if (!$mail['seen']) { + $unread = true; + } + + $id = Contact::getIdForURL($mail['from-url'], 0, false); + if (in_array($id, $ids)) { + continue; + } + + $ids[] = $id; + + if (empty($last_status)) { + $last_status = DI::mstdnStatus()->createFromMailId($mail['id'], $mail['uid']); + } + + $accounts[] = DI::mstdnAccount()->createFromContactId($id, $mail['uid']); + } + + return new \Friendica\Object\Api\Mastodon\Conversation($id, $accounts, $unread, $last_status); + } +} diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index cc8b7e6d87..f64dde29bb 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -27,6 +27,7 @@ use Friendica\Content\ContactSelector; use Friendica\Content\Text\BBCode; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Model\Verb; use Friendica\Network\HTTPException; @@ -119,4 +120,53 @@ class Status extends BaseFactory return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare); } + + /** + * @param int $uriId id of the mail + * @param int $uid mail user + * @return \Friendica\Object\Api\Mastodon\Status + * @throws HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public function createFromMailId(int $id, $uid = 0) + { + $mail = DBA::selectFirst('mail', [], ['id' => $id, 'uid' => $uid]); + if (!$mail) { + DI::mstdnError()->RecordNotFound(); + } + + $conv = DBA::selectFirst('conv', ['subject'], ['id' => $mail['convid'], 'uid' => $uid]); + if (!$conv) { + DI::mstdnError()->RecordNotFound(); + } + + $account = DI::mstdnAccount()->createFromContactId($mail['contact-id']); + + $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(0, 0, 0); + + $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false); + + $sensitive = false; + $application = new \Friendica\Object\Api\Mastodon\Application(''); + $mentions = []; + $tags = []; + $card = new \Friendica\Object\Api\Mastodon\Card([]); + $attachments = []; + $reshare = []; + + $item = [ + 'uri-id' => $mail['id'], + 'created' => $mail['created'], + 'thr-parent-id' => 0, + 'parent-author-id' => 0, + 'title' => $conv['subject'], + 'private' => Item::PRIVATE, + 'language' => '', + 'uri' => $mail['uri'], + 'plink' => '', + 'body' => BBCode::convert($mail['body'], false, BBCode::API) + ]; + + return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare); + } } diff --git a/src/Module/Api/Mastodon/Conversation.php b/src/Module/Api/Mastodon/Conversation.php new file mode 100644 index 0000000000..feeb52e243 --- /dev/null +++ b/src/Module/Api/Mastodon/Conversation.php @@ -0,0 +1,76 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon; + +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Module\BaseApi; + +/** + * @see https://docs.joinmastodon.org/methods/timelines/conversations/ + */ +class Conversation extends BaseApi +{ + public static function delete(array $parameters = []) + { + self::login(self::SCOPE_WRITE); + $uid = self::getCurrentUserID(); + + if (!empty($parameters['id'])) { + DI::mstdnError()->UnprocessableEntity(); + } + + DBA::delete('conv', ['id' => $parameters['id'], 'uid' => $uid]); + DBA::delete('mail', ['convid' => $parameters['id'], 'uid' => $uid]); + + System::jsonExit([]); + } + + /** + * @param array $parameters + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public static function rawContent(array $parameters = []) + { + self::login(self::SCOPE_READ); + $uid = self::getCurrentUserID(); + + $request = self::getRequest([ + 'limit' => 20, // Maximum number of results. Defaults to 20. Max 40. + 'max_id' => 0, // Return results older than this ID. Use HTTP Link header to paginate. + 'since_id' => 0, // Return results newer than this ID. Use HTTP Link header to paginate. + 'min_id' => 0, // Return results immediately newer than this ID. Use HTTP Link header to paginate. + ]); + + $params = ['order' => ['id' => true], 'limit' => $request['limit']]; + + $convs = DBA::select('conv', ['id'], ['uid' => $uid], $params); + $conversations = []; + + foreach ($convs as $conv) { + $conversations[] = DI::mstdnConversation()->CreateFromConvId($conv['id']); + } + + System::jsonExit($conversations); + } +} diff --git a/src/Module/Api/Mastodon/Conversations/Read.php b/src/Module/Api/Mastodon/Conversations/Read.php new file mode 100644 index 0000000000..346e78a501 --- /dev/null +++ b/src/Module/Api/Mastodon/Conversations/Read.php @@ -0,0 +1,47 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon\Conversation; + +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Module\BaseApi; + +/** + * @see https://docs.joinmastodon.org/methods/timelines/conversations/ + */ +class Read extends BaseApi +{ + public static function post(array $parameters = []) + { + self::login(self::SCOPE_WRITE); + $uid = self::getCurrentUserID(); + + if (!empty($parameters['id'])) { + DI::mstdnError()->UnprocessableEntity(); + } + + DBA::update('mail', ['seen' => true], ['convid' => $parameters['id'], 'uid' => $uid]); + + System::jsonExit(DI::mstdnConversation()->CreateFromConvId($parameters['id'])->toArray()); + } +} diff --git a/src/Object/Api/Mastodon/Conversation.php b/src/Object/Api/Mastodon/Conversation.php new file mode 100644 index 0000000000..911b4ad8af --- /dev/null +++ b/src/Object/Api/Mastodon/Conversation.php @@ -0,0 +1,54 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon; + +use Friendica\BaseDataTransferObject; + +/** + * Class Conversation + * + * @see https://docs.joinmastodon.org/entities/conversation/ + */ +class Conversation extends BaseDataTransferObject +{ + //Required attributes + /** @var string */ + protected $id; + /** @var array */ + protected $accounts; + /** @var bool */ + protected $unread; + + // Optional attributes + /** + * @var Status + */ + protected $last_status = true; + + public function __construct(string $id, array $accounts, bool $unread, \Friendica\Object\Api\Mastodon\Status $last_status) + { + $this->id = $id; + $this->accounts = $accounts; + $this->unread = $unread; + $this->last_status = $last_status; + } +} diff --git a/static/routes.config.php b/static/routes.config.php index a753b893d2..6b87d55bce 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -90,9 +90,9 @@ return [ '/apps/verify_credentials' => [Module\Api\Mastodon\Apps\VerifyCredentials::class, [R::GET ]], '/blocks' => [Module\Api\Mastodon\Blocks::class, [R::GET ]], '/bookmarks' => [Module\Api\Mastodon\Bookmarks::class, [R::GET ]], - '/conversations' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // @todo - '/conversations/{id:\d+}' => [Module\Api\Mastodon\Unimplemented::class, [R::DELETE ]], // @todo - '/conversations/{id:\d+}/read' => [Module\Api\Mastodon\Unimplemented::class, [R::POST ]], // @todo + '/conversations' => [Module\Api\Mastodon\Conversation::class, [R::GET ]], + '/conversations/{id:\d+}' => [Module\Api\Mastodon\Conversation::class, [R::DELETE ]], + '/conversations/{id:\d+}/read' => [Module\Api\Mastodon\Conversation\Read::class, [R::POST ]], '/custom_emojis' => [Module\Api\Mastodon\CustomEmojis::class, [R::GET ]], '/domain_blocks' => [Module\Api\Mastodon\Unimplemented::class, [R::GET, R::POST, R::DELETE]], // not supported '/directory' => [Module\Api\Mastodon\Directory::class, [R::GET ]], From c1212dce030670fdf117d8f7c1cb66487955515d Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 19 May 2021 22:09:25 +0000 Subject: [PATCH 02/10] Indentions --- src/Factory/Api/Mastodon/Conversation.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Factory/Api/Mastodon/Conversation.php b/src/Factory/Api/Mastodon/Conversation.php index f30ccf4394..70562f3a0d 100644 --- a/src/Factory/Api/Mastodon/Conversation.php +++ b/src/Factory/Api/Mastodon/Conversation.php @@ -35,6 +35,7 @@ class Conversation extends BaseFactory $last_status = null; $ids = []; + $mails = DBA::select('mail', ['id', 'from-url', 'uid', 'seen'], ['convid' => $id], ['order' => ['id' => true]]); while ($mail = DBA::fetch($mails)) { if (!$mail['seen']) { From ae6dae3b23f66b50299e0019d5cf616fa889d632 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 19 May 2021 22:10:05 +0000 Subject: [PATCH 03/10] Whitespace removed --- src/Module/Api/Mastodon/Conversation.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Module/Api/Mastodon/Conversation.php b/src/Module/Api/Mastodon/Conversation.php index feeb52e243..03ea737f07 100644 --- a/src/Module/Api/Mastodon/Conversation.php +++ b/src/Module/Api/Mastodon/Conversation.php @@ -39,7 +39,7 @@ class Conversation extends BaseApi if (!empty($parameters['id'])) { DI::mstdnError()->UnprocessableEntity(); } - + DBA::delete('conv', ['id' => $parameters['id'], 'uid' => $uid]); DBA::delete('mail', ['convid' => $parameters['id'], 'uid' => $uid]); @@ -65,6 +65,7 @@ class Conversation extends BaseApi $params = ['order' => ['id' => true], 'limit' => $request['limit']]; $convs = DBA::select('conv', ['id'], ['uid' => $uid], $params); + $conversations = []; foreach ($convs as $conv) { From d46150f1dccd263017584ebd3d1d7ebdd7771555 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 19 May 2021 22:18:30 +0000 Subject: [PATCH 04/10] Whitespaces ... --- src/Module/Api/Mastodon/Conversations/Read.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Module/Api/Mastodon/Conversations/Read.php b/src/Module/Api/Mastodon/Conversations/Read.php index 346e78a501..d1c349eaac 100644 --- a/src/Module/Api/Mastodon/Conversations/Read.php +++ b/src/Module/Api/Mastodon/Conversations/Read.php @@ -39,7 +39,7 @@ class Read extends BaseApi if (!empty($parameters['id'])) { DI::mstdnError()->UnprocessableEntity(); } - + DBA::update('mail', ['seen' => true], ['convid' => $parameters['id'], 'uid' => $uid]); System::jsonExit(DI::mstdnConversation()->CreateFromConvId($parameters['id'])->toArray()); From 3d65ef3af22114d70c9a65c7a4f2b1f3632b637a Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 19 May 2021 22:20:56 +0000 Subject: [PATCH 05/10] Indentions --- src/Factory/Api/Mastodon/Status.php | 18 +++++++++--------- src/Object/Api/Mastodon/Conversation.php | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index f64dde29bb..3df4e28e7b 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -155,16 +155,16 @@ class Status extends BaseFactory $reshare = []; $item = [ - 'uri-id' => $mail['id'], - 'created' => $mail['created'], - 'thr-parent-id' => 0, + 'uri-id' => $mail['id'], + 'created' => $mail['created'], + 'thr-parent-id' => 0, 'parent-author-id' => 0, - 'title' => $conv['subject'], - 'private' => Item::PRIVATE, - 'language' => '', - 'uri' => $mail['uri'], - 'plink' => '', - 'body' => BBCode::convert($mail['body'], false, BBCode::API) + 'title' => $conv['subject'], + 'private' => Item::PRIVATE, + 'language' => '', + 'uri' => $mail['uri'], + 'plink' => '', + 'body' => BBCode::convert($mail['body'], false, BBCode::API) ]; return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare); diff --git a/src/Object/Api/Mastodon/Conversation.php b/src/Object/Api/Mastodon/Conversation.php index 911b4ad8af..0b0136044f 100644 --- a/src/Object/Api/Mastodon/Conversation.php +++ b/src/Object/Api/Mastodon/Conversation.php @@ -46,9 +46,9 @@ class Conversation extends BaseDataTransferObject public function __construct(string $id, array $accounts, bool $unread, \Friendica\Object\Api\Mastodon\Status $last_status) { - $this->id = $id; - $this->accounts = $accounts; - $this->unread = $unread; + $this->id = $id; + $this->accounts = $accounts; + $this->unread = $unread; $this->last_status = $last_status; } } From 66ad777f4be5f4ed233da86e756c99cf3b5925f8 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 19 May 2021 22:33:40 +0000 Subject: [PATCH 06/10] Supporting all parameters --- src/Module/Api/Mastodon/Conversation.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Module/Api/Mastodon/Conversation.php b/src/Module/Api/Mastodon/Conversation.php index 03ea737f07..4b6c60e56f 100644 --- a/src/Module/Api/Mastodon/Conversation.php +++ b/src/Module/Api/Mastodon/Conversation.php @@ -64,7 +64,22 @@ class Conversation extends BaseApi $params = ['order' => ['id' => true], 'limit' => $request['limit']]; - $convs = DBA::select('conv', ['id'], ['uid' => $uid], $params); + $condition = ['uid' => $uid]; + + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]); + } + + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]); + } + + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]); + $params['order'] = ['id']; + } + + $convs = DBA::select('conv', ['id'], $condition, $params); $conversations = []; @@ -72,6 +87,10 @@ class Conversation extends BaseApi $conversations[] = DI::mstdnConversation()->CreateFromConvId($conv['id']); } + if (!empty($request['min_id'])) { + array_reverse($conversations); + } + System::jsonExit($conversations); } } From 8a3db0226b40420863b4d36b1d5afcac6ce08101 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 20 May 2021 03:53:48 +0000 Subject: [PATCH 07/10] Coding standards --- src/Module/Api/Mastodon/Conversation.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Module/Api/Mastodon/Conversation.php b/src/Module/Api/Mastodon/Conversation.php index 4b6c60e56f..c1badcdc4a 100644 --- a/src/Module/Api/Mastodon/Conversation.php +++ b/src/Module/Api/Mastodon/Conversation.php @@ -76,6 +76,7 @@ class Conversation extends BaseApi if (!empty($request['min_id'])) { $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]); + $params['order'] = ['id']; } From db8b0b9666f67fb0bd965c322bac4543f40ca230 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 20 May 2021 04:39:45 +0000 Subject: [PATCH 08/10] The next mail endpoint is done --- doc/API-Mastodon.md | 2 +- src/Factory/Api/Mastodon/Status.php | 28 ++----- src/Module/Api/Mastodon/Conversation.php | 4 +- src/Module/Api/Mastodon/Timelines/Direct.php | 84 ++++++++++++++++++++ src/Protocol/ActivityPub/Transmitter.php | 44 +++++----- static/routes.config.php | 2 +- 6 files changed, 120 insertions(+), 44 deletions(-) create mode 100644 src/Module/Api/Mastodon/Timelines/Direct.php diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index 5cb4535547..a734bd5445 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -108,6 +108,7 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en - [`POST /api/v1/statuses/:id/pin`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id/unpin`](https://docs.joinmastodon.org/methods/statuses/) - [`GET /api/v1/suggestions`](https://docs.joinmastodon.org/methods/accounts/suggestions/) +- [`GET /api/v1/timelines/direct`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/timelines/home`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/timelines/list/:id`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/timelines/public`](https://docs.joinmastodon.org/methods/timelines/) @@ -122,7 +123,6 @@ These emdpoints are planned to be implemented - [`PATCH /api/v1/accounts/update_credentials`](https://docs.joinmastodon.org/methods/accounts/) - [`GET /api/v1/instance/activity`](https://docs.joinmastodon.org/methods/instance#weekly-activity) -- [`GET /api/v1/timelines/direct`](https://docs.joinmastodon.org/methods/timelines/) ## Non supportable endpoints diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 3df4e28e7b..23825591fb 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -27,11 +27,11 @@ use Friendica\Content\ContactSelector; use Friendica\Content\Text\BBCode; use Friendica\Database\DBA; use Friendica\DI; -use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Model\Verb; use Friendica\Network\HTTPException; use Friendica\Protocol\Activity; +use Friendica\Protocol\ActivityPub; use Friendica\Repository\ProfileField; use Psr\Log\LoggerInterface; @@ -128,19 +128,14 @@ class Status extends BaseFactory * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public function createFromMailId(int $id, $uid = 0) + public function createFromMailId(int $id, $uid) { - $mail = DBA::selectFirst('mail', [], ['id' => $id, 'uid' => $uid]); - if (!$mail) { + $item = ActivityPub\Transmitter::ItemArrayFromMail($id, true); + if (empty($item)) { DI::mstdnError()->RecordNotFound(); } - $conv = DBA::selectFirst('conv', ['subject'], ['id' => $mail['convid'], 'uid' => $uid]); - if (!$conv) { - DI::mstdnError()->RecordNotFound(); - } - - $account = DI::mstdnAccount()->createFromContactId($mail['contact-id']); + $account = DI::mstdnAccount()->createFromContactId($item['author-id']); $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(0, 0, 0); @@ -154,19 +149,6 @@ class Status extends BaseFactory $attachments = []; $reshare = []; - $item = [ - 'uri-id' => $mail['id'], - 'created' => $mail['created'], - 'thr-parent-id' => 0, - 'parent-author-id' => 0, - 'title' => $conv['subject'], - 'private' => Item::PRIVATE, - 'language' => '', - 'uri' => $mail['uri'], - 'plink' => '', - 'body' => BBCode::convert($mail['body'], false, BBCode::API) - ]; - return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare); } } diff --git a/src/Module/Api/Mastodon/Conversation.php b/src/Module/Api/Mastodon/Conversation.php index c1badcdc4a..e87818ac27 100644 --- a/src/Module/Api/Mastodon/Conversation.php +++ b/src/Module/Api/Mastodon/Conversation.php @@ -84,10 +84,12 @@ class Conversation extends BaseApi $conversations = []; - foreach ($convs as $conv) { + while ($conv = DBA::fetch($convs)) { $conversations[] = DI::mstdnConversation()->CreateFromConvId($conv['id']); } + DBA::close($convs); + if (!empty($request['min_id'])) { array_reverse($conversations); } diff --git a/src/Module/Api/Mastodon/Timelines/Direct.php b/src/Module/Api/Mastodon/Timelines/Direct.php new file mode 100644 index 0000000000..5c454f2f6b --- /dev/null +++ b/src/Module/Api/Mastodon/Timelines/Direct.php @@ -0,0 +1,84 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon\Timelines; + +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\Post; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException; + +/** + * @see https://docs.joinmastodon.org/methods/timelines/ + */ +class Direct extends BaseApi +{ + /** + * @param array $parameters + * @throws HTTPException\InternalServerErrorException + */ + public static function rawContent(array $parameters = []) + { + self::login(self::SCOPE_READ); + $uid = self::getCurrentUserID(); + + $request = self::getRequest([ + '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, // Maximum number of results to return. Defaults to 20. + ]); + + $params = ['order' => ['id' => true], 'limit' => $request['limit']]; + + $condition = ['uid' => $uid]; + + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]); + } + + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]); + } + + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]); + + $params['order'] = ['id']; + } + + $mails = DBA::select('mail', ['id', 'uid'], $condition, $params); + + $statuses = []; + + while ($mail = DBA::fetch($mails)) { + $statuses[] = DI::mstdnStatus()->createFromMailId($mail['id'], $mail['uid']); + } + + if (!empty($request['min_id'])) { + array_reverse($statuses); + } + + System::jsonExit($statuses); + } +} diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 9773a5a48b..20c1e49b88 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -861,7 +861,7 @@ class Transmitter * @return array * @throws \Exception */ - public static function ItemArrayFromMail($mail_id) + public static function ItemArrayFromMail($mail_id, $use_title = false) { $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]); if (!DBA::isResult($mail)) { @@ -870,29 +870,37 @@ class Transmitter $mail['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]); - $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]); + $reply = DBA::selectFirst('mail', ['uri', 'from-url', 'guid'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]); // Making the post more compatible for Mastodon by: // - Making it a note and not an article (no title) // - Moving the title into the "summary" field that is used as a "content warning" - $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body']; - $mail['title'] = ''; - $mail['author-link'] = $mail['owner-link'] = $mail['from-url']; - $mail['allow_cid'] = '<'.$mail['contact-id'].'>'; - $mail['allow_gid'] = ''; - $mail['deny_cid'] = ''; - $mail['deny_gid'] = ''; - $mail['private'] = true; - $mail['deleted'] = false; - $mail['edited'] = $mail['created']; - $mail['plink'] = $mail['uri']; - $mail['thr-parent'] = $reply['uri']; - $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT); + if ($use_title) { + $mail['body'] = $mail['body']; + $mail['title'] = $mail['title']; + } else { + $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body']; + $mail['title'] = ''; + } - $mail['event-type'] = ''; - - $mail['parent'] = 0; + $mail['author-link'] = $mail['owner-link'] = $mail['from-url']; + $mail['author-id'] = Contact::getIdForURL($mail['author-link'], 0, false); + $mail['allow_cid'] = '<'.$mail['contact-id'].'>'; + $mail['allow_gid'] = ''; + $mail['deny_cid'] = ''; + $mail['deny_gid'] = ''; + $mail['private'] = Item::PRIVATE; + $mail['deleted'] = false; + $mail['edited'] = $mail['created']; + $mail['plink'] = $mail['uri']; + $mail['thr-parent'] = $reply['uri']; + $mail['thr-parent-id'] = ItemURI::insert(['uri' => $reply['uri'], 'guid' => $reply['guid']]); + $mail['parent-author-id'] = Contact::getIdForURL($reply['from-url'], 0, false); + $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT); + $mail['event-type'] = ''; + $mail['language'] = ''; + $mail['parent'] = 0; return $mail; } diff --git a/static/routes.config.php b/static/routes.config.php index 6b87d55bce..80b1aadeb8 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -142,7 +142,7 @@ return [ '/statuses/{id:\d+}/unpin' => [Module\Api\Mastodon\Statuses\Unpin::class, [ R::POST]], '/suggestions' => [Module\Api\Mastodon\Suggestions::class, [R::GET ]], '/suggestions/{id:\d+}' => [Module\Api\Mastodon\Unimplemented::class, [R::DELETE ]], // not implemented - '/timelines/direct' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // @todo + '/timelines/direct' => [Module\Api\Mastodon\Timelines\Direct::class, [R::GET ]], '/timelines/home' => [Module\Api\Mastodon\Timelines\Home::class, [R::GET ]], '/timelines/list/{id:\d+}' => [Module\Api\Mastodon\Timelines\ListTimeline::class, [R::GET ]], '/timelines/public' => [Module\Api\Mastodon\Timelines\PublicTimeline::class, [R::GET ]], From e10f3f26b9053442da5ddb465c368ba8b145fdae Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 20 May 2021 04:44:45 +0000 Subject: [PATCH 09/10] Removed unused parameter --- src/Factory/Api/Mastodon/Conversation.php | 4 ++-- src/Factory/Api/Mastodon/Status.php | 3 +-- src/Module/Api/Mastodon/Timelines/Direct.php | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Factory/Api/Mastodon/Conversation.php b/src/Factory/Api/Mastodon/Conversation.php index 70562f3a0d..d867dd0b2a 100644 --- a/src/Factory/Api/Mastodon/Conversation.php +++ b/src/Factory/Api/Mastodon/Conversation.php @@ -50,10 +50,10 @@ class Conversation extends BaseFactory $ids[] = $id; if (empty($last_status)) { - $last_status = DI::mstdnStatus()->createFromMailId($mail['id'], $mail['uid']); + $last_status = DI::mstdnStatus()->createFromMailId($mail['id']); } - $accounts[] = DI::mstdnAccount()->createFromContactId($id, $mail['uid']); + $accounts[] = DI::mstdnAccount()->createFromContactId($id, 0); } return new \Friendica\Object\Api\Mastodon\Conversation($id, $accounts, $unread, $last_status); diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 23825591fb..3f16b3c380 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -123,12 +123,11 @@ class Status extends BaseFactory /** * @param int $uriId id of the mail - * @param int $uid mail user * @return \Friendica\Object\Api\Mastodon\Status * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public function createFromMailId(int $id, $uid) + public function createFromMailId(int $id) { $item = ActivityPub\Transmitter::ItemArrayFromMail($id, true); if (empty($item)) { diff --git a/src/Module/Api/Mastodon/Timelines/Direct.php b/src/Module/Api/Mastodon/Timelines/Direct.php index 5c454f2f6b..7bc03c895e 100644 --- a/src/Module/Api/Mastodon/Timelines/Direct.php +++ b/src/Module/Api/Mastodon/Timelines/Direct.php @@ -67,12 +67,12 @@ class Direct extends BaseApi $params['order'] = ['id']; } - $mails = DBA::select('mail', ['id', 'uid'], $condition, $params); + $mails = DBA::select('mail', ['id'], $condition, $params); $statuses = []; while ($mail = DBA::fetch($mails)) { - $statuses[] = DI::mstdnStatus()->createFromMailId($mail['id'], $mail['uid']); + $statuses[] = DI::mstdnStatus()->createFromMailId($mail['id']); } if (!empty($request['min_id'])) { From f5f919aead458a7a003fff02e8956932a913c514 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 20 May 2021 06:58:15 +0000 Subject: [PATCH 10/10] Coding standards --- src/Module/Api/Mastodon/Timelines/Direct.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Module/Api/Mastodon/Timelines/Direct.php b/src/Module/Api/Mastodon/Timelines/Direct.php index 7bc03c895e..104f88d1c2 100644 --- a/src/Module/Api/Mastodon/Timelines/Direct.php +++ b/src/Module/Api/Mastodon/Timelines/Direct.php @@ -24,7 +24,6 @@ namespace Friendica\Module\Api\Mastodon\Timelines; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\DI; -use Friendica\Model\Post; use Friendica\Module\BaseApi; use Friendica\Network\HTTPException; @@ -43,10 +42,10 @@ class Direct extends BaseApi $uid = self::getCurrentUserID(); $request = self::getRequest([ - '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, // Maximum 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, // Maximum number of results to return. Defaults to 20. ]); $params = ['order' => ['id' => true], 'limit' => $request['limit']];