Swap to mautrix memory stores

This commit is contained in:
Astra Logical 2026-07-23 11:56:57 -04:00
parent 41bdeafcbc
commit eb35de374d

View file

@ -9,9 +9,8 @@ from keycloak.exceptions import KeycloakError
from mautrix.client import Client
from mautrix.crypto import OlmMachine
from mautrix.crypto.store.sql import SQLCryptoStore
from mautrix.client.state_store.sql import SQLStateStore
from mautrix.util.async_db import Database
from mautrix.crypto.store import MemoryCryptoStore
from mautrix.client.state_store import MemoryStateStore
from mautrix.types import (
EventType, MessageType, TextMessageEventContent, Format,
Event, RoomID, Membership, RelationType
@ -33,7 +32,6 @@ KEYCLOAK_CLIENT_ID = os.getenv("KEYCLOAK_CLIENT_ID", "nest-matrix-bot")
KEYCLOAK_CLIENT_SECRET = os.getenv("KEYCLOAK_CLIENT_SECRET", "your-client-secret")
CREDENTIALS_FILE = "/app/store/credentials.json"
DB_URI = "sqlite:///app/store/bot_state.db"
# In-memory tracking of active requests (Event ID -> Request Info)
pending_requests = {}
@ -222,7 +220,6 @@ async def on_message(evt: Event):
f"React with 👍 to **Approve** or 👎 to **Deny**."
)
# In mautrix, send_message returns the event ID string directly
event_id = await send_markdown_message(
room_id=ADMIN_ROOM_ID,
md_text=admin_msg_body,
@ -286,7 +283,6 @@ async def on_reaction(evt: Event):
else:
return
# Update original message via m.replace
await send_markdown_message(
room_id=ADMIN_ROOM_ID,
md_text=status_text,
@ -301,14 +297,10 @@ async def on_reaction(evt: Event):
async def main():
global client
# 1. Initialize SQLite backend for stores
db = Database.create(DB_URI)
await db.start()
# 2. Initialize client
# 1. Initialize client
client = Client(base_url=MATRIX_HOMESERVER, mxid=MATRIX_BOT_USER)
# 3. Session Persistence
# 2. Session Persistence
session_restored = False
if os.path.exists(CREDENTIALS_FILE):
log.info("Restoring existing Matrix session...")
@ -335,31 +327,30 @@ async def main():
}, f)
log.info("Logged in and saved credentials.")
# 4. Attach Crypto & State Stores
state_store = SQLStateStore(db)
# 3. Attach Memory Crypto & State Stores
state_store = MemoryStateStore()
client.state_store = state_store
crypto_store = SQLCryptoStore(db, client.mxid, client.device_id)
crypto_store = MemoryCryptoStore(client.mxid, client.device_id)
machine = OlmMachine(client, crypto_store, state_store)
await machine.load()
client.crypto = machine
# 5. SSSS Recovery Key Ingestion
# 4. SSSS Recovery Key Ingestion
if MATRIX_RECOVERY_KEY:
try:
log.info("🔑 SSSS Recovery key detected. Attempting to cross-sign session...")
# Instructs mautrix to decrypt your master key from secret storage
await machine.verify_session(MATRIX_RECOVERY_KEY)
log.info("✅ Session successfully self-verified!")
except Exception as e:
log.error(f"⚠️ Could not verify session using recovery key: {e}")
# 6. Register Event Handlers
# 5. Register Event Handlers
client.add_event_handler(EventType.ROOM_MEMBER, on_invite)
client.add_event_handler(EventType.ROOM_MESSAGE, on_message)
client.add_event_handler(EventType.REACTION, on_reaction)
# 7. Start Sync Loop
# 6. Start Sync Loop
log.info("Starting Mautrix sync loop...")
await client.start()