Implement Message related classes
This commit is contained in:
parent
06af0e7faa
commit
97f05272ca
3 changed files with 183 additions and 18 deletions
23
test.py
23
test.py
|
|
@ -22,12 +22,35 @@ logger.addHandler(handler)
|
||||||
class TestClient:
|
class TestClient:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.room = Room("test", target_nick="testbot")
|
self.room = Room("test", target_nick="testbot")
|
||||||
|
self.room.register_event("join", self.on_join)
|
||||||
|
self.room.register_event("part", self.on_part)
|
||||||
|
self.room.register_event("send", self.on_send)
|
||||||
|
|
||||||
self.stop = asyncio.Event()
|
self.stop = asyncio.Event()
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
await self.room.connect()
|
await self.room.connect()
|
||||||
await self.stop.wait()
|
await self.stop.wait()
|
||||||
|
|
||||||
|
async def on_join(self, user):
|
||||||
|
print()
|
||||||
|
print(f"{user.nick} ({user.atmention}) joined.")
|
||||||
|
if user.is_person:
|
||||||
|
print("They're a person!")
|
||||||
|
elif user.is_bot:
|
||||||
|
print("They're just a bot")
|
||||||
|
else:
|
||||||
|
print("This should never happen")
|
||||||
|
print()
|
||||||
|
|
||||||
|
async def on_part(self, user):
|
||||||
|
print(f"{user.nick} left")
|
||||||
|
|
||||||
|
async def on_send(self, message):
|
||||||
|
await message.reply(f"You said {message.content!r}.")
|
||||||
|
msg1 = await message.room.send(f"{message.sender.atmention} said something.")
|
||||||
|
await msg1.reply("Yes, they really did.")
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
tc = TestClient()
|
tc = TestClient()
|
||||||
await tc.run()
|
await tc.run()
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
from typing import TYPE_CHECKING, Any, Optional
|
from typing import TYPE_CHECKING, Any, List, Optional
|
||||||
|
|
||||||
from .session import LiveSession, Session
|
from .session import LiveSession, Session
|
||||||
|
|
||||||
|
|
@ -9,22 +9,162 @@ if TYPE_CHECKING:
|
||||||
__all__ = ["Message", "LiveMessage"]
|
__all__ = ["Message", "LiveMessage"]
|
||||||
|
|
||||||
class Message:
|
class Message:
|
||||||
pass
|
def __init__(self,
|
||||||
# @property
|
room_name: str,
|
||||||
# def room_name(self) -> str:
|
message_id: str,
|
||||||
# return self._room_name
|
parent_id: Optional[str],
|
||||||
#
|
previous_edit_id: Optional[str],
|
||||||
# @property
|
timestamp: int,
|
||||||
# def time(self) -> datetime.datetime:
|
sender: Session,
|
||||||
# return datetime.datetime.fromtimestamp(self.timestamp)
|
content: str,
|
||||||
#
|
encryption_key_id: Optional[str],
|
||||||
# @property
|
edited_timestamp: Optional[int],
|
||||||
# def timestamp(self) -> int:
|
deleted_timestamp: Optional[int],
|
||||||
# return self._timestamp
|
truncated: bool
|
||||||
|
) -> None:
|
||||||
class LiveMessage(Message):
|
self._room_name = room_name
|
||||||
pass
|
self._message_id = message_id
|
||||||
|
self._parent_id = parent_id
|
||||||
|
self._previous_edit_id = previous_edit_id
|
||||||
|
self._timestamp = timestamp
|
||||||
|
self._sender = sender
|
||||||
|
self._content = content
|
||||||
|
self._encryption_key_id = encryption_key_id
|
||||||
|
self._edited_timestamp = edited_timestamp
|
||||||
|
self._deleted_timestamp = deleted_timestamp
|
||||||
|
self._truncated = truncated
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_data(cls, room: "Room", data: Any) -> "LiveMessage":
|
def from_data(cls, room_name: str, data: Any) -> "Message":
|
||||||
pass
|
message_id = data["id"]
|
||||||
|
parent_id = data.get("parent")
|
||||||
|
previous_edit_id = data.get("previous_edit_id")
|
||||||
|
timestamp = data["time"]
|
||||||
|
sender = Session.from_data(room_name, data["sender"])
|
||||||
|
content = data["content"]
|
||||||
|
encryption_key_id = data.get("encryption_key_id")
|
||||||
|
edited_timestamp = data.get("edited")
|
||||||
|
deleted_timestamp = data.get("deleted")
|
||||||
|
truncated = data.get("truncated", False)
|
||||||
|
|
||||||
|
return cls(room_name, message_id, parent_id, previous_edit_id,
|
||||||
|
timestamp, sender, content, encryption_key_id,
|
||||||
|
edited_timestamp, deleted_timestamp, truncated)
|
||||||
|
|
||||||
|
# Attributes
|
||||||
|
|
||||||
|
@property
|
||||||
|
def room_name(self) -> str:
|
||||||
|
return self._room_name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def message_id(self) -> str:
|
||||||
|
return self._message_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parent_id(self) -> Optional[str]:
|
||||||
|
return self._parent_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def previous_edit_id(self) -> Optional[str]:
|
||||||
|
return self._previous_edit_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def time(self) -> datetime.datetime:
|
||||||
|
return datetime.datetime.fromtimestamp(self.timestamp)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def timestamp(self) -> int:
|
||||||
|
return self._timestamp
|
||||||
|
|
||||||
|
@property
|
||||||
|
def sender(self) -> Session:
|
||||||
|
return self._sender
|
||||||
|
|
||||||
|
@property
|
||||||
|
def content(self) -> str:
|
||||||
|
return self._content
|
||||||
|
|
||||||
|
@property
|
||||||
|
def encryption_key_id(self) -> Optional[str]:
|
||||||
|
return self._encryption_key_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def edited_time(self) -> Optional[datetime.datetime]:
|
||||||
|
if self.edited_timestamp is not None:
|
||||||
|
return datetime.datetime.fromtimestamp(self.edited_timestamp)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def edited_timestamp(self) -> Optional[int]:
|
||||||
|
return self._edited_timestamp
|
||||||
|
|
||||||
|
@property
|
||||||
|
def deleted_time(self) -> Optional[datetime.datetime]:
|
||||||
|
if self.deleted_timestamp is not None:
|
||||||
|
return datetime.datetime.fromtimestamp(self.deleted_timestamp)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def deleted_timestamp(self) -> Optional[int]:
|
||||||
|
return self._deleted_timestamp
|
||||||
|
|
||||||
|
@property
|
||||||
|
def truncated(self) -> bool:
|
||||||
|
return self._truncated
|
||||||
|
|
||||||
|
class LiveMessage(Message):
|
||||||
|
def __init__(self,
|
||||||
|
room: "Room",
|
||||||
|
message_id: str,
|
||||||
|
parent_id: Optional[str],
|
||||||
|
previous_edit_id: Optional[str],
|
||||||
|
timestamp: int,
|
||||||
|
sender: LiveSession,
|
||||||
|
content: str,
|
||||||
|
encryption_key_id: Optional[str],
|
||||||
|
edited_timestamp: Optional[int],
|
||||||
|
deleted_timestamp: Optional[int],
|
||||||
|
truncated: bool
|
||||||
|
) -> None:
|
||||||
|
super().__init__(room.name, message_id, parent_id, previous_edit_id,
|
||||||
|
timestamp, sender, content, encryption_key_id,
|
||||||
|
edited_timestamp, deleted_timestamp, truncated)
|
||||||
|
self._room = room
|
||||||
|
self._live_sender = sender
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_data(cls, # type: ignore
|
||||||
|
room: "Room",
|
||||||
|
data: Any
|
||||||
|
) -> "LiveMessage":
|
||||||
|
return cls.from_message(room, Message.from_data(room.name, data))
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_message(cls, room: "Room", message: Message) -> "LiveMessage":
|
||||||
|
live_sender = LiveSession.from_session(room, message.sender)
|
||||||
|
return cls(room, message.message_id, message.parent_id,
|
||||||
|
message.previous_edit_id, message.timestamp, live_sender,
|
||||||
|
message.content, message.encryption_key_id,
|
||||||
|
message.edited_timestamp, message.deleted_timestamp,
|
||||||
|
message.truncated)
|
||||||
|
|
||||||
|
# Attributes
|
||||||
|
|
||||||
|
@property
|
||||||
|
def room(self) -> "Room":
|
||||||
|
return self._room
|
||||||
|
|
||||||
|
@property
|
||||||
|
def sender(self) -> LiveSession:
|
||||||
|
return self._live_sender
|
||||||
|
|
||||||
|
# Live stuff
|
||||||
|
|
||||||
|
async def reply(self, content: str) -> "LiveMessage":
|
||||||
|
return await self.room.send(content, parent_id=self.message_id)
|
||||||
|
|
||||||
|
async def before(self, amount: int) -> List["LiveMessage"]:
|
||||||
|
return await self.room.log(amount, before_id=self.message_id)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
# TODO add more logging
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, Awaitable, Callable, List, Optional, TypeVar
|
from typing import Any, Awaitable, Callable, List, Optional, TypeVar
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue