diff --git a/activities/models/post_interaction.py b/activities/models/post_interaction.py index a8c7d6d..3746b3b 100644 --- a/activities/models/post_interaction.py +++ b/activities/models/post_interaction.py @@ -4,7 +4,7 @@ from django.utils import timezone from activities.models.fan_out import FanOut from activities.models.post import Post from activities.models.timeline_event import TimelineEvent -from core.ld import format_ld_date, parse_ld_date +from core.ld import format_ld_date, get_str_or_id, parse_ld_date from stator.models import State, StateField, StateGraph, StatorModel from users.models.follow import Follow from users.models.identity import Identity @@ -257,7 +257,7 @@ class PostInteraction(StatorModel): # Resolve the author identity = Identity.by_actor_uri(data["actor"], create=True) # Resolve the post - post = Post.by_object_uri(data["object"], fetch=True) + post = Post.by_object_uri(get_str_or_id(data["object"]), fetch=True) # Get the right type if data["type"].lower() == "like": type = cls.Types.like diff --git a/core/ld.py b/core/ld.py index 5ac48a7..18c351e 100644 --- a/core/ld.py +++ b/core/ld.py @@ -451,6 +451,17 @@ def get_list(container, key) -> list: return value +def get_str_or_id(value: str | dict | None) -> str | None: + """ + Given a value that could be a str or {"id": str}, return the str + """ + if isinstance(value, str): + return value + elif isinstance(value, dict): + return value.get("id") + return None + + def format_ld_date(value: datetime.datetime) -> str: return value.strftime(DATETIME_FORMAT) diff --git a/users/models/follow.py b/users/models/follow.py index 8e4aaa2..2f80824 100644 --- a/users/models/follow.py +++ b/users/models/follow.py @@ -3,7 +3,7 @@ from typing import Optional import httpx from django.db import models, transaction -from core.ld import canonicalise +from core.ld import canonicalise, get_str_or_id from stator.models import State, StateField, StateGraph, StatorModel from users.models.identity import Identity @@ -220,7 +220,7 @@ class Follow(StatorModel): """ # Resolve source and target and see if a Follow exists source = Identity.by_actor_uri(data["actor"], create=create) - target = Identity.by_actor_uri(data["object"]) + target = Identity.by_actor_uri(get_str_or_id(data["object"])) follow = cls.maybe_get(source=source, target=target) # If it doesn't exist, create one in the remote_requested state if follow is None: