Working DMs

This commit is contained in:
2025-08-25 17:47:36 +03:00
Unverified
parent 8acb2dd507
commit bddf34de2d
2 changed files with 35 additions and 4 deletions
+6
View File
@@ -246,6 +246,12 @@ class MessaggingSocketManager:
return None return None
if type == "ping": if type == "ping":
try:
current_user = get_current_user_inner()
if current_user:
self.user_by_ws[websocket] = current_user.id
except HTTPException:
pass
await websocket.send_json({"type": "ping", "data": {"status": "success"}}) await websocket.send_json({"type": "ping", "data": {"status": "success"}})
elif type == "getMessages": elif type == "getMessages":
try: try:
+29 -4
View File
@@ -117,8 +117,32 @@ async function loadUsers() {
websocket.send(JSON.stringify(payload)); websocket.send(JSON.stringify(payload));
} }
}, },
() => { async () => {
// For v1, load history for this DM // Load DM history for the active conversation
if (!activeDm?.publicKey) return;
const res = await fetch(`${API_BASE_URL}/dm/history/${activeDm.userId}`, { headers: getAuthHeaders(true) });
if (!res.ok) return;
const data = await res.json();
const messages: DmEnvelope[] = data.messages || [];
const container = document.getElementById("chat-messages")!;
container.innerHTML = "";
for (const env of messages) {
try {
const text = await decryptDm(env, activeDm.publicKey);
const div = document.createElement("div");
const isAuthor = env.senderId !== activeDm.userId;
div.className = `message ${isAuthor ? "sent" : "received"}`;
const inner = document.createElement("div");
inner.className = "message-inner";
const content = document.createElement("div");
content.className = "message-content";
content.textContent = text;
inner.appendChild(content);
div.appendChild(inner);
container.appendChild(div);
} catch {}
}
container.scrollTop = container.scrollHeight;
} }
); );
} }
@@ -159,11 +183,12 @@ init();
websocket.addEventListener("message", async (e) => { websocket.addEventListener("message", async (e) => {
try { try {
const msg = JSON.parse((e as MessageEvent).data); const msg = JSON.parse((e as MessageEvent).data);
if (msg?.type === "dmNew" && activeDm && msg.data.senderId === activeDm.userId) { if (msg?.type === "dmNew" && activeDm && (msg.data.senderId === activeDm.userId || msg.data.recipientId === activeDm.userId)) {
const plaintext = await decryptDm(msg.data, activeDm.publicKey!); const plaintext = await decryptDm(msg.data, activeDm.publicKey!);
const container = document.getElementById("chat-messages")!; const container = document.getElementById("chat-messages")!;
const div = document.createElement("div"); const div = document.createElement("div");
div.className = "message received"; const isAuthor = msg.data.senderId !== activeDm.userId;
div.className = `message ${isAuthor ? "sent" : "received"}`;
const inner = document.createElement("div"); const inner = document.createElement("div");
inner.className = "message-inner"; inner.className = "message-inner";
const content = document.createElement("div"); const content = document.createElement("div");