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