Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

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

Merged
merged 6 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions changelog.d/8545.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a long standing bug where email notifications for encrypted messages were blank.
16 changes: 11 additions & 5 deletions synapse/push/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ async def get_notif_vars(self, notif, user_id, notif_event, room_state_ids):
return ret

async def get_message_vars(self, notif, event, room_state_ids):
if event.type != EventTypes.Message:
return
if event.type != EventTypes.Message and event.type != EventTypes.Encrypted:
return None

sender_state_event_id = room_state_ids[("m.room.member", event.sender)]
sender_state_event = await self.store.get_event(sender_state_event_id)
Expand All @@ -399,10 +399,8 @@ async def get_message_vars(self, notif, event, room_state_ids):
# sender_hash % the number of default images to choose from
sender_hash = string_ordinal_total(event.sender)

msgtype = event.content.get("msgtype")

ret = {
"msgtype": msgtype,
"event_type": event.type,
"is_historical": event.event_id != notif["event_id"],
"id": event.event_id,
"ts": event.origin_server_ts,
Expand All @@ -411,6 +409,14 @@ async def get_message_vars(self, notif, event, room_state_ids):
"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":
self.add_text_message_vars(ret, event)
elif msgtype == "m.image":
Expand Down
56 changes: 31 additions & 25 deletions synapse/res/templates/notif.html
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
{% for message in notif.messages %}
{%- for message in notif.messages %}
clokep marked this conversation as resolved.
Show resolved Hide resolved
<tr class="{{ "historical_message" if message.is_historical else "message" }}">
<td class="sender_avatar">
{% if loop.index0 == 0 or notif.messages[loop.index0 - 1].sender_name != notif.messages[loop.index0].sender_name %}
{% if message.sender_avatar_url %}
{%- if loop.index0 == 0 or notif.messages[loop.index0 - 1].sender_name != notif.messages[loop.index0].sender_name %}
{%- if message.sender_avatar_url %}
<img alt="" class="sender_avatar" src="{{ message.sender_avatar_url|mxc_to_http(32,32) }}" />
{% else %}
{% if message.sender_hash % 3 == 0 %}
{%- else %}
{%- if message.sender_hash % 3 == 0 %}
<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" />
{% else %}
{%- else %}
<img class="sender_avatar" src="https://riot.im/img/external/avatar-3.png" />
{% endif %}
{% endif %}
{% endif %}
{%- endif %}
{%- endif %}
{%- endif %}
</td>
<td class="message_contents">
{% 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>
{% endif %}
{%- 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>
{%- endif %}
<div class="message_body">
{% if message.msgtype == "m.text" %}
{{ message.body_text_html }}
{% elif message.msgtype == "m.emote" %}
{{ message.body_text_html }}
{% elif message.msgtype == "m.notice" %}
{{ message.body_text_html }}
{% elif message.msgtype == "m.image" %}
<img src="{{ message.image_url|mxc_to_http(640, 480, scale) }}" />
{% elif message.msgtype == "m.file" %}
<span class="filename">{{ message.body_text_plain }}</span>
{% endif %}
{%- if message.event_type == "m.room.encrypted" %}
An encrypted message.
{%- elif message.event_type == "m.room.message" %}
{%- if message.msgtype == "m.text" %}
{{ message.body_text_html }}
{%- elif message.msgtype == "m.emote" %}
{{ message.body_text_html }}
{%- elif message.msgtype == "m.notice" %}
{{ message.body_text_html }}
{%- elif message.msgtype == "m.image" %}
<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>
</td>
<td class="message_time">{{ message.ts|format_ts("%H:%M") }}</td>
</tr>
{% endfor %}
{%- endfor %}
<tr class="notif_link">
<td></td>
<td>
Expand Down
24 changes: 15 additions & 9 deletions synapse/res/templates/notif.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
{% for message in notif.messages %}
{% if message.msgtype == "m.emote" %}* {% endif %}{{ message.sender_name }} ({{ message.ts|format_ts("%H:%M") }})
{% if message.msgtype == "m.text" %}
{%- for message in notif.messages %}
{%- if message.event_type == "m.room.encrypted" %}
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 }}
{% elif message.msgtype == "m.emote" %}
{%- elif message.msgtype == "m.emote" %}
{{ message.body_text_plain }}
{% elif message.msgtype == "m.notice" %}
{%- elif message.msgtype == "m.notice" %}
{{ message.body_text_plain }}
{% elif message.msgtype == "m.image" %}
{%- elif message.msgtype == "m.image" %}
{{ message.body_text_plain }}
{% elif message.msgtype == "m.file" %}
{%- elif message.msgtype == "m.file" %}
{{ message.body_text_plain }}
{% endif %}
{% endfor %}
{%- else %}
A message with unrecognised content.
{%- endif %}
{%- endif %}
{%- endfor %}

View {{ room.title }} at {{ notif.link }}
26 changes: 13 additions & 13 deletions synapse/res/templates/notif_mail.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<html lang="en">
<head>
<style type="text/css">
{% include 'mail.css' without context %}
{% include "mail-%s.css" % app_name ignore missing without context %}
{%- include 'mail.css' without context %}
{%- include "mail-%s.css" % app_name ignore missing without context %}
</style>
</head>
<body>
Expand All @@ -18,21 +18,21 @@
<div class="summarytext">{{ summary_text }}</div>
</td>
<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]"/>
{% elif app_name == "Vector" %}
{%- elif app_name == "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]"/>
{% else %}
{%- else %}
<img src="http://matrix.org/img/matrix-120x51.png" width="120" height="51" alt="[matrix]"/>
{% endif %}
{%- endif %}
</td>
</tr>
</table>
{% for room in rooms %}
{% include 'room.html' with context %}
{% endfor %}
{%- for room in rooms %}
{%- include 'room.html' with context %}
{%- endfor %}
<div class="footer">
<a href="{{ unsubscribe_link }}">Unsubscribe</a>
<br/>
Expand All @@ -41,12 +41,12 @@
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") }}
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") }},
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.
{% endif %}
{%- endif %}
</div>
</div>
</td>
Expand Down
6 changes: 3 additions & 3 deletions synapse/res/templates/notif_mail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ Hi {{ user_display_name }},

{{ summary_text }}

{% for room in rooms %}
{% include 'room.txt' with context %}
{% endfor %}
{%- for room in rooms %}
{%- include 'room.txt' with context %}
{%- endfor %}

You can disable these notifications at {{ unsubscribe_link }}

26 changes: 13 additions & 13 deletions synapse/res/templates/room.html
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<table class="room">
<tr class="room_header">
<td class="room_avatar">
{% if room.avatar_url %}
{%- if room.avatar_url %}
<img alt="" src="{{ room.avatar_url|mxc_to_http(48,48) }}" />
{% else %}
{% if room.hash % 3 == 0 %}
{%- else %}
{%- if room.hash % 3 == 0 %}
<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" />
{% else %}
{%- else %}
<img alt="" src="https://riot.im/img/external/avatar-3.png" />
{% endif %}
{% endif %}
{%- endif %}
{%- endif %}
</td>
<td class="room_name" colspan="2">
{{ room.title }}
</td>
</tr>
{% if room.invite %}
{%- if room.invite %}
<tr>
<td></td>
<td>
<a href="{{ room.link }}">Join the conversation.</a>
</td>
<td></td>
</tr>
{% else %}
{% for notif in room.notifs %}
{% include 'notif.html' with context %}
{% endfor %}
{% endif %}
{%- else %}
{%- for notif in room.notifs %}
{%- include 'notif.html' with context %}
{%- endfor %}
{%- endif %}
</table>
12 changes: 6 additions & 6 deletions synapse/res/templates/room.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{ room.title }}

{% if room.invite %}
{%- if room.invite %}
You've been invited, join at {{ room.link }}
{% else %}
{% for notif in room.notifs %}
{% include 'notif.txt' with context %}
{% endfor %}
{% endif %}
{%- else %}
{%- for notif in room.notifs %}
{%- include 'notif.txt' with context %}
{%- endfor %}
{%- endif %}
15 changes: 14 additions & 1 deletion tests/push/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,21 @@ def test_multiple_members_email(self):
# We should get emailed about those messages
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):
"Check that the user receives an email notification"
"""Check that the user receives an email notification"""

# Get the stream ordering before it gets sent
pushers = self.get_success(
Expand Down