Some servers send object as dict for PostInteraction and Follow (#261)

* Some servers send object as dict for PostInteraction and Follow

* Add core.ld.get_str_or_id helper
This commit is contained in:
Michael Manfre 2022-12-24 23:24:46 -05:00 committed by GitHub
parent ab398758a9
commit 8ab0e97a86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 4 deletions

View file

@ -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

View file

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

View file

@ -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: