Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions meeseeks/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class MeeseeksCore(Generic[_T]):
_apps: list = []

def __init__(self) -> None:
self._ctx: Context | None = None
self._request: str = ''
self._websocket: WebSocketClientProtocol = WebSocketClientProtocol
self._websocket_protocol: str = 'wss' if self._url.scheme == 'https' else 'ws'
Expand Down Expand Up @@ -86,13 +85,13 @@ async def login(self) -> bool:
serializer: ContextSerializer = ContextSerializer(
raw_context, raw_context['msg'], raw_context['id'],
)
self._ctx = serializer.serialize()
ctx: Context = serializer.serialize()
except (KeyError, ValueError, ):
continue

if isinstance(self._ctx, LoginCtx):
self._user_id = self._ctx.user_id
self._token = self._ctx.token
if isinstance(ctx, LoginCtx):
self._user_id = ctx.user_id
self._token = ctx.token
self._headers.update({
'X-Auth-Token': self._token,
'X-User-Id': self._user_id,
Expand Down Expand Up @@ -137,10 +136,10 @@ def _init_apps(self) -> list[_T]:

return app_instances

async def loop(self) -> None:
"""Method is intended for calling in endless loop to process Rocket.Chat callbacks. """
async def message_handler(self, message: str) -> None:
"""Serializes message context and run processing for each app. """

raw_context: WebSocketClientProtocol = json.loads(await self._websocket.recv())
raw_context: WebSocketClientProtocol = json.loads(message)
if raw_context.get('msg') == 'ping':
await self._rtapi.pong()
return None
Expand All @@ -153,23 +152,23 @@ async def loop(self) -> None:
return None

try:
self._ctx = serializer.serialize()
ctx: Context = serializer.serialize()
except SerializerError:
return None

if isinstance(self._ctx, ChangedRoomMessageCtx):
if isinstance(ctx, ChangedRoomMessageCtx):
exc_counter = 0
for app in self._apps:
try:
await app.process(self._ctx)
await app.process(ctx)
except AbortCommandExecution:
await self._restapi.write_msg(_ACCESS_DENIED_MSG, self._ctx.room.id)
await self._restapi.write_msg(_ACCESS_DENIED_MSG, ctx.room.id)
break
except CommandDoesNotExist:
exc_counter += 1

if exc_counter == len(self._apps):
await self._restapi.write_msg(_COMMAND_DOES_NOT_EXIST, self._ctx.room.id)
await self._restapi.write_msg(_COMMAND_DOES_NOT_EXIST, ctx.room.id)

async def setup(self) -> None:
"""Add functional in app after login. """
Expand All @@ -193,9 +192,9 @@ async def run(self) -> None:
self.check_app_name(app)
await app.setup()

while True:
async for message in websocket:
try:
await self.loop()
asyncio.create_task(self.message_handler(message))
except ClientResponseError as exc:
LOGGER.error(exc)
except ConnectionClosedOK:
Expand Down