patch 515b30d8c49e9ec8619b0c0e3249d093e88a2a74
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sun Apr 16 01:13:07 CEST 2023
* telegram: Add support for showing editions of messages, including highlight of differences
hunk ./irc.py 412
- text = '[{}] {}'.format(self.tg.mid.num_to_id_offset(tg_msg.id), message)
- self.tg.add_to_cache(tg_msg.id, None, text, user, chan)
+ mid = self.tg.mid.num_to_id_offset(tg_msg.id)
+ text = '[{}] {}'.format(mid, message)
+ self.tg.add_to_cache(tg_msg.id, mid, text, message, user, chan)
hunk ./telegram.py 24
-from utils import sanitize_filename, is_url_equiv, extract_url, get_human_size, get_human_duration
+from utils import sanitize_filename, is_url_equiv, extract_url, get_human_size, get_human_duration, get_highlighted
hunk ./telegram.py 90
+ (self.handle_telegram_edited, telethon.events.MessageEdited),
hunk ./telegram.py 314
- def add_to_cache(self, id, mid, message, user, chan):
+ def add_to_cache(self, id, mid, message, proc_message, user, chan):
hunk ./telegram.py 319
- 'rendered_text': message,
+ 'text': message,
+ 'rendered_text': proc_message,
hunk ./telegram.py 325
+ async def handle_telegram_edited(self, event):
+ self.logger.debug('Handling Telegram Message Edited: %s', event)
+
+ id = event.message.id
+ user = self.get_irc_user_from_telegram(event.sender_id)
+ mid = self.mid.num_to_id_offset(id)
+ fmid = '[{}]'.format(mid)
+ message = event.message.message
+ message_rendered = await self.render_text(event, mid, upd_to_webpend=None)
+
+ if id in self.cache:
+ t = self.cache[id]['text']
+ rt = self.cache[id]['rendered_text']
+
+ ht, is_ht = get_highlighted(t, message)
+
+ self.cache[id]['text'] = message
+ self.cache[id]['rendered_text'] = message_rendered
+ else:
+ rt = fmid
+ is_ht = False
+
+ if is_ht:
+ text_edited = ht
+ text_old = fmid
+ else:
+ text_edited = message
+ text_old = rt
+ if user is None:
+ self.refwd_me = True
+
+ text = '|Edited {}| {}'.format(text_old, text_edited)
+ chan = await self.relay_telegram_message(event, user, text)
+
+ if id not in self.cache:
+ self.add_to_cache(id, mid, message, message_rendered, user, chan)
+
hunk ./telegram.py 391
+ message = await self.render_text(event, mid, upd_to_webpend)
+
+ chan = await self.relay_telegram_message(event, user, message)
+
+ self.add_to_cache(event.message.id, mid, event.message.message, message, user, chan)
+
+ self.refwd_me = False
+
+ async def render_text(self, event, mid, upd_to_webpend):
hunk ./telegram.py 415
-
- chan = await self.relay_telegram_message(event, user, message)
-
- self.add_to_cache(event.message.id, mid, message, user, chan)
-
- self.refwd_me = False
+ return message
hunk ./utils.py 14
+import difflib
hunk ./utils.py 125
+
+def get_highlighted(a, b):
+ awl = len(a.split())
+ bwl = len(b.split())
+ delta_size = abs(awl - bwl)
+ highlighted = True
+
+ if not a:
+ res = '> {}'.format(b)
+ elif delta_size > 5:
+ res = b
+ highlighted = False
+ else:
+ al = a.split(' ')
+ bl = b.split(' ')
+ diff = difflib.ndiff(al, bl)
+ ld = list(diff)
+ res = ''
+ d = ''
+ eq = 0
+
+ for i in ld:
+ if i == '- ' or i[0] == '?':
+ continue
+ elif i == ' ' or i == '+ ':
+ res += ' '
+ continue
+ elif i[0] == '-':
+ res += '.{}. '.format(i[2:])
+ elif i[0] == '+':
+ res += '_{}_ '.format(i[2:])
+ else:
+ res += '{} '.format(i[2:])
+ eq += 1
+
+ delta_eq = bwl - eq
+ if delta_eq > 3:
+ res = b
+ highlighted = False
+
+ return res, highlighted