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 %} {% else %}
<p>Remote server {{ report.source_domain.domain }}</p> <p>Remote server {{ report.source_domain.domain }}</p>
{% endif %} {% endif %}
<label>Complaint Type</label>
<p>{{ report.type|title }}</p>
<label>Complaint</label> <label>Complaint</label>
<p>{{ report.complaint|linebreaks }}</p> <p>{{ report.complaint|linebreaks }}</p>
{% if report.resolved %} {% if report.resolved %}

View File

@ -16,7 +16,7 @@ class InboxMessageStates(StateGraph):
@classmethod @classmethod
async def handle_received(cls, instance: "InboxMessage"): async def handle_received(cls, instance: "InboxMessage"):
from activities.models import Post, PostInteraction from activities.models import Post, PostInteraction
from users.models import Follow, Identity from users.models import Follow, Identity, Report
match instance.message_type: match instance.message_type:
case "follow": case "follow":
@ -29,6 +29,8 @@ class InboxMessageStates(StateGraph):
match instance.message_object_type: match instance.message_object_type:
case "note": case "note":
await sync_to_async(Post.handle_create_ap)(instance.message) await sync_to_async(Post.handle_create_ap)(instance.message)
case "question":
pass # Drop for now
case unknown: case unknown:
raise ValueError( raise ValueError(
f"Cannot handle activity of type create.{unknown}" 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) await sync_to_async(Post.handle_update_ap)(instance.message)
case "person": case "person":
await sync_to_async(Identity.handle_update_ap)(instance.message) await sync_to_async(Identity.handle_update_ap)(instance.message)
case "question":
pass # Drop for now
case unknown: case unknown:
raise ValueError( raise ValueError(
f"Cannot handle activity of type update.{unknown}" f"Cannot handle activity of type update.{unknown}"
@ -87,6 +91,9 @@ class InboxMessageStates(StateGraph):
case "remove": case "remove":
# We are ignoring these right now (probably pinned items) # We are ignoring these right now (probably pinned items)
pass pass
case "flag":
# Received reports
await sync_to_async(Report.handle_ap)(instance.message)
case unknown: case unknown:
raise ValueError(f"Cannot handle activity of type {unknown}") raise ValueError(f"Cannot handle activity of type {unknown}")
return cls.processed return cls.processed

View File

@ -1,10 +1,12 @@
from urllib.parse import urlparse
import httpx import httpx
import urlman import urlman
from django.db import models 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 stator.models import State, StateField, StateGraph, StatorModel
from users.models import SystemActor from users.models import Domain
class ReportStates(StateGraph): class ReportStates(StateGraph):
@ -18,6 +20,8 @@ class ReportStates(StateGraph):
""" """
Sends the report to the remote server if we need to Sends the report to the remote server if we need to
""" """
from users.models import SystemActor
report = await instance.afetch_full() report = await instance.afetch_full()
if report.forward and not report.subject_identity.domain.local: if report.forward and not report.subject_identity.domain.local:
system_actor = SystemActor() system_actor = SystemActor()
@ -111,7 +115,41 @@ class Report(StatorModel):
"subject_post", "subject_post",
).aget(pk=self.pk) ).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): def to_ap(self):
from users.models import SystemActor
system_actor = SystemActor() system_actor = SystemActor()
if self.subject_post: if self.subject_post:
objects = [ objects = [