Implement all basic room commands

This commit is contained in:
Joscha 2017-09-02 13:31:17 +00:00
parent 6cc8094e0d
commit a199af40d9
2 changed files with 133 additions and 49 deletions

View file

@ -52,9 +52,10 @@ class Connection:
pid = self._new_pid() pid = self._new_pid()
packet = { packet = {
"type": ptype, "type": ptype,
"data": data,
"id": str(pid) "id": str(pid)
} }
if data:
packet["data"] = data
if await_response: if await_response:
wait_for = self._wait_for_response(pid) wait_for = self._wait_for_response(pid)
@ -86,24 +87,3 @@ class Connection:
self._pending_responses[pid] = future self._pending_responses[pid] = future
return future return future
#async def handle_packet(packet):
#if packet.get("type") == "ping-event":
#await c._ws.send('{"type":"ping-reply","data":{"time":' + str(packet.get("data").get("time")) + '}}')
##await c.send("ping-reply", {"time": packet.get("data").get("time")}, False)
#c = Connection("wss://euphoria.io/room/test/ws", handle_packet)
async def await_future(f):
await f
print(f.result())
def run():
f = asyncio.Future()
#f.set_result("Hello World!")
f.cancel()
#f.set_result("Hello World!")
loop = asyncio.get_event_loop()
loop.run_until_complete(await_future(f))
#loop.run_until_complete(c.run())

View file

@ -48,11 +48,32 @@ class Room:
# CATEGORY: SESSION COMMANDS # CATEGORY: SESSION COMMANDS
async def auth(self, atype, passcode): async def auth(self, atype, passcode=None):
pass # TODO """
success, reason=None = await auth(atype, passcode=None)
From api.euphoria.io:
The auth command attempts to join a private room. It should be sent in
response to a bounce-event at the beginning of a session.
The auth-reply packet reports whether the auth command succeeded.
"""
data = {"type": atype}
if passcode:
data["passcode"] = passcode
response = await self._send_packet("auth", data)
rdata = response.get("data")
success = rdata.get("success")
reason = rdata.get("reason", None)
return success, reason
async def ping_reply(self, time): async def ping_reply(self, time):
""" """
await ping_reply(time)
From api.euphoria.io: From api.euphoria.io:
The ping command initiates a client-to-server ping. The server will The ping command initiates a client-to-server ping. The server will
send back a ping-reply with the same timestamp as soon as possible. send back a ping-reply with the same timestamp as soon as possible.
@ -66,10 +87,46 @@ class Room:
# CATEGORY: CHAT ROOM COMMANDS # CATEGORY: CHAT ROOM COMMANDS
async def get_message(self, message_id): async def get_message(self, message_id):
pass # TODO """
message = await get_message(message_id)
From api.euphoria.io:
The get-message command retrieves the full content of a single message
in the room.
get-message-reply returns the message retrieved by get-message.
"""
data = {"id": message_id}
response = await self._send_packet("get-message", data)
rdata = response.get("data")
message = Message.from_dict(rdata)
return message
async def log(self, n, before=None): async def log(self, n, before=None):
pass # TODO """
log, before=None = await log(n, before=None)
From api.euphoria.io:
The log command requests messages from the rooms message log. This can
be used to supplement the log provided by snapshot-event (for example,
when scrolling back further in history).
The log-reply packet returns a list of messages from the rooms message
"""
data = {"n": n}
if before:
data["before"] = before
response = await self._send_packet("log", data)
rdata = response.get("data")
messages = [Message.from_dict(d) for d in rdata.get("log")]
before = rdata.get("before", None)
return messages, before
async def nick(self, name): async def nick(self, name):
""" """
@ -86,23 +143,44 @@ class Room:
data = {"name": name} data = {"name": name}
response = await self._conn.send("nick", data) response = await self._send_packet("nick", data)
self._check_for_errors(response) rdata = response.get("data")
session_id = response.get("session_id") session_id = rdata.get("session_id")
user_id = response.get("id") user_id = rdata.get("id")
from_nick = response.get("from") from_nick = rdata.get("from")
to_nick = response.get("to") to_nick = rdata.get("to")
# update self.session
self.session.nick = to_nick self.session.nick = to_nick
return session_id, user_id, from_nick, to_nick return session_id, user_id, from_nick, to_nick
async def pm_initiate(self, user_id): async def pm_initiate(self, user_id):
pass # TODO """
pm_id, to_nick = await pm_initiate(user_id)
From api.euphoria.io:
The pm-initiate command constructs a virtual room for private messaging
between the client and the given UserID.
The pm-initiate-reply provides the PMID for the requested private
messaging room.
"""
data = {"user_id": user_id}
response = await self._send_packet("pm-initiate", data)
rdata = response.get("data")
pm_id = rdata.get("pm_id")
to_nick = rdata.get("to_nick")
return pm_id, to_nick
async def send(self, content, parent=None): async def send(self, content, parent=None):
""" """
message = await send(content, parent=None)
From api.euphoria.io: From api.euphoria.io:
The send command sends a message to a room. The session must be The send command sends a message to a room. The session must be
successfully joined with the room. This message will be broadcast to successfully joined with the room. This message will be broadcast to
@ -119,14 +197,34 @@ class Room:
if parent: if parent:
data["parent"] = parent data["parent"] = parent
response = await self._conn.send("send", data) response = await self._send_packet("send", data)
self._check_for_errors(response) rdata = response.get("data")
message = Message.from_dict(response.get("data")) message = Message.from_dict(rdata)
return message return message
async def who(self): async def who(self):
pass # TODO """
sessions = await who()
From api.euphoria.io:
The who command requests a list of sessions currently joined in the
room.
The who-reply packet lists the sessions currently joined in the room.
"""
response = await self._send_packet("who")
rdata = response.get("data")
sessions = [Session.from_dict(d) for d in rdata.get("listing")]
# update self.listing
self.listing = Listing()
for session in sessions:
self.listing.add(session)
return sessions
# CATEGORY: ACCOUNT COMMANDS # CATEGORY: ACCOUNT COMMANDS
# NYI, and probably never will # NYI, and probably never will
@ -157,6 +255,12 @@ class Room:
self._callbacks["send-event"] = self._handle_send self._callbacks["send-event"] = self._handle_send
self._callbacks["snapshot-event"] = self._handle_snapshot self._callbacks["snapshot-event"] = self._handle_snapshot
async def _send_packet(self, *args, **kwargs):
response = await self._conn.send(*args, **kwargs)
self._check_for_errors(response)
return response
async def _handle_packet(self, packet): async def _handle_packet(self, packet):
self._check_for_errors(packet) self._check_for_errors(packet)