refactor(matrix): migrate from matrix-nio to mautrix-python - #27095
Closed
Master-Rensei wants to merge 9 commits into
Closed
refactor(matrix): migrate from matrix-nio to mautrix-python#27095Master-Rensei wants to merge 9 commits into
Master-Rensei wants to merge 9 commits into
Conversation
matrix-nio pulls in peewee -> atomicwrites (sdist-only, archived, missing build-system metadata) which breaks nix flake builds. mautrix-python publishes wheels, has a leaner dep tree, and its [encryption] extra uses the same python-olm without the problematic transitive chain.
Translate all nio SDK calls to mautrix equivalents while preserving the adapter structure, business logic, and all features (E2EE, reactions, threading, mention gating, text batching, media caching, voice MSC3245). Key changes: - nio.AsyncClient -> mautrix.client.Client + HTTPAPI + MemoryStateStore - Manual E2EE key management -> OlmMachine with auto key lifecycle - isinstance(resp, nio.XxxResponse) -> mautrix returns values directly - add_event_callback per type -> single ROOM_MESSAGE handler with msgtype dispatch - Room state (member_count, display_name) via async state store lookups - Upload/download return ContentURI/bytes directly (no wrapper objects)
Rewrite mock infrastructure across three test files: - test_matrix.py: replace fake nio module with fake mautrix module tree, update all client method mocks to new API names and return types - test_matrix_voice.py: update event construction, download/upload mocks, handler invocation (single event arg, no room object) - test_matrix_mention.py: update mock module, event construction, DM detection via _dm_rooms cache instead of room.member_count 157 tests passing.
Address two bugs found by code review: 1. MemoryCryptoStore loses all E2EE keys on restart — now pickle the store to disk on disconnect and restore on connect, preserving Megolm sessions across restarts. 2. Encrypted events buffered for retry were silently dropped after decryption because _on_encrypted_event registered the event ID in the dedup set, then _on_room_message rejected it as a duplicate. Now clear the dedup entry before routing decrypted events.
- Extract _resolve_message_context() to deduplicate ~40 lines of mention/thread/DM gating logic between text and media handlers - Move mautrix.types imports to module level (16 scattered local imports consolidated) - Parse mention/thread env vars once in __init__ instead of per-message - Cache _is_bot_mentioned() result instead of calling 3x per event - Consolidate send_emote/send_notice into shared _send_simple_message() - Use _is_dm_room() in get_chat_info() instead of inline duplication - Add _CRYPTO_PICKLE_PATH constant (was duplicated in 2 locations) - Fix fragile event_ts extraction (double getattr, None safety) - Clean up leaked aiohttp session on auth failure paths - Remove redundant trailing _track_thread() calls
…kle store - Add api.session.close() on E2EE dep check and E2EE setup failure paths (two missing cleanup points from the mautrix migration) - Replace raw pickle.load/dump with HMAC-SHA256 signed payloads to prevent arbitrary code execution from a tampered store file
The old nio code only handled RoomMessageText (m.text). The mautrix rewrite dispatched both m.text and m.notice, which would cause infinite loops between bots since m.notice is the conventional msgtype for bot responses in the Matrix ecosystem.
… to encrypted handler - Remove trailing _track_thread() in _resolve_message_context (already called in the DM-mention-thread and auto-thread branches) - Add sender == self check and startup grace period filter to _on_encrypted_event so own messages and old initial-sync events don't waste buffer space
This comment was marked as spam.
This comment was marked as spam.
Collaborator
|
Duplicate of #7518 (already merged). This PR reopens a previously closed fork branch, but the feature (matrix mautrix migration) was already landed via a salvage PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces
matrix-niowithmautrix-pythonas the Matrix SDK dependency, fixing Nix flake build failures caused by matrix-nio's transitive depatomicwrites(sdist-only, archived, missing build-system metadata).This reopens closed PR #7488 (by @alt-glitch, closed without merge) with 9 additional fix commits addressing code review findings from the original submission.
What's new since PR #7488 was closed
9 commits by @alt-glitch addressing review feedback:
_resolve_message_context(); consolidated imports; cached env var parsingm.noticemessages (conventional bot response msgtype)_track_thread()calls; added sender-self and startup-grace filters to encrypted handlerAll original adapter features preserved: E2EE, reactions, threading, mention gating, text batching, media caching, MSC3245 voice messages.
Test plan
pytest tests/gateway/test_matrix*.py tests/test_project_metadata.py)nix flake checkpasses cleanuv lock --checkvalidates lockfile consistency