Add updated_at and created_at to Playlist model

Introduces created_at and updated_at fields to the Playlist model for tracking creation and modification times. Refactors room user removal logic to handle owner transfer and participant count updates more robustly. Updates room query logic for more accurate filtering by category and status. Adds migration stubs for updated_at column in room playlists.
This commit is contained in:
咕谷酱
2025-08-24 00:18:20 +08:00
committed by MingxuanGame
parent 4c653552a1
commit ea575de250
6 changed files with 208 additions and 46 deletions

View File

@@ -50,14 +50,26 @@ async def get_all_rooms(
current_user: User = Security(get_current_user, scopes=["public"]),
):
resp_list: list[RoomResp] = []
where_clauses: list[ColumnElement[bool]] = [col(Room.category) == category]
if category == RoomCategory.REALTIME:
db_category = RoomCategory.NORMAL # 实际查询 normal
else:
db_category = category
where_clauses: list[ColumnElement[bool]] = [col(Room.category) == db_category]
now = utcnow()
if status is not None:
where_clauses.append(col(Room.status) == status)
print(mode, category, status, current_user.id)
if mode == "open":
where_clauses.append((col(Room.ends_at).is_(None)) | (col(Room.ends_at) > now.replace(tzinfo=UTC)))
if category == RoomCategory.REALTIME:
where_clauses.append(col(Room.id).in_(MultiplayerHubs.rooms.keys()))
# 修改为新的查询逻辑:状态为 idle 或 playingstarts_at 不为空ends_at 为空
where_clauses.extend([
col(Room.status).in_([RoomStatus.IDLE, RoomStatus.PLAYING]),
col(Room.starts_at).is_not(None),
col(Room.ends_at).is_(None)
])
#if category == RoomCategory.REALTIME:
# where_clauses.append(col(Room.id).in_(MultiplayerHubs.rooms.keys()))
if mode == "participated":
where_clauses.append(
exists().where(
@@ -65,11 +77,14 @@ async def get_all_rooms(
col(RoomParticipatedUser.user_id) == current_user.id,
)
)
if mode == "owned":
where_clauses.append(col(Room.host_id) == current_user.id)
if mode == "ended":
where_clauses.append((col(Room.ends_at).is_not(None)) & (col(Room.ends_at) < now.replace(tzinfo=UTC)))
# 使用 select 指定需要的字段,对应您的 SQL 语句
db_rooms = (
(
await db.exec(
@@ -81,7 +96,7 @@ async def get_all_rooms(
.unique()
.all()
)
print("Retrieved rooms:", db_rooms)
for room in db_rooms:
resp = await RoomResp.from_db(room, db)
if category == RoomCategory.REALTIME:
@@ -409,4 +424,4 @@ async def get_room_events(
playlist_items=playlist_items_resps,
room=room_resp,
user=user_resps,
)
)