Periodically update channel voices
patch 24aacd1a117ef092876ee5e0bc9522563dcdfd0e
Author: Peter Bui <pbui@bx612.space>
Date: Fri Jul 19 17:21:26 CEST 2019
* Periodically update channel voices
Rather than checking on individual users, simply check the whole channel
periodically... this is much more reliable than depending on messages
for active users.
hunk ./irtelegramd.py 20
-NICK_MAX_LENGTH = 20
-USER_STATUS_DELAY = 300
+NICK_MAX_LENGTH = 20
+UPDATE_CHANNEL_VOICES_DELAY = 300
hunk ./irtelegramd.py 105
- self.irc_online = set()
+ self.irc_voices = collections.defaultdict(set)
hunk ./irtelegramd.py 110
- def schedule_irc_mode_update(self, nick, channel, force=False):
- if nick not in self.irc_online or force:
- self.ioloop.call_later(USER_STATUS_DELAY, self.update_irc_mode, nick, channel)
- self.irc_online.add(nick)
-
hunk ./irtelegramd.py 185
- for user_nick in voices:
- await self.send_irc_command(':{} MODE {} +v {}'.format(
- self.hostname, channel, user_nick,
- ))
-
- self.schedule_irc_mode_update(user_nick, channel)
+ await self.update_channel_voices(channel, voices)
hunk ./irtelegramd.py 193
- async def update_irc_mode(self, nick, channel):
- tid = self.iid_to_tid[nick]
- user = await self.telegram_client.get_entity(tid)
- if isinstance(user.status, telethon.types.UserStatusOnline):
- self.schedule_irc_mode_update(nick, channel, True)
- else:
- self.irc_online.remove(nick)
+ async def update_channel_voices(self, channel, voices=None):
+ # Get voices for channel if not provided
+ if not voices:
+ tid = self.iid_to_tid[channel]
+ _, voices = await self.get_telegram_channel_participants(tid)
+
+ # Add new voices
+ for nick in voices:
+ if nick not in self.irc_voices[channel]:
+ self.irc_voices[channel].add(nick)
+ await self.send_irc_command(':{} MODE {} +v {}'.format(
+ self.hostname, channel, nick,
+ ))
+
+ # Remove old voices
+ for nick in self.irc_voices[channel].difference(voices):
+ self.irc_voices[channel].remove(nick)
hunk ./irtelegramd.py 214
+ self.ioloop.call_later(UPDATE_CHANNEL_VOICES_DELAY, self.update_channel_voices, channel)
+
hunk ./irtelegramd.py 284
- voices = []
+ voices = set()
hunk ./irtelegramd.py 294
- voices.append(user_nick)
+ voices.add(user_nick)
hunk ./irtelegramd.py 347
- # Give voice and schedule IRC mode update
- if nick not in self.irc_online:
- await self.send_irc_command(':{} MODE {} +v {}'.format(
- self.hostname, channel, nick,
- ))
- self.schedule_irc_mode_update(nick, channel)
-
hunk ./irtelegramd.py 362
- await get_telegram_channel_participants(tid)
+ await self.get_telegram_channel_participants(tid)