SHA256
Trim profile param flow to AddBlock-only write path
This commit is contained in:
@@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user