From c10aa46fc5c5b263e46b7e79da9faa9c3a356728 Mon Sep 17 00:00:00 2001 From: NaphalSec Date: Mon, 2 Jan 2023 18:31:32 -0500 Subject: [PATCH] New Moderation Report Email (#346) --- templates/emails/report_new.html | 33 ++++++++++++++++++++++++++ templates/emails/report_new.txt | 22 ++++++++++++++++++ users/models/report.py | 40 ++++++++++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 templates/emails/report_new.html create mode 100644 templates/emails/report_new.txt diff --git a/templates/emails/report_new.html b/templates/emails/report_new.html new file mode 100644 index 0000000..7ad6249 --- /dev/null +++ b/templates/emails/report_new.html @@ -0,0 +1,33 @@ +{% extends "emails/base.html" %} +{% load mail_tags %} +{% autoescape off %} +{% block title %}{{config.site_name}} - New Moderation Report{% endblock title %} +{% block subtitle %}{{config.site_name}} - New Moderation Report{% endblock subtitle %} + +{% block body_greeting %} + {% email_body_content "Moderation Team," %} + {% email_body_content "A new report has been recieved for {{config.site_name}}." %} +{% endblock body_greeting %} + +{% block body_content %} + {% email_body_content "=============== Report Summary ===============" %} + + {% if report.source_identity %} + {% email_body_content "Reporter Identity: {{report.source_identity}} @ {{report.source_domain}}" %} + {% else %} + {% email_body_content "Reporter Identity: Anonymous Report from {{report.source_domain}}" %} + {% endif %} + + {% email_body_content "Reported Identity: {{report.subject_identity}}" %} + {% email_body_content "Complaint Type: {{report.type}}" %} + {% email_body_content "Complaint Notes: {{report.complaint}}" %} +{% endblock body_content %} + +{% block body_button %} + {% email_button button_text="View Report" button_link="https://{{settings.MAIN_DOMAIN}}/admin/reports/{{report.id}}/" %} +{% endblock body_button %} + +{% block extra_footer %} + {% email_footer "You are recieving this email because you are a moderator on the Takahe instance https://{{settings.MAIN_DOMAIN}}" %} +{% endblock extra_footer %} +{% endautoescape %} diff --git a/templates/emails/report_new.txt b/templates/emails/report_new.txt new file mode 100644 index 0000000..b454f9c --- /dev/null +++ b/templates/emails/report_new.txt @@ -0,0 +1,22 @@ +A new report has been recieved for {{config.site_name}}. + +Please review the report at the link below: + +https://{{settings.MAIN_DOMAIN}}/admin/reports/{{report.id}}/ + +=============== Report Summary =============== +{% if report.source_identity %} +Reporter Identity: {{report.source_identity}} @ {{report.source_domain}} +{% else %} +Reporter Identity: Anonymous Report from {{report.source_domain}} +{% endif %} + +Reported Identity: {{report.subject_identity}} + +Complaint Type: {{report.type}} + +Complaint Notes: {{report.complaint}} + +=============== End Report =============== + +You are recieving this email because you are a moderator on the Takahe instance https://{{settings.MAIN_DOMAIN}} diff --git a/users/models/report.py b/users/models/report.py index 3a26897..3b60280 100644 --- a/users/models/report.py +++ b/users/models/report.py @@ -2,9 +2,14 @@ from urllib.parse import urlparse import httpx import urlman +from asgiref.sync import sync_to_async +from django.conf import settings +from django.core.mail import EmailMultiAlternatives from django.db import models +from django.template.loader import render_to_string from core.ld import canonicalise, get_list +from core.models import Config from stator.models import State, StateField, StateGraph, StatorModel from users.models import Domain @@ -20,9 +25,15 @@ class ReportStates(StateGraph): """ Sends the report to the remote server if we need to """ - from users.models import SystemActor + from users.models import SystemActor, User + recipients = [] report = await instance.afetch_full() + async for mod in User.objects.filter( + models.Q(moderator=True) | models.Q(admin=True) + ).values_list("email", flat=True): + recipients.append(mod) + if report.forward and not report.subject_identity.domain.local: system_actor = SystemActor() try: @@ -32,7 +43,32 @@ class ReportStates(StateGraph): body=canonicalise(report.to_ap()), ) except httpx.RequestError: - return + pass + email = EmailMultiAlternatives( + subject=f"{Config.system.site_name}: New Moderation Report", + body=render_to_string( + "emails/report_new.txt", + { + "report": report, + "config": Config.system, + "settings": settings, + }, + ), + from_email=settings.SERVER_EMAIL, + bcc=recipients, + ) + email.attach_alternative( + content=render_to_string( + "emails/report_new.html", + { + "report": report, + "config": Config.system, + "settings": settings, + }, + ), + mimetype="text/html", + ) + await sync_to_async(email.send)() return cls.sent