telegram: Add a cache of "volatile" events (delete, edit, react) to be shown in history
patch a39c65dc932ee95b44b5a759cad3e413177fc5aa
Author: E. Bosch <presidev@AT@gmail.com>
Date: Fri Aug 30 21:53:13 CEST 2024
* telegram: Add a cache of "volatile" events (delete, edit, react) to be shown in history
hunk ./telegram.py 71
+ self.volatile_cache = collections.OrderedDict()
+ self.prev_id = {}
hunk ./telegram.py 413
- if len(self.cache) >= 10000:
- self.cache.popitem(last=False)
+ self.limit_cache(self.cache)
hunk ./telegram.py 423
+ def to_volatile_cache(self, prev_id, id, ev, user, chan, date):
+ if chan in prev_id:
+ prid = prev_id[chan] if chan else prev_id[user]
+ self.limit_cache(self.volatile_cache)
+ elem = {
+ 'id': id,
+ 'rendered_event': ev,
+ 'user': user,
+ 'channel': chan,
+ 'date': date,
+ }
+ if prid not in self.volatile_cache:
+ self.volatile_cache[prid] = [elem]
+ else:
+ self.volatile_cache[prid].append(elem)
+
+ def limit_cache(self, cache):
+ if len(cache) >= 10000:
+ cache.popitem(last=False)
+
hunk ./telegram.py 560
+ self.to_volatile_cache(self.prev_id, id, text, user, chan, current_date())
hunk ./telegram.py 572
+ self.to_volatile_cache(self.prev_id, deleted_id, text, user, chan, current_date())
hunk ./telegram.py 595
+ await self.history_search_volatile(history, msg.id)
hunk ./telegram.py 598
+ peer = chan if chan else user
+ self.prev_id[peer] = msg.id
hunk ./telegram.py 638
+ async def history_search_volatile(self, history, id):
+ if history:
+ if id in self.volatile_cache:
+ for item in self.volatile_cache[id]:
+ user = item['user']
+ text = item['rendered_event']
+ chan = item['channel']
+ date = item['date']
+ text_send = self.set_history_timestamp(text, history=True, date=date, action=False)
+ await self.relay_telegram_message(None, user, text_send, chan)
+