Skip to content

Commit

Permalink
Merge pull request #261 from mhcomm/dvl/quentin/fix_remote_admin
Browse files Browse the repository at this point in the history
FIX: Remote Admin shell and plugin
  • Loading branch information
klausfmh authored Oct 19, 2023
2 parents 500ead3 + 0bff860 commit cba086b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 97 deletions.
46 changes: 32 additions & 14 deletions pypeman/plugins/remoteadmin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ async def stop_channel(request, ws=None):

async def list_msgs(request, ws=None):
"""
List first `count` messages from message store of specified channel.
List first `count` message infos from message store of specified channel.
Return list of dicts with the id, the status and the timestamp of the message
without the content.
:params channelname: The channel name.
Expand Down Expand Up @@ -112,8 +114,11 @@ async def list_msgs(request, ws=None):
text=text, rtext=rtext) or []

for res in messages:
res["id"] = res["id"]
res["state"] = res.get("state", "UNKNOWN")
res['timestamp'] = res['message'].timestamp_str()
res['message'] = res['message'].to_json()
if "message" in res:
res.pop("message")

resp_dict = {'messages': messages, 'total': await chan.message_store.total()}
if ws is not None:
Expand All @@ -124,21 +129,24 @@ async def list_msgs(request, ws=None):

async def replay_msg(request, ws=None):
"""
Replay messages from message store.
Replay message from message store.
:params channel: The channel name.
:params msg_ids: The message ids list to replay.
:params msg_id: The message id to replay.
"""
channelname = request.match_info['channelname']
message_id = request.match_info['message_id']

chan = get_channel(channelname)
result = []
try:
msg_res = await chan.replay(message_id)
result.append(msg_res.to_dict())
result = msg_res.to_dict()
except IndexError:
message = f"Cannot replay msg, id {message_id} probably doesn't exists"
logger.error(message)
result = {'error': message}
except Exception as exc:
result.append({'error': str(exc)})
logger.exception(f"Cannot replay msg {message_id}")
result = {'error': str(exc)}

if ws is not None:
await ws.send_jsonrpcresp(result)
Expand All @@ -158,12 +166,17 @@ async def view_msg(request, ws=None):
message_id = request.match_info['message_id']

chan = get_channel(channelname)
result = []

try:
msg_res = await chan.message_store.get_msg_content(message_id)
result.append(msg_res.to_dict())
result = msg_res.to_dict()
except IndexError:
message = f"Cannot view msg, id {message_id} probably doesn't exists"
logger.error(message)
result = {'error': message}
except Exception as exc:
result.append({'error': str(exc)})
logger.exception(f"Cannot view msg {message_id}")
result = {'error': str(exc)}

if ws is not None:
await ws.send_jsonrpcresp(result)
Expand All @@ -182,12 +195,16 @@ async def preview_msg(request, ws=None):
message_id = request.match_info['message_id']

chan = get_channel(channelname)
result = []
try:
msg_res = await chan.message_store.get_preview_str(message_id)
result.append(msg_res.to_dict())
result = msg_res.to_dict()
except IndexError:
message = f"Cannot preview msg, id {message_id} probably doesn't exists"
logger.error(message)
result = {'error': message}
except Exception as exc:
result.append({'error': str(exc)})
logger.exception(f"Cannot preview msg {message_id}")
result = {'error': str(exc)}

if ws is not None:
await ws.send_jsonrpcresp(result)
Expand All @@ -211,6 +228,7 @@ async def send_jsonrpcresp(self, message):

async def backport_old_client(request):
ws = RPCWebSocketResponse()
logger.debug("Receiving a request from the shell client (%r)", vars(request))
await ws.prepare(request)
async for msg in ws:
try:
Expand Down
4 changes: 2 additions & 2 deletions pypeman/plugins/tests/test_remoteadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ async def test_search_messages_with_rtext_flt(self, webremoteclient):
assert json_resp['messages'][0]['id'] == self.msg3.uuid

async def test_replay_message(self, webremoteclient):
assert await self.chan.message_store.total() == 4
resp = await webremoteclient.get(f"/channels/{self.chan_name}/messages/{self.msg3.uuid}/replay")
assert resp.status == 200

json_resp = json.loads(await resp.text())
assert len(json_resp) == 1
assert await self.chan.message_store.total() == 5

# Clean
await self.chan.message_store.delete(json_resp[0]["uuid"])
await self.chan.message_store.delete(json_resp["uuid"])

async def test_push_message(self, webremoteclient):
pass # actually not implemented
Expand Down
Loading

0 comments on commit cba086b

Please sign in to comment.