@@ -51,6 +51,7 @@ def __init__(
5151 client_session : ClientSession = None ,
5252 child : bool = False ,
5353 real_user : bool = False ,
54+ real_user_as_token : bool = False ,
5455 bridge_name : str | None = None ,
5556 default_retry_count : int = None ,
5657 loop : asyncio .AbstractEventLoop | None = None ,
@@ -66,6 +67,7 @@ def __init__(
6667 client_session: The aiohttp ClientSession to use.
6768 child: Whether or not this is instance is a child of another AppServiceAPI.
6869 real_user: Whether or not this is a real (non-appservice-managed) user.
70+ real_user_as_token: Whether this real user is actually using another ``as_token``.
6971 bridge_name: The name of the bridge to put in the ``fi.mau.double_puppet_source`` field
7072 in outgoing message events sent through real users.
7173 """
@@ -85,6 +87,7 @@ def __init__(
8587 self ._bot_intent = None
8688 self .state_store = state_store
8789 self .is_real_user = real_user
90+ self .is_real_user_as_token = real_user_as_token
8891 self .bridge_name = bridge_name
8992
9093 if not child :
@@ -113,7 +116,9 @@ def user(self, user: UserID) -> ChildAppServiceAPI:
113116 self .children [user ] = child
114117 return child
115118
116- def real_user (self , mxid : UserID , token : str , base_url : URL | None = None ) -> AppServiceAPI :
119+ def real_user (
120+ self , mxid : UserID , token : str , base_url : URL | None = None , as_token : bool = False
121+ ) -> AppServiceAPI :
117122 """
118123 Get the AppServiceAPI for a real (non-appservice-managed) Matrix user.
119124
@@ -122,6 +127,8 @@ def real_user(self, mxid: UserID, token: str, base_url: URL | None = None) -> Ap
122127 token: The access token for the user.
123128 base_url: The base URL of the homeserver client-server API to use. Defaults to the
124129 appservice homeserver URL.
130+ as_token: Whether the token is actually an as_token
131+ (meaning the ``user_id`` query parameter needs to be used).
125132
126133 Returns:
127134 The AppServiceAPI object for the user.
@@ -136,6 +143,7 @@ def real_user(self, mxid: UserID, token: str, base_url: URL | None = None) -> Ap
136143 child = self .real_users [mxid ]
137144 child .base_url = base_url or child .base_url
138145 child .token = token or child .token
146+ child .is_real_user_as_token = as_token
139147 except KeyError :
140148 child = type (self )(
141149 base_url = base_url or self .base_url ,
@@ -145,6 +153,7 @@ def real_user(self, mxid: UserID, token: str, base_url: URL | None = None) -> Ap
145153 state_store = self .state_store ,
146154 client_session = self .session ,
147155 real_user = True ,
156+ real_user_as_token = as_token ,
148157 bridge_name = self .bridge_name ,
149158 default_retry_count = self .default_retry_count ,
150159 )
@@ -163,7 +172,11 @@ def bot_intent(self) -> as_api.IntentAPI:
163172 return self ._bot_intent
164173
165174 def intent (
166- self , user : UserID = None , token : str | None = None , base_url : str | None = None
175+ self ,
176+ user : UserID = None ,
177+ token : str | None = None ,
178+ base_url : str | None = None ,
179+ real_user_as_token : bool = False ,
167180 ) -> as_api .IntentAPI :
168181 """
169182 Get the intent API of a child user.
@@ -173,6 +186,8 @@ def intent(
173186 token: The access token to use. Only applicable for non-appservice-managed users.
174187 base_url: The base URL of the homeserver client-server API to use. Only applicable for
175188 non-appservice users. Defaults to the appservice homeserver URL.
189+ real_user_as_token: When providing a token, whether it's actually another as_token
190+ (meaning the ``user_id`` query parameter needs to be used).
176191
177192 Returns:
178193 The IntentAPI object for the given user.
@@ -184,7 +199,10 @@ def intent(
184199 raise ValueError ("Can't get child intent of real user" )
185200 if token :
186201 return as_api .IntentAPI (
187- user , self .real_user (user , token , base_url ), self .bot_intent (), self .state_store
202+ user ,
203+ self .real_user (user , token , base_url , as_token = real_user_as_token ),
204+ self .bot_intent (),
205+ self .state_store ,
188206 )
189207 return as_api .IntentAPI (user , self .user (user ), self .bot_intent (), self .state_store )
190208
@@ -229,7 +247,7 @@ def request(
229247 if isinstance (timestamp , datetime ):
230248 timestamp = int (timestamp .replace (tzinfo = timezone .utc ).timestamp () * 1000 )
231249 query_params ["ts" ] = timestamp
232- if not self .is_real_user :
250+ if not self .is_real_user or self . is_real_user_as_token :
233251 query_params ["user_id" ] = self .identity or self .bot_mxid
234252
235253 return super ().request (
0 commit comments