From 7389936010367287fd03a00a36ce2efad829ee31 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 18 Jun 2024 20:25:50 +0100 Subject: [PATCH] Improve user in-guild sync process Closes #157 Previously we set all users in_guild to False, and relied on users being set back to in_guild when iterating through guild.members However, this caused two problems 1. For a short window a users in_guild status was incorrect 2. It required an update for all users in_guild to be sent to postgres to update in_guild back to True. This diff changes that, so instead only users who are not found in the guild have in_guild set to False --- .../exts/event_listeners/startup_sync.py | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/metricity/exts/event_listeners/startup_sync.py b/metricity/exts/event_listeners/startup_sync.py index 0f6264f..9452bca 100644 --- a/metricity/exts/event_listeners/startup_sync.py +++ b/metricity/exts/event_listeners/startup_sync.py @@ -35,9 +35,6 @@ async def sync_guild(self) -> None: await _syncer_utils.sync_thread_archive_state(guild) log.info("Beginning user synchronisation process") - async with async_session() as sess: - await sess.execute(update(models.User).values(in_guild=False)) - await sess.commit() users = ( { @@ -89,11 +86,25 @@ async def sync_guild(self) -> None: created += [obj[0] == 0 for obj in objs].count(True) updated += [obj[0] != 0 for obj in objs].count(True) - log.info("User upsert: inserted %d rows, updated %d rows, done %d rows, %d rows remaining", - created, updated, created + updated, total_users - (created + updated)) + log.info( + "User upsert: inserted %d rows, updated %d rows, done %d rows, %d rows remaining", + created, + updated, + created + updated, + total_users - (created + updated), + ) await sess.commit() + log.info("Syncing user in-guild status") + in_guild_user_ids = {str(member.id) for member in guild.members} + async with async_session() as sess: + await sess.execute( + update(models.User) + .where(~models.User.id.in_(in_guild_user_ids)) + .values(in_guild=False), + ) + await sess.commit() log.info("User upsert complete") self.bot.sync_process_complete.set()