mirror of
https://github.com/jointakahe/takahe.git
synced 2025-02-16 15:45:14 +00:00
Improve /api/v1/accounts/{id}/statuses perf (#355)
This commit is contained in:
parent
1425ae0bde
commit
be7ce6ed62
3 changed files with 35 additions and 14 deletions
|
@ -965,7 +965,12 @@ class Post(StatorModel):
|
||||||
def to_mastodon_json(self, interactions=None):
|
def to_mastodon_json(self, interactions=None):
|
||||||
reply_parent = None
|
reply_parent = None
|
||||||
if self.in_reply_to:
|
if self.in_reply_to:
|
||||||
reply_parent = Post.objects.filter(object_uri=self.in_reply_to).first()
|
# Load the PK and author.id explicitly to prevent a SELECT on the entire author Identity
|
||||||
|
reply_parent = (
|
||||||
|
Post.objects.filter(object_uri=self.in_reply_to)
|
||||||
|
.only("pk", "author_id")
|
||||||
|
.first()
|
||||||
|
)
|
||||||
visibility_mapping = {
|
visibility_mapping = {
|
||||||
self.Visibilities.public: "public",
|
self.Visibilities.public: "public",
|
||||||
self.Visibilities.unlisted: "unlisted",
|
self.Visibilities.unlisted: "unlisted",
|
||||||
|
@ -986,14 +991,7 @@ class Post(StatorModel):
|
||||||
attachment.to_mastodon_json() for attachment in self.attachments.all()
|
attachment.to_mastodon_json() for attachment in self.attachments.all()
|
||||||
],
|
],
|
||||||
"mentions": [
|
"mentions": [
|
||||||
{
|
mention.to_mastodon_mention_json() for mention in self.mentions.all()
|
||||||
"id": mention.id,
|
|
||||||
"username": mention.username or "",
|
|
||||||
"url": mention.absolute_profile_uri() or "",
|
|
||||||
"acct": mention.handle or "",
|
|
||||||
}
|
|
||||||
for mention in self.mentions.all()
|
|
||||||
if mention.username
|
|
||||||
],
|
],
|
||||||
"tags": (
|
"tags": (
|
||||||
[
|
[
|
||||||
|
@ -1006,13 +1004,21 @@ class Post(StatorModel):
|
||||||
if self.hashtags
|
if self.hashtags
|
||||||
else []
|
else []
|
||||||
),
|
),
|
||||||
"emojis": [emoji.to_mastodon_json() for emoji in self.emojis.usable()],
|
# Filter in the list comp rather than query because the common case is no emoji in the resultset
|
||||||
|
# When filter is on emojis like `emojis.usable()` it causes a query that is not cached by prefetch_related
|
||||||
|
"emojis": [
|
||||||
|
emoji.to_mastodon_json()
|
||||||
|
for emoji in self.emojis.all()
|
||||||
|
if emoji.is_usable
|
||||||
|
],
|
||||||
"reblogs_count": self.stats_with_defaults["boosts"],
|
"reblogs_count": self.stats_with_defaults["boosts"],
|
||||||
"favourites_count": self.stats_with_defaults["likes"],
|
"favourites_count": self.stats_with_defaults["likes"],
|
||||||
"replies_count": self.stats_with_defaults["replies"],
|
"replies_count": self.stats_with_defaults["replies"],
|
||||||
"url": self.absolute_object_uri(),
|
"url": self.absolute_object_uri(),
|
||||||
"in_reply_to_id": reply_parent.pk if reply_parent else None,
|
"in_reply_to_id": reply_parent.pk if reply_parent else None,
|
||||||
"in_reply_to_account_id": reply_parent.author.pk if reply_parent else None,
|
"in_reply_to_account_id": (
|
||||||
|
reply_parent.author_id if reply_parent else None
|
||||||
|
),
|
||||||
"reblog": None,
|
"reblog": None,
|
||||||
"poll": None,
|
"poll": None,
|
||||||
"card": None,
|
"card": None,
|
||||||
|
|
|
@ -133,8 +133,15 @@ def account_statuses(
|
||||||
queryset = (
|
queryset = (
|
||||||
identity.posts.not_hidden()
|
identity.posts.not_hidden()
|
||||||
.unlisted(include_replies=not exclude_replies)
|
.unlisted(include_replies=not exclude_replies)
|
||||||
.select_related("author")
|
.select_related("author", "author__domain")
|
||||||
.prefetch_related("attachments", "mentions__domain", "emojis")
|
.prefetch_related(
|
||||||
|
"attachments",
|
||||||
|
"mentions__domain",
|
||||||
|
"emojis",
|
||||||
|
"author__inbound_follows",
|
||||||
|
"author__outbound_follows",
|
||||||
|
"author__posts",
|
||||||
|
)
|
||||||
.order_by("-created")
|
.order_by("-created")
|
||||||
)
|
)
|
||||||
if pinned:
|
if pinned:
|
||||||
|
|
|
@ -812,7 +812,15 @@ class Identity(StatorModel):
|
||||||
|
|
||||||
### Mastodon Client API ###
|
### Mastodon Client API ###
|
||||||
|
|
||||||
def to_mastodon_json(self):
|
def to_mastodon_mention_json(self):
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"username": self.username or "",
|
||||||
|
"url": self.absolute_profile_uri() or "",
|
||||||
|
"acct": self.handle or "",
|
||||||
|
}
|
||||||
|
|
||||||
|
def to_mastodon_json(self, include_counts=True):
|
||||||
from activities.models import Emoji
|
from activities.models import Emoji
|
||||||
|
|
||||||
header_image = self.local_image_url()
|
header_image = self.local_image_url()
|
||||||
|
|
Loading…
Reference in a new issue