API: Speed improvements when fetching posts

This commit is contained in:
Michael 2022-06-10 18:49:03 +00:00
commit 7e747b2f41
9 changed files with 140 additions and 104 deletions

View file

@ -719,7 +719,7 @@ class App
} catch (HTTPException $e) {
(new ModuleHTTPException())->rawContent($e);
}
$page->logRuntime($this->config);
$page->logRuntime($this->config, 'runFrontend');
}
/**

View file

@ -98,7 +98,7 @@ class Page implements ArrayAccess
$this->method = $method;
}
public function logRuntime(IManageConfigValues $config)
public function logRuntime(IManageConfigValues $config, string $origin = '')
{
if (in_array($this->command, $config->get('system', 'runtime_ignore'))) {
return;
@ -106,7 +106,7 @@ class Page implements ArrayAccess
$runtime = number_format(microtime(true) - $this->timestamp, 3);
if ($runtime > $config->get('system', 'runtime_loglimit')) {
Logger::debug('Runtime', ['method' => $this->method, 'command' => $this->command, 'runtime' => $runtime]);
Logger::debug('Runtime', ['method' => $this->method, 'command' => $this->command, 'runtime' => $runtime, 'origin' => $origin]);
}
}

View file

@ -369,7 +369,7 @@ class System
*/
public static function exit()
{
DI::page()->logRuntime(DI::config());
DI::page()->logRuntime(DI::config(), 'exit');
exit();
}

View file

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

View file

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

View file

@ -86,52 +86,42 @@ class Account extends BaseDataTransferObject
* Creates an account record from a public contact record. Expects all contact table fields to be set.
*
* @param BaseURL $baseUrl
* @param array $publicContact Full contact table record with uid = 0
* @param array $apcontact Optional full apcontact table record
* @param array $userContact Optional full contact table record with uid != 0
* @param array $fcontact Optional full fcontact table record
* @param array $contact entry of "account-user-view"
* @param Fields $fields Profile fields
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(BaseURL $baseUrl, array $publicContact, Fields $fields, array $apcontact = [], array $userContact = [], array $fcontact = [])
public function __construct(BaseURL $baseUrl, array $contact, Fields $fields)
{
$this->id = (string)$publicContact['id'];
$this->username = $publicContact['nick'];
$this->id = (string)$contact['pid'];
$this->username = $contact['nick'];
$this->acct =
strpos($publicContact['url'], $baseUrl->get() . '/') === 0 ?
$publicContact['nick'] :
$publicContact['addr'];
$this->display_name = $publicContact['name'];
$this->locked = (bool)$publicContact['manually-approve'] ?? !empty($apcontact['manually-approve']);
$this->bot = ($publicContact['contact-type'] == Contact::TYPE_NEWS);
$this->discoverable = !$publicContact['unsearchable'];
$this->group = ($publicContact['contact-type'] == Contact::TYPE_COMMUNITY);
strpos($contact['url'], $baseUrl->get() . '/') === 0 ?
$contact['nick'] :
$contact['addr'];
$this->display_name = $contact['name'];
$this->locked = (bool)$contact['manually-approve'];
$this->bot = ($contact['contact-type'] == Contact::TYPE_NEWS);
$this->discoverable = !$contact['unsearchable'];
$this->group = ($contact['contact-type'] == Contact::TYPE_COMMUNITY);
$publicContactCreated = $publicContact['created'] ?: DBA::NULL_DATETIME;
$userContactCreated = $userContact['created'] ?? DBA::NULL_DATETIME;
$this->created_at = DateTimeFormat::utc($contact['created'] ?: DBA::NULL_DATETIME, DateTimeFormat::JSON);
$created = $userContactCreated < $publicContactCreated && ($userContactCreated != DBA::NULL_DATETIME) ? $userContactCreated : $publicContactCreated;
$this->created_at = DateTimeFormat::utc($created, DateTimeFormat::JSON);
$this->note = BBCode::convertForUriId($publicContact['uri-id'] ?? 0, $publicContact['about'], BBCode::EXTERNAL);
$this->url = $publicContact['url'];
$this->avatar = Contact::getAvatarUrlForId($userContact['id'] ?? 0 ?: $publicContact['id'], Proxy::SIZE_SMALL, $userContact['updated'] ?? '' ?: $publicContact['updated']);
$this->note = BBCode::convertForUriId($contact['uri-id'], $contact['about'], BBCode::EXTERNAL);
$this->url = $contact['url'];
$this->avatar = Contact::getAvatarUrlForId($contact['id'] ?? 0 ?: $contact['pid'], Proxy::SIZE_SMALL, $contact['updated'], $contact['guid'] ?? '');
$this->avatar_static = $this->avatar;
$this->header = Contact::getHeaderUrlForId($userContact['id'] ?? 0 ?: $publicContact['id'], '', $userContact['updated'] ?? '' ?: $publicContact['updated']);
$this->header = Contact::getHeaderUrlForId($contact['id'] ?? 0 ?: $contact['pid'], '', $contact['updated'], $contact['guid'] ?? '');
$this->header_static = $this->header;
$this->followers_count = $apcontact['followers_count'] ?? $fcontact['interacted_count'] ?? 0;
$this->following_count = $apcontact['following_count'] ?? $fcontact['interacting_count'] ?? 0;
$this->statuses_count = $apcontact['statuses_count'] ?? $fcontact['post_count'] ?? 0;
$this->followers_count = $contact['ap-followers_count'] ?? $contact['diaspora-interacted_count'] ?? 0;
$this->following_count = $contact['ap-following_count'] ?? $contact['diaspora-interacting_count'] ?? 0;
$this->statuses_count = $contact['ap-statuses_count'] ?? $contact['diaspora-post_count'] ?? 0;
$publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME;
$userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME;
$lastItem = $userContactLastItem > $publicContactLastItem ? $userContactLastItem : $publicContactLastItem;
$lastItem = $contact['last-item'] ?: DBA::NULL_DATETIME;
$this->last_status_at = $lastItem != DBA::NULL_DATETIME ? DateTimeFormat::utc($lastItem, 'Y-m-d') : null;
// No custom emojis per account in Friendica
$this->emojis = [];
$this->fields = $fields->getArrayCopy();
}
/**