Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -82,71 +82,32 @@ class NewMessage(BaseModel):
|
|
| 82 |
text: str = Field(..., min_length=1, max_length=5000)
|
| 83 |
|
| 84 |
@app.get("/chat/{video_id}")
|
| 85 |
-
async def get_messages(video_id: str
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
items = await _read_jsonl(path)
|
| 94 |
-
|
| 95 |
-
# Filter by 'since' if provided
|
| 96 |
-
if since:
|
| 97 |
-
try:
|
| 98 |
-
since_dt = datetime.fromisoformat(since.replace("Z", "+00:00"))
|
| 99 |
-
def _is_new(msg):
|
| 100 |
-
try:
|
| 101 |
-
ts = datetime.fromisoformat(str(msg.get("created_at", "")).replace("Z", "+00:00"))
|
| 102 |
-
return ts > since_dt
|
| 103 |
-
except Exception:
|
| 104 |
-
return False
|
| 105 |
-
items = list(filter(_is_new, items))
|
| 106 |
-
except Exception:
|
| 107 |
-
# If bad 'since', just ignore it
|
| 108 |
-
pass
|
| 109 |
-
|
| 110 |
-
# Return the most recent 'limit' messages (sorted ascending by created_at)
|
| 111 |
-
items.sort(key=lambda m: m.get("created_at", ""))
|
| 112 |
-
if len(items) > limit:
|
| 113 |
-
items = items[-limit:]
|
| 114 |
-
|
| 115 |
-
return {"video_id": video_id, "count": len(items), "messages": items}
|
| 116 |
|
| 117 |
@app.post("/chat/{video_id}")
|
| 118 |
-
async def
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
lock = _lock_for(path)
|
| 125 |
-
|
| 126 |
-
author = _valid_author(msg.author)
|
| 127 |
-
text = _valid_text(msg.text)
|
| 128 |
-
if not text:
|
| 129 |
-
return {"ok": False, "error": "Empty message"}
|
| 130 |
-
|
| 131 |
-
created = _now_iso()
|
| 132 |
-
# Lightweight id; good enough for demo
|
| 133 |
-
mid = hashlib.sha1(f"{video_id}|{author}|{created}|{text}".encode("utf-8")).hexdigest()[:16]
|
| 134 |
-
ip = request.client.host if request and request.client else None
|
| 135 |
-
|
| 136 |
-
record = {
|
| 137 |
-
"id": mid,
|
| 138 |
"video_id": video_id,
|
| 139 |
-
"author":
|
| 140 |
-
"text": text,
|
| 141 |
-
"created_at":
|
| 142 |
-
"ip":
|
| 143 |
-
"ua": request.headers.get("user-agent", "")
|
| 144 |
}
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
return {"ok": True, "message": record}
|
| 150 |
|
| 151 |
# ---------------------------
|
| 152 |
# (Your original iCloud album endpoints — unchanged)
|
|
|
|
| 82 |
text: str = Field(..., min_length=1, max_length=5000)
|
| 83 |
|
| 84 |
@app.get("/chat/{video_id}")
|
| 85 |
+
async def get_messages(video_id: str):
|
| 86 |
+
messages = app.state.chat_storage.get(video_id, [])
|
| 87 |
+
return {
|
| 88 |
+
"video_id": video_id,
|
| 89 |
+
"count": len(messages),
|
| 90 |
+
"messages": messages
|
| 91 |
+
}
|
| 92 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
@app.post("/chat/{video_id}")
|
| 95 |
+
async def send_message(video_id: str, msg: ChatMessage, request: Request):
|
| 96 |
+
if video_id not in app.state.chat_storage:
|
| 97 |
+
app.state.chat_storage[video_id] = []
|
| 98 |
+
|
| 99 |
+
message = {
|
| 100 |
+
"id": uuid.uuid4().hex[:16],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
"video_id": video_id,
|
| 102 |
+
"author": msg.user, # ✅ FIXED: use provided username
|
| 103 |
+
"text": msg.text,
|
| 104 |
+
"created_at": datetime.utcnow().isoformat(),
|
| 105 |
+
"ip": request.client.host if request.client else "unknown",
|
| 106 |
+
"ua": request.headers.get("user-agent", "")
|
| 107 |
}
|
| 108 |
|
| 109 |
+
app.state.chat_storage[video_id].append(message)
|
| 110 |
+
return {"status": "ok", "message": message}
|
|
|
|
|
|
|
| 111 |
|
| 112 |
# ---------------------------
|
| 113 |
# (Your original iCloud album endpoints — unchanged)
|