Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore using Jinja for templating #376

Merged
merged 40 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ed107ee
add jinja to setup
H-Shay Aug 9, 2021
338bd5d
update send_email function to render templates with jinja
H-Shay Aug 9, 2021
589c396
change templates to use jinja
H-Shay Aug 9, 2021
8e40335
lints
H-Shay Aug 9, 2021
7c81e22
Add changelog
H-Shay Aug 9, 2021
c34bd99
merge with main to update branch
H-Shay Aug 11, 2021
a2106c1
add .j2 templates
H-Shay Aug 11, 2021
35eea47
check if jinja templates available and if so use them
H-Shay Aug 11, 2021
3f22e70
rename changelog file, version jinja in setup.py
H-Shay Aug 11, 2021
a97b0ae
lints
H-Shay Aug 11, 2021
525be72
requested changes
H-Shay Aug 11, 2021
308f9dc
revoke change to terms.py
H-Shay Aug 11, 2021
a721485
add some tests
H-Shay Aug 11, 2021
a542201
use jinja environment and autoescape
H-Shay Aug 11, 2021
fb7db3c
format jinja templates for autoescape and test
H-Shay Aug 12, 2021
35c2640
fix tests, fix jinja templates
H-Shay Aug 12, 2021
a3adf42
lints + update tests
H-Shay Aug 12, 2021
dd46fae
reset terms
H-Shay Aug 12, 2021
96580b9
update templates with safe filter
H-Shay Aug 13, 2021
c35fd94
move jinja environment creation to startup, add env to sydent
H-Shay Aug 13, 2021
56e0750
update tests
H-Shay Aug 13, 2021
4606fde
lints
H-Shay Aug 13, 2021
a5f1416
update template files
H-Shay Aug 16, 2021
808b539
update tests and send_email function
H-Shay Aug 16, 2021
f0f25cd
reset terms
H-Shay Aug 16, 2021
e75c4d7
update templates
H-Shay Aug 18, 2021
ab9ccec
only manually escape old templates
H-Shay Aug 18, 2021
5c8dc2e
update tests
H-Shay Aug 18, 2021
e43f0f9
add smaple text for testing
H-Shay Aug 18, 2021
3e3b116
update tests name
H-Shay Aug 18, 2021
6c60693
lints + rename jinja tests
H-Shay Aug 18, 2021
9880e0c
update sydent.py
H-Shay Aug 18, 2021
5379f85
requested changes + lints
H-Shay Aug 19, 2021
611ee4a
update templates
H-Shay Aug 23, 2021
0baa33c
requested changes + add licence
H-Shay Aug 23, 2021
4010fd5
lints
H-Shay Aug 23, 2021
a9dcbcf
slight refactor
H-Shay Aug 23, 2021
d4d0486
requested change + lints
H-Shay Aug 24, 2021
2da3529
Merge branch 'main' into jinja
H-Shay Aug 24, 2021
b0599a7
fix error in redering emails
H-Shay Aug 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions res/matrix-org/invite_template.eml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Matrix client from https://matrix.org/docs/projects/try-matrix-now.html or use
the single-click link below to join via Element (requires Chrome, Firefox,
Safari, iOS or Android)

{{ web_client_location }}/#/room/{{ room_id_forurl }}?email={{ to_forurl }}&signurl=https%%3A%%2F%%2Fmatrix.org%%2F_matrix%%2Fidentity%%2Fapi%%2Fv1%%2Fsign-ed25519%%3Ftoken%%3D{{ token }}%%26private_key%%3D{{ ephemeral_private_key }}&room_name={{ room_name_forurl }}&room_avatar_url={{ room_avatar_url_forurl }}&inviter_name={{ sender_display_name_forurl }}&guest_access_token={{ guest_access_token_forurl }}&guest_user_id={{ guest_user_id_forurl }}

{{ web_client_location }}/#/room/{{ room_id|urlencode }}?email={{ to|urlencode }}&signurl=https%3A%2F%2Fmatrix.org%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3D{{ token|urlencode }}%26private_key%3D{{ ephemeral_private_key|urlencode }}&room_name={{ room_name|urlencode }}&room_avatar_url={{ room_avatar_url|urlencode }}&inviter_name={{ sender_display_name|urlencode }}&guest_access_token={{ guest_access_token|urlencode }}&guest_user_id={{ guest_user_id|urlencode }}


