diff --git a/backup-restore.js b/backup-restore.js index 7c09810..0526d8c 100644 --- a/backup-restore.js +++ b/backup-restore.js @@ -512,6 +512,7 @@ function normalizeRestoreData(data) { const chat_threads = (data.chat_threads || []).map(x => ({ id: reqInt(x.id), tutor_id: reqInt(x.tutor_id), + peer_id: optInt(x.peer_id), last_message_at: optTs(x.last_message_at), last_message_text: optStr(x.last_message_text, 300), tutor_unread: optInt(x.tutor_unread, 0, 2147483647) ?? 0, diff --git a/db/init.sql b/db/init.sql index 74ac455..6a255f7 100644 --- a/db/init.sql +++ b/db/init.sql @@ -398,14 +398,16 @@ CREATE INDEX IF NOT EXISTS idx_notification_reads_user ON notification_reads(use CREATE TABLE IF NOT EXISTS chat_threads ( id SERIAL PRIMARY KEY, - tutor_id INT NOT NULL UNIQUE REFERENCES users(id) ON DELETE CASCADE, + tutor_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + peer_id INT REFERENCES users(id) ON DELETE CASCADE, last_message_at TIMESTAMPTZ DEFAULT now(), last_message_text VARCHAR(300), tutor_unread INT NOT NULL DEFAULT 0, admin_unread INT NOT NULL DEFAULT 0, deleted_at TIMESTAMPTZ, purge_at TIMESTAMPTZ, - created_at TIMESTAMPTZ DEFAULT now() + created_at TIMESTAMPTZ DEFAULT now(), + UNIQUE (tutor_id, peer_id) ); CREATE INDEX IF NOT EXISTS idx_chat_threads_last_message ON chat_threads(last_message_at DESC); @@ -684,6 +686,7 @@ INSERT INTO settings (key, value) VALUES ('face_scan_on_upload', 'true') ON CONF INSERT INTO settings (key, value) VALUES ('notify_face_scan_done', 'false') ON CONFLICT (key) DO NOTHING; INSERT INTO settings (key, value) VALUES ('notify_face_recognized', 'true') ON CONFLICT (key) DO NOTHING; INSERT INTO settings (key, value) VALUES ('notify_face_scan_error', 'true') ON CONFLICT (key) DO NOTHING; +INSERT INTO settings (key, value) VALUES ('notify_chat_message', 'true') ON CONFLICT (key) DO NOTHING; CREATE TABLE IF NOT EXISTS achievement_types ( id SERIAL PRIMARY KEY, diff --git a/db/migration.sql b/db/migration.sql index f57ecfc..44ede95 100644 --- a/db/migration.sql +++ b/db/migration.sql @@ -383,18 +383,23 @@ CREATE INDEX IF NOT EXISTS idx_notification_reads_user ON notification_reads(use CREATE TABLE IF NOT EXISTS chat_threads ( id SERIAL PRIMARY KEY, - tutor_id INT NOT NULL UNIQUE REFERENCES users(id) ON DELETE CASCADE, + tutor_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + peer_id INT REFERENCES users(id) ON DELETE CASCADE, last_message_at TIMESTAMPTZ DEFAULT now(), last_message_text VARCHAR(300), tutor_unread INT NOT NULL DEFAULT 0, admin_unread INT NOT NULL DEFAULT 0, deleted_at TIMESTAMPTZ, purge_at TIMESTAMPTZ, - created_at TIMESTAMPTZ DEFAULT now() + created_at TIMESTAMPTZ DEFAULT now(), + UNIQUE (tutor_id, peer_id) ); ALTER TABLE chat_threads ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ; ALTER TABLE chat_threads ADD COLUMN IF NOT EXISTS purge_at TIMESTAMPTZ; +ALTER TABLE chat_threads DROP CONSTRAINT IF EXISTS chat_threads_tutor_id_key; +ALTER TABLE chat_threads ADD COLUMN IF NOT EXISTS peer_id INT REFERENCES users(id) ON DELETE CASCADE; +CREATE UNIQUE INDEX IF NOT EXISTS idx_chat_threads_pair ON chat_threads (tutor_id, peer_id); CREATE INDEX IF NOT EXISTS idx_chat_threads_last_message ON chat_threads(last_message_at DESC); @@ -861,6 +866,7 @@ INSERT INTO settings (key, value) VALUES ('face_scan_on_upload', 'true') ON CONF INSERT INTO settings (key, value) VALUES ('notify_face_scan_done', 'false') ON CONFLICT (key) DO NOTHING; INSERT INTO settings (key, value) VALUES ('notify_face_recognized', 'true') ON CONFLICT (key) DO NOTHING; INSERT INTO settings (key, value) VALUES ('notify_face_scan_error', 'true') ON CONFLICT (key) DO NOTHING; +INSERT INTO settings (key, value) VALUES ('notify_chat_message', 'true') ON CONFLICT (key) DO NOTHING; CREATE TABLE IF NOT EXISTS broken_images ( id SERIAL PRIMARY KEY, diff --git a/public/admin.css b/public/admin.css index 2c6f1b2..9b6c173 100644 --- a/public/admin.css +++ b/public/admin.css @@ -14,6 +14,9 @@ body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;b .user-box .user-meta{display:flex;flex-direction:column;min-width:0;flex:1} .user-box .user-name{font-weight:600;font-size:.85rem;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--text);line-height:1.25} .user-box .user-role{color:var(--muted);font-size:.7rem;font-weight:500;line-height:1.2} +.user-box .notify-bell{margin-left:auto;color:var(--muted);text-decoration:none;display:flex;align-items:center;gap:6px;padding:6px 8px;border-radius:8px;transition:all .14s ease} +.user-box .notify-bell:hover{color:var(--text);background:var(--bg)} +.user-box .notify-bell i{width:16px;height:16px;flex-shrink:0} .nav-scroll-wrap{flex:1 1 auto;min-height:0;overflow-y:auto;overflow-x:hidden;overscroll-behavior:contain;padding:8px 0;scrollbar-width:thin;scrollbar-color:rgba(0,0,0,.15) transparent} .nav-scroll-wrap::-webkit-scrollbar{width:5px} @@ -64,7 +67,7 @@ body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;b .sidebar .bottom-actions a{display:flex;align-items:center;gap:10px;padding:6px 10px;border-radius:8px;color:var(--muted);text-decoration:none;font-size:.82rem;font-weight:500;transition:all .14s ease;position:relative} .sidebar .bottom-actions a:hover{color:var(--text);background:var(--bg)} .sidebar .bottom-actions a[data-nav=home] svg{color:#2563eb} -.sidebar .bottom-actions .notify-bell svg{color:#f59e0b} +.user-box .notify-bell svg{color:#f59e0b} .sidebar .bottom-footer{display:flex;align-items:center;justify-content:space-between;gap:8px;padding-top:6px;border-top:1px dashed var(--border)} .sidebar .bottom-footer .logout-btn{display:inline-flex;align-items:center;gap:6px;padding:4px 8px;border-radius:6px;color:#ef4444;text-decoration:none;font-size:.78rem;font-weight:600;cursor:pointer;transition:background .14s ease} .sidebar .bottom-footer .logout-btn:hover{background:rgba(239,68,68,.08)} @@ -953,7 +956,7 @@ body[data-page=settings] .page-head{align-items:flex-end} .notify-bell{position:relative} .notify-badge{position:absolute;left:20px;top:-3px;min-width:16px;height:16px;padding:0 4px;border-radius:999px;background:#ef4444;color:#fff;font-size:.62rem;font-weight:700;line-height:16px;text-align:center} .notify-badge[hidden]{display:none} -.notify-panel{position:fixed;left:12px;bottom:96px;width:min(380px,calc(100vw - 24px));max-height:min(70vh,520px);display:flex;flex-direction:column;background:var(--card);border:1px solid var(--border);border-radius:14px;box-shadow:0 18px 40px rgba(0,0,0,.18);z-index:90;overflow:hidden} +.notify-panel{position:fixed;width:min(380px,calc(100vw - 24px));max-height:min(70vh,520px);display:flex;flex-direction:column;background:var(--card);border:1px solid var(--border);border-radius:14px;box-shadow:0 18px 40px rgba(0,0,0,.18);z-index:90;overflow:hidden} .notify-panel[hidden]{display:none} .notify-head{display:flex;align-items:center;justify-content:space-between;gap:10px;padding:12px 14px;border-bottom:1px solid var(--border);font-size:.9rem} .notify-list{overflow-y:auto;flex:1} diff --git a/public/admin.js b/public/admin.js index 3e821c3..d02f416 100644 --- a/public/admin.js +++ b/public/admin.js @@ -102,6 +102,10 @@ function buildSidebar(active) {