Trim profile param flow to AddBlock-only write path

This commit is contained in:
ai5590 2026-04-07 02:18:32 +03:00
parent 3a412fcd51
commit 4deaedf79f
5 changed files with 296 additions and 209 deletions

View File

@ -3,27 +3,21 @@ import { profile } from '../mock-data.js?v=20260405171816';
import { state } from '../state.js?v=20260405171816'; import { state } from '../state.js?v=20260405171816';
import { import {
loadProfileSnapshot, loadProfileSnapshot,
profileFieldDefs, saveProfileParamBlock,
saveProfileParams,
saveProfileToggle, saveProfileToggle,
} from '../services/user-profile-params.js?v=20260405171816'; } from '../services/user-profile-params.js?v=20260405171816';
export const pageMeta = { id: 'profile-view', title: 'Профиль' }; export const pageMeta = { id: 'profile-view', title: 'Профиль' };
function formatDateTime(timeMs) { function getDisplayName(fields) {
if (!timeMs) return 'ещё не заполнено'; const firstName = fields.find((field) => field.key === 'first_name')?.value?.trim() || '';
return new Date(timeMs).toLocaleString('ru-RU'); const lastName = fields.find((field) => field.key === 'last_name')?.value?.trim() || '';
}
function getDisplayName(fieldMap) {
const firstName = fieldMap.get('first_name')?.value?.trim() || '';
const lastName = fieldMap.get('last_name')?.value?.trim() || '';
const fullName = `${firstName} ${lastName}`.trim(); const fullName = `${firstName} ${lastName}`.trim();
return fullName || profile.name; return fullName || profile.name;
} }
function toggleText(enabled) { function toggleText(enabled) {
return enabled ? 'yes' : 'no'; return enabled ? 'Yes' : 'No';
} }
export function render({ navigate }) { export function render({ navigate }) {
@ -55,21 +49,21 @@ export function render({ navigate }) {
<p class="meta-muted">${login}</p> <p class="meta-muted">${login}</p>
</div> </div>
</div> </div>
<button class="primary-btn" type="button" data-open-edit="true">Обновить</button> <button class="primary-btn" type="button" data-reload="true">Обновить</button>
`; `;
const badgesRow = document.createElement('div'); const badgesRow = document.createElement('div');
badgesRow.className = 'row'; badgesRow.className = 'row';
badgesRow.innerHTML = ` badgesRow.innerHTML = `
<button class="badge profile-toggle-btn" type="button" data-toggle="official"> Официальный: no</button> <button class="badge profile-toggle-btn is-no" type="button" data-toggle="official">Официальный: No</button>
<button class="badge alt profile-toggle-btn" type="button" data-toggle="shine"> Сияющий: no</button> <button class="badge profile-toggle-btn is-no" type="button" data-toggle="shine">Сияющий: No</button>
`; `;
const hint = document.createElement('div'); const hint = document.createElement('div');
hint.className = 'card profile-data-help'; hint.className = 'card profile-data-help';
hint.innerHTML = ` hint.innerHTML = `
<div class="meta-muted">Личные данные пользователя</div> <div class="meta-muted">Личные данные пользователя</div>
<p>Поля ниже читаются из реальных пользовательских параметров сервера (ListUserParams). Любое изменение отправляется как блокчейн-запись параметра и требует подпись ключом пользователя.</p> <p>Параметры читаются через API GetUserParam. Изменения записываются в блокчейн и после этого список сразу обновляется.</p>
`; `;
const status = document.createElement('div'); const status = document.createElement('div');
@ -79,65 +73,48 @@ export function render({ navigate }) {
const listWrap = document.createElement('div'); const listWrap = document.createElement('div');
listWrap.className = 'stack profile-param-list'; listWrap.className = 'stack profile-param-list';
const editModal = document.createElement('div');
editModal.className = 'profile-help-modal';
editModal.hidden = true;
editModal.innerHTML = `
<div class="profile-help-backdrop" data-close="true"></div>
<div class="profile-help-dialog card" role="dialog" aria-modal="true" aria-labelledby="profile-edit-title" tabindex="-1">
<div class="row" style="align-items:flex-start;">
<div>
<div class="meta-muted" style="margin-bottom:4px;">Обновление личных данных</div>
<h3 id="profile-edit-title" style="font-size:18px;">Редактирование профиля</h3>
</div>
<button class="icon-btn profile-help-close" type="button" aria-label="Закрыть"></button>
</div>
<p class="profile-help-text">После сохранения по каждому полю отправляется запись параметра в блокчейн. Для подписи используется ключ пользователя на устройстве.</p>
<form class="stack" data-profile-form="true"></form>
<div class="row">
<button class="ghost-btn" type="button" data-cancel-edit="true">Отмена</button>
<button class="primary-btn" type="button" data-save-profile="true">Сохранить</button>
</div>
</div>
`;
const profileNameEl = topRow.querySelector('[data-profile-name="true"]'); const profileNameEl = topRow.querySelector('[data-profile-name="true"]');
const openEditBtn = topRow.querySelector('[data-open-edit="true"]'); const reloadBtn = topRow.querySelector('[data-reload="true"]');
const officialBtn = badgesRow.querySelector('[data-toggle="official"]'); const officialBtn = badgesRow.querySelector('[data-toggle="official"]');
const shineBtn = badgesRow.querySelector('[data-toggle="shine"]'); const shineBtn = badgesRow.querySelector('[data-toggle="shine"]');
const formEl = editModal.querySelector('[data-profile-form="true"]');
const dialogEl = editModal.querySelector('.profile-help-dialog');
const saveBtn = editModal.querySelector('[data-save-profile="true"]');
let currentFields = profileFieldDefs.map((field) => ({ ...field, value: '', timeMs: 0 })); let currentFields = [];
let currentToggles = [ let currentToggles = [];
{ key: 'official', enabled: false, timeMs: 0 },
{ key: 'shine', enabled: false, timeMs: 0 }, function updateToggleButton(button, prefix, enabled) {
]; button.textContent = `${prefix}: ${toggleText(enabled)}`;
button.classList.remove('is-no', 'is-yes-official', 'is-yes-shine');
if (!enabled) {
button.classList.add('is-no');
return;
}
if (prefix === 'Официальный') {
button.classList.add('is-yes-official');
} else {
button.classList.add('is-yes-shine');
}
}
function updateTogglesUi() { function updateTogglesUi() {
const official = currentToggles.find((item) => item.key === 'official') || { enabled: false }; const official = currentToggles.find((item) => item.key === 'official') || { enabled: false };
const shine = currentToggles.find((item) => item.key === 'shine') || { enabled: false }; const shine = currentToggles.find((item) => item.key === 'shine') || { enabled: false };
updateToggleButton(officialBtn, 'Официальный', official.enabled);
officialBtn.textContent = `✔ Официальный: ${toggleText(official.enabled)}`; updateToggleButton(shineBtn, 'Сияющий', shine.enabled);
shineBtn.textContent = `✨ Сияющий: ${toggleText(shine.enabled)}`;
} }
function renderFields(fields) { function renderFields(fields) {
const fieldMap = new Map(fields.map((field) => [field.key, field])); profileNameEl.textContent = getDisplayName(fields);
profileNameEl.textContent = getDisplayName(fieldMap);
listWrap.innerHTML = ''; listWrap.innerHTML = '';
fields.forEach((field) => { fields.forEach((field) => {
const row = document.createElement('div'); const row = document.createElement('div');
row.className = 'card profile-param-item'; row.className = 'card profile-param-item row';
const value = String(field.value || '').trim() || 'не заполнено';
row.innerHTML = ` row.innerHTML = `
<div class="profile-param-head"> <div class="profile-param-value"><b>${field.label}</b>: ${value}</div>
<span class="meta-muted">${field.label}</span> <button class="ghost-btn" type="button" data-edit-field="${field.key}">Изменить</button>
<span class="meta-muted">${field.key}</span>
</div>
<div class="profile-param-value">${field.value || '—'}</div>
<div class="meta-muted profile-param-time">Обновлено: ${formatDateTime(field.timeMs)}</div>
`; `;
listWrap.append(row); listWrap.append(row);
}); });
@ -146,7 +123,7 @@ export function render({ navigate }) {
async function refreshProfileSnapshot() { async function refreshProfileSnapshot() {
status.className = 'status-line'; status.className = 'status-line';
status.textContent = 'Загрузка параметров...'; status.textContent = 'Загрузка параметров...';
openEditBtn.disabled = true; reloadBtn.disabled = true;
officialBtn.disabled = true; officialBtn.disabled = true;
shineBtn.disabled = true; shineBtn.disabled = true;
@ -154,77 +131,35 @@ export function render({ navigate }) {
const snapshot = await loadProfileSnapshot(login); const snapshot = await loadProfileSnapshot(login);
currentFields = snapshot.fields; currentFields = snapshot.fields;
currentToggles = snapshot.toggles; currentToggles = snapshot.toggles;
renderFields(snapshot.fields);
updateTogglesUi();
status.className = 'status-line is-available';
status.textContent = 'Актуальные параметры загружены с сервера.';
} catch (error) {
renderFields(currentFields); renderFields(currentFields);
updateTogglesUi(); updateTogglesUi();
status.className = 'status-line is-available';
status.textContent = 'Актуальные параметры загружены.';
} catch (error) {
status.className = 'status-line is-unavailable'; status.className = 'status-line is-unavailable';
status.textContent = `Не удалось загрузить параметры: ${error.message || 'ошибка сети'}`; status.textContent = `Не удалось загрузить параметры: ${error.message || 'ошибка сети'}`;
} finally { } finally {
openEditBtn.disabled = false; reloadBtn.disabled = false;
officialBtn.disabled = false; officialBtn.disabled = false;
shineBtn.disabled = false; shineBtn.disabled = false;
} }
} }
function closeEditModal() {
editModal.hidden = true;
}
function openEditModal() {
formEl.innerHTML = '';
currentFields.forEach((field) => {
const fieldWrap = document.createElement('label');
fieldWrap.className = 'stack';
fieldWrap.innerHTML = `
<span class="field-label">${field.label}</span>
<input class="input" type="text" name="${field.key}" placeholder="${field.placeholder || ''}" value="${field.value || ''}" />
`;
formEl.append(fieldWrap);
});
editModal.hidden = false;
dialogEl.focus();
}
async function saveChanges() {
const valuesByKey = {};
currentFields.forEach((field) => {
const input = formEl.querySelector(`input[name="${field.key}"]`);
valuesByKey[field.key] = input instanceof HTMLInputElement ? input.value : '';
});
saveBtn.disabled = true;
try {
await saveProfileParams(login, valuesByKey);
closeEditModal();
await refreshProfileSnapshot();
} catch (error) {
status.className = 'status-line is-unavailable';
status.textContent = `Не удалось сохранить: ${error.message || 'ошибка сети'}`;
} finally {
saveBtn.disabled = false;
}
}
async function onToggleClick(toggleKey) { async function onToggleClick(toggleKey) {
const toggle = currentToggles.find((item) => item.key === toggleKey) || { enabled: false }; const toggle = currentToggles.find((item) => item.key === toggleKey) || { enabled: false };
const nextEnabled = !toggle.enabled; const nextEnabled = !toggle.enabled;
const title = toggleKey === 'official' ? 'Официальный аккаунт' : 'Сияющий аккаунт'; const title = toggleKey === 'official' ? 'официальный' : 'сияющий';
const confirmed = window.confirm( const confirmed = window.confirm(
`Изменить параметр «${title}» на ${toggleText(nextEnabled)}?\n\n` + `Хотите изменить «${title}» на ${toggleText(nextEnabled)}?\n` +
'Внимание: изменение будет записано как блокчейн-параметр пользователя и требует подписи ключом блокчейна/пользователя на устройстве.', 'Будет создана запись в блокчейне.',
); );
if (!confirmed) return; if (!confirmed) return;
status.className = 'status-line'; status.className = 'status-line';
status.textContent = 'Отправка изменения в блокчейн...'; status.textContent = 'Сохранение в блокчейн...';
try { try {
await saveProfileToggle(login, toggleKey, nextEnabled); await saveProfileToggle(login, toggleKey, nextEnabled);
@ -235,25 +170,44 @@ export function render({ navigate }) {
} }
} }
openEditBtn.addEventListener('click', openEditModal); async function onEditFieldClick(fieldKey) {
saveBtn.addEventListener('click', saveChanges); const field = currentFields.find((item) => item.key === fieldKey);
if (!field) return;
const entered = window.prompt(`Введите новое значение для «${field.label}»:`, field.value || '');
if (entered === null) return;
const confirmed = window.confirm(
`Записать новое значение параметра «${field.label}» в блокчейн?`,
);
if (!confirmed) return;
status.className = 'status-line';
status.textContent = 'Сохранение в блокчейн...';
try {
await saveProfileParamBlock(login, field.key, entered);
await refreshProfileSnapshot();
} catch (error) {
status.className = 'status-line is-unavailable';
status.textContent = `Не удалось изменить ${field.key}: ${error.message || 'ошибка сети'}`;
}
}
listWrap.addEventListener('click', (event) => {
const target = event.target;
if (!(target instanceof HTMLElement)) return;
const fieldKey = target.dataset.editField;
if (!fieldKey) return;
onEditFieldClick(fieldKey);
});
reloadBtn.addEventListener('click', refreshProfileSnapshot);
officialBtn.addEventListener('click', () => onToggleClick('official')); officialBtn.addEventListener('click', () => onToggleClick('official'));
shineBtn.addEventListener('click', () => onToggleClick('shine')); shineBtn.addEventListener('click', () => onToggleClick('shine'));
editModal.querySelector('[data-cancel-edit="true"]').addEventListener('click', closeEditModal);
editModal.addEventListener('click', (event) => {
const target = event.target;
if (target instanceof HTMLElement && (target.dataset.close === 'true' || target.classList.contains('profile-help-close'))) {
closeEditModal();
}
});
editModal.addEventListener('keydown', (event) => {
if (event.key === 'Escape') closeEditModal();
});
card.append(topRow, badgesRow, hint, status, listWrap); card.append(topRow, badgesRow, hint, status, listWrap);
screen.append(card, editModal); screen.append(card);
refreshProfileSnapshot(); refreshProfileSnapshot();

View File

@ -1,12 +1,16 @@
import { WsJsonClient } from './ws-client.js?v=20260405171816'; import { WsJsonClient } from './ws-client.js?v=20260405171816';
import { import {
bytesToBase64,
deriveEd25519FromPassword, deriveEd25519FromPassword,
exportEd25519PublicKeyB64, exportEd25519PublicKeyB64,
exportPkcs8B64, exportPkcs8B64,
generateEd25519Pair, generateEd25519Pair,
importPkcs8Ed25519, importPkcs8Ed25519,
randomBase64, randomBase64,
sha256Bytes,
signBytes,
signBase64, signBase64,
utf8Bytes,
} from './crypto-utils.js?v=20260405171816'; } from './crypto-utils.js?v=20260405171816';
import { import {
loadEncryptedUserSecrets, loadEncryptedUserSecrets,
@ -16,7 +20,6 @@ import {
} from './key-vault.js?v=20260405171816'; } from './key-vault.js?v=20260405171816';
const BCH_SUFFIX = '001'; const BCH_SUFFIX = '001';
const USER_PARAMETER_PREFIX = 'SHiNe/UserParameter:';
function normalizeServerUrl(url) { function normalizeServerUrl(url) {
const value = (url || '').trim(); const value = (url || '').trim();
@ -43,6 +46,67 @@ function makeClientInfo() {
return ua.slice(0, 50); return ua.slice(0, 50);
} }
function hexToBytes(hex) {
const clean = String(hex || '').trim().toLowerCase();
if (!clean || clean.length % 2 !== 0) throw new Error('Некорректный hex');
const out = new Uint8Array(clean.length / 2);
for (let i = 0; i < out.length; i += 1) {
out[i] = Number.parseInt(clean.slice(i * 2, i * 2 + 2), 16);
}
return out;
}
function concatBytes(...chunks) {
const total = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
const out = new Uint8Array(total);
let offset = 0;
chunks.forEach((chunk) => {
out.set(chunk, offset);
offset += chunk.length;
});
return out;
}
function int32Bytes(value) {
const bytes = new Uint8Array(4);
const view = new DataView(bytes.buffer);
view.setInt32(0, Number(value), false);
return bytes;
}
function int16Bytes(value) {
const bytes = new Uint8Array(2);
const view = new DataView(bytes.buffer);
view.setUint16(0, Number(value), false);
return bytes;
}
function int64Bytes(value) {
const bytes = new Uint8Array(8);
const view = new DataView(bytes.buffer);
view.setBigInt64(0, BigInt(value), false);
return bytes;
}
function makeUserParamBodyBytes({ lineCode, prevLineNumber, prevLineHashHex, thisLineNumber, key, value }) {
const keyBytes = utf8Bytes(String(key || ''));
const valueBytes = utf8Bytes(String(value || ''));
const prevHashBytes = hexToBytes(prevLineHashHex);
if (!keyBytes.length || !valueBytes.length) throw new Error('Пустые key/value для блока параметра');
if (prevHashBytes.length !== 32) throw new Error('prevLineHash должен быть 32 байта');
return concatBytes(
int32Bytes(lineCode),
int32Bytes(prevLineNumber),
prevHashBytes,
int32Bytes(thisLineNumber),
int16Bytes(keyBytes.length),
keyBytes,
int16Bytes(valueBytes.length),
valueBytes,
);
}
export class AuthService { export class AuthService {
constructor(serverUrl) { constructor(serverUrl) {
this.serverUrl = normalizeServerUrl(serverUrl); this.serverUrl = normalizeServerUrl(serverUrl);
@ -289,47 +353,91 @@ export class AuthService {
return response.payload?.logins || []; return response.payload?.logins || [];
} }
async listUserParams(login) {
const cleanLogin = (login || '').trim();
if (!cleanLogin) throw new Error('Не передан login');
const response = await this.ws.request('ListUserParams', { login: cleanLogin });
if (response.status !== 200) throw opError('ListUserParams', response);
return response.payload || {};
}
async upsertUserParam({ login, param, value, timeMs = Date.now(), storagePwd }) {
async getUserParam(login, param) {
const cleanLogin = (login || '').trim(); const cleanLogin = (login || '').trim();
const cleanParam = (param || '').trim(); const cleanParam = (param || '').trim();
if (!cleanLogin || !cleanParam) throw new Error('Не переданы login/param'); if (!cleanLogin || !cleanParam) throw new Error('Не переданы login/param');
if (!storagePwd) throw new Error('Не передан storagePwd для подписи UserParam');
const response = await this.ws.request('GetUserParam', { login: cleanLogin, param: cleanParam });
if (response.status === 200) return response.payload || {};
if (response.status === 404 || response.status === 204) return {};
throw opError('GetUserParam', response);
}
async addBlockUserParam({ login, param, value, storagePwd }) {
const cleanLogin = (login || '').trim();
const cleanParam = (param || '').trim();
const cleanValue = String(value ?? '').trim();
if (!cleanLogin || !cleanParam) throw new Error('Не переданы login/param.');
if (!cleanValue) throw new Error('Значение параметра не может быть пустым.');
if (!storagePwd) throw new Error('Не передан storagePwd для подписи AddBlock.');
const user = await this.getUser(cleanLogin); const user = await this.getUser(cleanLogin);
const deviceKey = String(user?.deviceKey || '').trim(); const blockchainName = String(user?.blockchainName || `${cleanLogin}-${BCH_SUFFIX}`).trim();
if (!deviceKey) {
throw new Error('GetUser не вернул deviceKey для подписи UserParam');
}
const savedKeys = await loadEncryptedUserSecrets(cleanLogin, storagePwd); const savedKeys = await loadEncryptedUserSecrets(cleanLogin, storagePwd);
const devicePrivatePkcs8 = savedKeys?.deviceKey; const blockchainPrivatePkcs8 = savedKeys?.blockchainKey;
if (!devicePrivatePkcs8) { if (!blockchainPrivatePkcs8) {
throw new Error('На устройстве нет сохраненного приватного deviceKey'); throw new Error('На устройстве нет сохраненного приватного blockchainKey');
} }
const privateKey = await importPkcs8Ed25519(devicePrivatePkcs8); const privateKey = await importPkcs8Ed25519(blockchainPrivatePkcs8);
const cleanValue = String(value ?? '');
const signText = `${USER_PARAMETER_PREFIX}${cleanLogin}${cleanParam}${timeMs}${cleanValue}`;
const signature = await signBase64(privateKey, signText);
const response = await this.ws.request('UpsertUserParam', { const tryAdd = async (cursor) => {
login: cleanLogin, const blockNumber = Number(cursor?.serverLastGlobalNumber ?? -1) + 1;
param: cleanParam, const prevBlockHash = String(cursor?.serverLastGlobalHash || '0'.repeat(64));
time_ms: Number(timeMs),
const bodyBytes = makeUserParamBodyBytes({
lineCode: Number(cursor?.serverLastGlobalNumber ?? 0),
prevLineNumber: Number(cursor?.serverLastGlobalNumber ?? 0),
prevLineHashHex: prevBlockHash,
thisLineNumber: 1,
key: cleanParam,
value: cleanValue, value: cleanValue,
device_key: deviceKey,
signature,
}); });
if (response.status !== 200) throw opError('UpsertUserParam', response); const preimage = concatBytes(
int16Bytes(0),
hexToBytes(prevBlockHash),
int32Bytes(2 + 32 + 4 + 4 + 8 + 2 + 2 + 2 + bodyBytes.length),
int32Bytes(blockNumber),
int64Bytes(Math.floor(Date.now() / 1000)),
int16Bytes(4),
int16Bytes(1),
int16Bytes(1),
bodyBytes,
);
const hash32 = await sha256Bytes(preimage);
const signatureBytes = await signBytes(privateKey, hash32);
const fullBlock = concatBytes(preimage, int16Bytes(0x0100), signatureBytes);
const response = await this.ws.request('AddBlock', {
blockchainName,
blockNumber,
prevBlockHash,
blockBytesB64: bytesToBase64(fullBlock),
});
return response;
};
let cursor = { serverLastGlobalNumber: -1, serverLastGlobalHash: '0'.repeat(64) };
let response = await tryAdd(cursor);
if (response.status !== 200) {
const knownNum = Number(response?.payload?.serverLastGlobalNumber);
const knownHash = String(response?.payload?.serverLastGlobalHash || '');
if (Number.isFinite(knownNum) && knownHash.length === 64) {
cursor = { serverLastGlobalNumber: knownNum, serverLastGlobalHash: knownHash };
response = await tryAdd(cursor);
}
}
if (response.status !== 200) throw opError('AddBlock', response);
return response.payload || {}; return response.payload || {};
} }

View File

@ -148,3 +148,8 @@ export async function signBase64(privateKey, text) {
const signature = await crypto.subtle.sign({ name: 'Ed25519' }, privateKey, utf8Bytes(text)); const signature = await crypto.subtle.sign({ name: 'Ed25519' }, privateKey, utf8Bytes(text));
return bytesToBase64(new Uint8Array(signature)); return bytesToBase64(new Uint8Array(signature));
} }
export async function signBytes(privateKey, bytes) {
const signature = await crypto.subtle.sign({ name: 'Ed25519' }, privateKey, bytes);
return new Uint8Array(signature);
}

View File

@ -1,11 +1,11 @@
import { authService, state } from '../state.js?v=20260403081123'; import { authService, state } from '../state.js?v=20260403081123';
export const profileFieldDefs = [ export const profileFieldDefs = [
{ key: 'first_name', readKeys: ['first_name', 'name'], label: 'First name', placeholder: 'Введите имя' }, { key: 'first_name', readKeys: ['first_name'], label: 'Имя', placeholder: 'Введите имя' },
{ key: 'last_name', readKeys: ['last_name'], label: 'Last name', placeholder: 'Введите фамилию' }, { key: 'last_name', readKeys: ['last_name'], label: 'Фамилия', placeholder: 'Введите фамилию' },
{ key: 'address_physical', readKeys: ['address_physical'], label: 'Address physical', placeholder: 'Город, улица, дом' }, { key: 'address', readKeys: ['address'], label: 'Адрес', placeholder: 'Город, улица, дом' },
{ key: 'address_web', readKeys: ['address_web'], label: 'Address web', placeholder: 'Сайт или профиль' }, { key: 'web', readKeys: ['web'], label: 'Веб', placeholder: 'Сайт или профиль' },
{ key: 'phone', readKeys: ['phone'], label: 'Phone', placeholder: '+7 ...' }, { key: 'phone', readKeys: ['phone'], label: 'Телефон', placeholder: '+7 ...' },
]; ];
export const profileToggleDefs = [ export const profileToggleDefs = [
@ -13,27 +13,17 @@ export const profileToggleDefs = [
{ key: 'shine', label: 'Сияющий' }, { key: 'shine', label: 'Сияющий' },
]; ];
function normalizeItems(responsePayload) { function normalizeItem(param, payload) {
const params = responsePayload?.params; if (!param) return null;
if (!Array.isArray(params)) return [];
return params if (payload && typeof payload === 'object') {
.map((item) => ({ const value = String(payload?.value || payload?.param_value || '');
param: String(item?.param || '').trim(), const timeMs = Number(payload?.time_ms || payload?.timeMs || 0);
value: String(item?.value || ''), if (!value && !timeMs) return null;
timeMs: Number(item?.time_ms || 0), return { param, value, timeMs };
}))
.filter((item) => item.param);
} }
function getLatestByAliases(items, aliases) { return null;
let latest = null;
items.forEach((item) => {
if (!aliases.includes(item.param)) return;
if (!latest || item.timeMs >= latest.timeMs) {
latest = item;
}
});
return latest;
} }
function parseToggleValue(value) { function parseToggleValue(value) {
@ -49,58 +39,70 @@ async function getStoragePwd() {
return storagePwd; return storagePwd;
} }
export async function loadProfileSnapshot(login) { async function loadLatestByAliases(login, aliases) {
const payload = await authService.listUserParams(login); const collected = [];
const items = normalizeItems(payload);
const fields = profileFieldDefs.map((field) => { for (let i = 0; i < aliases.length; i += 1) {
const latest = getLatestByAliases(items, field.readKeys); const alias = aliases[i];
return { try {
const payload = await authService.getUserParam(login, alias);
const normalized = normalizeItem(alias, payload);
if (normalized) collected.push(normalized);
} catch {
// Пусто — параметр ещё не создан или endpoint не отвечает для конкретного ключа.
}
}
if (!collected.length) return null;
return collected.sort((a, b) => b.timeMs - a.timeMs)[0];
}
export async function loadProfileSnapshot(login) {
const fields = [];
for (let i = 0; i < profileFieldDefs.length; i += 1) {
const field = profileFieldDefs[i];
const latest = await loadLatestByAliases(login, field.readKeys);
fields.push({
key: field.key, key: field.key,
label: field.label, label: field.label,
placeholder: field.placeholder, placeholder: field.placeholder,
value: latest?.value || '', value: latest?.value || '',
timeMs: latest?.timeMs || 0, timeMs: latest?.timeMs || 0,
};
}); });
}
const toggles = profileToggleDefs.map((toggle) => { const toggles = [];
const latest = getLatestByAliases(items, [toggle.key]); for (let i = 0; i < profileToggleDefs.length; i += 1) {
return { const toggle = profileToggleDefs[i];
const latest = await loadLatestByAliases(login, [toggle.key]);
toggles.push({
key: toggle.key, key: toggle.key,
label: toggle.label, label: toggle.label,
enabled: latest ? parseToggleValue(latest.value) : false, enabled: latest ? parseToggleValue(latest.value) : false,
rawValue: latest?.value || 'no', rawValue: latest?.value || 'no',
timeMs: latest?.timeMs || 0, timeMs: latest?.timeMs || 0,
};
}); });
}
return { fields, toggles }; return { fields, toggles };
} }
export async function saveProfileParams(login, valuesByKey) { export async function saveProfileParamBlock(login, key, value) {
const storagePwd = await getStoragePwd(); const storagePwd = await getStoragePwd();
const baseTime = Date.now(); await authService.addBlockUserParam({
for (let i = 0; i < profileFieldDefs.length; i += 1) {
const field = profileFieldDefs[i];
await authService.upsertUserParam({
login, login,
param: field.key, param: key,
value: String(valuesByKey[field.key] || '').trim(), value: String(value ?? '').trim(),
timeMs: baseTime + i,
storagePwd, storagePwd,
}); });
} }
}
export async function saveProfileToggle(login, key, enabled) { export async function saveProfileToggle(login, key, enabled) {
const storagePwd = await getStoragePwd(); const storagePwd = await getStoragePwd();
await authService.upsertUserParam({ await authService.addBlockUserParam({
login, login,
param: key, param: key,
value: enabled ? 'yes' : 'no', value: enabled ? 'yes' : 'no',
timeMs: Date.now(),
storagePwd, storagePwd,
}); });
} }

View File

@ -97,6 +97,24 @@
background: rgba(83, 216, 251, 0.11); background: rgba(83, 216, 251, 0.11);
} }
.badge.profile-toggle-btn.is-no {
border-color: rgba(170, 180, 205, 0.3);
color: #c5cedd;
background: rgba(152, 164, 190, 0.14);
}
.badge.profile-toggle-btn.is-yes-official {
border-color: rgba(132, 244, 161, 0.5);
color: #ddffe7;
background: rgba(132, 244, 161, 0.2);
}
.badge.profile-toggle-btn.is-yes-shine {
border-color: rgba(183, 122, 255, 0.6);
color: #f4e7ff;
background: rgba(176, 102, 255, 0.22);
}
.profile-help-modal[hidden] { .profile-help-modal[hidden] {
display: none; display: none;
} }