fix: improve thread existence check to allow new conversations without 404

This commit is contained in:
Alexa
2026-07-28 09:48:38 +03:00
parent 936aa061c0
commit dc1f4c3492
@@ -151,7 +151,19 @@ public class ChatController(
.OrderByDescending(c => c.SentAt) .OrderByDescending(c => c.SentAt)
.Take(take) .Take(take)
.ToListAsync(); .ToListAsync();
if (rows.Count == 0) return NotFound();
// An empty thread is NOT a missing thread. Starting a chat with someone
// goes POST thread/withmembers (which creates the thread) and then
// immediately GET thread/{id} — at that moment it has no messages yet.
// 404-ing on message count meant a brand-new conversation could never be
// opened: the client got "not found" for the thread it had just been
// handed. Only 404 when nothing actually records the thread.
if (rows.Count == 0)
{
var exists = await db.ChatThreads.AnyAsync(t => t.ThreadKey == key)
|| await db.ChatThreadMembers.AnyAsync(m => m.ThreadKey == key);
if (!exists) return NotFound();
}
var meta = (await EnsureThreadRowsAsync(new[] { key }))[key]; var meta = (await EnsureThreadRowsAsync(new[] { key }))[key];
var member = await db.ChatThreadMembers var member = await db.ChatThreadMembers
.FirstOrDefaultAsync(m => m.ThreadKey == key && m.PlayerId == Me); .FirstOrDefaultAsync(m => m.ThreadKey == key && m.PlayerId == Me);