radames commited on
Commit
e0e26e9
1 Parent(s): d982737

room users api v2

Browse files
Files changed (1) hide show
  1. stablediffusion-infinity/app.py +5 -5
stablediffusion-infinity/app.py CHANGED
@@ -261,9 +261,10 @@ def generateAuthToken():
261
  raise Exception(response.status_code, response.text)
262
 
263
 
264
- def get_room_count(room_id: str, jwtToken: str = ''):
265
  response = requests.get(
266
- f"https://liveblocks.net/api/v1/room/{room_id}/users", headers={"Authorization": f"Bearer {jwtToken}", "Content-Type": "application/json"})
 
267
  if response.status_code == 200:
268
  res = response.json()
269
  if "data" in res:
@@ -274,16 +275,15 @@ def get_room_count(room_id: str, jwtToken: str = ''):
274
 
275
 
276
  @ app.on_event("startup")
277
- @ repeat_every(seconds=120)
278
  async def sync_rooms():
279
- # print("Syncing rooms")
280
  try:
281
  jwtToken = generateAuthToken()
282
  for db in get_db():
283
  rooms = db.execute("SELECT * FROM rooms").fetchall()
284
  for row in rooms:
285
  room_id = row["room_id"]
286
- users_count = get_room_count(room_id, jwtToken)
287
  cursor = db.cursor()
288
  cursor.execute(
289
  "UPDATE rooms SET users_count = ? WHERE room_id = ?", (users_count, room_id))
 
261
  raise Exception(response.status_code, response.text)
262
 
263
 
264
+ def get_room_count(room_id: str):
265
  response = requests.get(
266
+ f"https://api.liveblocks.io/v2/rooms/{room_id}/active_users",
267
+ headers={"Authorization": f"Bearer {LIVEBLOCKS_SECRET}", "Content-Type": "application/json"})
268
  if response.status_code == 200:
269
  res = response.json()
270
  if "data" in res:
 
275
 
276
 
277
  @ app.on_event("startup")
278
+ @ repeat_every(seconds=100)
279
  async def sync_rooms():
 
280
  try:
281
  jwtToken = generateAuthToken()
282
  for db in get_db():
283
  rooms = db.execute("SELECT * FROM rooms").fetchall()
284
  for row in rooms:
285
  room_id = row["room_id"]
286
+ users_count = get_room_count(room_id)
287
  cursor = db.cursor()
288
  cursor.execute(
289
  "UPDATE rooms SET users_count = ? WHERE room_id = ?", (users_count, room_id))