Trim profile param flow to AddBlock-only write path
This commit is contained in:
parent
3a412fcd51
commit
4deaedf79f
@ -3,27 +3,21 @@ import { profile } from '../mock-data.js?v=20260405171816';
|
||||
import { state } from '../state.js?v=20260405171816';
|
||||
import {
|
||||
loadProfileSnapshot,
|
||||
profileFieldDefs,
|
||||
saveProfileParams,
|
||||
saveProfileParamBlock,
|
||||
saveProfileToggle,
|
||||
} from '../services/user-profile-params.js?v=20260405171816';
|
||||
|
||||
export const pageMeta = { id: 'profile-view', title: 'Профиль' };
|
||||
|
||||
function formatDateTime(timeMs) {
|
||||
if (!timeMs) return 'ещё не заполнено';
|
||||
return new Date(timeMs).toLocaleString('ru-RU');
|
||||
}
|
||||
|
||||
function getDisplayName(fieldMap) {
|
||||
const firstName = fieldMap.get('first_name')?.value?.trim() || '';
|
||||
const lastName = fieldMap.get('last_name')?.value?.trim() || '';
|
||||
function getDisplayName(fields) {
|
||||
const firstName = fields.find((field) => field.key === 'first_name')?.value?.trim() || '';
|
||||
const lastName = fields.find((field) => field.key === 'last_name')?.value?.trim() || '';
|
||||
const fullName = `${firstName} ${lastName}`.trim();
|
||||
return fullName || profile.name;
|
||||
}
|
||||
|
||||
function toggleText(enabled) {
|
||||
return enabled ? 'yes' : 'no';
|
||||
return enabled ? 'Yes' : 'No';
|
||||
}
|
||||
|
||||
export function render({ navigate }) {
|
||||
@ -55,21 +49,21 @@ export function render({ navigate }) {
|
||||
<p class="meta-muted">${login}</p>
|
||||
</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');
|
||||
badgesRow.className = 'row';
|
||||
badgesRow.innerHTML = `
|
||||
<button class="badge profile-toggle-btn" 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="official">Официальный: No</button>
|
||||
<button class="badge profile-toggle-btn is-no" type="button" data-toggle="shine">Сияющий: No</button>
|
||||
`;
|
||||
|
||||
const hint = document.createElement('div');
|
||||
hint.className = 'card profile-data-help';
|
||||
hint.innerHTML = `
|
||||
<div class="meta-muted">Личные данные пользователя</div>
|
||||
<p>Поля ниже читаются из реальных пользовательских параметров сервера (ListUserParams). Любое изменение отправляется как блокчейн-запись параметра и требует подпись ключом пользователя.</p>
|
||||
<p>Параметры читаются через API GetUserParam. Изменения записываются в блокчейн и после этого список сразу обновляется.</p>
|
||||
`;
|
||||
|
||||
const status = document.createElement('div');
|
||||
@ -79,65 +73,48 @@ export function render({ navigate }) {
|
||||
const listWrap = document.createElement('div');
|
||||
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 openEditBtn = topRow.querySelector('[data-open-edit="true"]');
|
||||
const reloadBtn = topRow.querySelector('[data-reload="true"]');
|
||||
const officialBtn = badgesRow.querySelector('[data-toggle="official"]');
|
||||
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 currentToggles = [
|
||||
{ key: 'official', enabled: false, timeMs: 0 },
|
||||
{ key: 'shine', enabled: false, timeMs: 0 },
|
||||
];
|
||||
let currentFields = [];
|
||||
let currentToggles = [];
|
||||
|
||||
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() {
|
||||
const official = currentToggles.find((item) => item.key === 'official') || { enabled: false };
|
||||
const shine = currentToggles.find((item) => item.key === 'shine') || { enabled: false };
|
||||
|
||||
officialBtn.textContent = `✔ Официальный: ${toggleText(official.enabled)}`;
|
||||
shineBtn.textContent = `✨ Сияющий: ${toggleText(shine.enabled)}`;
|
||||
updateToggleButton(officialBtn, 'Официальный', official.enabled);
|
||||
updateToggleButton(shineBtn, 'Сияющий', shine.enabled);
|
||||
}
|
||||
|
||||
function renderFields(fields) {
|
||||
const fieldMap = new Map(fields.map((field) => [field.key, field]));
|
||||
profileNameEl.textContent = getDisplayName(fieldMap);
|
||||
profileNameEl.textContent = getDisplayName(fields);
|
||||
|
||||
listWrap.innerHTML = '';
|
||||
fields.forEach((field) => {
|
||||
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 = `
|
||||
<div class="profile-param-head">
|
||||
<span class="meta-muted">${field.label}</span>
|
||||
<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>
|
||||
<div class="profile-param-value"><b>${field.label}</b>: ${value}</div>
|
||||
<button class="ghost-btn" type="button" data-edit-field="${field.key}">Изменить</button>
|
||||
`;
|
||||
listWrap.append(row);
|
||||
});
|
||||
@ -146,7 +123,7 @@ export function render({ navigate }) {
|
||||
async function refreshProfileSnapshot() {
|
||||
status.className = 'status-line';
|
||||
status.textContent = 'Загрузка параметров...';
|
||||
openEditBtn.disabled = true;
|
||||
reloadBtn.disabled = true;
|
||||
officialBtn.disabled = true;
|
||||
shineBtn.disabled = true;
|
||||
|
||||
@ -154,77 +131,35 @@ export function render({ navigate }) {
|
||||
const snapshot = await loadProfileSnapshot(login);
|
||||
currentFields = snapshot.fields;
|
||||
currentToggles = snapshot.toggles;
|
||||
renderFields(snapshot.fields);
|
||||
updateTogglesUi();
|
||||
status.className = 'status-line is-available';
|
||||
status.textContent = 'Актуальные параметры загружены с сервера.';
|
||||
} catch (error) {
|
||||
|
||||
renderFields(currentFields);
|
||||
updateTogglesUi();
|
||||
|
||||
status.className = 'status-line is-available';
|
||||
status.textContent = 'Актуальные параметры загружены.';
|
||||
} catch (error) {
|
||||
status.className = 'status-line is-unavailable';
|
||||
status.textContent = `Не удалось загрузить параметры: ${error.message || 'ошибка сети'}`;
|
||||
} finally {
|
||||
openEditBtn.disabled = false;
|
||||
reloadBtn.disabled = false;
|
||||
officialBtn.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) {
|
||||
const toggle = currentToggles.find((item) => item.key === toggleKey) || { enabled: false };
|
||||
const nextEnabled = !toggle.enabled;
|
||||
const title = toggleKey === 'official' ? 'Официальный аккаунт' : 'Сияющий аккаунт';
|
||||
const title = toggleKey === 'official' ? 'официальный' : 'сияющий';
|
||||
|
||||
const confirmed = window.confirm(
|
||||
`Изменить параметр «${title}» на ${toggleText(nextEnabled)}?\n\n` +
|
||||
'Внимание: изменение будет записано как блокчейн-параметр пользователя и требует подписи ключом блокчейна/пользователя на устройстве.',
|
||||
`Хотите изменить «${title}» на ${toggleText(nextEnabled)}?\n` +
|
||||
'Будет создана запись в блокчейне.',
|
||||
);
|
||||
|
||||
if (!confirmed) return;
|
||||
|
||||
status.className = 'status-line';
|
||||
status.textContent = 'Отправка изменения в блокчейн...';
|
||||
status.textContent = 'Сохранение в блокчейн...';
|
||||
|
||||
try {
|
||||
await saveProfileToggle(login, toggleKey, nextEnabled);
|
||||
@ -235,25 +170,44 @@ export function render({ navigate }) {
|
||||
}
|
||||
}
|
||||
|
||||
openEditBtn.addEventListener('click', openEditModal);
|
||||
saveBtn.addEventListener('click', saveChanges);
|
||||
async function onEditFieldClick(fieldKey) {
|
||||
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'));
|
||||
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);
|
||||
screen.append(card, editModal);
|
||||
screen.append(card);
|
||||
|
||||
refreshProfileSnapshot();
|
||||
|
||||
|
||||
@ -1,12 +1,16 @@
|
||||
import { WsJsonClient } from './ws-client.js?v=20260405171816';
|
||||
import {
|
||||
bytesToBase64,
|
||||
deriveEd25519FromPassword,
|
||||
exportEd25519PublicKeyB64,
|
||||
exportPkcs8B64,
|
||||
generateEd25519Pair,
|
||||
importPkcs8Ed25519,
|
||||
randomBase64,
|
||||
sha256Bytes,
|
||||
signBytes,
|
||||
signBase64,
|
||||
utf8Bytes,
|
||||
} from './crypto-utils.js?v=20260405171816';
|
||||
import {
|
||||
loadEncryptedUserSecrets,
|
||||
@ -16,7 +20,6 @@ import {
|
||||
} from './key-vault.js?v=20260405171816';
|
||||
|
||||
const BCH_SUFFIX = '001';
|
||||
const USER_PARAMETER_PREFIX = 'SHiNe/UserParameter:';
|
||||
|
||||
function normalizeServerUrl(url) {
|
||||
const value = (url || '').trim();
|
||||
@ -43,6 +46,67 @@ function makeClientInfo() {
|
||||
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 {
|
||||
constructor(serverUrl) {
|
||||
this.serverUrl = normalizeServerUrl(serverUrl);
|
||||
@ -289,47 +353,91 @@ export class AuthService {
|
||||
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 cleanParam = (param || '').trim();
|
||||
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 deviceKey = String(user?.deviceKey || '').trim();
|
||||
if (!deviceKey) {
|
||||
throw new Error('GetUser не вернул deviceKey для подписи UserParam');
|
||||
}
|
||||
const blockchainName = String(user?.blockchainName || `${cleanLogin}-${BCH_SUFFIX}`).trim();
|
||||
|
||||
const savedKeys = await loadEncryptedUserSecrets(cleanLogin, storagePwd);
|
||||
const devicePrivatePkcs8 = savedKeys?.deviceKey;
|
||||
if (!devicePrivatePkcs8) {
|
||||
throw new Error('На устройстве нет сохраненного приватного deviceKey');
|
||||
const blockchainPrivatePkcs8 = savedKeys?.blockchainKey;
|
||||
if (!blockchainPrivatePkcs8) {
|
||||
throw new Error('На устройстве нет сохраненного приватного blockchainKey');
|
||||
}
|
||||
|
||||
const privateKey = await importPkcs8Ed25519(devicePrivatePkcs8);
|
||||
const cleanValue = String(value ?? '');
|
||||
const signText = `${USER_PARAMETER_PREFIX}${cleanLogin}${cleanParam}${timeMs}${cleanValue}`;
|
||||
const signature = await signBase64(privateKey, signText);
|
||||
const privateKey = await importPkcs8Ed25519(blockchainPrivatePkcs8);
|
||||
|
||||
const response = await this.ws.request('UpsertUserParam', {
|
||||
login: cleanLogin,
|
||||
param: cleanParam,
|
||||
time_ms: Number(timeMs),
|
||||
value: cleanValue,
|
||||
device_key: deviceKey,
|
||||
signature,
|
||||
});
|
||||
const tryAdd = async (cursor) => {
|
||||
const blockNumber = Number(cursor?.serverLastGlobalNumber ?? -1) + 1;
|
||||
const prevBlockHash = String(cursor?.serverLastGlobalHash || '0'.repeat(64));
|
||||
|
||||
if (response.status !== 200) throw opError('UpsertUserParam', response);
|
||||
const bodyBytes = makeUserParamBodyBytes({
|
||||
lineCode: Number(cursor?.serverLastGlobalNumber ?? 0),
|
||||
prevLineNumber: Number(cursor?.serverLastGlobalNumber ?? 0),
|
||||
prevLineHashHex: prevBlockHash,
|
||||
thisLineNumber: 1,
|
||||
key: cleanParam,
|
||||
value: cleanValue,
|
||||
});
|
||||
|
||||
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 || {};
|
||||
}
|
||||
|
||||
|
||||
@ -148,3 +148,8 @@ export async function signBase64(privateKey, text) {
|
||||
const signature = await crypto.subtle.sign({ name: 'Ed25519' }, privateKey, utf8Bytes(text));
|
||||
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);
|
||||
}
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import { authService, state } from '../state.js?v=20260403081123';
|
||||
|
||||
export const profileFieldDefs = [
|
||||
{ key: 'first_name', readKeys: ['first_name', 'name'], label: 'First name', placeholder: 'Введите имя' },
|
||||
{ key: 'last_name', readKeys: ['last_name'], label: 'Last name', placeholder: 'Введите фамилию' },
|
||||
{ key: 'address_physical', readKeys: ['address_physical'], label: 'Address physical', placeholder: 'Город, улица, дом' },
|
||||
{ key: 'address_web', readKeys: ['address_web'], label: 'Address web', placeholder: 'Сайт или профиль' },
|
||||
{ key: 'phone', readKeys: ['phone'], label: 'Phone', placeholder: '+7 ...' },
|
||||
{ key: 'first_name', readKeys: ['first_name'], label: 'Имя', placeholder: 'Введите имя' },
|
||||
{ key: 'last_name', readKeys: ['last_name'], label: 'Фамилия', placeholder: 'Введите фамилию' },
|
||||
{ key: 'address', readKeys: ['address'], label: 'Адрес', placeholder: 'Город, улица, дом' },
|
||||
{ key: 'web', readKeys: ['web'], label: 'Веб', placeholder: 'Сайт или профиль' },
|
||||
{ key: 'phone', readKeys: ['phone'], label: 'Телефон', placeholder: '+7 ...' },
|
||||
];
|
||||
|
||||
export const profileToggleDefs = [
|
||||
@ -13,27 +13,17 @@ export const profileToggleDefs = [
|
||||
{ key: 'shine', label: 'Сияющий' },
|
||||
];
|
||||
|
||||
function normalizeItems(responsePayload) {
|
||||
const params = responsePayload?.params;
|
||||
if (!Array.isArray(params)) return [];
|
||||
return params
|
||||
.map((item) => ({
|
||||
param: String(item?.param || '').trim(),
|
||||
value: String(item?.value || ''),
|
||||
timeMs: Number(item?.time_ms || 0),
|
||||
}))
|
||||
.filter((item) => item.param);
|
||||
}
|
||||
function normalizeItem(param, payload) {
|
||||
if (!param) return null;
|
||||
|
||||
function getLatestByAliases(items, aliases) {
|
||||
let latest = null;
|
||||
items.forEach((item) => {
|
||||
if (!aliases.includes(item.param)) return;
|
||||
if (!latest || item.timeMs >= latest.timeMs) {
|
||||
latest = item;
|
||||
}
|
||||
});
|
||||
return latest;
|
||||
if (payload && typeof payload === 'object') {
|
||||
const value = String(payload?.value || payload?.param_value || '');
|
||||
const timeMs = Number(payload?.time_ms || payload?.timeMs || 0);
|
||||
if (!value && !timeMs) return null;
|
||||
return { param, value, timeMs };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseToggleValue(value) {
|
||||
@ -49,58 +39,70 @@ async function getStoragePwd() {
|
||||
return storagePwd;
|
||||
}
|
||||
|
||||
export async function loadProfileSnapshot(login) {
|
||||
const payload = await authService.listUserParams(login);
|
||||
const items = normalizeItems(payload);
|
||||
async function loadLatestByAliases(login, aliases) {
|
||||
const collected = [];
|
||||
|
||||
const fields = profileFieldDefs.map((field) => {
|
||||
const latest = getLatestByAliases(items, field.readKeys);
|
||||
return {
|
||||
for (let i = 0; i < aliases.length; i += 1) {
|
||||
const alias = aliases[i];
|
||||
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,
|
||||
label: field.label,
|
||||
placeholder: field.placeholder,
|
||||
value: latest?.value || '',
|
||||
timeMs: latest?.timeMs || 0,
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const toggles = profileToggleDefs.map((toggle) => {
|
||||
const latest = getLatestByAliases(items, [toggle.key]);
|
||||
return {
|
||||
const toggles = [];
|
||||
for (let i = 0; i < profileToggleDefs.length; i += 1) {
|
||||
const toggle = profileToggleDefs[i];
|
||||
const latest = await loadLatestByAliases(login, [toggle.key]);
|
||||
toggles.push({
|
||||
key: toggle.key,
|
||||
label: toggle.label,
|
||||
enabled: latest ? parseToggleValue(latest.value) : false,
|
||||
rawValue: latest?.value || 'no',
|
||||
timeMs: latest?.timeMs || 0,
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return { fields, toggles };
|
||||
}
|
||||
|
||||
export async function saveProfileParams(login, valuesByKey) {
|
||||
export async function saveProfileParamBlock(login, key, value) {
|
||||
const storagePwd = await getStoragePwd();
|
||||
const baseTime = Date.now();
|
||||
|
||||
for (let i = 0; i < profileFieldDefs.length; i += 1) {
|
||||
const field = profileFieldDefs[i];
|
||||
await authService.upsertUserParam({
|
||||
login,
|
||||
param: field.key,
|
||||
value: String(valuesByKey[field.key] || '').trim(),
|
||||
timeMs: baseTime + i,
|
||||
storagePwd,
|
||||
});
|
||||
}
|
||||
await authService.addBlockUserParam({
|
||||
login,
|
||||
param: key,
|
||||
value: String(value ?? '').trim(),
|
||||
storagePwd,
|
||||
});
|
||||
}
|
||||
|
||||
export async function saveProfileToggle(login, key, enabled) {
|
||||
const storagePwd = await getStoragePwd();
|
||||
await authService.upsertUserParam({
|
||||
await authService.addBlockUserParam({
|
||||
login,
|
||||
param: key,
|
||||
value: enabled ? 'yes' : 'no',
|
||||
timeMs: Date.now(),
|
||||
storagePwd,
|
||||
});
|
||||
}
|
||||
|
||||
@ -97,6 +97,24 @@
|
||||
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] {
|
||||
display: none;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user