Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.

Conversation

H-Shay
Copy link
Contributor

@H-Shay H-Shay commented Aug 9, 2021

This PR implements Jinja as a templating engine for emails in Sydent.

Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

Definitely on the right track. Don't think we will need too much back and forth on this.

I had in mind that we were going to keep support for old-school templates; if someone else has said we're not, then let me know :).

@clokep clokep requested a review from reivilibre August 11, 2021 16:58
Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

very close to ready to roll, I think :)

@H-Shay H-Shay requested a review from reivilibre August 11, 2021 17:47
@H-Shay
Copy link
Contributor Author

H-Shay commented Aug 12, 2021

Okay so we've got some tests, jinja is now using an environment and autoescaping is enabled-turns out we were already manually cleaning the substitutions for html/url, so I left that in the code. Jinja doesn't seem to be double escaping anything so I think we are okay? Happy to rework that if this solution isn't amenable. Let me know!

@H-Shay H-Shay requested a review from clokep August 12, 2021 17:06
@H-Shay H-Shay requested a review from reivilibre August 16, 2021 18:34
Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

I think the tests are likely to need a little bit of change once you make some of these changes.

I wonder if it might be sensible to, in one of the tests:

  • patch out the path to the templates (so you can load a test-only template)
  • have a little test template, testing a variety of things
  • check the entire output to see that it's what we wanted.

You might be able to inject templates straight into the Jinja environment, that might be a good way to go about it instead.

@H-Shay H-Shay requested a review from reivilibre August 18, 2021 17:47
Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

this is coming along :)

@H-Shay H-Shay requested a review from reivilibre August 19, 2021 18:08
Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

looking great, let's get another pair of eyes on this

Copy link
Member

@clokep clokep left a comment

Choose a reason for hiding this comment

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

Looks good overall, but some of the code is a bit confusing still!


<p>
<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>
<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.

@H-Shay H-Shay requested a review from clokep August 23, 2021 16:50
Copy link
Member

@clokep clokep left a comment

Choose a reason for hiding this comment

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

I think this looks good with the one proposed change! It would be nice to have some upgrade notes for this too, although I'm not sure if we have a normal place for that in sydent or if we just put them in the release notes.

Comment on lines 76 to 83
with open(templateFile) as template_file:
for k, v in substitutions.items():
allSubstitutions[k + "_forhtml"] = escape(v)
allSubstitutions[k + "_forurl"] = urllib.parse.quote(v)
allSubstitutions["multipart_boundary"] = generateAlphanumericTokenOfLength(
32
)
mailString = template_file.read() % allSubstitutions
Copy link
Member

Choose a reason for hiding this comment

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

Since allSubstitutions is now only used in this branch, we can move it into here. It is also usually best to keep the file open for as short as possible! (I think I gave you the previous code, my bad!)

Suggested change
with open(templateFile) as template_file:
for k, v in substitutions.items():
allSubstitutions[k + "_forhtml"] = escape(v)
allSubstitutions[k + "_forurl"] = urllib.parse.quote(v)
allSubstitutions["multipart_boundary"] = generateAlphanumericTokenOfLength(
32
)
mailString = template_file.read() % allSubstitutions
allSubstitutions = {}
for k, v in substitutions.items():
allSubstitutions[k + "_forhtml"] = escape(v)
allSubstitutions[k + "_forurl"] = urllib.parse.quote(v)
allSubstitutions["multipart_boundary"] = generateAlphanumericTokenOfLength(
32
)
with open(templateFile) as template_file:
mailString = template_file.read() % allSubstitutions

Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

veerrry close now :)

Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

seems good to me

@reivilibre reivilibre merged commit 17ba907 into matrix-org:main Aug 24, 2021
clokep added a commit to t3chguy/sydent that referenced this pull request Aug 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants