Include a simple message in email notifications that include encrypted content (#8545)

This commit is contained in:
Patrick Cloke 2020-10-19 09:12:39 -04:00 committed by GitHub
parent 85c56445fb
commit c356b4bf42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 107 additions and 75 deletions

1
changelog.d/8545.bugfix Normal file
View file

@ -0,0 +1 @@
Fix a long standing bug where email notifications for encrypted messages were blank.

View file

@ -387,8 +387,8 @@ class Mailer:
return ret return ret
async def get_message_vars(self, notif, event, room_state_ids): async def get_message_vars(self, notif, event, room_state_ids):
if event.type != EventTypes.Message: if event.type != EventTypes.Message and event.type != EventTypes.Encrypted:
return return None
sender_state_event_id = room_state_ids[("m.room.member", event.sender)] sender_state_event_id = room_state_ids[("m.room.member", event.sender)]
sender_state_event = await self.store.get_event(sender_state_event_id) sender_state_event = await self.store.get_event(sender_state_event_id)
@ -399,10 +399,8 @@ class Mailer:
# sender_hash % the number of default images to choose from # sender_hash % the number of default images to choose from
sender_hash = string_ordinal_total(event.sender) sender_hash = string_ordinal_total(event.sender)
msgtype = event.content.get("msgtype")
ret = { ret = {
"msgtype": msgtype, "event_type": event.type,
"is_historical": event.event_id != notif["event_id"], "is_historical": event.event_id != notif["event_id"],
"id": event.event_id, "id": event.event_id,
"ts": event.origin_server_ts, "ts": event.origin_server_ts,
@ -411,6 +409,14 @@ class Mailer:
"sender_hash": sender_hash, "sender_hash": sender_hash,
} }
# Encrypted messages don't have any additional useful information.
if event.type == EventTypes.Encrypted:
return ret
msgtype = event.content.get("msgtype")
ret["msgtype"] = msgtype
if msgtype == "m.text": if msgtype == "m.text":
self.add_text_message_vars(ret, event) self.add_text_message_vars(ret, event)
elif msgtype == "m.image": elif msgtype == "m.image":

View file

@ -1,41 +1,47 @@
{% for message in notif.messages %} {%- for message in notif.messages %}
<tr class="{{ "historical_message" if message.is_historical else "message" }}"> <tr class="{{ "historical_message" if message.is_historical else "message" }}">
<td class="sender_avatar"> <td class="sender_avatar">
{% if loop.index0 == 0 or notif.messages[loop.index0 - 1].sender_name != notif.messages[loop.index0].sender_name %} {%- if loop.index0 == 0 or notif.messages[loop.index0 - 1].sender_name != notif.messages[loop.index0].sender_name %}
{% if message.sender_avatar_url %} {%- if message.sender_avatar_url %}
<img alt="" class="sender_avatar" src="{{ message.sender_avatar_url|mxc_to_http(32,32) }}" /> <img alt="" class="sender_avatar" src="{{ message.sender_avatar_url|mxc_to_http(32,32) }}" />
{% else %} {%- else %}
{% if message.sender_hash % 3 == 0 %} {%- if message.sender_hash % 3 == 0 %}
<img class="sender_avatar" src="https://riot.im/img/external/avatar-1.png" /> <img class="sender_avatar" src="https://riot.im/img/external/avatar-1.png" />
{% elif message.sender_hash % 3 == 1 %} {%- elif message.sender_hash % 3 == 1 %}
<img class="sender_avatar" src="https://riot.im/img/external/avatar-2.png" /> <img class="sender_avatar" src="https://riot.im/img/external/avatar-2.png" />
{% else %} {%- else %}
<img class="sender_avatar" src="https://riot.im/img/external/avatar-3.png" /> <img class="sender_avatar" src="https://riot.im/img/external/avatar-3.png" />
{% endif %} {%- endif %}
{% endif %} {%- endif %}
{% endif %} {%- endif %}
</td> </td>
<td class="message_contents"> <td class="message_contents">
{% if loop.index0 == 0 or notif.messages[loop.index0 - 1].sender_name != notif.messages[loop.index0].sender_name %} {%- if loop.index0 == 0 or notif.messages[loop.index0 - 1].sender_name != notif.messages[loop.index0].sender_name %}
<div class="sender_name">{% if message.msgtype == "m.emote" %}*{% endif %} {{ message.sender_name }}</div> <div class="sender_name">{%- if message.msgtype == "m.emote" %}*{%- endif %} {{ message.sender_name }}</div>
{% endif %} {%- endif %}
<div class="message_body"> <div class="message_body">
{% if message.msgtype == "m.text" %} {%- if message.event_type == "m.room.encrypted" %}
{{ message.body_text_html }} An encrypted message.
{% elif message.msgtype == "m.emote" %} {%- elif message.event_type == "m.room.message" %}
{{ message.body_text_html }} {%- if message.msgtype == "m.text" %}
{% elif message.msgtype == "m.notice" %} {{ message.body_text_html }}
{{ message.body_text_html }} {%- elif message.msgtype == "m.emote" %}
{% elif message.msgtype == "m.image" %} {{ message.body_text_html }}
<img src="{{ message.image_url|mxc_to_http(640, 480, scale) }}" /> {%- elif message.msgtype == "m.notice" %}
{% elif message.msgtype == "m.file" %} {{ message.body_text_html }}
<span class="filename">{{ message.body_text_plain }}</span> {%- elif message.msgtype == "m.image" %}
{% endif %} <img src="{{ message.image_url|mxc_to_http(640, 480, scale) }}" />
{%- elif message.msgtype == "m.file" %}
<span class="filename">{{ message.body_text_plain }}</span>
{%- else %}
A message with unrecognised content.
{%- endif %}
{%- endif %}
</div> </div>
</td> </td>
<td class="message_time">{{ message.ts|format_ts("%H:%M") }}</td> <td class="message_time">{{ message.ts|format_ts("%H:%M") }}</td>
</tr> </tr>
{% endfor %} {%- endfor %}
<tr class="notif_link"> <tr class="notif_link">
<td></td> <td></td>
<td> <td>

View file

@ -1,16 +1,22 @@
{% for message in notif.messages %} {%- for message in notif.messages %}
{% if message.msgtype == "m.emote" %}* {% endif %}{{ message.sender_name }} ({{ message.ts|format_ts("%H:%M") }}) {%- if message.event_type == "m.room.encrypted" %}
{% if message.msgtype == "m.text" %} An encrypted message.
{%- elif message.event_type == "m.room.message" %}
{%- if message.msgtype == "m.emote" %}* {%- endif %}{{ message.sender_name }} ({{ message.ts|format_ts("%H:%M") }})
{%- if message.msgtype == "m.text" %}
{{ message.body_text_plain }} {{ message.body_text_plain }}
{% elif message.msgtype == "m.emote" %} {%- elif message.msgtype == "m.emote" %}
{{ message.body_text_plain }} {{ message.body_text_plain }}
{% elif message.msgtype == "m.notice" %} {%- elif message.msgtype == "m.notice" %}
{{ message.body_text_plain }} {{ message.body_text_plain }}
{% elif message.msgtype == "m.image" %} {%- elif message.msgtype == "m.image" %}
{{ message.body_text_plain }} {{ message.body_text_plain }}
{% elif message.msgtype == "m.file" %} {%- elif message.msgtype == "m.file" %}
{{ message.body_text_plain }} {{ message.body_text_plain }}
{% endif %} {%- else %}
{% endfor %} A message with unrecognised content.
{%- endif %}
{%- endif %}
{%- endfor %}
View {{ room.title }} at {{ notif.link }} View {{ room.title }} at {{ notif.link }}

View file

@ -2,8 +2,8 @@
<html lang="en"> <html lang="en">
<head> <head>
<style type="text/css"> <style type="text/css">
{% include 'mail.css' without context %} {%- include 'mail.css' without context %}
{% include "mail-%s.css" % app_name ignore missing without context %} {%- include "mail-%s.css" % app_name ignore missing without context %}
</style> </style>
</head> </head>
<body> <body>
@ -18,21 +18,21 @@
<div class="summarytext">{{ summary_text }}</div> <div class="summarytext">{{ summary_text }}</div>
</td> </td>
<td class="logo"> <td class="logo">
{% if app_name == "Riot" %} {%- if app_name == "Riot" %}
<img src="http://riot.im/img/external/riot-logo-email.png" width="83" height="83" alt="[Riot]"/> <img src="http://riot.im/img/external/riot-logo-email.png" width="83" height="83" alt="[Riot]"/>
{% elif app_name == "Vector" %} {%- elif app_name == "Vector" %}
<img src="http://matrix.org/img/vector-logo-email.png" width="64" height="83" alt="[Vector]"/> <img src="http://matrix.org/img/vector-logo-email.png" width="64" height="83" alt="[Vector]"/>
{% elif app_name == "Element" %} {%- elif app_name == "Element" %}
<img src="https://static.element.io/images/email-logo.png" width="83" height="83" alt="[Element]"/> <img src="https://static.element.io/images/email-logo.png" width="83" height="83" alt="[Element]"/>
{% else %} {%- else %}
<img src="http://matrix.org/img/matrix-120x51.png" width="120" height="51" alt="[matrix]"/> <img src="http://matrix.org/img/matrix-120x51.png" width="120" height="51" alt="[matrix]"/>
{% endif %} {%- endif %}
</td> </td>
</tr> </tr>
</table> </table>
{% for room in rooms %} {%- for room in rooms %}
{% include 'room.html' with context %} {%- include 'room.html' with context %}
{% endfor %} {%- endfor %}
<div class="footer"> <div class="footer">
<a href="{{ unsubscribe_link }}">Unsubscribe</a> <a href="{{ unsubscribe_link }}">Unsubscribe</a>
<br/> <br/>
@ -41,12 +41,12 @@
Sending email at {{ reason.now|format_ts("%c") }} due to activity in room {{ reason.room_name }} because Sending email at {{ reason.now|format_ts("%c") }} due to activity in room {{ reason.room_name }} because
an event was received at {{ reason.received_at|format_ts("%c") }} an event was received at {{ reason.received_at|format_ts("%c") }}
which is more than {{ "%.1f"|format(reason.delay_before_mail_ms / (60*1000)) }} ({{ reason.delay_before_mail_ms }}) mins ago, which is more than {{ "%.1f"|format(reason.delay_before_mail_ms / (60*1000)) }} ({{ reason.delay_before_mail_ms }}) mins ago,
{% if reason.last_sent_ts %} {%- if reason.last_sent_ts %}
and the last time we sent a mail for this room was {{ reason.last_sent_ts|format_ts("%c") }}, and the last time we sent a mail for this room was {{ reason.last_sent_ts|format_ts("%c") }},
which is more than {{ "%.1f"|format(reason.throttle_ms / (60*1000)) }} (current throttle_ms) mins ago. which is more than {{ "%.1f"|format(reason.throttle_ms / (60*1000)) }} (current throttle_ms) mins ago.
{% else %} {%- else %}
and we don't have a last time we sent a mail for this room. and we don't have a last time we sent a mail for this room.
{% endif %} {%- endif %}
</div> </div>
</div> </div>
</td> </td>

View file

@ -2,9 +2,9 @@ Hi {{ user_display_name }},
{{ summary_text }} {{ summary_text }}
{% for room in rooms %} {%- for room in rooms %}
{% include 'room.txt' with context %} {%- include 'room.txt' with context %}
{% endfor %} {%- endfor %}
You can disable these notifications at {{ unsubscribe_link }} You can disable these notifications at {{ unsubscribe_link }}

View file

@ -1,23 +1,23 @@
<table class="room"> <table class="room">
<tr class="room_header"> <tr class="room_header">
<td class="room_avatar"> <td class="room_avatar">
{% if room.avatar_url %} {%- if room.avatar_url %}
<img alt="" src="{{ room.avatar_url|mxc_to_http(48,48) }}" /> <img alt="" src="{{ room.avatar_url|mxc_to_http(48,48) }}" />
{% else %} {%- else %}
{% if room.hash % 3 == 0 %} {%- if room.hash % 3 == 0 %}
<img alt="" src="https://riot.im/img/external/avatar-1.png" /> <img alt="" src="https://riot.im/img/external/avatar-1.png" />
{% elif room.hash % 3 == 1 %} {%- elif room.hash % 3 == 1 %}
<img alt="" src="https://riot.im/img/external/avatar-2.png" /> <img alt="" src="https://riot.im/img/external/avatar-2.png" />
{% else %} {%- else %}
<img alt="" src="https://riot.im/img/external/avatar-3.png" /> <img alt="" src="https://riot.im/img/external/avatar-3.png" />
{% endif %} {%- endif %}
{% endif %} {%- endif %}
</td> </td>
<td class="room_name" colspan="2"> <td class="room_name" colspan="2">
{{ room.title }} {{ room.title }}
</td> </td>
</tr> </tr>
{% if room.invite %} {%- if room.invite %}
<tr> <tr>
<td></td> <td></td>
<td> <td>
@ -25,9 +25,9 @@
</td> </td>
<td></td> <td></td>
</tr> </tr>
{% else %} {%- else %}
{% for notif in room.notifs %} {%- for notif in room.notifs %}
{% include 'notif.html' with context %} {%- include 'notif.html' with context %}
{% endfor %} {%- endfor %}
{% endif %} {%- endif %}
</table> </table>

View file

@ -1,9 +1,9 @@
{{ room.title }} {{ room.title }}
{% if room.invite %} {%- if room.invite %}
You've been invited, join at {{ room.link }} You've been invited, join at {{ room.link }}
{% else %} {%- else %}
{% for notif in room.notifs %} {%- for notif in room.notifs %}
{% include 'notif.txt' with context %} {%- include 'notif.txt' with context %}
{% endfor %} {%- endfor %}
{% endif %} {%- endif %}

View file

@ -158,8 +158,21 @@ class EmailPusherTests(HomeserverTestCase):
# We should get emailed about those messages # We should get emailed about those messages
self._check_for_mail() self._check_for_mail()
def test_encrypted_message(self):
room = self.helper.create_room_as(self.user_id, tok=self.access_token)
self.helper.invite(
room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
)
self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
# The other user sends some messages
self.helper.send_event(room, "m.room.encrypted", {}, tok=self.others[0].token)
# We should get emailed about that message
self._check_for_mail()
def _check_for_mail(self): def _check_for_mail(self):
"Check that the user receives an email notification" """Check that the user receives an email notification"""
# Get the stream ordering before it gets sent # Get the stream ordering before it gets sent
pushers = self.get_success( pushers = self.get_success(