Skip to content

Commit 2e621b2

Browse files
committed
fix(groups): group updates by chat_id to avoid duplicate requests and notifications
1 parent 7cc450a commit 2e621b2

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

handlers/groups.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,31 +155,43 @@ async def update_group_url(context: ContextTypes.DEFAULT_TYPE, chat_id: str) ->
155155
async def _update_groups(context: ContextTypes.DEFAULT_TYPE):
156156
logger.info("Starting update_groups job")
157157
with get_session() as session:
158-
chats = [(c.id, c.chat_id, c.name) for c in session.query(Listable).filter_by(validated=True).filter(Listable.chat_id != None, Listable.chat_id != '').all()]
159-
logger.info(f"Found {len(chats)} groups to update")
160-
161-
for chat_db_id, chat_chat_id, chat_name in chats:
158+
all_chats = session.query(Listable).filter_by(validated=True).filter(Listable.chat_id != None, Listable.chat_id != '').all()
159+
160+
chats_by_id = {}
161+
for c in all_chats:
162+
if c.chat_id not in chats_by_id:
163+
chats_by_id[c.chat_id] = []
164+
chats_by_id[c.chat_id].append((c.id, c.name))
165+
166+
logger.info(f"Found {len(chats_by_id)} unique groups to update")
167+
168+
for chat_chat_id, db_entries in chats_by_id.items():
162169
await asyncio.sleep(1)
163170
chat_id, url, validated = await update_group_url(context, chat_chat_id)
171+
172+
primary_name = db_entries[0][1]
173+
164174
if validated is False:
165-
logger.warning(f"Failed to update URL for group '{chat_name}'. De-validating.")
175+
logger.warning(f"Failed to update URL for group '{primary_name}'. De-validating.")
166176
with get_session() as session:
167-
c = session.query(Listable).filter_by(id=chat_db_id).first()
168-
if c:
169-
c.validated = False
177+
for db_id, _ in db_entries:
178+
c = session.query(Listable).filter_by(id=db_id).first()
179+
if c:
180+
c.validated = False
170181
try:
171-
await context.bot.send_message(chat_id=DC_GROUP_CHATID, text=f"El grupo {chat_name} murió 💀")
182+
await context.bot.send_message(chat_id=DC_GROUP_CHATID, text=f"El grupo {primary_name} murió 💀")
172183
except Exception as e:
173-
logger.error(f"Failed to send death message for {chat_name}: {e}")
184+
logger.error(f"Failed to send death message for {primary_name}: {e}")
174185
elif validated is True:
175-
logger.info(f"Updating URL for group '{chat_name}'")
186+
logger.info(f"Updating URL for group '{primary_name}'")
176187
with get_session() as session:
177-
c = session.query(Listable).filter_by(id=chat_db_id).first()
178-
if c:
179-
c.url = url
180-
if str(c.chat_id) != str(chat_id):
181-
logger.info(f"Updating chat_id for group '{chat_name}' from {c.chat_id} to {chat_id}")
182-
c.chat_id = str(chat_id)
188+
for db_id, _ in db_entries:
189+
c = session.query(Listable).filter_by(id=db_id).first()
190+
if c:
191+
c.url = url
192+
if str(c.chat_id) != str(chat_id):
193+
logger.info(f"Updating chat_id for group '{primary_name}' from {c.chat_id} to {chat_id}")
194+
c.chat_id = str(chat_id)
183195
logger.info("Finished update_groups job")
184196

185197
from handlers.admin import admin_ids

0 commit comments

Comments
 (0)