The next mail endpoint is done

This commit is contained in:
Michael 2021-05-20 04:39:45 +00:00
parent 8a3db0226b
commit db8b0b9666
6 changed files with 120 additions and 44 deletions

View file

@ -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

View file

@ -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);
}
}

View file

@ -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);
}

View file

@ -0,0 +1,84 @@
<?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\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);
}
}

View file

@ -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;
}

View file

@ -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 ]],