Merge pull request #12727 from annando/api-quotes

API: added positiv list for quote support
This commit is contained in:
Hypolite Petovan 2023-01-25 15:32:40 -05:00 committed by GitHub
commit efc4e5668a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 132 additions and 71 deletions

View file

@ -958,10 +958,11 @@ class Item
$post['wall'] = $post['wall'] ?? true;
$post['guid'] = $post['guid'] ?? System::createUUID();
$post['verb'] = $post['verb'] ?? Activity::POST;
$post['uri'] = $post['uri'] ?? ItemModel::newURI($post['guid']);
$post['thr-parent'] = $post['thr-parent'] ?? $post['uri'];
if (empty($post['uri'])) {
$post['thr-parent'] = $post['uri'] = ItemModel::newURI($post['guid']);
$post['gravity'] = ItemModel::GRAVITY_PARENT;
if (empty($post['gravity'])) {
$post['gravity'] = ($post['uri'] == $post['thr-parent']) ? ItemModel::GRAVITY_PARENT : ItemModel::GRAVITY_COMMENT;
}
$owner = User::getOwnerDataById($post['uid']);

View file

@ -79,15 +79,15 @@ class Status extends BaseFactory
/**
* @param int $uriId Uri-ID of the item
* @param int $uid Item user
* @param bool $display_quote Display quoted posts
* @param bool $reblog Check for reblogged post
* @param bool $in_reply_status Add an "in_reply_status" element
* @param bool $display_quote Display quoted posts
*
* @return \Friendica\Object\Api\Mastodon\Status
* @throws HTTPException\InternalServerErrorException
* @throws ImagickException|HTTPException\NotFoundException
*/
public function createFromUriId(int $uriId, int $uid = 0, bool $reblog = true, bool $in_reply_status = true, bool $display_quote = false): \Friendica\Object\Api\Mastodon\Status
public function createFromUriId(int $uriId, int $uid = 0, bool $display_quote = false, bool $reblog = true, bool $in_reply_status = true): \Friendica\Object\Api\Mastodon\Status
{
$fields = ['uri-id', 'uid', 'author-id', 'causer-id', 'author-uri-id', 'author-link', 'causer-uri-id', 'post-reason', 'starred', 'app', 'title', 'body', 'raw-body', 'content-warning', 'question-id',
'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'featured', 'has-media', 'quote-uri-id'];
@ -198,6 +198,22 @@ class Status extends BaseFactory
$poll = null;
}
if ($display_quote) {
$quote = self::createQuote($item, $uid);
$item['body'] = BBCode::removeSharedData($item['body']);
if (!is_null($item['raw-body'])) {
$item['raw-body'] = BBCode::removeSharedData($item['raw-body']);
}
} else {
// We can always safely add attached activities. Real quotes are added to the body via "addSharedPost".
if (empty($item['quote-uri-id'])) {
$quote = self::createQuote($item, $uid);
} else {
$quote = [];
}
$shared = $this->contentItem->getSharedPost($item, ['uri-id']);
if (!empty($shared)) {
$shared_uri_id = $shared['post']['uri-id'];
@ -225,20 +241,6 @@ class Status extends BaseFactory
}
}
if ($display_quote) {
$quote = self::createQuote($item, $uid);
$item['body'] = BBCode::removeSharedData($item['body']);
if (!is_null($item['raw-body'])) {
$item['raw-body'] = BBCode::removeSharedData($item['raw-body']);
}
} else {
// We can always safely add attached activities. Real quotes are added to the body via "addSharedPost".
if (empty($item['quote-uri-id'])) {
$quote = self::createQuote($item, $uid);
}
$item['body'] = $this->contentItem->addSharedPost($item);
if (!is_null($item['raw-body'])) {
@ -247,13 +249,13 @@ class Status extends BaseFactory
}
if ($is_reshare) {
$reshare = $this->createFromUriId($uriId, $uid, false, false)->toArray();
$reshare = $this->createFromUriId($uriId, $uid, $display_quote, false, false)->toArray();
} else {
$reshare = [];
}
if ($in_reply_status && ($item['gravity'] == Item::GRAVITY_COMMENT)) {
$in_reply = $this->createFromUriId($item['thr-parent-id'], $uid, false, false)->toArray();
$in_reply = $this->createFromUriId($item['thr-parent-id'], $uid, $display_quote, false, false)->toArray();
} else {
$in_reply = [];
}
@ -281,7 +283,7 @@ class Status extends BaseFactory
}
if (!empty($quote_id)) {
$quote = $this->createFromUriId($quote_id, $uid, false, false)->toArray();
$quote = $this->createFromUriId($quote_id, $uid, false, false, false)->toArray();
} else {
$quote = [];
}

View file

@ -113,11 +113,13 @@ class Statuses extends BaseApi
$items = Post::selectForUser($uid, ['uri-id'], $condition, $params);
}
$display_quotes = self::appSupportsQuotes();
$statuses = [];
while ($item = Post::fetch($items)) {
self::setBoundaries($item['uri-id']);
try {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
} catch (\Throwable $th) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
}

View file

@ -69,10 +69,12 @@ class Bookmarks extends BaseApi
$items = Post::selectThreadForUser($uid, ['uri-id'], $condition, $params);
$display_quotes = self::appSupportsQuotes();
$statuses = [];
while ($item = Post::fetch($items)) {
self::setBoundaries($item['uri-id']);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
}
DBA::close($items);

View file

@ -45,8 +45,9 @@ class Favourited extends BaseApi
$request = $this->getRequest([
'limit' => 20, // Maximum number of results to return. Defaults to 20.
'min_id' => 0, // Return results immediately newer than id
'max_id' => 0, // Return results older than id
'since_id' => 0, // Return results newer than this ID. Use HTTP Link header to paginate.
'min_id' => 0, // Return results immediately newer than id
'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users.
], $request);
@ -58,6 +59,10 @@ class Favourited extends BaseApi
$condition = DBA::mergeConditions($condition, ["`thr-parent-id` < ?", $request['max_id']]);
}
if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`thr-parent-id` > ?", $request['since_id']]);
}
if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`thr-parent-id` > ?", $request['min_id']]);
@ -66,10 +71,12 @@ class Favourited extends BaseApi
$items = Post::selectForUser($uid, ['thr-parent-id'], $condition, $params);
$display_quotes = self::appSupportsQuotes();
$statuses = [];
while ($item = Post::fetch($items)) {
self::setBoundaries($item['thr-parent-id']);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['thr-parent-id'], $uid);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['thr-parent-id'], $uid, $display_quotes);
}
DBA::close($items);

