Make html messages work

This commit is contained in:
David Baker 2016-04-27 17:18:51 +01:00
parent 8781083960
commit 4ed1e45869

View File

@ -59,7 +59,8 @@ ALLOWED_ATTRS = {
# would make sense if we did # would make sense if we did
"img": ["src"], "img": ["src"],
} }
ALLOWED_SCHEMES = ["http", "https", "ftp", "mailto"] # When bleach release a version with this option, we can specify schemes
#ALLOWED_SCHEMES = ["http", "https", "ftp", "mailto"]
class Mailer(object): class Mailer(object):
@ -184,7 +185,15 @@ class Mailer(object):
defer.returnValue(ret) defer.returnValue(ret)
def get_message_vars(self, notif, event, room_state): def get_message_vars(self, notif, event, room_state):
msgtype = event.content["msgtype"] if event.type != "m.room.message":
return None
if event.content["msgtype"] != "m.text":
return None
if "format" in event.content:
msgformat = event.content["format"]
else:
msgformat = None
sender_state_event = room_state[("m.room.member", event.sender)] sender_state_event = room_state[("m.room.member", event.sender)]
sender_name = name_from_member_event(sender_state_event) sender_name = name_from_member_event(sender_state_event)
@ -195,7 +204,7 @@ class Mailer(object):
sender_hash = string_ordinal_total(event.sender) sender_hash = string_ordinal_total(event.sender)
ret = { ret = {
"msgtype": msgtype, "format": msgformat,
"is_historical": event.event_id != notif['event_id'], "is_historical": event.event_id != notif['event_id'],
"ts": event.origin_server_ts, "ts": event.origin_server_ts,
"sender_name": sender_name, "sender_name": sender_name,
@ -203,10 +212,10 @@ class Mailer(object):
"sender_hash": sender_hash, "sender_hash": sender_hash,
} }
if msgtype == "m.text": if msgformat == "org.matrix.custom.html":
ret["body_text_plain"] = event.content["body"]
elif msgtype == "org.matrix.custom.html":
ret["body_text_html"] = safe_markup(event.content["formatted_body"]) ret["body_text_html"] = safe_markup(event.content["formatted_body"])
else:
ret["body_text_plain"] = event.content["body"]
return ret return ret
@ -263,14 +272,14 @@ class Mailer(object):
def make_unsubscribe_link(self): def make_unsubscribe_link(self):
return "https://vector.im/#/settings" # XXX: matrix.to return "https://vector.im/#/settings" # XXX: matrix.to
def mxc_to_http_filter(self, value, width, height, resizeMethod="crop"): def mxc_to_http_filter(self, value, width, height, resize_method="crop"):
if value[0:6] != "mxc://": if value[0:6] != "mxc://":
return "" return ""
serverAndMediaId = value[6:] serverAndMediaId = value[6:]
params = { params = {
"width": width, "width": width,
"height": height, "height": height,
"method": resizeMethod, "method": resize_method,
} }
return "%s_matrix/media/v1/thumbnail/%s?%s" % ( return "%s_matrix/media/v1/thumbnail/%s?%s" % (
self.hs.config.public_baseurl, self.hs.config.public_baseurl,
@ -279,10 +288,12 @@ class Mailer(object):
) )
def safe_markup(self, raw_html): def safe_markup(raw_html):
return jinja2.Markup(bleach.linkify(bleach.clean( return jinja2.Markup(bleach.linkify(bleach.clean(
raw_html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRS, raw_html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRS,
protocols=ALLOWED_SCHEMES, strip=True # bleach master has this, but it isn't released yet
# protocols=ALLOWED_SCHEMES,
strip=True
))) )))