Receive inbound reports

This commit is contained in:
Andrew Godwin 2022-12-17 14:58:04 -07:00
parent e8d6dccbb2
commit 64f113dd8d
3 changed files with 50 additions and 3 deletions

View file

@ -19,6 +19,8 @@
{% else %}
<p>Remote server {{ report.source_domain.domain }}</p>
{% endif %}
<label>Complaint Type</label>
<p>{{ report.type|title }}</p>
<label>Complaint</label>
<p>{{ report.complaint|linebreaks }}</p>
{% if report.resolved %}

View file

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

View file

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