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,12 +48,33 @@ 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):
""" """
From api.euphoria.io: await ping_reply(time)
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,16 +87,52 @@ 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):
""" """
session_id, user_id, from_nick, to_nick = await nick(name) session_id, user_id, from_nick, to_nick = await nick(name)
From api.euphoria.io: From api.euphoria.io:
The nick command sets the name you present to the room. This name The nick command sets the name you present to the room. This name
applies to all messages sent during this session, until the nick applies to all messages sent during this session, until the nick
command is called again. command is called again.
@ -86,24 +143,45 @@ 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):
""" """
From api.euphoria.io: message = await send(content, parent=None)
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
all sessions joined with the room. all sessions joined with the room.
@ -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)
@ -178,7 +282,7 @@ class Room:
async def _handle_bounce(self, packet): async def _handle_bounce(self, packet):
""" """
From api.euphoria.io: From api.euphoria.io:
A bounce-event indicates that access to a room is denied. A bounce-event indicates that access to a room is denied.
""" """
@ -193,7 +297,7 @@ class Room:
async def _handle_disconnect(self, packet): async def _handle_disconnect(self, packet):
""" """
From api.euphoria.io: From api.euphoria.io:
A disconnect-event indicates that the session is being closed. The A disconnect-event indicates that the session is being closed. The
client will subsequently be disconnected. client will subsequently be disconnected.
@ -207,7 +311,7 @@ class Room:
async def _handle_hello(self, packet): async def _handle_hello(self, packet):
""" """
From api.euphoria.io: From api.euphoria.io:
A hello-event is sent by the server to the client when a session is A hello-event is sent by the server to the client when a session is
started. It includes information about the clients authentication and started. It includes information about the clients authentication and
associated identity. associated identity.
@ -233,7 +337,7 @@ class Room:
async def _handle_join(self, packet): async def _handle_join(self, packet):
""" """
From api.euphoria.io: From api.euphoria.io:
A join-event indicates a session just joined the room. A join-event indicates a session just joined the room.
""" """
@ -256,7 +360,7 @@ class Room:
async def _handle_nick(self, packet): async def _handle_nick(self, packet):
""" """
From api.euphoria.io: From api.euphoria.io:
nick-event announces a nick change by another session in the room. nick-event announces a nick change by another session in the room.
""" """
@ -281,7 +385,7 @@ class Room:
async def _handle_part(self, packet): async def _handle_part(self, packet):
""" """
From api.euphoria.io: From api.euphoria.io:
A part-event indicates a session just disconnected from the room. A part-event indicates a session just disconnected from the room.
""" """
@ -295,7 +399,7 @@ class Room:
async def _handle_ping(self, packet): async def _handle_ping(self, packet):
""" """
From api.euphoria.io: From api.euphoria.io:
A ping-event represents a server-to-client ping. The client should send A ping-event represents a server-to-client ping. The client should send
back a ping-reply with the same value for the time field as soon as back a ping-reply with the same value for the time field as soon as
possible (or risk disconnection). possible (or risk disconnection).
@ -313,7 +417,7 @@ class Room:
async def _handle_send(self, packet): async def _handle_send(self, packet):
""" """
From api.euphoria.io: From api.euphoria.io:
A send-event indicates a message received by the room from another A send-event indicates a message received by the room from another
session. session.
""" """
@ -325,7 +429,7 @@ class Room:
async def _handle_snapshot(self, packet): async def _handle_snapshot(self, packet):
""" """
From api.euphoria.io: From api.euphoria.io:
A snapshot-event indicates that a session has successfully joined a A snapshot-event indicates that a session has successfully joined a
room. It also offers a snapshot of the rooms state and recent history. room. It also offers a snapshot of the rooms state and recent history.
""" """