From e8d1c5bf9017843c82448307183378975cb99d09 Mon Sep 17 00:00:00 2001 From: Ske Date: Tue, 18 Dec 2018 18:30:10 +0100 Subject: [PATCH] Refactor pk;switch move, specify UTC everywhere --- src/pluralkit/bot/commands/switch_commands.py | 26 +++++++++---------- src/pluralkit/bot/commands/system_commands.py | 4 +-- src/pluralkit/bot/embeds.py | 2 +- src/pluralkit/db.py | 2 +- src/pluralkit/switch.py | 3 +++ 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/pluralkit/bot/commands/switch_commands.py b/src/pluralkit/bot/commands/switch_commands.py index ebf15ec3..3896951d 100644 --- a/src/pluralkit/bot/commands/switch_commands.py +++ b/src/pluralkit/bot/commands/switch_commands.py @@ -3,7 +3,6 @@ from typing import List import dateparser -import pluralkit.utils from pluralkit.bot.commands import * from pluralkit.member import Member from pluralkit.utils import display_relative @@ -108,29 +107,31 @@ async def switch_move(ctx: CommandContext): # Make sure it all runs in a big transaction for atomicity async with ctx.conn.transaction(): # Get the last two switches to make sure the switch to move isn't before the second-last switch - last_two_switches = await pluralkit.utils.get_front_history(ctx.conn, system.id, count=2) + last_two_switches = await system.get_switches(ctx.conn, 2) if len(last_two_switches) == 0: raise CommandError("There are no registered switches for this system.") - last_timestamp, last_fronters = last_two_switches[0] + last_switch = last_two_switches[0] if len(last_two_switches) > 1: - second_last_timestamp, _ = last_two_switches[1] + second_last_switch = last_two_switches[1] - if new_time < second_last_timestamp: - time_str = display_relative(second_last_timestamp) + if new_time < second_last_switch.timestamp: + time_str = display_relative(second_last_switch.timestamp) raise CommandError( "Can't move switch to before last switch time ({} ago), as it would cause conflicts.".format(time_str)) # Display the confirmation message w/ humanized times + last_fronters = await last_switch.fetch_members(ctx.conn) + members = ", ".join([member.name for member in last_fronters]) or "nobody" - last_absolute = last_timestamp.isoformat(sep=" ", timespec="seconds") - last_relative = display_relative(last_timestamp) + last_absolute = last_switch.timestamp.isoformat(sep=" ", timespec="seconds") + last_relative = display_relative(last_switch.timestamp) new_absolute = new_time.isoformat(sep=" ", timespec="seconds") new_relative = display_relative(new_time) # Confirm with user switch_confirm_message = await ctx.reply( - "This will move the latest switch ({}) from {} ({} ago) to {} ({} ago). Is this OK?".format(members, + "This will move the latest switch ({}) from {} UTC ({} ago) to {} UTC ({} ago). Is this OK?".format(members, last_absolute, last_relative, new_absolute, @@ -139,9 +140,6 @@ async def switch_move(ctx: CommandContext): if not await ctx.confirm_react(ctx.message.author, switch_confirm_message): raise CommandError("Switch move cancelled.") - # DB requires the actual switch ID which our utility method above doesn't return, do this manually - switch_id = (await db.front_history(ctx.conn, system.id, count=1))[0]["id"] - - # Change the switch in the DB - await db.move_last_switch(ctx.conn, system.id, switch_id, new_time) + # Actually move the switch + await last_switch.move(ctx.conn, new_time) await ctx.reply_ok("Switch moved.") diff --git a/src/pluralkit/bot/commands/system_commands.py b/src/pluralkit/bot/commands/system_commands.py index a45fcfdb..2c56176f 100644 --- a/src/pluralkit/bot/commands/system_commands.py +++ b/src/pluralkit/bot/commands/system_commands.py @@ -203,7 +203,7 @@ async def system_fronthistory(ctx: CommandContext, system: System): if i > 0: last_switch_time = front_history[i - 1][0] delta_text = ", for {}".format(display_relative(timestamp - last_switch_time)) - lines.append("**{}** ({}, {} ago{})".format(name, time_text, rel_text, delta_text)) + lines.append("**{}** ({} UTC, {} ago{})".format(name, time_text, rel_text, delta_text)) embed = embeds.status("\n".join(lines) or "(none)") embed.title = "Past switches" @@ -302,6 +302,6 @@ async def system_frontpercent(ctx: CommandContext, system: System): embed.add_field(name=member.name if member else "(no fronter)", value="{}% ({})".format(percent, humanize.naturaldelta(front_time))) - embed.set_footer(text="Since {} ({} ago)".format(span_start.isoformat(sep=" ", timespec="seconds"), + embed.set_footer(text="Since {} UTC ({} ago)".format(span_start.isoformat(sep=" ", timespec="seconds"), display_relative(span_start))) await ctx.reply(embed=embed) diff --git a/src/pluralkit/bot/embeds.py b/src/pluralkit/bot/embeds.py index 152daf42..2f9efa05 100644 --- a/src/pluralkit/bot/embeds.py +++ b/src/pluralkit/bot/embeds.py @@ -186,7 +186,7 @@ async def front_status(switch: Switch, conn) -> discord.Embed: if switch.timestamp: embed.add_field(name="Since", - value="{} ({})".format(switch.timestamp.isoformat(sep=" ", timespec="seconds"), + value="{} UTC ({})".format(switch.timestamp.isoformat(sep=" ", timespec="seconds"), display_relative(switch.timestamp))) else: embed = error("No switches logged.") diff --git a/src/pluralkit/db.py b/src/pluralkit/db.py index e421afe5..dd500e29 100644 --- a/src/pluralkit/db.py +++ b/src/pluralkit/db.py @@ -285,7 +285,7 @@ async def add_switch(conn, system_id: int): return res["id"] @db_wrap -async def move_last_switch(conn, system_id: int, switch_id: int, new_time: datetime): +async def move_switch(conn, system_id: int, switch_id: int, new_time: datetime): logger.debug("Moving latest switch (system={}, id={}, new_time={})".format(system_id, switch_id, new_time)) await conn.execute("update switches set timestamp = $1 where system = $2 and id = $3", new_time, system_id, switch_id) diff --git a/src/pluralkit/switch.py b/src/pluralkit/switch.py index d2c86757..86d6b539 100644 --- a/src/pluralkit/switch.py +++ b/src/pluralkit/switch.py @@ -18,6 +18,9 @@ class Switch(namedtuple("Switch", ["id", "system", "timestamp", "members"])): async def delete(self, conn): await db.delete_switch(conn, self.id) + async def move(self, conn, new_timestamp): + await db.move_switch(conn, self.system, self.id, new_timestamp) + async def to_json(self, conn): return { "timestamp": self.timestamp.isoformat(),