Change the state verification save to be a 2 step process

The insert or update logic would not work with postgresql. The new
implementation is database agnostic, so long as the response is still
the same on sqllite
This commit is contained in:
David Kowis
2025-06-28 22:38:55 -05:00
parent d6076b8d2f
commit 8637603210
+31 -5
View File
@@ -974,8 +974,10 @@ class CommunityBot(Plugin):
state = await self.get_verification_state(evt.room_id) state = await self.get_verification_state(evt.room_id)
if not state: if not state:
# self.log.debug(f"No verification state stored for {evt.room_id}")
return return
#self.log.debug(f"Checking verification for {evt.sender} in {evt.room_id}")
user_phrase = evt.content.body.strip().lower() user_phrase = evt.content.body.strip().lower()
expected_phrase = state["phrase"].lower() expected_phrase = state["phrase"].lower()
@@ -2144,17 +2146,41 @@ class CommunityBot(Plugin):
async def store_verification_state(self, dm_room_id: str, state: dict) -> None: async def store_verification_state(self, dm_room_id: str, state: dict) -> None:
"""Store verification state in the database.""" """Store verification state in the database."""
await self.database.execute( # First try to update
"""INSERT OR REPLACE INTO verification_states update_query = """UPDATE verification_states
(dm_room_id, user_id, target_room_id, verification_phrase, attempts_remaining, required_power_level) SET verification_phrase = $4, \
VALUES ($1, $2, $3, $4, $5, $6)""", attempts_remaining = $5, \
dm_room_id, required_power_level = $6, \
user_id = $2, \
target_room_id = $3 \
WHERE dm_room_id = $1"""
self.log.debug(f"Attempting update for verification state begin, specifically for {dm_room_id}")
result = await self.database.execute(update_query, dm_room_id,
state["user"], state["user"],
state["target_room"], state["target_room"],
state["phrase"], state["phrase"],
state["attempts"], state["attempts"],
state["required_level"] state["required_level"]
) )
self.log.debug(f"Result is: {result}")
# If no rows were updated, insert a new record
# postgresql response is "UPDATE 0"
# sqllite response is ???
if result == "UPDATE 0": # No rows affected
self.log.debug("No rows updated, so doing insert!")
insert_query = """INSERT INTO verification_states
(dm_room_id, user_id, target_room_id, verification_phrase, attempts_remaining, \
required_power_level)
VALUES ($1, $2, $3, $4, $5, $6)"""
await self.database.execute(insert_query, dm_room_id,
state["user"],
state["target_room"],
state["phrase"],
state["attempts"],
state["required_level"]
)
self.log.debug("Should be done with verification state storage, should've updated or inserted!")
async def get_verification_state(self, dm_room_id: str) -> Optional[dict]: async def get_verification_state(self, dm_room_id: str) -> Optional[dict]:
"""Retrieve verification state from the database.""" """Retrieve verification state from the database."""