@@ -44,6 +44,15 @@ async def put_state(
44
44
45
45
access_token , homeserver_url , _ = creds
46
46
47
+ if homeserver_url != channel .homeserver .url :
48
+ raise Exception (
49
+ f"You are logged into the wrong homeserver ({ homeserver_url } ). You must be logged into the homeserver { channel .homeserver .url } "
50
+ )
51
+ else :
52
+ # credentials will work with the local URL as well
53
+ # prefer the local URL if it exists
54
+ homeserver_url = channel .homeserver .local_url or homeserver_url
55
+
47
56
async with MatrixClient (homeserver_url , access_token , max_timeouts = 15 ) as client :
48
57
res = await client .room_put_state (
49
58
room_id ,
@@ -81,6 +90,13 @@ async def create_room(
81
90
82
91
access_token , homeserver_url , _ = creds
83
92
93
+ if homeserver_url != channel .homeserver .url :
94
+ raise Exception ("You must be logged into the correct homeserver" )
95
+ else :
96
+ # credentials will work with the local URL as well
97
+ # prefer the local URL if it exists
98
+ homeserver_url = channel .homeserver .local_url or homeserver_url
99
+
84
100
# verify that matrix IDs passed in invite are all lowercase
85
101
if invite :
86
102
if not any ([matrix_id .split ("@" )[1 ].islower () for matrix_id in invite ]):
@@ -117,6 +133,13 @@ async def add_subspace(
117
133
118
134
access_token , homeserver_url , _ = creds
119
135
136
+ if homeserver_url != channel .homeserver .url :
137
+ raise Exception ("You must be logged into the correct homeserver" )
138
+ else :
139
+ # credentials will work with the local URL as well
140
+ # prefer the local URL if it exists
141
+ homeserver_url = channel .homeserver .local_url or homeserver_url
142
+
120
143
async with MatrixClient (homeserver_url , access_token , max_timeouts = 15 ) as client :
121
144
res = await client .room_put_state (
122
145
parent_room_id ,
@@ -133,7 +156,10 @@ async def add_subspace(
133
156
)
134
157
135
158
async def accept_invite_as_user (
136
- self , room_id : str , homeserver_url : str , matrix_creds : Optional [tuple [str , str ]] = None
159
+ self ,
160
+ room_id : str ,
161
+ channel : "MatrixReplicationChannel" ,
162
+ matrix_creds : Optional [tuple [str , str ]] = None ,
137
163
):
138
164
"""
139
165
Args:
@@ -150,16 +176,27 @@ async def accept_invite_as_user(
150
176
else :
151
177
user_matrix_id , access_token = matrix_creds
152
178
179
+ if homeserver_url != channel .homeserver .url :
180
+ raise Exception (
181
+ f"You are currently logged into { homeserver_url } not { channel .homeserver .url } "
182
+ )
183
+ else :
184
+ # credentials will work with the local URL as well
185
+ # prefer the local URL if it exists
186
+ homeserver_url = channel .homeserver .local_url or homeserver_url
187
+
153
188
async with MatrixClient (
154
189
homeserver_url = homeserver_url , access_token = access_token , max_timeouts = 15
155
190
) as client :
156
191
logger .info ("Accepting invite for %s as %s" % (room_id , user_matrix_id ))
157
192
await client .join_room (room_id )
158
193
159
194
async def accept_invite_as_device (
160
- self , device_creds : "MatrixCredentials" , room_id : str , homeserver_url : str
195
+ self , device_creds : "MatrixCredentials" , room_id : str , channel : "MatrixReplicationChannel"
161
196
):
162
197
device_matrix_id = device_creds .matrix_id
198
+ homeserver_url = channel .homeserver .local_url or channel .homeserver .url
199
+
163
200
# accept invite on behalf of device
164
201
async with MatrixClient (
165
202
homeserver_url = homeserver_url ,
@@ -169,7 +206,9 @@ async def accept_invite_as_device(
169
206
logger .info ("Accepting invite for %s as %s" % (room_id , device_matrix_id ))
170
207
await client .join_room (room_id )
171
208
172
- async def invite_user (self , matrix_id : str , room_id : str ) -> None :
209
+ async def invite_user (
210
+ self , matrix_id : str , channel : "MatrixReplicationChannel" , room_id : str
211
+ ) -> None :
173
212
# FIXME: Once user has accounts on many homeservers, we need to strip the
174
213
# host off of the room id and try to find credentials that match that host
175
214
creds = AuthenticatedController .get_creds ()
@@ -178,6 +217,15 @@ async def invite_user(self, matrix_id: str, room_id: str) -> None:
178
217
else :
179
218
raise Exception ("You must be logged in to Matrix to invite a device" )
180
219
220
+ if homeserver_url != channel .homeserver .url :
221
+ raise Exception (
222
+ f"You are currently logged into { homeserver_url } not { channel .homeserver .url } "
223
+ )
224
+ else :
225
+ # credentials will work with the local URL as well
226
+ # prefer the local URL if it exists
227
+ homeserver_url = channel .homeserver .local_url or homeserver_url
228
+
181
229
async with MatrixClient (
182
230
homeserver_url = homeserver_url ,
183
231
access_token = access_token ,
@@ -216,7 +264,7 @@ async def register_device_account(
216
264
217
265
async def set_display_name (
218
266
self ,
219
- homeserver_url : str ,
267
+ channel : "MatrixReplicationChannel" ,
220
268
creds : "MatrixCredentials" ,
221
269
display_name : str ,
222
270
owner_matrix_id : Optional [str ] = None ,
@@ -226,6 +274,8 @@ async def set_display_name(
226
274
owner_username = owner_matrix_id .split ("@" )[1 ].split (":" )[0 ]
227
275
display_name = f"{ owner_username } 's { display_name } "
228
276
277
+ homeserver_url = channel .homeserver .local_url or channel .homeserver .url
278
+
229
279
async with MatrixClient (
230
280
homeserver_url = homeserver_url ,
231
281
access_token = creds .access_token ,
@@ -293,7 +343,7 @@ async def run(self, operation: "DurableOperation") -> dict[str, str]:
293
343
async for account in membership .device .matrixcredentials_set .filter (
294
344
homeserver = channel .homeserver
295
345
):
296
- await self .accept_invite_as_device (account , room_id , channel . homeserver . url )
346
+ await self .accept_invite_as_device (account , room_id , channel )
297
347
298
348
logger .info ("Successfully created Matrix Room for %s" % name )
299
349
return {metadata_label : room_id }
@@ -545,7 +595,7 @@ async def run(self, operation: "DurableOperation") -> None:
545
595
raise Exception (f"Failed to find room id in channel metadata for { metadata_label } " )
546
596
547
597
try :
548
- await self .invite_user (device_creds .matrix_id , room_id )
598
+ await self .invite_user (device_creds .matrix_id , channel , room_id )
549
599
except Exception as e :
550
600
# if the device is already in the room, no need to accept the invite
551
601
if "is already in the room" in str (e ):
@@ -594,7 +644,7 @@ async def run(self, operation: "DurableOperation") -> None:
594
644
% (metadata_label , room_id , device_creds .matrix_id )
595
645
)
596
646
# accept invite on behalf of device
597
- await self .accept_invite_as_device (device_creds , room_id , channel . homeserver . url )
647
+ await self .accept_invite_as_device (device_creds , room_id , channel )
598
648
logger .info ("Device has successfully joined space %s for channel %s" % (room_id , channel ))
599
649
600
650
return None
@@ -698,9 +748,7 @@ async def run(self, operation: "DurableOperation") -> None:
698
748
raise Exception (f"Failed to find device credentials for { membership .device } " )
699
749
700
750
# accept invite on behalf of device
701
- await self .accept_invite_as_device (
702
- device_creds , channel .device_space , channel .homeserver .url
703
- )
751
+ await self .accept_invite_as_device (device_creds , channel .device_space , channel )
704
752
logger .info (
705
753
"Device has successfully joined the devices subspace for channel %s" % channel
706
754
)
@@ -753,7 +801,7 @@ async def run(self, operation: "DurableOperation") -> None:
753
801
raise Exception (f"Failed to find device credentials for { membership .device } " )
754
802
755
803
try :
756
- await self .invite_user (device_creds .matrix_id , channel .device_space )
804
+ await self .invite_user (device_creds .matrix_id , channel , channel .device_space )
757
805
except Exception as e :
758
806
# if the device is already in the room, no need to accept the invite
759
807
if "is already in the room" in str (e ):
@@ -1115,7 +1163,7 @@ async def run(self, operation: DurableOperation) -> None:
1115
1163
)
1116
1164
1117
1165
await self .set_display_name (
1118
- channel . homeserver . url , device_creds , display_name , owner_matrix_id = owner_matrix_id
1166
+ channel , device_creds , display_name , owner_matrix_id = owner_matrix_id
1119
1167
)
1120
1168
1121
1169
@@ -1213,7 +1261,7 @@ async def run(self, operation: "DurableOperation") -> None:
1213
1261
% (room_id_label , user_matrix_id , channel )
1214
1262
)
1215
1263
try :
1216
- await self .accept_invite_as_user (room_id , homeserver_url = channel . homeserver . url )
1264
+ await self .accept_invite_as_user (room_id , channel = channel )
1217
1265
except Exception as e :
1218
1266
# if the user is already in the room, no need to accept the invite
1219
1267
if "is already in the room" in str (e ):
@@ -1297,10 +1345,10 @@ async def run(self, operation: "DurableOperation") -> None:
1297
1345
) # type: ignore
1298
1346
1299
1347
# fetch channel in order to get room_id for the group to invite user to
1300
- channel : (
1301
- "MatrixReplicationChannel"
1302
- ) = await operation . channel_type . model_class (). objects .aget (
1303
- pk = operation .channel_id
1348
+ channel : "MatrixReplicationChannel" = (
1349
+ await operation . channel_type . model_class ()
1350
+ . objects .select_related ( "homeserver" )
1351
+ . aget ( pk = operation .channel_id )
1304
1352
) # type: ignore
1305
1353
1306
1354
user_matrix_id = membership .user .matrix_id
@@ -1317,7 +1365,7 @@ async def run(self, operation: "DurableOperation") -> None:
1317
1365
% (room_id_label , user_matrix_id , channel )
1318
1366
)
1319
1367
try :
1320
- await self .invite_user (user_matrix_id , room_id )
1368
+ await self .invite_user (user_matrix_id , channel , room_id )
1321
1369
except Exception as e :
1322
1370
# if the user is already in the room, no need to accept the invite
1323
1371
if "is already in the room" in str (e ):
0 commit comments