Add option to underline edited part of message in edits. Fixes #61

This commit is contained in:
Tulir Asokan
2018-03-08 19:44:53 +02:00
parent 61d9d6890a
commit b3082da999
4 changed files with 36 additions and 4 deletions
+2
View File
@@ -74,6 +74,8 @@ bridge:
# Show message editing as a reply to the original message.
# If this is false, message edits are not shown at all, as Matrix does not support editing yet.
edits_as_replies: false
# Highlight changed/added parts in edits. Requires lxml.
highlight_edits: false
# Whether or not Matrix bot messages (type m.notice) should be bridged.
bridge_notices: false
# The maximum number of simultaneous Telegram deletions to handle.
+30 -4
View File
@@ -15,7 +15,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from html import escape
try:
from lxml.html.diff import htmldiff
except ImportError:
htmldiff = None
import logging
import re
from telethon_aio.tl.types import *
from mautrix_appservice import MatrixRequestError
@@ -26,6 +31,7 @@ from .util import (add_surrogates, remove_surrogates, trim_reply_fallback_html,
trim_reply_fallback_text, unicode_to_html)
log = logging.getLogger("mau.fmt.tg")
should_highlight_edits = False
def telegram_reply_to_matrix(evt, source):
@@ -68,6 +74,21 @@ async def _add_forward_header(source, text, html, fwd_from_id):
return text, html
def highlight_edits(new_html, old_html):
# Don't include `Edit:` text in diff.
if old_html.startswith("<u>Edit:</u> "):
old_html = old_html[len("<u>Edit:</u> "):]
# Generate diff with lxml
new_html = htmldiff(old_html, new_html)
# Replace <ins> with <u> since Riot doesn't allow <ins>
new_html = new_html.replace("<ins>", "<u>").replace("</ins>", "</u>")
# Remove <del>s since we just want to hide deletions.
new_html = re.sub("<del>.+?</del>", "", new_html)
return new_html
async def _add_reply_header(source, text, html, evt, relates_to, main_intent, is_edit):
space = (evt.to_id.channel_id
if isinstance(evt, Message) and isinstance(evt.to_id, PeerChannel)
@@ -81,9 +102,6 @@ async def _add_reply_header(source, text, html, evt, relates_to, main_intent, is
"event_id": msg.mxid,
"room_id": msg.mx_room,
}
if is_edit:
html = f"<u>Edit:</u> {html or escape(text)}"
text = f"Edit: {text}"
try:
event = await main_intent.get_event(msg.mx_room, msg.mxid)
@@ -99,12 +117,19 @@ async def _add_reply_header(source, text, html, evt, relates_to, main_intent, is
puppet = pu.Puppet.get_by_mxid(r_sender, create=False)
r_displayname = puppet.displayname if puppet else r_sender
r_sender_link = f"<a href='https://matrix.to/#/{r_sender}'>{r_displayname}</a>"
if is_edit and should_highlight_edits:
html = highlight_edits(html or escape(text), r_html_body)
except (ValueError, KeyError, MatrixRequestError):
r_sender_link = "unknown user"
# r_sender = "unknown user"
r_text_body = "Failed to fetch message"
r_html_body = "<em>Failed to fetch message</em>"
if is_edit:
html = f"<u>Edit:</u> {html or escape(text)}"
text = f"Edit: {text}"
r_keyword = "In reply to" if not is_edit else "Edit to"
r_msg_link = f"<a href='https://matrix.to/#/{msg.mx_room}/{msg.mxid}'>{r_keyword}</a>"
html = (f"<blockquote data-mx-reply>{r_msg_link} {r_sender_link} {r_html_body}</blockquote>"
@@ -257,4 +282,5 @@ def _parse_url(html, entity_text, url):
def init_tg(context):
pass
global should_highlight_edits
should_highlight_edits = htmldiff and context.config["bridge.highlight_edits"]
+1
View File
@@ -0,0 +1 @@
lxml
+3
View File
@@ -31,6 +31,9 @@ setuptools.setup(
dependency_links=[
"https://github.com/tulir/telethon-asyncio/tarball/9b389cfb4b6d3876e9661c23507f17e96897e4b0#egg=telethon-aio-git-0.18.0+1"
],
extras_require={
"highlight_edits": ["lxml>=4.1.1,<5"],
},
classifiers=[
"Development Status :: 4 Beta",