fix: activate E2E and accept room invites in Matrix channels
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import asyncio
|
||||
from typing import Any
|
||||
|
||||
from nio import AsyncClient, MatrixRoom, RoomMessageText
|
||||
from nio import AsyncClient, AsyncClientConfig, InviteEvent, MatrixRoom, RoomMessageText
|
||||
|
||||
from nanobot.channels.base import BaseChannel
|
||||
from nanobot.bus.events import OutboundMessage
|
||||
from nanobot.channels.base import BaseChannel
|
||||
from nanobot.config.loader import get_data_dir
|
||||
|
||||
|
||||
class MatrixChannel(BaseChannel):
|
||||
@@ -22,18 +23,28 @@ class MatrixChannel(BaseChannel):
|
||||
async def start(self) -> None:
|
||||
self._running = True
|
||||
|
||||
self.client = AsyncClient(
|
||||
homeserver=self.config.homeserver,
|
||||
user=self.config.user_id,
|
||||
)
|
||||
|
||||
self.client.access_token = self.config.access_token
|
||||
store_path = get_data_dir() / "matrix-store"
|
||||
store_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
self.client.add_event_callback(
|
||||
self._on_message,
|
||||
RoomMessageText
|
||||
self.client = AsyncClient(
|
||||
homeserver=self.config.homeserver,
|
||||
user=self.config.user_id,
|
||||
store_path=store_path, # Where tokens are saved
|
||||
config=AsyncClientConfig(
|
||||
store_sync_tokens=True, # Auto-persists next_batch tokens
|
||||
encryption_enabled=True,
|
||||
),
|
||||
)
|
||||
|
||||
self.client.user_id = self.config.user_id
|
||||
self.client.access_token = self.config.access_token
|
||||
self.client.device_id = self.config.device_id
|
||||
|
||||
self.client.add_event_callback(self._on_message, RoomMessageText)
|
||||
self.client.add_event_callback(self._on_room_invite, InviteEvent)
|
||||
|
||||
self.client.load_store()
|
||||
|
||||
self._sync_task = asyncio.create_task(self._sync_loop())
|
||||
|
||||
async def stop(self) -> None:
|
||||
@@ -51,22 +62,23 @@ class MatrixChannel(BaseChannel):
|
||||
room_id=msg.chat_id,
|
||||
message_type="m.room.message",
|
||||
content={"msgtype": "m.text", "body": msg.content},
|
||||
ignore_unverified_devices=True,
|
||||
)
|
||||
|
||||
async def _sync_loop(self) -> None:
|
||||
while self._running:
|
||||
try:
|
||||
await self.client.sync(timeout=30000)
|
||||
await self.client.sync_forever(timeout=30000, full_state=True)
|
||||
except asyncio.CancelledError:
|
||||
break
|
||||
except Exception:
|
||||
await asyncio.sleep(2)
|
||||
|
||||
async def _on_message(
|
||||
self,
|
||||
room: MatrixRoom,
|
||||
event: RoomMessageText
|
||||
) -> None:
|
||||
async def _on_room_invite(self, room: MatrixRoom, event: RoomMessageText) -> None:
|
||||
if event.sender in self.config.allow_from:
|
||||
await self.client.join(room.room_id)
|
||||
|
||||
async def _on_message(self, room: MatrixRoom, event: RoomMessageText) -> None:
|
||||
# Ignore self messages
|
||||
if event.sender == self.config.user_id:
|
||||
return
|
||||
@@ -76,4 +88,4 @@ class MatrixChannel(BaseChannel):
|
||||
chat_id=room.room_id,
|
||||
content=event.body,
|
||||
metadata={"room": room.display_name},
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user