Fetch post parents live too

This commit is contained in:
Andrew Godwin 2022-12-04 21:22:20 -07:00
parent 93ccb5dd32
commit 107c3d6cf8
3 changed files with 11 additions and 10 deletions

View File

@ -15,7 +15,7 @@ from activities.models.hashtag import Hashtag
from core.html import sanitize_post, strip_html from core.html import sanitize_post, strip_html
from core.ld import canonicalise, format_ld_date, get_list, parse_ld_date from core.ld import canonicalise, format_ld_date, get_list, parse_ld_date
from stator.models import State, StateField, StateGraph, StatorModel from stator.models import State, StateField, StateGraph, StatorModel
from users.models.identity import Identity from users.models.identity import Identity, IdentityStates
from users.models.system_actor import SystemActor from users.models.system_actor import SystemActor
@ -625,11 +625,15 @@ class Post(StatorModel):
f"Error fetching post from {object_uri}: {response.status_code}", f"Error fetching post from {object_uri}: {response.status_code}",
{response.content}, {response.content},
) )
return cls.by_ap( post = cls.by_ap(
canonicalise(response.json(), include_security=True), canonicalise(response.json(), include_security=True),
create=True, create=True,
update=True, update=True,
) )
# We may need to fetch the author too
if post.author.state == IdentityStates.outdated:
async_to_sync(post.author.fetch_actor)()
return post
else: else:
raise cls.DoesNotExist(f"Cannot find Post with URI {object_uri}") raise cls.DoesNotExist(f"Cannot find Post with URI {object_uri}")

View File

@ -33,9 +33,10 @@ class Individual(TemplateView):
def get_context_data(self): def get_context_data(self):
if self.post_obj.in_reply_to: if self.post_obj.in_reply_to:
parent = Post.objects.filter(object_uri=self.post_obj.in_reply_to).first() try:
else: parent = Post.by_object_uri(self.post_obj.in_reply_to, fetch=True)
parent = None except Post.DoesNotExist:
parent = None
return { return {
"identity": self.identity, "identity": self.identity,
"post": self.post_obj, "post": self.post_obj,

View File

@ -101,11 +101,7 @@ class Search(FormView):
# Try and retrieve the post by URI # Try and retrieve the post by URI
# (we do not trust the JSON we just got - fetch from source!) # (we do not trust the JSON we just got - fetch from source!)
try: try:
post = Post.by_object_uri(document["id"], fetch=True) return Post.by_object_uri(document["id"], fetch=True)
# We may need to live-fetch the identity too
if post.author.state == IdentityStates.outdated:
async_to_sync(post.author.fetch_actor)()
return post
except Post.DoesNotExist: except Post.DoesNotExist:
return None return None