formatting

This commit is contained in:
William Kray
2025-09-09 21:21:51 -07:00
parent 5f42420619
commit a92759c100
14 changed files with 1380 additions and 967 deletions
+46 -34
View File
@@ -8,13 +8,13 @@ from mautrix.types import PaginationDirection
async def get_messages_to_redact(client, room_id: str, mxid: str, logger) -> List:
"""Get messages from a user in a room that should be redacted.
Args:
client: Matrix client instance
room_id: The room ID to search in
mxid: The user ID whose messages to find
logger: Logger instance for error reporting
Returns:
list: List of message events to redact
"""
@@ -40,16 +40,18 @@ async def get_messages_to_redact(client, room_id: str, mxid: str, logger) -> Lis
return []
async def redact_messages(client, database, room_id: str, sleep_time: float, logger) -> Dict[str, int]:
async def redact_messages(
client, database, room_id: str, sleep_time: float, logger
) -> Dict[str, int]:
"""Redact messages queued for redaction in a room.
Args:
client: Matrix client instance
database: Database instance
room_id: The room ID to redact messages in
sleep_time: Sleep time between redactions
logger: Logger instance for error reporting
Returns:
dict: Counters for successful and failed redactions
"""
@@ -59,9 +61,7 @@ async def redact_messages(client, database, room_id: str, sleep_time: float, log
)
for event in events:
try:
await client.redact(
room_id, event["event_id"], reason="content removed"
)
await client.redact(room_id, event["event_id"], reason="content removed")
counters["success"] += 1
await database.execute(
"DELETE FROM redaction_tasks WHERE event_id = $1", event["event_id"]
@@ -81,7 +81,7 @@ async def redact_messages(client, database, room_id: str, sleep_time: float, log
async def upsert_user_timestamp(database, mxid: str, timestamp: int, logger) -> None:
"""Insert or update user activity timestamp.
Args:
database: Database instance
mxid: User Matrix ID
@@ -103,16 +103,17 @@ async def upsert_user_timestamp(database, mxid: str, timestamp: int, logger) ->
logger.error(f"Failed to upsert user timestamp: {e}")
async def get_inactive_users(database, warn_threshold_days: int, kick_threshold_days: int,
logger) -> Dict[str, List[str]]:
async def get_inactive_users(
database, warn_threshold_days: int, kick_threshold_days: int, logger
) -> Dict[str, List[str]]:
"""Get lists of users who should be warned or kicked for inactivity.
Args:
database: Database instance
warn_threshold_days: Days threshold for warning
kick_threshold_days: Days threshold for kicking
logger: Logger instance for error reporting
Returns:
dict: Contains 'warn' and 'kick' lists of user IDs
"""
@@ -120,7 +121,7 @@ async def get_inactive_users(database, warn_threshold_days: int, kick_threshold_
current_time = int(time.time())
warn_threshold = current_time - (warn_threshold_days * 24 * 60 * 60)
kick_threshold = current_time - (kick_threshold_days * 24 * 60 * 60)
# Get users to warn
warn_results = await database.fetch(
"""
@@ -132,7 +133,7 @@ async def get_inactive_users(database, warn_threshold_days: int, kick_threshold_
warn_threshold,
kick_threshold,
)
# Get users to kick
kick_results = await database.fetch(
"""
@@ -142,10 +143,10 @@ async def get_inactive_users(database, warn_threshold_days: int, kick_threshold_
""",
kick_threshold,
)
return {
"warn": [row["mxid"] for row in warn_results],
"kick": [row["mxid"] for row in kick_results]
"kick": [row["mxid"] for row in kick_results],
}
except Exception as e:
logger.error(f"Failed to get inactive users: {e}")
@@ -154,7 +155,7 @@ async def get_inactive_users(database, warn_threshold_days: int, kick_threshold_
async def cleanup_stale_verification_states(database, logger) -> None:
"""Clean up stale verification states older than 24 hours.
Args:
database: Database instance
logger: Logger instance for error reporting
@@ -172,29 +173,34 @@ async def cleanup_stale_verification_states(database, logger) -> None:
async def get_verification_state(database, dm_room_id: str) -> Dict[str, Any]:
"""Get verification state for a DM room.
Args:
database: Database instance
dm_room_id: The DM room ID
Returns:
dict: Verification state data or None if not found
"""
try:
result = await database.fetchrow(
"SELECT * FROM verification_states WHERE dm_room_id = $1",
dm_room_id
"SELECT * FROM verification_states WHERE dm_room_id = $1", dm_room_id
)
return dict(result) if result else None
except Exception as e:
return None
async def create_verification_state(database, dm_room_id: str, user_id: str,
target_room_id: str, verification_phrase: str,
attempts_remaining: int, required_power_level: int) -> None:
async def create_verification_state(
database,
dm_room_id: str,
user_id: str,
target_room_id: str,
verification_phrase: str,
attempts_remaining: int,
required_power_level: int,
) -> None:
"""Create a new verification state.
Args:
database: Database instance
dm_room_id: The DM room ID
@@ -211,16 +217,22 @@ async def create_verification_state(database, dm_room_id: str, user_id: str,
(dm_room_id, user_id, target_room_id, verification_phrase, attempts_remaining, required_power_level)
VALUES ($1, $2, $3, $4, $5, $6)
""",
dm_room_id, user_id, target_room_id, verification_phrase,
attempts_remaining, required_power_level
dm_room_id,
user_id,
target_room_id,
verification_phrase,
attempts_remaining,
required_power_level,
)
except Exception as e:
pass # Verification state creation is not critical
async def update_verification_attempts(database, dm_room_id: str, attempts_remaining: int) -> None:
async def update_verification_attempts(
database, dm_room_id: str, attempts_remaining: int
) -> None:
"""Update verification attempts remaining.
Args:
database: Database instance
dm_room_id: The DM room ID
@@ -229,7 +241,8 @@ async def update_verification_attempts(database, dm_room_id: str, attempts_remai
try:
await database.execute(
"UPDATE verification_states SET attempts_remaining = $1 WHERE dm_room_id = $2",
attempts_remaining, dm_room_id
attempts_remaining,
dm_room_id,
)
except Exception as e:
pass # Verification state update is not critical
@@ -237,15 +250,14 @@ async def update_verification_attempts(database, dm_room_id: str, attempts_remai
async def delete_verification_state(database, dm_room_id: str) -> None:
"""Delete a verification state.
Args:
database: Database instance
dm_room_id: The DM room ID
"""
try:
await database.execute(
"DELETE FROM verification_states WHERE dm_room_id = $1",
dm_room_id
"DELETE FROM verification_states WHERE dm_room_id = $1", dm_room_id
)
except Exception as e:
pass # Verification state deletion is not critical