Send out notifications to users via meta.sr.ht GQL

This moves handling of (potentially) encrypted emails into the
meta.sr.ht API. Emails to external addresses are still sent
(unencrypted) via the core.sr.ht Pyhon module.
This commit is contained in:
Conrad Hoffmann 2022-11-10 13:51:13 +01:00 committed by Drew DeVault
parent 5c4ba72503
commit ed005d6b4f
1 changed files with 21 additions and 11 deletions

View File

@ -1,9 +1,10 @@
import html.parser
import os
import textwrap
from string import Template
from srht.config import cfg, cfgi
from srht.email import send_email, lookup_key
from srht.crypto import internal_anon
from srht.email import send_email
from srht.graphql import exec_gql
from srht.oauth import current_user
from todosrht.types import ParticipantType
@ -15,15 +16,8 @@ def format_lines(text, quote=False):
return "\n".join(wrapped)
def notify(sub, template, subject, headers, **kwargs):
encrypt_key = None
to = sub.participant
if to.participant_type == ParticipantType.email:
to = to.email
elif to.participant_type == ParticipantType.user:
user = to.user
to = user.email
encrypt_key = lookup_key(user)
else:
if to.participant_type == ParticipantType.external:
return
with open(os.path.join(os.path.dirname(__file__), "emails", template)) as f:
tmpl = Template(f.read())
@ -31,4 +25,20 @@ def notify(sub, template, subject, headers, **kwargs):
'root': origin,
**kwargs,
})
send_email(body, to, subject, encrypt_key=encrypt_key, **headers)
if to.participant_type == ParticipantType.email:
to = to.email
send_email(body, to, subject, **headers)
elif to.participant_type == ParticipantType.user:
user = to.user
to = user.username
msg = f"Subject: {subject}\n"
for hdr, val in headers.items():
msg += f"{hdr}: {val}\n"
msg += "\n" + body
email_mutation = """
mutation SendEmail($username: String!, $msg: String!) {
sendEmailNotification(username: $username, message: $msg)
}
"""
r = exec_gql("meta.sr.ht", email_mutation, user=internal_anon,
username=to, msg=msg)