fix: Add lock to prevent update race.

This commit is contained in:
2025-11-01 11:49:27 +10:00
parent a14cc4056e
commit 1bd2e9607a

View File

@@ -1,3 +1,4 @@
import asyncio
from string import punctuation from string import punctuation
import datetime as dt import datetime as dt
from datetime import datetime, timedelta from datetime import datetime, timedelta
@@ -40,6 +41,7 @@ class FocusComponent(cmds.Component):
register_channel(self.channel.name, self.channel) register_channel(self.channel.name, self.channel)
self._last_deleted: dict[int, datetime] = {} self._last_deleted: dict[int, datetime] = {}
self.hyperfocus_lock = asyncio.Lock()
# ----- API ----- # ----- API -----
async def component_load(self): async def component_load(self):
@@ -88,6 +90,10 @@ class FocusComponent(cmds.Component):
@cmds.Component.listener() @cmds.Component.listener()
async def event_message(self, payload: twitchio.ChatMessage): async def event_message(self, payload: twitchio.ChatMessage):
async with self.hyperfocus_lock:
await self.handle_message(payload)
async def handle_message(self, payload: twitchio.ChatMessage):
# Check if chatter is currently hyperfocused # Check if chatter is currently hyperfocused
profile = await self.bot.profiles.fetch_profile(payload.chatter, touch=True) profile = await self.bot.profiles.fetch_profile(payload.chatter, touch=True)
hyperfocused = await self.get_hyperfocus(profile.profileid) hyperfocused = await self.get_hyperfocus(profile.profileid)
@@ -173,6 +179,7 @@ class FocusComponent(cmds.Component):
pid = profile.profileid pid = profile.profileid
comm = await self.bot.profiles.fetch_community(ctx.broadcaster, touch=True) comm = await self.bot.profiles.fetch_community(ctx.broadcaster, touch=True)
async with self.hyperfocus_lock:
await Hyperfocuser.table.delete_where(profileid=pid) await Hyperfocuser.table.delete_where(profileid=pid)
focuser = await Hyperfocuser.create( focuser = await Hyperfocuser.create(
profileid=pid, profileid=pid,
@@ -193,6 +200,7 @@ class FocusComponent(cmds.Component):
@cmds.command(name="unfocus") @cmds.command(name="unfocus")
async def unfocus_cmd(self, ctx): async def unfocus_cmd(self, ctx):
profile = await self.bot.profiles.fetch_profile(ctx.chatter, touch=True) profile = await self.bot.profiles.fetch_profile(ctx.chatter, touch=True)
async with self.hyperfocus_lock:
row = await Hyperfocuser.fetch(profile.profileid) row = await Hyperfocuser.fetch(profile.profileid)
if row: if row:
await row.delete() await row.delete()
@@ -209,6 +217,7 @@ class FocusComponent(cmds.Component):
profile = await self.bot.profiles.fetch_profile(user, touch=False) profile = await self.bot.profiles.fetch_profile(user, touch=False)
async with self.hyperfocus_lock:
if hyper := (await self.get_hyperfocus(profile.profileid)): if hyper := (await self.get_hyperfocus(profile.profileid)):
durstr = strfdelta(hyper.ends_at - utc_now()) durstr = strfdelta(hyper.ends_at - utc_now())
await ctx.reply( await ctx.reply(
@@ -228,3 +237,10 @@ class FocusComponent(cmds.Component):
await ctx.reply( await ctx.reply(
"Add HYPERFOCUS to your channel by authorising me here: https://croccyfocus.thewisewolf.dev/invite" "Add HYPERFOCUS to your channel by authorising me here: https://croccyfocus.thewisewolf.dev/invite"
) )
@cmds.command(name="focuslist")
@cmds.is_moderator()
async def focuslist_cmd(self, ctx):
comm = await self.bot.profiles.fetch_community(ctx.broadcaster, touch=True)
link = f"https://croccyfocus.thewisewolf.dev/widget/?community={comm}"
await ctx.reply(f"Browser source link for your channel's hyperfocus: {link}")