Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(portal): 🐛 Initialise room on creation #53

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions gupshup_matrix/example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions gupshup_matrix/gupshup/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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
51 changes: 51 additions & 0 deletions gupshup_matrix/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Loading