From 829f4f8466906a693193123641a93bfde3b401b3 Mon Sep 17 00:00:00 2001 From: Esteban Galvis Date: Fri, 26 Apr 2024 10:50:56 -0500 Subject: [PATCH] fix(portal): :bug: Initialise room on creation --- gupshup_matrix/example-config.yaml | 2 -- gupshup_matrix/gupshup/api.py | 3 +- gupshup_matrix/portal.py | 51 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/gupshup_matrix/example-config.yaml b/gupshup_matrix/example-config.yaml index 325136d..55de1e6 100644 --- a/gupshup_matrix/example-config.yaml +++ b/gupshup_matrix/example-config.yaml @@ -147,8 +147,6 @@ gupshup: cloud_url: https://api.gupshup.io/wa/api/v1/msg # If the Gupshup API is hosted on Whatsapp cloud, set this to true. is_cloud: false - # Gupshup reaction URL - cloud_api_url: https://api.gupshup.io/wa/api/v1/msg # Path prefix for webhook endpoints. Subpaths are /status and /receive. # Note that the webhook must be put behind a reverse proxy with https. webhook_path: /gupshup diff --git a/gupshup_matrix/gupshup/api.py b/gupshup_matrix/gupshup/api.py index 68efaf0..ead5e02 100644 --- a/gupshup_matrix/gupshup/api.py +++ b/gupshup_matrix/gupshup/api.py @@ -18,7 +18,6 @@ class GupshupClient: def __init__(self, config: Config, loop: asyncio.AbstractEventLoop) -> None: self.base_url = config["gupshup.base_url"] self.read_url = config["gupshup.read_url"] - self.cloud_api_url = config["gupshup.cloud_api_url"] self.cloud_url = config["gupshup.cloud_url"] self.is_cloud = config["gupshup.is_cloud"] self.app_name = config["gupshup.app_name"] @@ -239,6 +238,6 @@ async def send_reaction(self, message_id: str, emoji: str, type: str, data: dict data.pop("headers") data["message"] = json.dumps({"msgId": message_id, "type": type, "emoji": emoji}) - resp = await self.http.post(self.cloud_api_url, data=data, headers=headers) + resp = await self.http.post(self.cloud_url, data=data, headers=headers) response_data = json.loads(await resp.text()) return response_data diff --git a/gupshup_matrix/portal.py b/gupshup_matrix/portal.py index ad12501..0956b86 100644 --- a/gupshup_matrix/portal.py +++ b/gupshup_matrix/portal.py @@ -209,6 +209,14 @@ async def _create_matrix_room(self, source: u.User, info: ChatInfo) -> RoomID: self.mxid, source.mxid, extra_content=self._get_invite_content(puppet) ) + for attempt in range(10): + self.log.debug(f"Attempt {attempt} to set power levels to {source.mxid} logged user") + response = await self.set_member_power_level(source.mxid, 100) + if response: + break + await asyncio.sleep(1) + await self.set_relay_user(source) + return self.mxid async def handle_matrix_leave(self, user: u.User) -> None: @@ -846,3 +854,46 @@ async def handle_matrix_redaction( return await DBReaction.delete_by_event_mxid(message.event_mxid, self.mxid, user.mxid) + + async def get_joined_users(self) -> List[UserID] | None: + """get a list of all users in the room + + Returns + ------- + A list of User objects. + + """ + try: + members = await self.main_intent.get_joined_members(room_id=self.mxid) + except Exception as e: + self.log.error(e) + return + + return members.keys() + + async def set_member_power_level(self, member: UserID, power_level: int) -> None: + """It sets the power level of a member in the room + + Parameters + ---------- + member : User + The user ID of the member. + power_level : int + The power level of the member. + + """ + room_members = await self.get_joined_users() + if member not in room_members: + self.log.warning( + f"Unable to set power level for {member} in {self.mxid}, user not in room" + ) + return False + + portal_pl = await self.main_intent.get_power_levels(room_id=self.mxid) + portal_pl.users[member] = power_level + await self.main_intent.set_power_levels( + room_id=self.mxid, + content=portal_pl, + ) + + return True