From 64f113dd8d102282da8f013ab2acf6e1c24a5934 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sat, 17 Dec 2022 14:58:04 -0700 Subject: Receive inbound reports --- users/models/inbox_message.py | 9 ++++++++- users/models/report.py | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) (limited to 'users') diff --git a/users/models/inbox_message.py b/users/models/inbox_message.py index 6c36043..d6d4f43 100644 --- a/users/models/inbox_message.py +++ b/users/models/inbox_message.py @@ -16,7 +16,7 @@ class InboxMessageStates(StateGraph): @classmethod async def handle_received(cls, instance: "InboxMessage"): from activities.models import Post, PostInteraction - from users.models import Follow, Identity + from users.models import Follow, Identity, Report match instance.message_type: case "follow": @@ -29,6 +29,8 @@ class InboxMessageStates(StateGraph): match instance.message_object_type: case "note": await sync_to_async(Post.handle_create_ap)(instance.message) + case "question": + pass # Drop for now case unknown: raise ValueError( f"Cannot handle activity of type create.{unknown}" @@ -39,6 +41,8 @@ class InboxMessageStates(StateGraph): await sync_to_async(Post.handle_update_ap)(instance.message) case "person": await sync_to_async(Identity.handle_update_ap)(instance.message) + case "question": + pass # Drop for now case unknown: raise ValueError( f"Cannot handle activity of type update.{unknown}" @@ -87,6 +91,9 @@ class InboxMessageStates(StateGraph): case "remove": # We are ignoring these right now (probably pinned items) pass + case "flag": + # Received reports + await sync_to_async(Report.handle_ap)(instance.message) case unknown: raise ValueError(f"Cannot handle activity of type {unknown}") return cls.processed diff --git a/users/models/report.py b/users/models/report.py index 7bafd0c..3a26897 100644 --- a/users/models/report.py +++ b/users/models/report.py @@ -1,10 +1,12 @@ +from urllib.parse import urlparse + import httpx import urlman from django.db import models -from core.ld import canonicalise +from core.ld import canonicalise, get_list from stator.models import State, StateField, StateGraph, StatorModel -from users.models import SystemActor +from users.models import Domain class ReportStates(StateGraph): @@ -18,6 +20,8 @@ class ReportStates(StateGraph): """ Sends the report to the remote server if we need to """ + from users.models import SystemActor + report = await instance.afetch_full() if report.forward and not report.subject_identity.domain.local: system_actor = SystemActor() @@ -111,7 +115,41 @@ class Report(StatorModel): "subject_post", ).aget(pk=self.pk) + @classmethod + def handle_ap(cls, data): + """ + Handles an incoming flag + """ + from activities.models import Post + from users.models import Identity + + # Fetch the system actor + domain_id = urlparse(data["actor"]).hostname + # Resolve the objects into items + objects = get_list(data, "object") + subject_identity = None + subject_post = None + for object in objects: + identity = Identity.objects.filter(local=True, actor_uri=object).first() + post = Post.objects.filter(local=True, object_uri=object).first() + if identity: + subject_identity = identity + if post: + subject_post = post + if subject_identity is None: + raise ValueError("Cannot handle flag: no identity object") + # Make a report object + cls.objects.create( + subject_identity=subject_identity, + subject_post=subject_post, + source_domain=Domain.get_remote_domain(domain_id), + type="remote", + complaint=data.get("content"), + ) + def to_ap(self): + from users.models import SystemActor + system_actor = SystemActor() if self.subject_post: objects = [ -- cgit v1.2.3