notes: AI titles for new notes, personal-only access, modal polish

- new note created without a title gets one generated by a background
  worker (createNoteTitleWorker, ai_status column, wake channel,
  notes_changed SSE event, settings toggle notes_ai_enabled)
- notes are strictly personal: list/update/delete/reorder always scoped
  to req.user.id, admin no longer sees other users' notes (403)
- notes modal: focus goes to the text body, random color for new notes,
  ctrl+enter hint, tags hint styling, empty-title 400 fix (optText)
- backup/restore: user_notes.ai_status normalized and restored
This commit is contained in:
dev
2026-10-08 21:47:37 +03:00
parent f226c3f144
commit b5d1fd0948
10 changed files with 427 additions and 34 deletions
+5
View File
@@ -97,6 +97,11 @@ const ACTION_LABELS = {
'feedback.update': 'Изменён отзыв',
'feedback.ai.run': 'ИИ сформировал отзыв',
'feedback.ai.error': 'Ошибка ИИ при формировании отзыва',
'note.reorder': 'Изменён порядок заметок',
'note.delete': 'Удалена заметка',
'note.ai.title': 'ИИ придумал заголовок заметки',
'note.ai.error': 'Ошибка ИИ при генерации заголовка заметки',
'note.ai.retry': 'ИИ повторил генерацию заголовка заметки',
};
const AUDIT_PAGE_SIZE = 50;
+40 -3
View File
@@ -5,6 +5,11 @@
let editingId = null;
let dragId = null;
let saving = false;
const NOTE_COLORS = ['#2563eb', '#4f46e5', '#7c3aed', '#db2777', '#dc2626', '#ea580c', '#ca8a04', '#16a34a', '#0d9488', '#0891b2'];
function randomColor() {
return NOTE_COLORS[Math.floor(Math.random() * NOTE_COLORS.length)];
}
function hdr() {
const t = sessionStorage.getItem('authToken');
@@ -29,6 +34,12 @@
const body = n.note
? esc(n.note)
: '<span class="note-empty-note">Без текста</span>';
const generating = !n.title && (n.ai_status === 'pending' || n.ai_status === 'processing');
const titleHtml = n.title
? esc(n.title)
: generating
? '<span class="note-empty-note">Заголовок генерируется…</span>'
: esc('Без заголовка');
return '<div class="note-card" data-id="' + n.id + '" style="--ncolor:' + color + '">' +
'<div class="note-card-head">' +
'<span class="note-drag" draggable="true" title="Перетащите, чтобы изменить порядок">⠿</span>' +
@@ -36,7 +47,7 @@
'<input type="color" id="noteColorPicker-' + n.id + '" value="' + color + '" style="display:none">' +
'<span class="note-color-swatch" style="background:' + color + '" title="Изменить цвет"></span>' +
'</div>' +
'<div class="note-card-title">' + esc(n.title || 'Без заголовка') + '</div>' +
'<div class="note-card-title">' + titleHtml + '</div>' +
tagsHtml +
'</div>' +
'<div class="note-card-note">' + body + '</div>' +
@@ -127,14 +138,14 @@
document.getElementById('notesTitle').value = note ? note.title : '';
document.getElementById('notesNote').value = note ? note.note : '';
document.getElementById('notesTags').value = note ? note.tags : '';
const color = note && /^#[0-9a-fA-F]{6}$/.test(note.color || '') ? note.color : '#2563eb';
const color = note && /^#[0-9a-fA-F]{6}$/.test(note.color || '') ? note.color : randomColor();
document.getElementById('notesColor').value = color;
document.getElementById('notesColorPreview').style.background = color;
const row = document.getElementById('notesColorRow');
if (row) row.style.setProperty('--ncolor', color);
document.getElementById('notesModal').classList.add('open');
renderIcons();
setTimeout(() => document.getElementById('notesTitle').focus(), 50);
setTimeout(() => document.getElementById('notesNote').focus(), 50);
}
function closeModal() {
@@ -291,6 +302,31 @@
}
}
let liveSource = null;
let liveReconnectTimer = null;
let liveReloadTimer = null;
function connectNotesLive() {
if (liveSource) { liveSource.close(); liveSource = null; }
const connect = () => {
const t = sessionStorage.getItem('authToken');
if (!t) return;
liveSource = new EventSource(`${API}/api/events?token=${encodeURIComponent(t)}`);
const scheduleReload = () => {
if (liveReloadTimer) clearTimeout(liveReloadTimer);
liveReloadTimer = setTimeout(() => { liveReloadTimer = null; loadNotes(); }, 300);
};
liveSource.addEventListener('notes_changed', scheduleReload);
liveSource.onerror = () => {
if (liveSource) { liveSource.close(); liveSource = null; }
if (liveReconnectTimer) clearTimeout(liveReconnectTimer);
liveReconnectTimer = setTimeout(connect, 5000);
};
};
connect();
window.addEventListener('beforeunload', () => { if (liveSource) liveSource.close(); });
}
async function init() {
if (!(await checkAuth())) return;
buildSidebar(document.body.dataset.page);
@@ -298,6 +334,7 @@
bindPage();
renderIcons();
await loadNotes();
connectNotesLive();
}
init();
+4 -2
View File
@@ -1,6 +1,6 @@
const DIRTY_FIELDS = ['systemName', 'timezone', 'timeFormat', 'spamInterval', 'footerLeft', 'footerRight', 'aiPrompt', 'aiAutoCheck', 'lessonAiPrompt', 'lessonAiEnabled', 'shareShowMessage', 'shareShowDate', 'shareShowNames', 'shareShowPhotos', 'cookieNoticeText', 'photoResolution', 'photoQuality', 'photoEngine', 'camEnabled', 'photoAiFaceMode', 'photoAiFaceModel', 'photoAiDevicePref', 'trashPurgeDays', 'notifyEnabled', 'notifyRetentionDays'];
const DIRTY_FIELDS = ['systemName', 'timezone', 'timeFormat', 'spamInterval', 'footerLeft', 'footerRight', 'aiPrompt', 'aiAutoCheck', 'lessonAiPrompt', 'lessonAiEnabled', 'notesAiEnabled', 'shareShowMessage', 'shareShowDate', 'shareShowNames', 'shareShowPhotos', 'cookieNoticeText', 'photoResolution', 'photoQuality', 'photoEngine', 'camEnabled', 'photoAiFaceMode', 'photoAiFaceModel', 'photoAiDevicePref', 'trashPurgeDays', 'notifyEnabled', 'notifyRetentionDays'];
const NOTIFY_IDS = [];
const SECTION_IDS = ['sec-system', 'sec-stack', 'sec-brand', 'sec-datetime', 'sec-antispam', 'sec-share', 'sec-ai', 'sec-lesson-ai', 'sec-backup', 'sec-trash', 'sec-form', 'sec-notify', 'sec-photo', 'sec-photo-ai', 'sec-bans'];
const SECTION_IDS = ['sec-system', 'sec-stack', 'sec-brand', 'sec-datetime', 'sec-antispam', 'sec-share', 'sec-ai', 'sec-lesson-ai', 'sec-notes-ai', 'sec-backup', 'sec-trash', 'sec-form', 'sec-notify', 'sec-photo', 'sec-photo-ai', 'sec-bans'];
const SPAM_MAX = 10080;
const BAN_REASONS = {
'honeypot': 'Антиспам-поле',
@@ -285,6 +285,7 @@ async function loadSettings() {
document.getElementById('aiAutoCheck').checked = s.ai_autocheck_enabled !== 'false';
document.getElementById('lessonAiEnabled').checked = s.lesson_ai_enabled !== 'false';
document.getElementById('lessonAiPrompt').value = s.lesson_ai_prompt || '';
document.getElementById('notesAiEnabled').checked = s.notes_ai_enabled !== 'false';
document.getElementById('shareShowMessage').checked = s.share_show_student_message === 'true';
document.getElementById('shareShowDate').checked = s.share_show_entry_date === 'true';
document.getElementById('shareShowNames').checked = s.share_show_student_names !== 'false';
@@ -890,6 +891,7 @@ async function saveSettings() {
ai_autocheck_enabled: document.getElementById('aiAutoCheck').checked ? 'true' : 'false',
lesson_ai_enabled: document.getElementById('lessonAiEnabled').checked ? 'true' : 'false',
lesson_ai_prompt: document.getElementById('lessonAiPrompt').value.trim(),
notes_ai_enabled: document.getElementById('notesAiEnabled').checked ? 'true' : 'false',
share_show_student_message: document.getElementById('shareShowMessage').checked ? 'true' : 'false',
share_show_entry_date: document.getElementById('shareShowDate').checked ? 'true' : 'false',
share_show_student_names: document.getElementById('shareShowNames').checked ? 'true' : 'false',
+3 -1
View File
@@ -57,6 +57,8 @@
.note-empty { text-align: center; color: var(--muted); padding: 32px 16px; font-size: .9rem; border: 1px dashed var(--border); border-radius: 12px; }
.note-modal-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.note-modal-head h3 { margin: 0; font-size: 1.05rem; }
.settings-field .hint { font-size: .78rem; color: var(--muted); line-height: 1.5; }
.settings-field label .hint { font-weight: 400; margin-left: 6px; }
</style>
</head>
<body data-page="notes">
@@ -83,7 +85,7 @@
<input id="notesTitle" class="settings-input" maxlength="200" placeholder="Краткое название заметки">
</div>
<div class="settings-field">
<label>Заметка</label>
<label>Заметка<span class="hint">ctrl+enter отправить</span></label>
<textarea id="notesNote" class="settings-input" rows="6" placeholder="Текст заметки"></textarea>
</div>
<div class="settings-field">
+14
View File
@@ -31,6 +31,7 @@
<a href="#sec-notify" data-nav="sec-notify"><i data-lucide="bell"></i> Уведомления</a>
<a href="#sec-ai" data-nav="sec-ai"><i data-lucide="bot"></i> ИИ</a>
<a href="#sec-lesson-ai" data-nav="sec-lesson-ai"><i data-lucide="notebook-pen"></i> Шаблон отчёта</a>
<a href="#sec-notes-ai" data-nav="sec-notes-ai"><i data-lucide="sticky-note"></i> Заголовки заметок</a>
<a href="#sec-backup" data-nav="sec-backup"><i data-lucide="database"></i> Бэкапы</a>
<a href="#sec-trash" data-nav="sec-trash"><i data-lucide="trash-2"></i> Корзина</a>
<a href="#sec-bans" data-nav="sec-bans"><i data-lucide="shield-off"></i> Блокировки</a>
@@ -209,6 +210,19 @@
</div>
</div>
<div class="settings-card" id="sec-notes-ai">
<div class="card-head">
<div class="ic"><i data-lucide="sticky-note"></i></div>
<h3>Заголовки заметок</h3>
</div>
<p class="hint">Если заголовок не заполнен при создании заметки, модель сама придумает его по тексту. Заголовок можно вручную заменить в любой момент.</p>
<label class="toggle">
<input type="checkbox" id="notesAiEnabled">
<span class="toggle-track"><span class="toggle-thumb"></span></span>
<span class="toggle-text"><b>Генерировать заголовок автоматически</b><small>Работает в фоне — карточка обновится сама</small></span>
</label>
</div>
<div class="settings-card">
<div class="card-head">
<div class="ic"><i data-lucide="boxes"></i></div>