From ae5eedaeeb5dd5978a278e848d40fa7436da9837 Mon Sep 17 00:00:00 2001 From: orfelorfel23 Date: Sun, 25 Oct 2026 20:51:57 +0100 Subject: [PATCH] Auto-Commit: 2026-10-25 20:51:57 --- public/app.js | 10 ++++++ public/styles.css | 5 +++ src/index.js | 81 ++++++++++++++++++++++++++++++++++------------- 3 files changed, 74 insertions(+), 22 deletions(-) diff --git a/public/app.js b/public/app.js index 7001fb0..0a31d99 100644 --- a/public/app.js +++ b/public/app.js @@ -37,6 +37,11 @@ const I18N = { chat_ph:'Nachricht schreiben…', no_msgs:'Sag hallo – schreib ihr/ihm was.', ctx_note:'🤖 KI-Charakter – schreibt mit dir', send_err:'Konnte nicht antworten, bitte nochmal.', no_chats:'Noch keine Chats.', profile:'Dein Profil', + delete_chat:'Chat löschen', + confirm_delete_chat:'Diesen Chat wirklich komplett löschen? Alle Nachrichten werden unwiderruflich entfernt.', + chat_deleted:'Chat gelöscht ✓', + new_chat:'Neuer Chat', + select_char_chat:'Wähle einen Charakter für einen neuen Chat:', me_bio:'Der einzige echte Mensch hier.', chars:'Charaktere', newest:'Neu', view_profile:'Profil', create_first:'Erstell deinen ersten Charakter unten.', post_empty:'Schreib erst etwas…', user:'Du', bot:'KI', sub_ok:'Erledigt ✓', @@ -161,6 +166,11 @@ const I18N = { chat_ph:'Write a message…', no_msgs:'Say hi – send them something.', ctx_note:'🤖 AI character – chatting with you', send_err:'Could not reply, please try again.', no_chats:'No chats yet.', profile:'Your profile', + delete_chat:'Delete chat', + confirm_delete_chat:'Do you really want to delete this chat? All messages will be permanently removed.', + chat_deleted:'Chat deleted ✓', + new_chat:'New chat', + select_char_chat:'Select a character to start a new chat:', me_bio:'The only real human here.', chars:'Characters', newest:'New', view_profile:'Profile', create_first:'Create your first character below.', post_empty:'Write something first…', user:'You', bot:'AI', sub_ok:'Done ✓', diff --git a/public/styles.css b/public/styles.css index b9338b5..b95fe02 100644 --- a/public/styles.css +++ b/public/styles.css @@ -185,6 +185,11 @@ input[type=text]:focus,input[type=number]:focus,input[type=password]:focus,texta .post-del:hover{opacity:1;background:rgba(255,75,75,0.15);color:#ff6b6b} .badge-dot{display:inline-block;width:8px;height:8px;border-radius:50%;background:#ff4b4b;margin-left:4px;vertical-align:middle} .chat-item.unread{border-left:3px solid var(--acc)} +.chat-del{opacity:0.5;font-size:13px;padding:4px 7px;border-radius:8px;background:transparent;border:none;color:inherit;cursor:pointer;transition:all .15s;flex:none} +.chat-del:hover{opacity:1;background:rgba(255,75,75,0.18);color:#ff6b6b} +.btn.ghost.danger{border-color:rgba(220,53,69,0.4);color:#ff6b6b} +.btn.ghost.danger:hover{background:rgba(220,53,69,0.15);border-color:#dc3545;color:#ff8585} +.char-picker-item:hover{border-color:var(--acc)!important;background:var(--card)!important} /* ---------- Admin Panel Styles ---------- */ .badge-pause{border-color:#664d00;background:#2b2300;color:#ffd35c;font-weight:600;display:inline-flex;align-items:center;gap:4px} diff --git a/src/index.js b/src/index.js index 505ed91..c7442d2 100644 --- a/src/index.js +++ b/src/index.js @@ -571,28 +571,50 @@ app.post('/api/feed-tick', async (c) => { // ---------- DM CHAT ---------- app.get('/api/chats', async (c) => { const allChars = await getChars({ includeBanned: true }); + const charMap = new Map(allChars.map((ch) => [ch.id, ch])); + + const query = ` + WITH stats AS ( + SELECT + char_id, + COUNT(*)::int AS count, + COUNT(*) FILTER (WHERE sender = 'char' AND is_read = 0)::int AS unread_count + FROM messages + GROUP BY char_id + ), + latest AS ( + SELECT DISTINCT ON (char_id) char_id, text, sender, created_at + FROM messages + ORDER BY char_id, created_at DESC + ) + SELECT + s.char_id, + s.count, + s.unread_count, + l.text AS last_text, + l.sender AS last_sender, + l.created_at AS last_created_at + FROM stats s + JOIN latest l ON s.char_id = l.char_id + WHERE s.count > 0; + `; + + const { rows } = await pool.query(query); const chats = []; - for (const ch of allChars) { - const lastRes = await pool.query( - 'SELECT * FROM messages WHERE char_id = $1 ORDER BY created_at DESC LIMIT 1;', - [ch.id] - ); - const countRes = await pool.query( - 'SELECT COUNT(*)::int AS n FROM messages WHERE char_id = $1;', - [ch.id] - ); - const count = countRes.rows[0]?.n || 0; - if (ch.is_banned && count === 0) continue; - - const last = lastRes.rows[0]; - const isUnread = last && last.sender === 'char'; - + for (const row of rows) { + const ch = charMap.get(row.char_id); + if (!ch) continue; chats.push({ char: charBrief(ch), - lastMessage: last ? { text: last.text, sender: last.sender, createdAt: Number(last.created_at) } : null, - count, - isUnread: Boolean(isUnread), + lastMessage: row.last_text ? { + text: row.last_text, + sender: row.last_sender, + createdAt: Number(row.last_created_at), + } : null, + count: row.count, + unreadCount: row.unread_count, + isUnread: row.unread_count > 0, isBanned: ch.is_banned === 1, }); } @@ -607,6 +629,9 @@ app.get('/api/chats/:id', async (c) => { const ch = res.rows[0]; if (!ch) return c.json({ error: 'Nicht gefunden' }, 404); + // Mark all unread messages for this character as read + await pool.query('UPDATE messages SET is_read = 1 WHERE char_id = $1 AND is_read = 0;', [ch.id]); + const msgs = await pool.query( 'SELECT * FROM messages WHERE char_id = $1 ORDER BY created_at ASC LIMIT 200;', [ch.id] @@ -623,6 +648,18 @@ app.get('/api/chats/:id', async (c) => { }); }); +app.post('/api/chats/:id/read', async (c) => { + const charId = c.req.param('id'); + await pool.query('UPDATE messages SET is_read = 1 WHERE char_id = $1 AND is_read = 0;', [charId]); + return c.json({ ok: true }); +}); + +app.delete('/api/chats/:id', async (c) => { + const charId = c.req.param('id'); + await pool.query('DELETE FROM messages WHERE char_id = $1;', [charId]); + return c.json({ ok: true }); +}); + app.post('/api/chats/:id/messages', async (c) => { const res = await pool.query('SELECT * FROM characters WHERE id = $1 LIMIT 1;', [c.req.param('id')]); const raw = res.rows[0]; @@ -640,8 +677,8 @@ app.post('/api/chats/:id/messages', async (c) => { const clean = text.trim().slice(0, 2000); await pool.query( - 'INSERT INTO messages (id, char_id, sender, text, created_at) VALUES ($1, $2, $3, $4, $5);', - [uid(), ch.id, 'user', clean, Date.now()] + 'INSERT INTO messages (id, char_id, sender, text, created_at, is_read) VALUES ($1, $2, $3, $4, $5, $6);', + [uid(), ch.id, 'user', clean, Date.now(), 1] ); // build context: persona + own recent activity + last 20 messages @@ -669,8 +706,8 @@ app.post('/api/chats/:id/messages', async (c) => { } await pool.query( - 'INSERT INTO messages (id, char_id, sender, text, created_at) VALUES ($1, $2, $3, $4, $5);', - [uid(), ch.id, 'char', reply, Date.now() + 1] + 'INSERT INTO messages (id, char_id, sender, text, created_at, is_read) VALUES ($1, $2, $3, $4, $5, $6);', + [uid(), ch.id, 'char', reply, Date.now() + 1, 1] ); return c.json({ ok: true, reply });