View file

@ -142,7 +142,7 @@ class Search extends BaseApi
// If the user-specific search failed, we search and probe a public post
$item_id = Item::fetchByLink($q, $uid) ?: Item::fetchByLink($q);
if ($item_id && $item = Post::selectFirst(['uri-id'], ['id' => $item_id])) {
return DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
return DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, self::appSupportsQuotes());
}
}
@ -176,10 +176,12 @@ class Search extends BaseApi
$items = DBA::select($table, ['uri-id'], $condition, $params);
$display_quotes = self::appSupportsQuotes();
$statuses = [];
while ($item = Post::fetch($items)) {
self::setBoundaries($item['uri-id']);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
}
DBA::close($items);

View file

@ -88,7 +88,7 @@ class Statuses extends BaseApi
Item::update($item, ['id' => $post['id']]);
Item::updateDisplayCache($post['uri-id']);
System::jsonExit(DI::mstdnStatus()->createFromUriId($post['uri-id'], $uid));
System::jsonExit(DI::mstdnStatus()->createFromUriId($post['uri-id'], $uid, self::appSupportsQuotes()));
}
protected function post(array $request = [])
@ -269,7 +269,7 @@ class Statuses extends BaseApi
if (!empty($id)) {
$item = Post::selectFirst(['uri-id'], ['id' => $id]);
if (!empty($item['uri-id'])) {
System::jsonExit(DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid));
System::jsonExit(DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, self::appSupportsQuotes()));
}
}
@ -308,7 +308,7 @@ class Statuses extends BaseApi
DI::mstdnError()->UnprocessableEntity();
}
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, false));
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes(), false));
}
private function getApp(): string

View file

@ -65,6 +65,6 @@ class Bookmark extends BaseApi
Item::update(['starred' => true], ['id' => $item['id']]);
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
}
}

View file

@ -45,6 +45,9 @@ class Context extends BaseApi
}
$request = $this->getRequest([
'max_id' => 0, // Return results older than this id
'since_id' => 0, // Return results newer than this id
'min_id' => 0, // Return results immediately newer than this id
'limit' => 40, // Maximum number of results to return. Defaults to 40.
], $request);
@ -55,17 +58,36 @@ class Context extends BaseApi
$parent = Post::selectFirst(['parent-uri-id'], ['uri-id' => $id]);
if (DBA::isResult($parent)) {
$posts = Post::selectPosts(['uri-id', 'thr-parent-id'],
['parent-uri-id' => $parent['parent-uri-id'], 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]]);
$params = ['order' => ['uri-id' => true]];
$condition = ['parent-uri-id' => $parent['parent-uri-id'], 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]];
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 = Post::selectPosts(['uri-id', 'thr-parent-id'], $condition, $params);
while ($post = Post::fetch($posts)) {
if ($post['uri-id'] == $post['thr-parent-id']) {
continue;
}
self::setBoundaries($post['uri-id']);
$parents[$post['uri-id']] = $post['thr-parent-id'];
$children[$post['thr-parent-id']][] = $post['uri-id'];
}
DBA::close($posts);
self::setLinkHeader();
} else {
$parent = DBA::selectFirst('mail', ['parent-uri-id'], ['uri-id' => $id, 'uid' => $uid]);
if (DBA::isResult($parent)) {
@ -90,8 +112,10 @@ class Context extends BaseApi
asort($ancestors);
$display_quotes = self::appSupportsQuotes();
foreach (array_slice($ancestors, 0, $request['limit']) as $ancestor) {
$statuses['ancestors'][] = DI::mstdnStatus()->createFromUriId($ancestor, $uid);;
$statuses['ancestors'][] = DI::mstdnStatus()->createFromUriId($ancestor, $uid, $display_quotes);
}
$descendants = self::getChildren($id, $children);
@ -99,7 +123,7 @@ class Context extends BaseApi
asort($descendants);
foreach (array_slice($descendants, 0, $request['limit']) as $descendant) {
$statuses['descendants'][] = DI::mstdnStatus()->createFromUriId($descendant, $uid);
$statuses['descendants'][] = DI::mstdnStatus()->createFromUriId($descendant, $uid, $display_quotes);
}
System::jsonExit($statuses);

View file

@ -49,6 +49,6 @@ class Favourite extends BaseApi
Item::performActivity($item['id'], 'like', $uid);
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
}
}

