Lab 2: fix pagination and finalize report
This commit is contained in:
@@ -393,12 +393,11 @@ class Store:
|
||||
JOIN chat_members cm ON cm.chat_id = c.id
|
||||
WHERE cm.user_id = %s
|
||||
ORDER BY c.created_at ASC, c.id ASC
|
||||
LIMIT %s
|
||||
""",
|
||||
(user_id, limit + 1),
|
||||
(user_id,),
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
rows = [
|
||||
items = [
|
||||
{
|
||||
"id": str(row[0]),
|
||||
"title": row[1],
|
||||
@@ -408,39 +407,45 @@ class Store:
|
||||
for row in rows
|
||||
]
|
||||
|
||||
start_index = 0
|
||||
if cursor:
|
||||
cursor_parts = cursor.split("|", 1)
|
||||
if len(cursor_parts) != 2:
|
||||
prefix = f"user:{user_id}|"
|
||||
if not cursor.startswith(prefix):
|
||||
raise ApiError(400, "invalid_request", "cursor does not match user chat list")
|
||||
remainder = cursor[len(prefix):]
|
||||
if not remainder:
|
||||
raise ApiError(400, "invalid_request", "invalid cursor")
|
||||
try:
|
||||
cursor_ts_raw, cursor_id = cursor_parts
|
||||
cursor_ts_raw, cursor_id = remainder.split("|", 1)
|
||||
cursor_dt = datetime.fromisoformat(cursor_ts_raw.replace("Z", "+00:00"))
|
||||
except ValueError as exc:
|
||||
raise ApiError(400, "invalid_request", "invalid cursor") from exc
|
||||
filtered = []
|
||||
for item in rows:
|
||||
for idx, item in enumerate(items):
|
||||
item_dt = datetime.fromisoformat(item["created_at"].replace("Z", "+00:00"))
|
||||
if (item_dt, item["id"]) > (cursor_dt.astimezone(timezone.utc), cursor_id):
|
||||
filtered.append(item)
|
||||
rows = filtered
|
||||
start_index = idx
|
||||
break
|
||||
else:
|
||||
start_index = len(items)
|
||||
|
||||
page = items[start_index:start_index + limit]
|
||||
next_cursor = None
|
||||
if len(rows) > limit:
|
||||
next_cursor = f"{rows[limit-1]['created_at']}|{rows[limit-1]['id']}"
|
||||
rows = rows[:limit]
|
||||
if start_index + limit < len(items):
|
||||
last_item = page[-1]
|
||||
next_cursor = f"user:{user_id}|{last_item['created_at']}|{last_item['id']}"
|
||||
|
||||
return {"items": rows, "next_cursor": next_cursor}
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
|
||||
def list_messages_for_chat(self, chat_id: str, limit: int, cursor: Optional[str]):
|
||||
with self.connect() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = %s ORDER BY created_at ASC, id ASC LIMIT %s",
|
||||
(chat_id, limit + 1),
|
||||
"SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = %s ORDER BY created_at ASC, id ASC",
|
||||
(chat_id,),
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
|
||||
normalised = [{
|
||||
items = [{
|
||||
"id": str(row[0]),
|
||||
"chat_id": str(row[1]),
|
||||
"sender_id": str(row[2]),
|
||||
@@ -448,29 +453,34 @@ class Store:
|
||||
"created_at": self.isoformat_utc(row[4]),
|
||||
} for row in rows]
|
||||
|
||||
start_index = 0
|
||||
if cursor:
|
||||
cursor_parts = cursor.split("|", 1)
|
||||
if len(cursor_parts) != 2:
|
||||
prefix = f"chat:{chat_id}|"
|
||||
if not cursor.startswith(prefix):
|
||||
raise ApiError(400, "invalid_request", "cursor does not match chat")
|
||||
remainder = cursor[len(prefix):]
|
||||
if not remainder:
|
||||
raise ApiError(400, "invalid_request", "invalid cursor")
|
||||
try:
|
||||
cursor_ts_raw, cursor_id = cursor_parts
|
||||
cursor_ts_raw, cursor_id = remainder.split("|", 1)
|
||||
cursor_dt = datetime.fromisoformat(cursor_ts_raw.replace("Z", "+00:00"))
|
||||
except ValueError as exc:
|
||||
raise ApiError(400, "invalid_request", "invalid cursor") from exc
|
||||
filtered = []
|
||||
for item in normalised:
|
||||
for idx, item in enumerate(items):
|
||||
item_dt = datetime.fromisoformat(item["created_at"].replace("Z", "+00:00"))
|
||||
if (item_dt, item["id"]) > (cursor_dt.astimezone(timezone.utc), cursor_id):
|
||||
filtered.append(item)
|
||||
normalised = filtered
|
||||
start_index = idx
|
||||
break
|
||||
else:
|
||||
start_index = len(items)
|
||||
|
||||
page = items[start_index:start_index + limit]
|
||||
next_cursor = None
|
||||
if len(normalised) > limit:
|
||||
last_item = normalised[limit - 1]
|
||||
next_cursor = f"{last_item['created_at']}|{last_item['id']}"
|
||||
normalised = normalised[:limit]
|
||||
if start_index + limit < len(items):
|
||||
last_item = page[-1]
|
||||
next_cursor = f"chat:{chat_id}|{last_item['created_at']}|{last_item['id']}"
|
||||
|
||||
return {"items": normalised, "next_cursor": next_cursor}
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
|
||||
def create_session(self, user_id: str, username: str):
|
||||
token = secrets.token_urlsafe(32)
|
||||
|
||||
Reference in New Issue
Block a user