mirror of
https://git.friendi.ca/friendica/friendica.git
synced 2025-06-11 16:34:27 +02:00
API: Speed improvements when fetching posts
This commit is contained in:
parent
86837ddb4a
commit
7e747b2f41
9 changed files with 140 additions and 104 deletions
|
@ -24,9 +24,8 @@ namespace Friendica\Factory\Api\Mastodon;
|
|||
use Friendica\App\BaseURL;
|
||||
use Friendica\BaseFactory;
|
||||
use Friendica\Collection\Api\Mastodon\Fields;
|
||||
use Friendica\Model\APContact;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\FContact;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Profile\ProfileField\Repository\ProfileField as ProfileFieldRepository;
|
||||
use ImagickException;
|
||||
|
@ -60,31 +59,39 @@ class Account extends BaseFactory
|
|||
*/
|
||||
public function createFromContactId(int $contactId, $uid = 0): \Friendica\Object\Api\Mastodon\Account
|
||||
{
|
||||
$cdata = Contact::getPublicAndUserContactID($contactId, $uid);
|
||||
if (!empty($cdata)) {
|
||||
$publicContact = Contact::getById($cdata['public']);
|
||||
$userContact = Contact::getById($cdata['user']);
|
||||
} else {
|
||||
$publicContact = Contact::getById($contactId);
|
||||
$userContact = [];
|
||||
}
|
||||
|
||||
if (empty($publicContact)) {
|
||||
$contact = Contact::getById($contactId, ['uri-id']);
|
||||
if (empty($contact)) {
|
||||
throw new HTTPException\NotFoundException('Contact ' . $contactId . ' not found');
|
||||
}
|
||||
return self::createFromUriId($contact['uri-id'], $uid);
|
||||
}
|
||||
|
||||
$apcontact = APContact::getByURL($publicContact['url'], false);
|
||||
$fcontact = FContact::getByURL($publicContact['url'], false);
|
||||
|
||||
$self_contact = Contact::selectFirst(['uid'], ['nurl' => $publicContact['nurl'], 'self' => true]);
|
||||
if (!empty($self_contact['uid'])) {
|
||||
$profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($self_contact['uid']);
|
||||
$fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
|
||||
} else {
|
||||
$fields = new Fields();
|
||||
/**
|
||||
* @param int $contactUriId
|
||||
* @param int $uid Public contact (=0) or owner user id
|
||||
*
|
||||
* @return \Friendica\Object\Api\Mastodon\Account
|
||||
* @throws HTTPException\InternalServerErrorException
|
||||
* @throws ImagickException|HTTPException\NotFoundException
|
||||
*/
|
||||
public function createFromUriId(int $contactUriId, $uid = 0): \Friendica\Object\Api\Mastodon\Account
|
||||
{
|
||||
$contact = DBA::selectFirst('account-user-view', [], ['uri-id' => $contactUriId, 'uid' => [0, $uid]], ['order' => ['id' => true]]);
|
||||
if (empty($contact)) {
|
||||
throw new HTTPException\NotFoundException('Contact ' . $contactUriId . ' not found');
|
||||
}
|
||||
|
||||
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apcontact, $userContact, $fcontact);
|
||||
$fields = new Fields();
|
||||
|
||||
if (Contact::isLocal($contact['url'])) {
|
||||
$self_contact = Contact::selectFirst(['uid'], ['nurl' => $contact['nurl'], 'self' => true]);
|
||||
if (!empty($self_contact['uid'])) {
|
||||
$profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($self_contact['uid']);
|
||||
$fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
|
||||
}
|
||||
}
|
||||
|
||||
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $contact, $fields);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -94,13 +101,10 @@ class Account extends BaseFactory
|
|||
*/
|
||||
public function createFromUserId(int $userId): \Friendica\Object\Api\Mastodon\Account
|
||||
{
|
||||
$publicContact = Contact::selectFirst([], ['uid' => $userId, 'self' => true]);
|
||||
|
||||
$contact = DBA::selectFirst('account-user-view', [], ['uid' => $userId, 'self' => true]);
|
||||
$profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($userId);
|
||||
$fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
|
||||
|
||||
$apContact = APContact::getByURL($publicContact['url'], false);
|
||||
|
||||
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apContact);
|
||||
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $contact, $fields);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -78,10 +78,10 @@ class Status extends BaseFactory
|
|||
* @throws HTTPException\InternalServerErrorException
|
||||
* @throws ImagickException|HTTPException\NotFoundException
|
||||
*/
|
||||
public function createFromUriId(int $uriId, $uid = 0): \Friendica\Object\Api\Mastodon\Status
|
||||
public function createFromUriId(int $uriId, $uid = 0, $item = []): \Friendica\Object\Api\Mastodon\Status
|
||||
{
|
||||
$fields = ['uri-id', 'uid', 'author-id', 'author-link', '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'];
|
||||
$fields = ['uri-id', 'uid', 'author-id', 'author-uri-id', 'author-link', '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'];
|
||||
$item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
|
||||
if (!$item) {
|
||||
$mail = DBA::selectFirst('mail', ['id'], ['uri-id' => $uriId, 'uid' => $uid]);
|
||||
|
@ -90,42 +90,46 @@ class Status extends BaseFactory
|
|||
}
|
||||
throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
|
||||
}
|
||||
$account = $this->mstdnAccountFactory->createFromUriId($item['author-uri-id'], $uid);
|
||||
|
||||
$account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
|
||||
$count_announce = Post::countPosts([
|
||||
'thr-parent-id' => $uriId,
|
||||
'gravity' => GRAVITY_ACTIVITY,
|
||||
'vid' => Verb::getID(Activity::ANNOUNCE),
|
||||
'deleted' => false
|
||||
], []);
|
||||
$count_like = Post::countPosts([
|
||||
'thr-parent-id' => $uriId,
|
||||
'gravity' => GRAVITY_ACTIVITY,
|
||||
'vid' => Verb::getID(Activity::LIKE),
|
||||
'deleted' => false
|
||||
], []);
|
||||
|
||||
$counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
|
||||
Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_COMMENT, 'deleted' => false], []),
|
||||
Post::countPosts([
|
||||
'thr-parent-id' => $uriId,
|
||||
'gravity' => GRAVITY_ACTIVITY,
|
||||
'vid' => Verb::getID(Activity::ANNOUNCE),
|
||||
'deleted' => false
|
||||
], []),
|
||||
Post::countPosts([
|
||||
'thr-parent-id' => $uriId,
|
||||
'gravity' => GRAVITY_ACTIVITY,
|
||||
'vid' => Verb::getID(Activity::LIKE),
|
||||
'deleted' => false
|
||||
], [])
|
||||
$count_announce,
|
||||
$count_like
|
||||
);
|
||||
|
||||
$origin_like = ($count_like == 0) ? false : Post::exists([
|
||||
'thr-parent-id' => $uriId,
|
||||
'uid' => $uid,
|
||||
'origin' => true,
|
||||
'gravity' => GRAVITY_ACTIVITY,
|
||||
'vid' => Verb::getID(Activity::LIKE),
|
||||
'deleted' => false
|
||||
]);
|
||||
$origin_announce = ($count_announce == 0) ? false : Post::exists([
|
||||
'thr-parent-id' => $uriId,
|
||||
'uid' => $uid,
|
||||
'origin' => true,
|
||||
'gravity' => GRAVITY_ACTIVITY,
|
||||
'vid' => Verb::getID(Activity::ANNOUNCE),
|
||||
'deleted' => false
|
||||
]);
|
||||
$userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
|
||||
Post::exists([
|
||||
'thr-parent-id' => $uriId,
|
||||
'uid' => $uid,
|
||||
'origin' => true,
|
||||
'gravity' => GRAVITY_ACTIVITY,
|
||||
'vid' => Verb::getID(Activity::LIKE)
|
||||
, 'deleted' => false
|
||||
]),
|
||||
Post::exists([
|
||||
'thr-parent-id' => $uriId,
|
||||
'uid' => $uid,
|
||||
'origin' => true,
|
||||
'gravity' => GRAVITY_ACTIVITY,
|
||||
'vid' => Verb::getID(Activity::ANNOUNCE),
|
||||
'deleted' => false
|
||||
]),
|
||||
$origin_like,
|
||||
$origin_announce,
|
||||
Post\ThreadUser::getIgnored($uriId, $uid),
|
||||
(bool)($item['starred'] && ($item['gravity'] == GRAVITY_PARENT)),
|
||||
$item['featured']
|
||||
|
@ -136,8 +140,13 @@ class Status extends BaseFactory
|
|||
|
||||
$mentions = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
|
||||
$tags = $this->mstdnTagFactory->createFromUriId($uriId);
|
||||
$card = $this->mstdnCardFactory->createFromUriId($uriId);
|
||||
$attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
|
||||
if ($item['has-media']) {
|
||||
$card = $this->mstdnCardFactory->createFromUriId($uriId);
|
||||
$attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
|
||||
} else {
|
||||
$card = new \Friendica\Object\Api\Mastodon\Card([]);
|
||||
$attachments = [];
|
||||
}
|
||||
|
||||
if (!empty($item['question-id'])) {
|
||||
$poll = $this->mstdnPollFactory->createFromId($item['question-id'], $uid)->toArray();
|
||||
|
@ -160,7 +169,6 @@ class Status extends BaseFactory
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
|
||||
$reshare = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
|
||||
$reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue