Skip to content

Commit

Permalink
Merge pull request #27 from chatbot-smooth/loading-room-constants
Browse files Browse the repository at this point in the history
New behavior added for loading Jinja environment constants by room
  • Loading branch information
bramenn committed Apr 12, 2023
2 parents d6d8335 + 6bc4b81 commit feb05d8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
55 changes: 44 additions & 11 deletions menuflow/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,47 @@ def lock_room(self, room_id: RoomID):
self.log.debug(f"LOCKING ROOM... {room_id}")
self.LOCKED_ROOMS.add(room_id)

async def load_all_room_constants(self):
"""This function loads room constants for joined rooms in a Matrix chat using Python.
Returns
-------
If there are no joined rooms, the function will return nothing.
If there are joined rooms, the function will execute the code block and return nothing.
"""

joined_room = await self.get_joined_rooms()

if not joined_room:
return

self.log.debug("Loading rooms constants ...")

for joined_room in joined_room:
await self.load_room_constants(room_id=joined_room)

async def load_room_constants(self, room_id: RoomID):
"""This function loads constants for a given room and sets variables if they do not exist.
Parameters
----------
room_id : RoomID
The ID of the Matrix room that the constants are being loaded for.
"""

room = await Room.get_by_room_id(room_id=room_id)

room.config = self.config
room.matrix_client = self

if not await room.get_variable("customer_room_id"):
await room.set_variable("customer_room_id", room_id)

if not await room.get_variable("bot_mxid"):
await room.set_variable("bot_mxid", self.mxid)

async def handle_join(self, evt: StrippedStateEvent):
if evt.room_id in self.LOCKED_ROOMS:
self.log.debug(f"Ignoring menu request in {evt.room_id} Menu locked")
Expand All @@ -119,17 +160,9 @@ async def handle_join(self, evt: StrippedStateEvent):
self.log.debug(f"{evt.state_key} ACCEPTED -- EVENT JOIN ... {evt.room_id}")
self.lock_room(evt.room_id)

try:
room = await Room.get_by_room_id(room_id=evt.room_id)
room.config = self.config
room.matrix_client = self
if not await room.get_variable("bot_mxid"):
await room.set_variable("bot_mxid", self.mxid)
await room.set_variable("customer_room_id", evt.room_id)
except Exception as e:
self.log.exception(e)
self.unlock_room(evt.room_id)
return
room = await Room.get_by_room_id(room_id=evt.room_id)

await self.load_room_constants(evt.room_id)

await self.algorithm(room=room)

Expand Down
1 change: 1 addition & 0 deletions menuflow/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def postinit(self) -> None:
self.started = False
self.sync_ok = True
self.matrix_handler: MatrixHandler = self._make_client()
asyncio.create_task(self.matrix_handler.load_all_room_constants())
# if self.enable_crypto:
# self._prepare_crypto()
# else:
Expand Down

0 comments on commit feb05d8

Please sign in to comment.