Update Client to send read receipts for every message
Fix bugs in events due to m.related_to in text events
This commit is contained in:
parent
353990a621
commit
d3acd0a114
@ -1,7 +1,7 @@
|
||||
import asyncio
|
||||
from typing import Union, Optional, Dict
|
||||
|
||||
from .api import API, APIConfig
|
||||
from .api import API
|
||||
from .room import Room
|
||||
|
||||
|
||||
@ -111,6 +111,10 @@ class Client:
|
||||
handler = self.event_dispatchers.get(event.type)
|
||||
if handler:
|
||||
await self.invoke(handler, event)
|
||||
try:
|
||||
await self.mark_event_read(event)
|
||||
except RuntimeError as e:
|
||||
pass
|
||||
|
||||
# Process ephemeral events
|
||||
for event in data['ephemeral']['events']:
|
||||
@ -160,6 +164,14 @@ class Client:
|
||||
raise TypeError(f'handler must be a callable not {type(handler)}')
|
||||
self.event_dispatchers[event_type] = handler
|
||||
|
||||
async def mark_event_read(self, event, receipt_type: str = 'm.read'):
|
||||
from .events import RoomEvent
|
||||
if isinstance(event, RoomEvent):
|
||||
path = self.api.build_url(f'rooms/{event.room.id}/receipt/{receipt_type}/{event.event_id}')
|
||||
await self.api.send('POST', path)
|
||||
else:
|
||||
raise RuntimeError(f'Event to mark read must be an instance of RoomEvent. Not {type(event)}')
|
||||
|
||||
async def send_room_message(self, room: Room, content: dict):
|
||||
await self.api.room_send(room_id=room.id, event_type='m.room.message', content=content)
|
||||
|
||||
|
||||
@ -11,7 +11,8 @@ from .utils import (
|
||||
PreviousRoom,
|
||||
Invite,
|
||||
ReactionRelation,
|
||||
notification_power_levels_default_factory
|
||||
notification_power_levels_default_factory,
|
||||
MessageRelation
|
||||
)
|
||||
|
||||
|
||||
@ -31,6 +32,7 @@ class MTextContent(MessageContentBase):
|
||||
format: Optional[str] = None
|
||||
formatted_body: Optional[str] = None
|
||||
msgtype = "m.text"
|
||||
relates_to: Optional[MessageRelation] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
@ -4,7 +4,7 @@ from typing import Optional, List
|
||||
from .client import Client
|
||||
from .room import Room
|
||||
from .content import ContentBase
|
||||
from .utils import ReactionRelation
|
||||
from .utils import ReactionRelation, MessageRelation
|
||||
|
||||
|
||||
@dataclass
|
||||
@ -28,6 +28,17 @@ class EventBase:
|
||||
content_dict = {'options': event_dict['content']}
|
||||
else:
|
||||
content_dict = event_dict['content']
|
||||
|
||||
if content_dict.get('m.relates_to'):
|
||||
if content_dict['m.relates_to'].get('m.in_reply_to'):
|
||||
content_dict['relates_to'] = MessageRelation(
|
||||
event_id=content_dict['m.relates_to']['m.in_reply_to']['event_id']
|
||||
)
|
||||
del content_dict['m.relates_to']
|
||||
|
||||
if content_dict.get('m.new_content'):
|
||||
del content_dict['m.new_content']
|
||||
|
||||
del event_dict['content']
|
||||
|
||||
return cls(
|
||||
|
||||
@ -92,5 +92,10 @@ class ReactionRelation:
|
||||
key: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class MessageRelation:
|
||||
event_id: str
|
||||
|
||||
|
||||
def notification_power_levels_default_factory():
|
||||
return {'room': 50}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user