Skip to content

Commit

Permalink
feat(mailer): Assign comments to users
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekSuchanek committed Jul 8, 2024
1 parent 8b0f6d5 commit fffa33a
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 43 deletions.
63 changes: 63 additions & 0 deletions packages/dsw-mailer/dsw/mailer/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import jinja2.sandbox
import json
import logging
import markdown
import markupsafe
import pathlib
import re

from typing import Optional, Union

Expand Down Expand Up @@ -77,6 +80,8 @@ def __init__(self, cfg: MailerConfig, workdir: pathlib.Path):
def _set_filters(self):
self.j2_env.filters.update({
'datetime_format': datetime_format,
'markdown': xmarkdown,
'no_markdown': remove_markdown,
})

def _load_jinja2(self, file_path: pathlib.Path) -> Optional[jinja2.Template]:
Expand Down Expand Up @@ -170,3 +175,61 @@ def datetime_format(iso_timestamp: Union[None, datetime.datetime, str], fmt: str
if not isinstance(iso_timestamp, datetime.datetime):
iso_timestamp = dateutil.parser.isoparse(iso_timestamp)
return iso_timestamp.strftime(fmt)


class DSWMarkdownExt(markdown.extensions.Extension):
def extendMarkdown(self, md):
md.preprocessors.register(DSWMarkdownProcessor(md), 'dsw_markdown', 27)
md.registerExtension(self)


class DSWMarkdownProcessor(markdown.preprocessors.Preprocessor):

def __init__(self, md):
super().__init__(md)
self.LI_RE = re.compile(r'^[ ]*((\d+\.)|[*+-])[ ]+.*')

def run(self, lines):
prev_li = False
new_lines = []

for line in lines:
# Add line break before the first list item
if self.LI_RE.match(line):
if not prev_li:
new_lines.append('')
prev_li = True
elif line == '':
prev_li = False

# Replace trailing un-escaped backslash with (supported) two spaces
_line = line.rstrip('\\')
if line[-1:] == '\\' and (len(line) - len(_line)) % 2 == 1:
new_lines.append(f'{line[:-1]} ')
continue

new_lines.append(line)

return new_lines


def xmarkdown(md_text: str):
if md_text is None:
return ''
return markupsafe.Markup(markdown.markdown(
text=md_text,
extensions=[
DSWMarkdownExt(),
]
))


def remove_markdown(md_text: str):
if md_text is None:
return ''
return re.sub(r'<[^>]*>', '', markdown.markdown(
text=md_text,
extensions=[
DSWMarkdownExt(),
]
))
1 change: 1 addition & 0 deletions packages/dsw-mailer/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies = [
'click',
'dkimpy',
'Jinja2',
'Markdown',
'pathvalidate',
'python-dateutil',
'sentry-sdk',
Expand Down
1 change: 1 addition & 0 deletions packages/dsw-mailer/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ click==8.1.7
dkimpy==1.1.7
dnspython==2.6.1
Jinja2==3.1.4
Markdown==3.6
MarkupSafe==2.1.5
pathvalidate==3.2.0
psycopg==3.1.19
Expand Down
4 changes: 2 additions & 2 deletions packages/dsw-mailer/templates/_common/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ h4 {

.headerContainer .dswTextContent,
.headerContainer .dswTextContent p {
color: #666666;
color: #212529;
font-family: Helvetica;
font-size: 16px;
line-height: 150%;
Expand Down Expand Up @@ -239,7 +239,7 @@ h4 {

.bodyContainer .dswTextContent,
.bodyContainer .dswTextContent p {
color: #666666;
color: #212529;
font-family: Helvetica;
font-size: 16px;
line-height: 150%;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<tbody>
<tr>
<td valign="top" class="dswTextContent" style="padding-top: 0; padding-right: 18px; padding-bottom: 9px; padding-left: 18px; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #757575; font-family: Helvetica; font-size: 16px; line-height: 150%; text-align: left;">
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #666666; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #212529; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
Hello,<br><br>
Your registration of <strong>{{ ctx.organizationName }}</strong> (<code>{{ ctx.organizationId }}</code>) is almost done. Just one more step to go. To activate the account, please click on the button below.
</p>
Expand Down Expand Up @@ -71,9 +71,9 @@
<table align="left" border="0" cellpadding="0" cellspacing="0" style="max-width: 100%; min-width: 100%; border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt;-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;" width="100%" class="dswTextContentContainer">
<tbody>
<tr>
<td valign="top" class="dswTextContent" style="padding: 0px 18px 9px; line-height: 150%; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #666666; font-family: Helvetica; font-size: 16px; text-align: left;">
<p dir="ltr" style="line-height: 150%; margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #666666; font-family: Helvetica; font-size: 14px; text-align: left;">
<em>Thank you for using the DSW!</em><br><br>
<td valign="top" class="dswTextContent" style="padding: 0px 18px 9px; line-height: 150%; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #212529; font-family: Helvetica; font-size: 16px; text-align: left;">
<p dir="ltr" style="line-height: 150%; margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #212529; font-family: Helvetica; font-size: 14px; text-align: left;">
Thank you for using DSW!<br><br>
{% if ctx.appTitle %}<strong>{{ ctx.appTitle }}</strong> team{% endif %}
</p>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<tbody>
<tr>
<td valign="top" class="dswTextContent" style="padding-top: 0; padding-right: 18px; padding-bottom: 9px; padding-left: 18px; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #757575; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #666666; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #212529; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
Hello!<br><br>
Good news &mdash; we have a new organization {% if ctx.appTitle %}in {{ ctx.appTitle }}{% endif %} (<a href="{{ ctx.clientUrl }}" target="_blank">{{ ctx.clientUrl }}</a>):
</p>
Expand All @@ -28,7 +28,7 @@
<dd>{{ ctx.organizationEmail }}</dd>
</dl>
<br>
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #666666; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #212529; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<em>Have a nice day!</em><br><br>
{% if ctx.appTitle %}<strong>{{ ctx.appTitle }}</strong> team{% endif %}
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<table align="left" border="0" cellpadding="0" cellspacing="0" style="max-width: 100%; min-width: 100%; border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;" width="100%" class="dswTextContentContainer">
<tbody>
<tr>
<td valign="top" class="dswTextContent" style="padding-top: 0; padding-right: 18px; padding-bottom: 9px; padding-left: 18px; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #666666; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #666666; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<td valign="top" class="dswTextContent" style="padding-top: 0; padding-right: 18px; padding-bottom: 9px; padding-left: 18px; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #212529; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #212529; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
Hello,<br><br>
Lost the token for <strong>{{ ctx.organizationName }}</strong> (<code>{{ ctx.organizationId }}</code>)? No problem, just click the button below to set up a new one.
</p>
Expand Down Expand Up @@ -65,9 +65,9 @@
<table align="left" border="0" cellpadding="0" cellspacing="0" style="max-width: 100%; min-width: 100%; border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt;-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;" width="100%" class="dswTextContentContainer">
<tbody>
<tr>
<td valign="top" class="dswTextContent" style="padding: 0px 18px 9px; line-height: 150%; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #666666; font-family: Helvetica; font-size: 16px; text-align: left;">
<p dir="ltr" style="line-height: 150%; margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #666666; font-family: Helvetica; font-size: 14px; text-align: left;">
<em>Thank you for using the DSW!</em><br><br>
<td valign="top" class="dswTextContent" style="padding: 0px 18px 9px; line-height: 150%; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #212529; font-family: Helvetica; font-size: 16px; text-align: left;">
<p dir="ltr" style="line-height: 150%; margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #212529; font-family: Helvetica; font-size: 14px; text-align: left;">
Thank you for using DSW!<br><br>
{% if ctx.appTitle %}<strong>{{ ctx.appTitle }}</strong> team{% endif %}
</p>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<table align="left" border="0" cellpadding="0" cellspacing="0" style="max-width: 100%; min-width: 100%; border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;" width="100%" class="dswTextContentContainer">
<tbody>
<tr>
<td valign="top" class="dswTextContent" style="padding-top: 0; padding-right: 18px; padding-bottom: 9px; padding-left: 18px; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #666666; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #666666; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<td valign="top" class="dswTextContent" style="padding-top: 0; padding-right: 18px; padding-bottom: 9px; padding-left: 18px; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #212529; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #212529; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
Dear {{ ctx.userFirstName }},<br><br>
A new API key named <em>{{ ctx.tokenName }}</em> has been created for your account.<br>
It will expire on {{ ctx.tokenExpiresAt|datetime_format("%B %-d, %Y (at %H:%M %Z)") }}.
Expand Down Expand Up @@ -66,12 +66,12 @@
<table align="left" border="0" cellpadding="0" cellspacing="0" style="max-width: 100%; min-width: 100%; border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt;-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;" width="100%" class="dswTextContentContainer">
<tbody>
<tr>
<td valign="top" class="dswTextContent" style="padding: 0px 18px 9px; line-height: 150%; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #666666; font-family: Helvetica; font-size: 16px; text-align: left;">
<p dir="ltr" style="line-height: 150%; margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #666666; font-family: Helvetica; font-size: 14px; text-align: left;">
<td valign="top" class="dswTextContent" style="padding: 0px 18px 9px; line-height: 150%; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #212529; font-family: Helvetica; font-size: 16px; text-align: left;">
<p dir="ltr" style="line-height: 150%; margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #212529; font-family: Helvetica; font-size: 14px; text-align: left;">
{% if ctx.supportEmail %}
If you did not create this API key or encounter any issues, please contact us via <a href="mailto:{{ ctx.supportEmail }}" style="mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #007C89; font-weight: normal; text-decoration: underline;">{{ ctx.supportEmail }}</a>.
{% endif %}<br><br>
<em>Thank you for using the DSW!</em><br><br>
Thank you for using DSW!<br><br>
{% if ctx.appTitle %}<strong>{{ ctx.appTitle }}</strong> team{% endif %}
</p>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<table align="left" border="0" cellpadding="0" cellspacing="0" style="max-width: 100%; min-width: 100%; border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;" width="100%" class="dswTextContentContainer">
<tbody>
<tr>
<td valign="top" class="dswTextContent" style="padding-top: 0; padding-right: 18px; padding-bottom: 9px; padding-left: 18px; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #666666; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #666666; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<td valign="top" class="dswTextContent" style="padding-top: 0; padding-right: 18px; padding-bottom: 9px; padding-left: 18px; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #212529; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
<p dir="ltr" style="margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #212529; font-family: Helvetica; font-size: 14px; line-height: 150%; text-align: left;">
Dear {{ ctx.userFirstName }},<br><br>
Your API key, identified as <em>{{ ctx.tokenName }}</em>, is scheduled to expire on {{ ctx.tokenExpiresAt|datetime_format("%B %-d, %Y (at %H:%M %Z)") }}.
</p>
Expand Down Expand Up @@ -65,12 +65,12 @@
<table align="left" border="0" cellpadding="0" cellspacing="0" style="max-width: 100%; min-width: 100%; border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt;-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;" width="100%" class="dswTextContentContainer">
<tbody>
<tr>
<td valign="top" class="dswTextContent" style="padding: 0px 18px 9px; line-height: 150%; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #666666; font-family: Helvetica; font-size: 16px; text-align: left;">
<p dir="ltr" style="line-height: 150%; margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #666666; font-family: Helvetica; font-size: 14px; text-align: left;">
<td valign="top" class="dswTextContent" style="padding: 0px 18px 9px; line-height: 150%; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; word-break: break-word; color: #212529; font-family: Helvetica; font-size: 16px; text-align: left;">
<p dir="ltr" style="line-height: 150%; margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #212529; font-family: Helvetica; font-size: 14px; text-align: left;">
{% if ctx.supportEmail %}
If you encounter any issues, please contact us via <a href="mailto:{{ ctx.supportEmail }}" style="mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #007C89; font-weight: normal; text-decoration: underline;">{{ ctx.supportEmail }}</a>.
{% endif %}<br><br>
<em>Thank you for using the DSW!</em><br><br>
Thank you for using DSW!<br><br>
{% if ctx.appTitle %}<strong>{{ ctx.appTitle }}</strong> team{% endif %}
</p>
</td>
Expand Down
Loading

0 comments on commit fffa33a

Please sign in to comment.