ausername-12345 commited on
Commit
ff7a24d
·
1 Parent(s): eab9f14

fix message squish + optimistic send

Browse files
Files changed (1) hide show
  1. src/static/app.js +25 -13
src/static/app.js CHANGED
@@ -332,9 +332,24 @@ async function handleWsMessage(evt) {
332
 
333
  async function handleIncomingDm(msg) {
334
  const { conversation_id, message } = msg;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
  if (!msgCache[conversation_id]) msgCache[conversation_id] = [];
336
 
337
- // Try to decrypt
338
  let plaintext = null;
339
  if (privateKey) {
340
  try {
@@ -346,22 +361,11 @@ async function handleIncomingDm(msg) {
346
  const withPlain = { ...message, plaintext };
347
  msgCache[conversation_id].push(withPlain);
348
 
349
- // For pending DM (no id yet), link it when we get the echo or a reply
350
- if (currentChat && currentChat.type === "dm" && !currentChat.id) {
351
- const matchesEcho = message.sender_id === me.id;
352
- const matchesReply = message.sender_id === currentChat.other_user?.id;
353
- if (matchesEcho || matchesReply) {
354
- currentChat.id = conversation_id;
355
- }
356
- }
357
-
358
- // If this is the active chat, append the message
359
  if (currentChat && currentChat.type === "dm" && currentChat.id === conversation_id) {
360
  appendMessage(withPlain, currentChat.other_user);
361
  scrollToBottom();
362
  }
363
 
364
- // Update sidebar last message
365
  await loadChats();
366
  }
367
 
@@ -577,7 +581,7 @@ function appendMessage(m, otherUser, isGroup = false) {
577
  </div>`;
578
  } else if (isMe) {
579
  div.innerHTML = `
580
- <div class="flex flex-col items-end gap-0.5">
581
  <div class="msg-bubble bg-indigo-500 rounded-2xl rounded-tr-sm px-4 py-2.5 text-sm text-white">${escHtml(text)}</div>
582
  <span class="text-xs text-gray-600">${time}</span>
583
  </div>`;
@@ -643,6 +647,14 @@ async function sendDm(text) {
643
  payload.target_user_id = other.id;
644
  }
645
  ws.send(JSON.stringify(payload));
 
 
 
 
 
 
 
 
646
  }
647
 
648
  async function sendGroupMsg(text) {
 
332
 
333
  async function handleIncomingDm(msg) {
334
  const { conversation_id, message } = msg;
335
+
336
+ // For pending DM (no id yet), link it when we get the echo or a reply
337
+ if (currentChat && currentChat.type === "dm" && !currentChat.id) {
338
+ const matchesEcho = message.sender_id === me.id;
339
+ const matchesReply = message.sender_id === currentChat.other_user?.id;
340
+ if (matchesEcho || matchesReply) {
341
+ currentChat.id = conversation_id;
342
+ }
343
+ }
344
+
345
+ // Skip our own messages — shown optimistically in sendDm
346
+ if (message.sender_id === me.id) {
347
+ await loadChats();
348
+ return;
349
+ }
350
+
351
  if (!msgCache[conversation_id]) msgCache[conversation_id] = [];
352
 
 
353
  let plaintext = null;
354
  if (privateKey) {
355
  try {
 
361
  const withPlain = { ...message, plaintext };
362
  msgCache[conversation_id].push(withPlain);
363
 
 
 
 
 
 
 
 
 
 
 
364
  if (currentChat && currentChat.type === "dm" && currentChat.id === conversation_id) {
365
  appendMessage(withPlain, currentChat.other_user);
366
  scrollToBottom();
367
  }
368
 
 
369
  await loadChats();
370
  }
371
 
 
581
  </div>`;
582
  } else if (isMe) {
583
  div.innerHTML = `
584
+ <div class="flex flex-col items-end gap-0.5 max-w-[72%]">
585
  <div class="msg-bubble bg-indigo-500 rounded-2xl rounded-tr-sm px-4 py-2.5 text-sm text-white">${escHtml(text)}</div>
586
  <span class="text-xs text-gray-600">${time}</span>
587
  </div>`;
 
647
  payload.target_user_id = other.id;
648
  }
649
  ws.send(JSON.stringify(payload));
650
+
651
+ // Show message immediately
652
+ const optimistic = { id: Date.now(), sender_id: me.id, plaintext: text, created_at: new Date().toISOString(), is_read: false };
653
+ if (currentChat.id && msgCache[currentChat.id]) {
654
+ msgCache[currentChat.id].push(optimistic);
655
+ }
656
+ appendMessage(optimistic, other);
657
+ scrollToBottom();
658
  }
659
 
660
  async function sendGroupMsg(text) {