About Matrix:
Expand Down Expand Up @@ -104,7 +105,7 @@ pre, code {

<p>Hi,</p>

<p>{{ sender_display_name_forhtml }} <span class="low-contrast">{{ bracketed_verified_sender_forhtml }}</span> has invited you into a room {{ bracketed_room_name_forhtml }} on
<p>{{ sender_display_name }} <span class="low-contrast">{{ bracketed_verified_sender }}</span> has invited you into a room {{ bracketed_room_name }} on
Matrix. To join the conversation, either <a href="https://matrix.org/docs/projects/try-matrix-now.html">pick a Matrix client</a> or use the single-click
link below to join via Element (requires
<a href="https://www.google.com/chrome">Chrome</a>,
Expand All @@ -115,7 +116,7 @@ or iOS or Android on mobile.)</p>

<p>
<a
href="https://app.element.io/#/room/{{ room_id_forurl }}?email={{ to_forurl }}&signurl=https%%3A%%2F%%2Fmatrix.org%%2F_matrix%%2Fidentity%%2Fapi%%2Fv1%%2Fsign-ed25519%%3Ftoken%%3D{{ token }}%%26private_key%%3D{{ ephemeral_private_key }}&room_name={{ room_name_forurl }}&room_avatar_url={{ room_avatar_url_forurl }}&inviter_name={{ sender_display_name_forurl }}&guest_access_token={{ guest_access_token_forurl }}&guest_user_id={{ guest_user_id_forurl }}">Join the conversation.</a>
href="https://app.element.io/#/room/{{ room_id|urlencode }}?email={{ to|urlencode }}&signurl=https%3A%2F%2Fmatrix.org%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3D{{ token|urlencode }}%26private_key%3D{{ ephemeral_private_key|urlencode }}&room_name={{ room_name|urlencode }}&room_avatar_url={{ room_avatar_url|urlencode }}&inviter_name={{ sender_display_name|urlencode }}&guest_access_token={{ guest_access_token|urlencode }}&guest_user_id={{ guest_user_id|urlencode }}">Join the conversation.</a>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, should this be using {{ web_client_location }} here too? (Probably copied from the non-Jinja version though.) We might want to fix that separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed from the older version, I will fix in separate PR.

</p>

<br>
Expand Down
6 changes: 3 additions & 3 deletions res/matrix-org/verification_template.eml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ We have received a request to use this email address with a matrix.org identity
server. If this was you who made this request, you may use the following link
to complete the verification of your email address:

{{ link|safe }}
{{ link|urlencode }}
reivilibre marked this conversation as resolved.
Show resolved Hide resolved

If your client requires a code, the code is {{ token|safe }}

Expand Down Expand Up @@ -59,11 +59,11 @@ body {
identity server. If this was you who made this request, you may use the
following link to complete the verification of your email address:</p>

<p><a href="{{ link|safe }}">Complete email verification</a></p>
<p><a href="{{ link|urlencode }}">Complete email verification</a></p>
reivilibre marked this conversation as resolved.
Show resolved Hide resolved

<p>...or copy this link into your web browser:</p>

<p>{{ link|safe }}</p>
<p>{{ link|urlencode }}</p>
reivilibre marked this conversation as resolved.
Show resolved Hide resolved

<p>If your client requires a code, the code is {{ token }}</p>

Expand Down
8 changes: 4 additions & 4 deletions res/vector-im/invite_template.eml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Hi,
{{ bracketed_room_name|safe }}on Element. To join the conversation please follow the
link below.

{{ web_client_location }}/#/room/{{ room_id_forurl }}?email={{ to_forurl }}&signurl=https%%3A%%2F%%2Fvector.im%%2F_matrix%%2Fidentity%%2Fapi%%2Fv1%%2Fsign-ed25519%%3Ftoken%%3D{{ token }}%%26private_key%%3D{{ ephemeral_private_key }}&room_name={{ room_name_forurl }}&room_avatar_url={{ room_avatar_url_forurl }}&inviter_name={{ sender_display_name_forurl }}&guest_access_token={{ guest_access_token_forurl }}&guest_user_id={{ guest_user_id_forurl }}
{{ web_client_location }}/#/room/{{ room_id|urlencode }}?email={{ to|urlencode }}&signurl=https%3A%2F%2Fvector.im%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3D{{ token|urlencode }}%26private_key%3D{{ ephemeral_private_key|urlencode }}&room_name={{ room_name|urlencode }}&room_avatar_url={{ room_avatar_url|urlencode }}&inviter_name={{ sender_display_name|urlencode }}&guest_access_token={{ guest_access_token|urlencode }}&guest_user_id={{ guest_user_id|urlencode }}

Element is an open source collaboration app built on the Matrix.org
open standard for interoperable communication: supporting group chat,
Expand Down Expand Up @@ -123,12 +123,12 @@ pre, code {

<p>Hi,</p>

<p>{{ sender_display_name_forhtml }} <span class="low-contrast">{{ bracketed_verified_sender_forhtml }}</span> has invited you into a
room {{ bracketed_room_name_forhtml }} on Element.</p>
<p>{{ sender_display_name }} <span class="low-contrast">{{ bracketed_verified_sender }}</span> has invited you into a
room {{ bracketed_room_name }} on Element.</p>

<p>
<a
href="https://app.element.io/#/room/{{ room_id_forurl }}?email={{ to_forurl }}&signurl=https%%3A%%2F%%2Fvector.im%%2F_matrix%%2Fidentity%%2Fapi%%2Fv1%%2Fsign-ed25519%%3Ftoken%%3D{{ token }}%%26private_key%%3D{{ ephemeral_private_key }}&room_name={{ room_name_forurl }}&room_avatar_url={{ room_avatar_url_forurl }}&inviter_name={{ sender_display_name_forurl }}&guest_access_token={{ guest_access_token_forurl }}&guest_user_id={{ guest_user_id_forurl }}">Join the conversation.</a>
href="https://app.element.io/#/room/{{ room_id|urlencode }}?email={{ to|urlencode }}&signurl=https%3A%2F%2Fvector.im%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3D{{ token|urlencode }}%26private_key%3D{{ ephemeral_private_key|urlencode }}&room_name={{ room_name|urlencode }}&room_avatar_url={{ room_avatar_url|urlencode }}&inviter_name={{ sender_display_name|urlencode }}&guest_access_token={{ guest_access_token|urlencode }}&guest_user_id={{ guest_user_id|urlencode }}">Join the conversation.</a>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing about web_client_location.

</p>

<p>Element is an open source collaboration app built on the Matrix.org
Expand Down
4 changes: 2 additions & 2 deletions res/vector-im/verification_template.eml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ distributed and secure shared workspace for the web that's built on Matrix.
If it was really you who made this request, you can click on the following link to
complete the verification of your email address:

{{ link|safe }}
{{ link|urlencode }}
reivilibre marked this conversation as resolved.
Show resolved Hide resolved

Please note that you will need to use Chrome, Firefox or Safari on the web, or
iOS or Android on mobile.
Expand Down Expand Up @@ -125,7 +125,7 @@ pre, code {
<p>If it was really you who made this request, you can click on the following link to
complete the verification of your email address:</p>

<p><a href="{{ link|safe }}">Complete email verification</a></p>
<p><a href="{{ link|urlencode }}">Complete email verification</a></p>
reivilibre marked this conversation as resolved.
Show resolved Hide resolved


<p>Please note that Element requires
Expand Down
155 changes: 155 additions & 0 deletions res/vector_verification_sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
Hello there!

You have asked us to register this email address with element.io - the open source,
distributed and secure shared workspace for the web that's built on Matrix.

If it was really you who made this request, you can click on the following link to
complete the verification of your email address:

https%3A//link_test.com
reivilibre marked this conversation as resolved.
Show resolved Hide resolved

Please note that you will need to use Chrome, Firefox or Safari on the web, or
iOS or Android on mobile.

If you didn't make this request, you can safely disregard this email.

Thanks!

Element


About Element:

Break through - Element allows teams to communicate across a wide range of collaboration
apps. If some team members use Element while others use IRC, Slack or Gitter, Element will
allow these team members to seamlessly work together. Element offers the richest
network of communication bridges.

Own Your Own Data - No one should control your communication and data but you. Element
lets you run your own server, and provides users and teams with the most advanced
crypto ratchet technology available today for a decentralized secure Internet.

Open Source - Element is entirely open source: all the code is published on GitHub
(Apache License) for anyone to see and extend. This means teams can customize or
contribute to the code and everyone can benefit from the speed of community innovation.

Made on Matrix - Element is built on top of Matrix. Matrix is an open network for secure,
decentralized communication delivering a community of users, bridged networks,
integrated bots and applications plus full end-to-end encryption. To learn more about
Matrix visit https://matrix.org.

Content-Type: text/html; charset=UTF-8
Content-Disposition: inline

<!doctype html>
<html lang="en">
<head>
<style type="text/css">
body {
margin: 0px;
}

pre, code {
word-break: break-word;
white-space: pre-wrap;
}

#page {
font-family: 'Open Sans', Helvetica, Arial, Sans-Serif;
font-color: #454545;
font-size: 12pt;
width: 100%%;
padding: 20px;
}

#inner {
width: 640px;
}

.header {
width: 100%%;
height: 87px;
color: #454545;
border-bottom: 4px solid #e5e5e5;
}

.logo {
text-align: right;
margin-left: 20px;
}

.header {
border-bottom: 4px solid #e4f7ed ! important;
}


.notif_link a, .footer a {
color: #76CFA6 ! important;
}
</style>
</head>
<body>
<table id="page">
<tr>
<td> </td>
<td id="inner">
<table class="header">
<tr>
<td>
</td>
<td class="logo">
<img src="https://static.element.io/images/email-logo.png" width="64" height="64" alt="Element"/>
</td>
</tr>
</table>

<p>Hello there!</p>

<p>You have asked us to register this email address with element.io - the open source,
distributed and secure shared workspace for the web that's built on Matrix.</p>

<p>If it was really you who made this request, you can click on the following link to
complete the verification of your email address:</p>

<p><a href="https%3A//link_test.com">Complete email verification</a></p>


<p>Please note that Element requires
<a href="https://www.google.com/chrome">Chrome</a>,
<a href="https://www.getfirefox.com">Firefox</a> or
<a href="https://www.apple.com/safari">Safari</a> on the web,
or iOS or Android on mobile.</p>

<p>If you didn't make this request, you can safely disregard this email.</p>

<p>Thanks!</p>

<p>Element</p>

<br>
<p>About Element:</p>

<p><em>Break through</em> - Element allows teams to communicate across a wide range of collaboration
apps. If some team members use Element while others use IRC, Slack or Gitter, Element will
allow these team members to seamlessly work together. Element offers the richest
network of communication bridges.</p>

<p><em>Own Your Own Data</em> - No one should control your communication and data but you. Element
lets you run your own server, and provides users and teams with the most advanced
crypto ratchet technology available today for a decentralized secure Internet.</p>

<p><em>Open Source</em> - Element is entirely open source: all the code is published on GitHub
(Apache License) for anyone to see and extend. This means teams can customize or
contribute to the code and everyone can benefit from the speed of community innovation.</p>

<p><em>Made on Matrix</em> - Element is built on top of Matrix. Matrix is an open network for secure,
decentralized communication delivering a community of users, bridged networks,
integrated bots and applications plus full end-to-end encryption. To learn more about
Matrix visit https://matrix.org.</p>

</td>
<td> </td>
</tr>
</table>
</body>
</html>
4 changes: 2 additions & 2 deletions sydent/sydent.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from typing import Set

import twisted.internet.reactor
from jinja2 import Environment, FileSystemLoader, select_autoescape
from jinja2 import Environment, FileSystemLoader
from twisted.internet import address, task
from twisted.python import log

Expand Down Expand Up @@ -297,7 +297,7 @@ def __init__(
)
self.template_environment = Environment(
loader=FileSystemLoader(self.cfg.get("general", "templates.path")),
autoescape=select_autoescape(default=True),
autoescape=True,
)

# See if a pepper already exists in the database
Expand Down
6 changes: 2 additions & 4 deletions sydent/util/emailutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ def sendEmail(

for k, v in substitutions.items():
allSubstitutions[k] = v
if templateFile.endswith(".j2"):
allSubstitutions[k + "_forhtml"] = v
else:
if not templateFile.endswith(".j2"):
allSubstitutions[k + "_forhtml"] = escape(v)
allSubstitutions[k + "_forurl"] = urllib.parse.quote(v)
allSubstitutions[k + "_forurl"] = urllib.parse.quote(v)

# We add randomize the multipart boundary to stop user input from
# conflicting with it.
Expand Down
Loading