2022-05-12 03:56:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from copy import deepcopy
|
|
|
|
from typing import Union, Optional, Sequence, Dict, Any
|
|
|
|
|
2022-08-24 11:54:46 +00:00
|
|
|
from nio import (
|
|
|
|
RoomBanError,
|
|
|
|
RoomBanResponse,
|
|
|
|
RoomCreateResponse,
|
|
|
|
RoomCreateError,
|
|
|
|
RoomGetStateResponse,
|
|
|
|
RoomGetStateError,
|
|
|
|
RoomInviteResponse,
|
|
|
|
RoomInviteError,
|
|
|
|
JoinResponse,
|
|
|
|
JoinError,
|
|
|
|
JoinedRoomsResponse,
|
|
|
|
JoinedRoomsError,
|
|
|
|
RoomKickResponse,
|
|
|
|
RoomKickError,
|
|
|
|
RoomResolveAliasError,
|
|
|
|
RoomResolveAliasResponse,
|
|
|
|
RoomSendResponse,
|
|
|
|
RoomSendError,
|
|
|
|
RoomUnbanResponse,
|
|
|
|
RoomUnbanError,
|
|
|
|
RoomGetStateEventResponse,
|
|
|
|
RoomGetStateEventError,
|
|
|
|
RoomPutStateResponse,
|
|
|
|
RoomPutStateError,
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
from nio.api import RoomVisibility, RoomPreset
|
|
|
|
|
2022-08-24 11:54:46 +00:00
|
|
|
from ansible_collections.famedly.matrix.tests.unit.mock_nio.utils.RoomSimulator import (
|
|
|
|
RoomEvents,
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RoomBan:
|
2022-08-24 11:54:46 +00:00
|
|
|
async def room_ban(
|
|
|
|
self, room_id: str, user_id: str, reason: Optional[str] = None
|
|
|
|
) -> Union[RoomBanResponse, RoomBanError]:
|
|
|
|
self.room_sim.m_room_member(
|
|
|
|
room_id=room_id, membership="ban", sender=self.user, state_key=user_id
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
return RoomBanResponse()
|
|
|
|
|
|
|
|
|
|
|
|
class RoomCreate:
|
|
|
|
async def room_create(
|
2022-08-24 11:54:46 +00:00
|
|
|
self,
|
|
|
|
visibility: RoomVisibility = RoomVisibility.private,
|
|
|
|
alias: Optional[str] = None,
|
|
|
|
name: Optional[str] = None,
|
|
|
|
topic: Optional[str] = None,
|
|
|
|
room_version: Optional[str] = None,
|
|
|
|
federate: bool = True,
|
|
|
|
is_direct: bool = False,
|
|
|
|
preset: Optional[RoomPreset] = None,
|
|
|
|
invite: Sequence[str] = (),
|
|
|
|
initial_state: Sequence[Dict[str, Any]] = (),
|
|
|
|
power_level_override: Optional[Dict[str, Any]] = None,
|
|
|
|
predecessor: Optional[Dict[str, Any]] = None,
|
|
|
|
space: bool = False,
|
2022-05-12 03:56:52 +00:00
|
|
|
) -> Union[RoomCreateResponse, RoomCreateError]:
|
|
|
|
room_id = self.room_sim.create_room(alias=alias)
|
|
|
|
return RoomCreateResponse(room_id)
|
|
|
|
|
|
|
|
|
|
|
|
class RoomGetState:
|
2022-08-24 11:54:46 +00:00
|
|
|
async def room_get_state(
|
|
|
|
self, room_id: str
|
|
|
|
) -> Union[RoomGetStateResponse, RoomGetStateError]:
|
|
|
|
return RoomGetStateResponse(
|
|
|
|
events=self.room_sim.get_events(room_id), room_id=room_id
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RoomPutState:
|
2022-08-24 11:54:46 +00:00
|
|
|
async def room_put_state(
|
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
event_type: str,
|
|
|
|
content: Dict[Any, Any],
|
|
|
|
state_key: str = "",
|
|
|
|
) -> Union[RoomPutStateResponse, RoomPutStateError]:
|
2022-05-12 03:56:52 +00:00
|
|
|
data = deepcopy(RoomEvents.M_ROOM_DUMMY)
|
2022-08-24 11:54:46 +00:00
|
|
|
data["content"] = content
|
|
|
|
data["type"] = event_type
|
|
|
|
data["state_key"] = state_key
|
2022-05-12 03:56:52 +00:00
|
|
|
event = self.room_sim.send_event(room_id=room_id, event=data)
|
2022-08-24 11:54:46 +00:00
|
|
|
return RoomPutStateResponse(event_id=event["event_id"], room_id=room_id)
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RoomGetStateEvent:
|
2022-08-24 11:54:46 +00:00
|
|
|
async def room_get_state_event(
|
|
|
|
self, room_id: str, event_type: str, state_key: str = ""
|
|
|
|
) -> Union[RoomGetStateEventResponse, RoomGetStateEventError]:
|
2022-05-12 03:56:52 +00:00
|
|
|
event = self.room_sim.get_state_event(room_id, event_type, state_key)
|
|
|
|
if event is None:
|
|
|
|
return RoomGetStateEventError(room_id=room_id, message="event not found!")
|
2022-08-24 11:54:46 +00:00
|
|
|
return RoomGetStateEventResponse(
|
|
|
|
content=event["content"],
|
|
|
|
event_type=event_type,
|
|
|
|
state_key=state_key,
|
|
|
|
room_id=room_id,
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RoomInvite:
|
2022-08-24 11:54:46 +00:00
|
|
|
async def room_invite(
|
|
|
|
self, room_id: str, user_id: str
|
|
|
|
) -> Union[RoomInviteResponse, RoomInviteError]:
|
|
|
|
self.room_sim.m_room_member(
|
|
|
|
room_id=room_id, membership="join", sender=self.user, state_key=user_id
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
return RoomInviteResponse()
|
|
|
|
|
|
|
|
|
|
|
|
class RoomJoin:
|
|
|
|
async def join(self, room_id: str) -> Union[JoinResponse, JoinError]:
|
|
|
|
self.client_sim.join(room_id)
|
|
|
|
return JoinResponse(room_id)
|
|
|
|
|
|
|
|
|
|
|
|
class RoomJoinedRooms:
|
|
|
|
async def joined_rooms(self) -> Union[JoinedRoomsResponse, JoinedRoomsError]:
|
|
|
|
return JoinedRoomsResponse(self.client_sim.get_joined_rooms())
|
|
|
|
|
|
|
|
|
|
|
|
class RoomKick:
|
2022-08-24 11:54:46 +00:00
|
|
|
async def room_kick(
|
|
|
|
self, room_id: str, user_id: str, reason: Optional[str] = None
|
|
|
|
) -> Union[RoomKickResponse, RoomKickError]:
|
|
|
|
self.room_sim.m_room_member(
|
|
|
|
room_id=room_id, membership="leave", sender=self.user, state_key=user_id
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
return RoomKickResponse()
|
|
|
|
|
|
|
|
|
|
|
|
class RoomResolveAlias:
|
2022-08-24 11:54:46 +00:00
|
|
|
async def room_resolve_alias(
|
|
|
|
self, room_alias: str
|
|
|
|
) -> Union[RoomResolveAliasResponse, RoomResolveAliasError]:
|
2022-05-12 03:56:52 +00:00
|
|
|
room_id = self.room_sim.resolve_alias(room_alias)
|
|
|
|
if room_id is not None:
|
2022-08-24 11:54:46 +00:00
|
|
|
return RoomResolveAliasResponse(
|
|
|
|
room_id=room_id, room_alias=room_alias, servers=[]
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
else:
|
2022-08-24 11:54:46 +00:00
|
|
|
return RoomResolveAliasError(message="Room not found!")
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RoomSend:
|
2022-08-24 11:54:46 +00:00
|
|
|
async def room_send(
|
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
message_type: str,
|
|
|
|
content: Dict[Any, Any],
|
|
|
|
tx_id: Optional[str] = None,
|
|
|
|
ignore_unverified_devices: bool = False,
|
|
|
|
) -> Union[RoomSendResponse, RoomSendError]:
|
|
|
|
return RoomSendResponse(room_id=room_id, event_id="asdf")
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RoomUnban:
|
2022-08-24 11:54:46 +00:00
|
|
|
async def room_unban(
|
|
|
|
self, room_id: str, user_id: str
|
|
|
|
) -> Union[RoomUnbanResponse, RoomUnbanError]:
|
|
|
|
self.room_sim.m_room_member(
|
|
|
|
room_id=room_id, membership="leave", sender=self.user, state_key=user_id
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
return RoomUnbanResponse()
|