Skip to content

Commit

Permalink
src/email.py: Remove send_email and use only Email dataclass for emai…
Browse files Browse the repository at this point in the history
…l ops
  • Loading branch information
cbrxyz committed Jan 23, 2025
1 parent 57e11bb commit 33ae485
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 53 deletions.
42 changes: 25 additions & 17 deletions src/anonymous.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING, Literal

import discord

from .email import send_email
from .constants import SCHWARTZ_EMAIL
from .email import Email
from .views import MILBotModal, MILBotView

if TYPE_CHECKING:
Expand All @@ -14,6 +16,27 @@
IntendedTargets = Literal["schwartz", "operations", "leaders"]


@dataclass
class SchwartzAnonymousEmail(Email):
def __init__(self, report: str):
html = f"""A new anonymous report has been submitted. The report is as follows:
<blockquote>{report}</blockquote>
Replies to this email will not be received. Please address any concerns with the appropriate leadership team."""
text = f"""A new anonymous report has been submitted. The report is as follows:
{report}
Replies to this email will not be received. Please address any concerns with the appropriate leadership team."""
super().__init__(
receiver_emails=[SCHWARTZ_EMAIL],
subject="New Anonymous Report Received",
html=html,
text=text,
)


class AnonymousReportModal(MILBotModal):

report = discord.ui.TextInput(
Expand All @@ -36,22 +59,7 @@ async def on_submit(self, interaction: discord.Interaction):
)
embed.set_footer(text="Submitted by an anonymous user")
if self.target == "schwartz":
html = f"""A new anonymous report has been submitted. The report is as follows:
<blockquote>{self.report.value}</blockquote>
Replies to this email will not be received. Please address any concerns with the appropriate leadership team."""
text = f"""A new anonymous report has been submitted. The report is as follows:
{self.report.value}
Replies to this email will not be received. Please address any concerns with the appropriate leadership team."""
await send_email(
"ems@ufl.edu",
"New Anonymous Report Received",
html,
text,
)
await SchwartzAnonymousEmail(self.report.value).send()
elif self.target == "operations":
await self.bot.operations_leaders_channel.send(embed=embed)
elif self.target == "leaders":
Expand Down
11 changes: 0 additions & 11 deletions src/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,3 @@ async def send(self) -> None:
message.as_string(),
)
await smtp_server.quit()


async def send_email(receiver_email, subject, html, text) -> bool:
email = Email(
receiver_emails=[receiver_email],
subject=subject,
html=html,
text=text,
)
await email.send()
return True
58 changes: 33 additions & 25 deletions src/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import discord

from .email import send_email
from .email import Email
from .roles import TeamRolesView
from .views import MILBotModal, MILBotView

Expand All @@ -26,6 +26,36 @@ class VerificationCandidate:
code: int


@dataclass
class VerificationEmail(Email):
def _generate_message(self, code: int) -> tuple[str, str]:
return (
f"""
<html>
<head></head>
<body>
Welcome to the Machine Intelligence Laboratory Discord server!<br><br>
Your verification code is: <b>{code}</b><br><br>
Thanks,<br>Machine Intelligence Laboratory
</html>
""",
f"""Welcome to the Machine Intelligence Laboratory Discord server!
Your verification code is: {code}
Thanks,
Machine Intelligence Laboratory
""",
)

def __init__(self, email: str, code: int):
html, text = self._generate_message(code)
super().__init__(
receiver_emails=[email],
subject="MIL Discord Verification Code",
html=html,
text=text,
)


class Verifier:
def _generate_new_code(self) -> int:
"""
Expand All @@ -49,30 +79,8 @@ async def send_verification_email(self, candidate: VerificationCandidate):
"""
Sends a verification email to the candidate.
"""
messages = self._generate_message(candidate.code)
await send_email(
candidate.email,
"MIL Discord Verification Code",
*messages,
)

def _generate_message(self, code: int) -> tuple[str, str]:
return (
f"""
<html>
<head></head>
<body>
Welcome to the Machine Intelligence Laboratory Discord server!<br><br>
Your verification code is: <b>{code}</b><br><br>
Thanks,<br>Machine Intelligence Laboratory
</html>
""",
f"""Welcome to the Machine Intelligence Laboratory Discord server!
Your verification code is: {code}
Thanks,
Machine Intelligence Laboratory
""",
)
email = VerificationEmail(candidate.email, candidate.code)
await email.send()


class VerificationCodeModal(MILBotModal):
Expand Down

0 comments on commit 33ae485

Please sign in to comment.