View file

@ -53,6 +53,6 @@ class Mute extends BaseApi
Post\ThreadUser::setIgnored($this->parameters['id'], $uid, true);
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
}
}

View file

@ -48,6 +48,6 @@ class Pin extends BaseApi
Post\Collection::add($this->parameters['id'], Post\Collection::FEATURED, $item['author-id'], $uid);
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
}
}

View file

@ -58,6 +58,6 @@ class Reblog extends BaseApi
Item::performActivity($item['id'], 'announce', $uid);
}
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
}
}

View file

@ -65,6 +65,6 @@ class Unbookmark extends BaseApi
Item::update(['starred' => false], ['id' => $item['id']]);
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
}
}

View file

@ -49,6 +49,6 @@ class Unfavourite extends BaseApi
Item::performActivity($item['id'], 'unlike', $uid);
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
}
}

View file

@ -53,6 +53,6 @@ class Unmute extends BaseApi
Post\ThreadUser::setIgnored($this->parameters['id'], $uid, false);
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
}
}

View file

@ -48,6 +48,6 @@ class Unpin extends BaseApi
Post\Collection::remove($this->parameters['id'], Post\Collection::FEATURED, $uid);
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
}
}

View file

@ -64,6 +64,6 @@ class Unreblog extends BaseApi
Item::performActivity($item['id'], 'unannounce', $uid);
}
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
}
}

View file

@ -91,10 +91,12 @@ class Home extends BaseApi
$items = Post::selectForUser($uid, ['uri-id'], $condition, $params);
$display_quotes = self::appSupportsQuotes();
$statuses = [];
while ($item = Post::fetch($items)) {
self::setBoundaries($item['uri-id']);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
}
DBA::close($items);

View file

@ -96,10 +96,12 @@ class ListTimeline extends BaseApi
$items = Post::selectForUser($uid, ['uri-id'], $condition, $params);
$display_quotes = self::appSupportsQuotes();
$statuses = [];
while ($item = Post::fetch($items)) {
self::setBoundaries($item['uri-id']);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
}
DBA::close($items);

View file

@ -97,11 +97,13 @@ class PublicTimeline extends BaseApi
$items = Post::selectPostsForUser($uid, ['uri-id'], $condition, $params);
$display_quotes = self::appSupportsQuotes();
$statuses = [];
while ($item = Post::fetch($items)) {
self::setBoundaries($item['uri-id']);
try {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
} catch (\Throwable $th) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
}

View file

@ -105,10 +105,12 @@ class Tag extends BaseApi
$items = DBA::select('tag-search-view', ['uri-id'], $condition, $params);
$display_quotes = self::appSupportsQuotes();
$statuses = [];
while ($item = Post::fetch($items)) {
self::setBoundaries($item['uri-id']);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
}
DBA::close($items);

View file

@ -48,10 +48,12 @@ class Statuses extends BaseApi
$condition = ["NOT `private` AND `commented` > ? AND `created` > ?", DateTimeFormat::utc('now -1 day'), DateTimeFormat::utc('now -1 week')];
$condition = DBA::mergeConditions($condition, ['network' => Protocol::FEDERATED]);
$display_quotes = self::appSupportsQuotes();
$trending = [];
$statuses = Post::selectPostThread(['uri-id'], $condition, ['limit' => $request['limit'], 'order' => ['total-actors' => true]]);
while ($status = Post::fetch($statuses)) {
$trending[] = DI::mstdnStatus()->createFromUriId($status['uri-id'], $uid);
$trending[] = DI::mstdnStatus()->createFromUriId($status['uri-id'], $uid, $display_quotes);
}
DBA::close($statuses);

View file

@ -180,6 +180,17 @@ class BaseApi extends BaseModule
}
}
/**
* Check if the app is known to support quoted posts
*
* @return bool
*/
public static function appSupportsQuotes(): bool
{
$token = self::getCurrentApplication();
return (!empty($token['name']) && in_array($token['name'], ['Fedilab']));
}
/**
* Get current application token
*