Skip to content

Commit 29bbbc9

Browse files
Raise max timeouts to avoid failing on transient dns issues. Allow accepting an invite with provided creds (#11)
1 parent 841ae27 commit 29bbbc9

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

fractal_database_matrix/operations.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async def put_state(
4444

4545
access_token, homeserver_url, _ = creds
4646

47-
async with MatrixClient(homeserver_url, access_token) as client:
47+
async with MatrixClient(homeserver_url, access_token, max_timeouts=15) as client:
4848
res = await client.room_put_state(
4949
room_id,
5050
state_type,
@@ -86,7 +86,7 @@ async def create_room(
8686
if not any([matrix_id.split("@")[1].islower() for matrix_id in invite]):
8787
raise Exception("Matrix IDs must be lowercase")
8888

89-
async with MatrixClient(homeserver_url, access_token) as client:
89+
async with MatrixClient(homeserver_url, access_token, max_timeouts=15) as client:
9090
res = await client.room_create(
9191
name=name,
9292
space=space,
@@ -117,7 +117,7 @@ async def add_subspace(
117117

118118
access_token, homeserver_url, _ = creds
119119

120-
async with MatrixClient(homeserver_url, access_token) as client:
120+
async with MatrixClient(homeserver_url, access_token, max_timeouts=15) as client:
121121
res = await client.room_put_state(
122122
parent_room_id,
123123
"m.space.child",
@@ -132,16 +132,26 @@ async def add_subspace(
132132
% (child_room_id, parent_room_id)
133133
)
134134

135-
async def accept_invite_as_user(self, room_id: str, homeserver_url: str):
136-
creds = AuthenticatedController.get_creds()
137-
if not creds:
138-
raise Exception("You must be logged in to accept an invite to a space")
139-
140-
access_token, homeserver_url, user_matrix_id = creds
135+
async def accept_invite_as_user(
136+
self, room_id: str, homeserver_url: str, matrix_creds: Optional[tuple[str, str]] = None
137+
):
138+
"""
139+
Args:
140+
room_id (str): The room ID to accept the invite to
141+
homeserver_url (str): The homeserver URL
142+
matrix_creds (tuple[matrix_id, access_token], optional): The Matrix credentials to use.
143+
Defaults to logged in user's credentials.
144+
"""
145+
if not matrix_creds:
146+
creds = AuthenticatedController.get_creds()
147+
if not creds:
148+
raise Exception("You must be logged in to accept an invite to a space")
149+
access_token, homeserver_url, user_matrix_id = creds
150+
else:
151+
user_matrix_id, access_token = matrix_creds
141152

142153
async with MatrixClient(
143-
homeserver_url=homeserver_url,
144-
access_token=access_token,
154+
homeserver_url=homeserver_url, access_token=access_token, max_timeouts=15
145155
) as client:
146156
logger.info("Accepting invite for %s as %s" % (room_id, user_matrix_id))
147157
await client.join_room(room_id)
@@ -154,13 +164,12 @@ async def accept_invite_as_device(
154164
async with MatrixClient(
155165
homeserver_url=homeserver_url,
156166
access_token=device_creds.access_token,
167+
max_timeouts=15,
157168
) as client:
158169
logger.info("Accepting invite for %s as %s" % (room_id, device_matrix_id))
159170
await client.join_room(room_id)
160171

161172
async def invite_user(self, matrix_id: str, room_id: str) -> None:
162-
from fractal.cli.controllers.auth import AuthenticatedController
163-
164173
# FIXME: Once user has accounts on many homeservers, we need to strip the
165174
# host off of the room id and try to find credentials that match that host
166175
creds = AuthenticatedController.get_creds()
@@ -172,6 +181,7 @@ async def invite_user(self, matrix_id: str, room_id: str) -> None:
172181
async with MatrixClient(
173182
homeserver_url=homeserver_url,
174183
access_token=access_token,
184+
max_timeouts=15,
175185
) as client:
176186
logger.info("Inviting %s to %s" % (matrix_id, room_id))
177187
await client.invite(user_id=matrix_id, room_id=room_id, admin=True)
@@ -189,6 +199,7 @@ async def register_device_account(
189199
async with MatrixClient(
190200
homeserver_url=homeserver_url,
191201
access_token=access_token,
202+
max_timeouts=15,
192203
) as client:
193204
registration_token = await client.generate_registration_token()
194205
await client.whoami()
@@ -218,6 +229,7 @@ async def set_display_name(
218229
async with MatrixClient(
219230
homeserver_url=homeserver_url,
220231
access_token=creds.access_token,
232+
max_timeouts=15,
221233
) as client:
222234
await client.set_displayname(display_name)
223235

0 commit comments

Comments
 (0)