feat: Authorize attachment reads and message links (U4)
This commit is contained in:
+31
-9
@@ -80,13 +80,17 @@ def message_view(row: dict) -> dict:
|
||||
"id": str(row["id"]), "chat_id": str(row["chat_id"]),
|
||||
"sender_id": str(row["sender_id"]), "text": row["text"],
|
||||
"created_at": timestamp(row["created_at"]),
|
||||
"attachment_ids": [],
|
||||
"attachment_ids": [str(attachment_id) for attachment_id in row.get("attachment_ids", [])],
|
||||
}
|
||||
|
||||
|
||||
CHAT_COLUMNS = """c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT m.user_id FROM memberships m WHERE m.chat_id=c.id ORDER BY m.position) AS member_ids"""
|
||||
|
||||
MESSAGE_COLUMNS = """m.id, m.chat_id, m.sender_id, m.text, m.created_at,
|
||||
ARRAY(SELECT a.attachment_id FROM message_attachments a
|
||||
WHERE a.message_id=m.id ORDER BY a.position) AS attachment_ids"""
|
||||
|
||||
|
||||
class Store:
|
||||
def ready(self) -> dict:
|
||||
@@ -208,10 +212,13 @@ class Store:
|
||||
return {"items": items, "next_cursor": encode_cursor(f"chats:{actor_id}", items[-1]) if len(rows) > limit else None}
|
||||
|
||||
def create_message(self, actor_id: str, chat_id: str, text: str, key: str, attachment_ids: list[str] | None = None) -> dict:
|
||||
if attachment_ids:
|
||||
raise HTTPException(404)
|
||||
ids = [UUID(item) for item in attachment_ids or []]
|
||||
if len(ids) > 10 or len(set(ids)) != len(ids) or (not text.strip() and not ids):
|
||||
raise HTTPException(400)
|
||||
chat_uuid, actor_uuid = UUID(chat_id), UUID(actor_id)
|
||||
request_hash = hashlib.sha256(json.dumps({"text": text, "attachment_ids": []}, ensure_ascii=False, sort_keys=True).encode()).digest()
|
||||
request_hash = hashlib.sha256(json.dumps(
|
||||
{"text": text, "attachment_ids": [str(item) for item in ids]}, ensure_ascii=False, sort_keys=True,
|
||||
).encode()).digest()
|
||||
with connection() as conn:
|
||||
member = conn.execute(
|
||||
"SELECT 1 FROM memberships WHERE chat_id=%s AND user_id=%s", (chat_uuid, actor_uuid)
|
||||
@@ -230,7 +237,7 @@ class Store:
|
||||
).fetchone()
|
||||
if reserved is None:
|
||||
replay = conn.execute(
|
||||
"SELECT r.request_hash,m.id,m.chat_id,m.sender_id,m.text,m.created_at "
|
||||
f"SELECT r.request_hash,{MESSAGE_COLUMNS} "
|
||||
"FROM message_replays r JOIN messages m ON m.id=r.message_id "
|
||||
"WHERE r.user_id=%s AND r.chat_id=%s AND r.request_key=%s",
|
||||
(actor_uuid, chat_uuid, key),
|
||||
@@ -240,26 +247,41 @@ class Store:
|
||||
if replay["request_hash"] != request_hash:
|
||||
raise HTTPException(409)
|
||||
return message_view(replay)
|
||||
if ids:
|
||||
attachments = conn.execute(
|
||||
"SELECT id,status FROM attachments WHERE chat_id=%s AND id=ANY(%s)",
|
||||
(chat_uuid, ids),
|
||||
).fetchall()
|
||||
if len(attachments) != len(ids):
|
||||
raise HTTPException(404)
|
||||
if any(item["status"] == "failed" for item in attachments):
|
||||
raise HTTPException(409)
|
||||
row = conn.execute(
|
||||
"INSERT INTO messages (id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) "
|
||||
"RETURNING id,chat_id,sender_id,text,created_at",
|
||||
(uuid4(), chat_uuid, actor_uuid, text),
|
||||
).fetchone()
|
||||
for position, attachment_id in enumerate(ids):
|
||||
conn.execute(
|
||||
"INSERT INTO message_attachments (message_id,chat_id,attachment_id,position) "
|
||||
"VALUES (%s,%s,%s,%s)",
|
||||
(row["id"], chat_uuid, attachment_id, position),
|
||||
)
|
||||
conn.execute(
|
||||
"UPDATE message_replays SET message_id=%s WHERE user_id=%s AND chat_id=%s AND request_key=%s",
|
||||
(row["id"], actor_uuid, chat_uuid, key),
|
||||
)
|
||||
return message_view(row)
|
||||
return message_view({**row, "attachment_ids": ids})
|
||||
|
||||
def list_messages(self, actor_id: str, chat_id: str, limit: int, cursor: str | None) -> dict:
|
||||
after = decode_cursor(cursor, f"messages:{chat_id}")
|
||||
chat_uuid, actor_uuid = UUID(chat_id), UUID(actor_id)
|
||||
query = "SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=%s"
|
||||
query = f"SELECT {MESSAGE_COLUMNS} FROM messages m WHERE m.chat_id=%s"
|
||||
params = [chat_uuid]
|
||||
if after:
|
||||
query += " AND (created_at,id)>(%s,%s)"
|
||||
query += " AND (m.created_at,m.id)>(%s,%s)"
|
||||
params.extend(after)
|
||||
query += " ORDER BY created_at,id LIMIT %s"
|
||||
query += " ORDER BY m.created_at,m.id LIMIT %s"
|
||||
params.append(limit + 1)
|
||||
with connection() as conn:
|
||||
member = conn.execute(
|
||||
|
||||
Reference in New Issue
Block a user