Maintain order of fronters when switching

This commit is contained in:
Ske 2018-07-14 02:55:23 +02:00
parent 799ea01f03
commit 16b757ae36
3 changed files with 8 additions and 19 deletions

View File

@ -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"])

View File

@ -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

View File

@ -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