From 16b757ae36ec598ba117096c96540c91a7ff2b78 Mon Sep 17 00:00:00 2001 From: Ske Date: Sat, 14 Jul 2018 02:55:23 +0200 Subject: [PATCH] Maintain order of fronters when switching --- bot/pluralkit/commands.py | 6 +++--- bot/pluralkit/db.py | 15 +-------------- bot/pluralkit/utils.py | 6 ++++-- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/bot/pluralkit/commands.py b/bot/pluralkit/commands.py index 89a6da7d..62820eab 100644 --- a/bot/pluralkit/commands.py +++ b/bot/pluralkit/commands.py @@ -426,9 +426,9 @@ async def switch_member(conn, message, args): return False, "Couldn't find member \"{}\".".format(member_name) members.append(member) - member_ids = {member["id"] for member in members} - - fronter_ids = set((await get_fronter_ids(conn, system["id"]))[0]) + # Lists, because order matters, it makes sense to just swap fronters + member_ids = [member["id"] for member in members] + fronter_ids = (await get_fronter_ids(conn, system["id"]))[0] if member_ids == fronter_ids: if len(members) == 1: return False, "{} is already fronting.".format(members[0]["name"]) diff --git a/bot/pluralkit/db.py b/bot/pluralkit/db.py index dce54e0f..e6108e01 100644 --- a/bot/pluralkit/db.py +++ b/bot/pluralkit/db.py @@ -189,20 +189,6 @@ async def delete_message(conn, message_id: str): logger.debug("Deleting message (id={})".format(message_id)) await conn.execute("delete from messages where mid = $1", int(message_id)) -# @db_wrap -# async def front_history(conn, system_id: int, count: int): -# return await conn.fetch("""select -# switches.timestamp, members.name, members.id, switches.id as switch_id -# from -# ( -# select * from switches where system = $1 order by timestamp desc limit $2 -# ) as switches -# left outer join switch_members -# on switch_members.switch = switches.id -# left outer join members -# on switch_members.member = members.id -# order by switches.timestamp desc""", system_id, count) - @db_wrap async def front_history(conn, system_id: int, count: int): return await conn.fetch("""select @@ -210,6 +196,7 @@ async def front_history(conn, system_id: int, count: int): array( select member from switch_members where switch_members.switch = switches.id + order by switch_members.id asc ) as members from switches where switches.system = $1 diff --git a/bot/pluralkit/utils.py b/bot/pluralkit/utils.py index 5e04fe42..6178dfcb 100644 --- a/bot/pluralkit/utils.py +++ b/bot/pluralkit/utils.py @@ -59,8 +59,10 @@ async def get_fronter_ids(conn, system_id): async def get_fronters(conn, system_id): member_ids, timestamp = await get_fronter_ids(conn, system_id) - members = await db.get_members(conn, member_ids) - return members, timestamp + + # Collect in dict and then look up as list, to preserve return order + members = {member["id"]: member for member in await db.get_members(conn, member_ids)} + return [members[member_id] for member_id in member_ids], timestamp async def get_front_history(conn, system_id, count): # Get history from DB