11
11
from app .models .event import Event
12
12
from app .models .microlocation import Microlocation
13
13
from app .models .user import User
14
+ from app .models .video_stream import VideoStream
14
15
from app .settings import get_settings
15
16
16
17
logger = logging .getLogger (__name__ )
@@ -142,6 +143,53 @@ def get_token(
142
143
143
144
return self .register (user , event )
144
145
146
+ def get_token_virtual_room (
147
+ self ,
148
+ user : User ,
149
+ event : Optional [Event ] = None ,
150
+ retried = False ,
151
+ videoStream : Optional [VideoStream ] = None ,
152
+ ):
153
+ if user .rocket_chat_token :
154
+ res = requests .post (self .login_url , json = dict (resume = user .rocket_chat_token ))
155
+
156
+ data = res .json ()
157
+ if res .status_code == 200 :
158
+ if event :
159
+ self .add_in_room_virtual_room (
160
+ event , data ['data' ]['userId' ], videoStream
161
+ )
162
+ return dict (method = 'resumed' , token = user .rocket_chat_token , res = data )
163
+ elif res .status_code == 401 :
164
+ # Token Expired. Login again
165
+
166
+ try :
167
+ return self .login (user , event , 'login' )
168
+ except RocketChatException as rce :
169
+ if (
170
+ not retried
171
+ and rce .response is not None
172
+ and rce .response .status_code == 401
173
+ ):
174
+ # Invalid credentials stored. Reset credentials and retry
175
+ # If we have already retried, give up
176
+ user .rocket_chat_token = None
177
+ db .session .add (user )
178
+ db .session .commit ()
179
+ return self .get_token_virtual_room (user , event , retried = True )
180
+ else :
181
+ raise rce
182
+ else :
183
+ # Unhandled Case
184
+ logger .error ('Error while rocket chat resume or login: %s' , data )
185
+ raise RocketChatException (
186
+ 'Error while resume or logging in' , response = res
187
+ )
188
+ else :
189
+ # No token. Try creating profile, else login
190
+
191
+ return self .register (user , event )
192
+
145
193
def check_or_create_bot (self ):
146
194
147
195
bot_user , _ = get_or_create (
@@ -184,6 +232,40 @@ def create_room(self, event: Event, microlocation: Optional[Microlocation], data
184
232
db .session .add (event )
185
233
db .session .commit ()
186
234
235
+ def create_room_virtual_room (
236
+ self , event : Event , videoStream : Optional [VideoStream ], data
237
+ ):
238
+ bot_token = data ['token' ]
239
+ bot_id = data ['res' ]['data' ]['userId' ]
240
+ if videoStream :
241
+ chat_room_name = videoStream .chat_room_name
242
+ else :
243
+ chat_room_name = event .chat_room_name
244
+
245
+ res = requests .post (
246
+ self .api_url + '/api/v1/groups.create' ,
247
+ json = dict (
248
+ name = chat_room_name ,
249
+ members = [bot_id ],
250
+ ),
251
+ headers = {
252
+ 'X-Auth-Token' : bot_token ,
253
+ 'X-User-Id' : bot_id ,
254
+ },
255
+ )
256
+ if not res .status_code == 200 :
257
+ logger .error ('Error while creating room : %s' , res .json ())
258
+ raise RocketChatException ('Error while creating room' , response = res )
259
+ else :
260
+ group_data = res .json ()
261
+ if videoStream :
262
+ videoStream .chat_room_id = group_data ['group' ]['_id' ]
263
+ db .session .add (videoStream )
264
+ else :
265
+ event .chat_room_id = group_data ['group' ]['_id' ]
266
+ db .session .add (event )
267
+ db .session .commit ()
268
+
187
269
def add_in_room (
188
270
self , event : Event , rocket_user_id , microlocation : Optional [Microlocation ] = None
189
271
):
@@ -215,6 +297,36 @@ def add_in_room(
215
297
logger .error ('Error while adding user : %s' , res .json ())
216
298
raise RocketChatException ('Error while adding user' , response = res )
217
299
300
+ def add_in_room_virtual_room (
301
+ self , event : Event , rocket_user_id , videoStream : Optional [VideoStream ] = None
302
+ ):
303
+ bot = self .check_or_create_bot ()
304
+ data = self .get_token_virtual_room (bot , videoStream = videoStream )
305
+ if (not event .chat_room_id ) or (videoStream and not videoStream .chat_room_id ):
306
+ self .create_room_virtual_room (event = event , videoStream = videoStream , data = data )
307
+
308
+ if videoStream is not None :
309
+ chat_room_id = videoStream .chat_room_id
310
+ else :
311
+ chat_room_id = event .chat_room_id
312
+
313
+ bot_token = data ['token' ]
314
+ bot_id = data ['res' ]['data' ]['userId' ]
315
+ room_info = {'roomId' : chat_room_id , 'userId' : rocket_user_id }
316
+
317
+ res = requests .post (
318
+ self .api_url + '/api/v1/groups.invite' ,
319
+ json = room_info ,
320
+ headers = {
321
+ 'X-Auth-Token' : bot_token ,
322
+ 'X-User-Id' : bot_id ,
323
+ },
324
+ )
325
+
326
+ if res .status_code != 200 :
327
+ logger .error ('Error while adding user : %s' , res .json ())
328
+ raise RocketChatException ('Error while adding user' , response = res )
329
+
218
330
219
331
def generate_pass (size = 10 , chars = string .ascii_lowercase + string .digits ):
220
332
return '' .join (random .choice (chars ) for _ in range (size ))
@@ -235,6 +347,21 @@ def get_rocket_chat_token(
235
347
return rocket_chat .get_token (user , event , microlocation = microlocation )
236
348
237
349
350
+ def get_rocket_chat_token_virtual_room (
351
+ user : User ,
352
+ event : Optional [Event ] = None ,
353
+ videoStream : Optional [VideoStream ] = None ,
354
+ ):
355
+ settings = get_settings ()
356
+ if not (api_url := settings ['rocket_chat_url' ]):
357
+ raise RocketChatException (
358
+ 'Rocket Chat Integration is not enabled' , RocketChatException .CODES .DISABLED
359
+ )
360
+
361
+ rocket_chat = RocketChat (api_url )
362
+ return rocket_chat .get_token_virtual_room (user , event , videoStream = videoStream )
363
+
364
+
238
365
def rename_rocketchat_room (event : Event ):
239
366
settings = get_settings ()
240
367
if not event .chat_room_id or not (api_url := settings ['rocket_chat_url' ]):
0 commit comments