Fix quoting formatting and change Matrix->Telegram username formatting
This commit is contained in:
parent
15e84f6861
commit
d537cfb1b3
@ -81,26 +81,25 @@ def sanitize_html(string):
|
|||||||
soup = BeautifulSoup(string, 'html.parser')
|
soup = BeautifulSoup(string, 'html.parser')
|
||||||
for tag in soup.find_all(True):
|
for tag in soup.find_all(True):
|
||||||
if tag.name == 'blockquote':
|
if tag.name == 'blockquote':
|
||||||
tag.string = ('\n' + tag.text).replace('\n', '\n> ').rstrip('\n>')
|
tag.string = ('\n' + tag.text).replace('\n', '\n> ')[3:-3]
|
||||||
if tag.name not in VALID_TAGS:
|
if tag.name not in VALID_TAGS:
|
||||||
tag.hidden = True
|
tag.hidden = True
|
||||||
return soup.renderContents().decode('utf-8')
|
return soup.renderContents().decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
def format_matrix_msg(form, username, content):
|
def format_matrix_msg(form, content):
|
||||||
"""
|
"""
|
||||||
Formats a matrix message for sending to Telegram
|
Formats a matrix message for sending to Telegram
|
||||||
:param form: The format string of the message, where the first parameter
|
:param form: The format string of the message, where the first parameter
|
||||||
is the username and the second one the message.
|
is the username and the second one the message.
|
||||||
:param username: The username of the user.
|
|
||||||
:param content: The content to be sent.
|
:param content: The content to be sent.
|
||||||
:return: The formatted string.
|
:return: The formatted string.
|
||||||
"""
|
"""
|
||||||
if 'format' in content and content['format'] == 'org.matrix.custom.html':
|
if 'format' in content and content['format'] == 'org.matrix.custom.html':
|
||||||
sanitized = sanitize_html(content['formatted_body'])
|
sanitized = sanitize_html(content['formatted_body'])
|
||||||
return html.escape(form).format(username, sanitized), 'HTML'
|
return html.escape(form).format(sanitized), 'HTML'
|
||||||
else:
|
else:
|
||||||
return form.format(username, content['body']), None
|
return form.format(html.escape(content['body'])), None
|
||||||
|
|
||||||
|
|
||||||
async def download_matrix_file(url, filename):
|
async def download_matrix_file(url, filename):
|
||||||
@ -171,7 +170,7 @@ async def matrix_transaction(request):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if event['type'] == 'm.room.aliases':
|
if event['type'] == 'm.room.aliases' and event['state_key'] == MATRIX_HOST_BARE:
|
||||||
aliases = event['content']['aliases']
|
aliases = event['content']['aliases']
|
||||||
|
|
||||||
links = db.session.query(db.ChatLink)\
|
links = db.session.query(db.ChatLink)\
|
||||||
@ -228,14 +227,14 @@ async def matrix_transaction(request):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if content['msgtype'] == 'm.text':
|
if content['msgtype'] == 'm.text':
|
||||||
msg, mode = format_matrix_msg('<{}> {}', displayname, content)
|
msg, mode = format_matrix_msg('{}', content)
|
||||||
response = await group.send_text(msg, parse_mode=mode)
|
response = await group.send_text("<b>{}:</b> {}".format(displayname, msg), parse_mode='HTML')
|
||||||
elif content['msgtype'] == 'm.notice':
|
elif content['msgtype'] == 'm.notice':
|
||||||
msg, mode = format_matrix_msg('[{}] {}', displayname, content)
|
msg, mode = format_matrix_msg('{}', content)
|
||||||
response = await group.send_text(msg, parse_mode=mode)
|
response = await group.send_text("[{}] {}".format(displayname, msg), parse_mode=mode)
|
||||||
elif content['msgtype'] == 'm.emote':
|
elif content['msgtype'] == 'm.emote':
|
||||||
msg, mode = format_matrix_msg('* {} {}', displayname, content)
|
msg, mode = format_matrix_msg('{}', content)
|
||||||
response = await group.send_text(msg, parse_mode=mode)
|
response = await group.send_text("* {} {}".format(displayname, msg), parse_mode=mode)
|
||||||
elif content['msgtype'] == 'm.image':
|
elif content['msgtype'] == 'm.image':
|
||||||
try:
|
try:
|
||||||
url = urlparse(content['url'])
|
url = urlparse(content['url'])
|
||||||
@ -254,8 +253,7 @@ async def matrix_transaction(request):
|
|||||||
.format(url.netloc, quote(url.path))
|
.format(url.netloc, quote(url.path))
|
||||||
url_str = await shorten_url(url_str)
|
url_str = await shorten_url(url_str)
|
||||||
|
|
||||||
caption = '<{}> {} ({})'.format(displayname,
|
caption = '{} sent an image'.format(displayname)
|
||||||
content['body'], url_str)
|
|
||||||
response = await group.send_photo(img_file, caption=caption)
|
response = await group.send_photo(img_file, caption=caption)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user