SHA256
233 lines
8.8 KiB
JavaScript
233 lines
8.8 KiB
JavaScript
import { createTopBar } from '../components/topbar.js';
|
|
import { authService, state } from '../state.js';
|
|
import { loadRelationsForPair, loadUserProfileCard } from '../services/user-connections.js';
|
|
import { navigateBack } from '../router.js';
|
|
import { escapeProfileHtml } from '../components/profile-card.js';
|
|
|
|
export const pageMeta = { id: 'user-relation-manage-view', title: 'Добавить связь' };
|
|
|
|
const SOCIAL_LABELS = {
|
|
none: 'Нет связи',
|
|
contact: 'Контакт',
|
|
friend: 'Друг',
|
|
close_friend: 'Близкий друг',
|
|
};
|
|
|
|
const MUTUAL_LABELS = {
|
|
contact: 'контакты',
|
|
friend: 'друзья',
|
|
close_friend: 'близкие друзья',
|
|
};
|
|
|
|
function effectiveSocial(flags = {}, direction = 'out') {
|
|
const prefix = direction === 'in' ? 'in' : 'out';
|
|
if (flags[`${prefix}CloseFriend`]) return 'close_friend';
|
|
if (flags[`${prefix}Friend`]) return 'friend';
|
|
if (flags[`${prefix}Contact`]) return 'contact';
|
|
return 'none';
|
|
}
|
|
|
|
function relationSummaryHtml(flags = {}) {
|
|
const outgoing = effectiveSocial(flags, 'out');
|
|
const incoming = effectiveSocial(flags, 'in');
|
|
|
|
if (outgoing !== 'none' && outgoing === incoming) {
|
|
return `
|
|
<div class="relation-manage-mutual">
|
|
<span class="relation-manage-mutual-label">Взаимно:</span>
|
|
<strong>${escapeProfileHtml(MUTUAL_LABELS[outgoing])}</strong>
|
|
</div>`;
|
|
}
|
|
|
|
return `
|
|
<div class="relation-manage-current">
|
|
<div class="relation-manage-current-row">
|
|
<span>Вы к пользователю</span>
|
|
<strong>${escapeProfileHtml(SOCIAL_LABELS[outgoing])}</strong>
|
|
</div>
|
|
${incoming !== 'none' ? `
|
|
<div class="relation-manage-current-row">
|
|
<span>Пользователь к вам</span>
|
|
<strong>${escapeProfileHtml(SOCIAL_LABELS[incoming])}</strong>
|
|
</div>` : ''}
|
|
</div>`;
|
|
}
|
|
|
|
function socialButtonsHtml(flags = {}) {
|
|
const current = effectiveSocial(flags, 'out');
|
|
const kinds = ['none', 'contact', 'friend', 'close_friend'];
|
|
return kinds.map((kind) => `
|
|
<button type="button" class="secondary-btn relation-manage-choice${current === kind ? ' is-active' : ''}" data-social-kind="${kind}">
|
|
${escapeProfileHtml(SOCIAL_LABELS[kind])}
|
|
</button>`).join('');
|
|
}
|
|
|
|
function officialBlockHtml(flags = {}) {
|
|
return `
|
|
<section class="card stack relation-manage-card relation-manage-card--official">
|
|
<div class="relation-manage-card-head">
|
|
<strong>Основной аккаунт</strong>
|
|
<span>Этот аккаунт — основной</span>
|
|
</div>
|
|
<div class="relation-manage-confirmations">
|
|
${flags.outOfficialAccount ? '<div class="relation-manage-confirmation">✓ Вы подтвердили его основной аккаунт</div>' : ''}
|
|
${flags.inOfficialAccount ? '<div class="relation-manage-confirmation">✓ Этот пользователь подтвердил ваш основной аккаунт</div>' : ''}
|
|
</div>
|
|
<button type="button" class="secondary-btn relation-manage-confirm-btn" data-confirm="official_account">
|
|
${flags.outOfficialAccount ? 'Убрать подтверждение' : 'Подтвердить основной аккаунт'}
|
|
</button>
|
|
</section>`;
|
|
}
|
|
|
|
function shineBlockHtml(flags = {}) {
|
|
return `
|
|
<section class="card stack relation-manage-card relation-manage-card--shine">
|
|
<div class="relation-manage-card-head">
|
|
<strong>Сияющий</strong>
|
|
<span>Этот аккаунт — сияющий</span>
|
|
</div>
|
|
<div class="relation-manage-confirmations">
|
|
${flags.outShineConfirmed ? '<div class="relation-manage-confirmation">✓ Вы подтверждаете его сияющим</div>' : ''}
|
|
${flags.inShineConfirmed ? '<div class="relation-manage-confirmation">✓ Этот пользователь подтверждает вас сияющим</div>' : ''}
|
|
</div>
|
|
<button type="button" class="secondary-btn relation-manage-confirm-btn" data-confirm="shine_confirmed">
|
|
${flags.outShineConfirmed ? 'Убрать подтверждение' : 'Подтвердить как сияющего'}
|
|
</button>
|
|
</section>`;
|
|
}
|
|
|
|
export function render({ route, chrome }) {
|
|
const targetLogin = String(route?.params?.login || '').trim();
|
|
const selfLogin = String(state.session.login || '').trim();
|
|
const screen = document.createElement('section');
|
|
screen.className = 'stack relation-manage-screen';
|
|
|
|
const body = document.createElement('div');
|
|
body.className = 'stack relation-manage-body';
|
|
const status = document.createElement('div');
|
|
status.className = 'status-line relation-manage-status';
|
|
|
|
chrome?.setTopbar(createTopBar({
|
|
title: 'Добавить связь',
|
|
back: { label: '←', onClick: () => navigateBack() },
|
|
}));
|
|
screen.append(status, body);
|
|
|
|
let flags = {};
|
|
let selfCard = null;
|
|
let targetCard = null;
|
|
let busy = false;
|
|
|
|
async function setKind(kind, enabled) {
|
|
await authService.setUserRelation({
|
|
login: selfLogin,
|
|
toLogin: targetLogin,
|
|
kind,
|
|
enabled,
|
|
storagePwd: state.session.storagePwdInMemory,
|
|
});
|
|
}
|
|
|
|
async function changeSocial(next) {
|
|
const current = effectiveSocial(flags, 'out');
|
|
if (current === next) return;
|
|
|
|
if (next === 'none') {
|
|
if (flags.outCloseFriend) await setKind('close_friend', false);
|
|
if (flags.outFriend) await setKind('friend', false);
|
|
if (flags.outContact) await setKind('contact', false);
|
|
}
|
|
if (next === 'contact') {
|
|
if (flags.outCloseFriend) await setKind('close_friend', false);
|
|
if (flags.outFriend) await setKind('friend', false);
|
|
if (!flags.outContact) await setKind('contact', true);
|
|
}
|
|
if (next === 'friend') {
|
|
if (flags.outCloseFriend) await setKind('close_friend', false);
|
|
if (!flags.outFriend) await setKind('friend', true);
|
|
}
|
|
if (next === 'close_friend' && !flags.outCloseFriend) {
|
|
await setKind('close_friend', true);
|
|
}
|
|
}
|
|
|
|
function renderBody() {
|
|
const canPrimary = selfCard?.accountRole === 'primary' && targetCard?.accountRole === 'primary';
|
|
const canShine = selfCard?.shineStatus === 'shining' && targetCard?.shineStatus === 'shining';
|
|
|
|
body.innerHTML = `
|
|
<section class="card stack relation-manage-card relation-manage-card--social">
|
|
<div class="relation-manage-card-head">
|
|
<strong>Связь</strong>
|
|
<span>@${escapeProfileHtml(targetCard?.login || targetLogin)}</span>
|
|
</div>
|
|
${relationSummaryHtml(flags)}
|
|
<div class="relation-manage-choices" aria-label="Изменить ваше отношение к пользователю">
|
|
${socialButtonsHtml(flags)}
|
|
</div>
|
|
</section>
|
|
${canPrimary ? officialBlockHtml(flags) : ''}
|
|
${canShine ? shineBlockHtml(flags) : ''}
|
|
`;
|
|
}
|
|
|
|
async function refresh({ loading = true } = {}) {
|
|
if (loading) {
|
|
status.className = 'status-line relation-manage-status';
|
|
status.textContent = 'Загрузка...';
|
|
}
|
|
[selfCard, targetCard, flags] = await Promise.all([
|
|
loadUserProfileCard(selfLogin),
|
|
loadUserProfileCard(targetLogin),
|
|
loadRelationsForPair({ currentLogin: selfLogin, targetLogin }),
|
|
]);
|
|
renderBody();
|
|
status.className = 'status-line relation-manage-status';
|
|
status.textContent = '';
|
|
}
|
|
|
|
body.addEventListener('click', async (event) => {
|
|
const button = event.target.closest('button');
|
|
if (!button || busy) return;
|
|
|
|
const social = button.dataset.socialKind;
|
|
const confirm = button.dataset.confirm;
|
|
if (!social && !confirm) return;
|
|
|
|
busy = true;
|
|
body.classList.add('is-busy');
|
|
status.className = 'status-line relation-manage-status';
|
|
status.textContent = 'Сохранение...';
|
|
|
|
try {
|
|
if (social) {
|
|
await changeSocial(social);
|
|
} else if (confirm === 'official_account') {
|
|
await setKind('official_account', !flags.outOfficialAccount);
|
|
} else if (confirm === 'shine_confirmed') {
|
|
await setKind('shine_confirmed', !flags.outShineConfirmed);
|
|
}
|
|
await refresh({ loading: false });
|
|
} catch (error) {
|
|
status.className = 'status-line relation-manage-status is-unavailable';
|
|
status.textContent = `Ошибка: ${error?.message || 'Не удалось изменить связь'}`;
|
|
} finally {
|
|
busy = false;
|
|
body.classList.remove('is-busy');
|
|
}
|
|
});
|
|
|
|
if (!selfLogin || !targetLogin) {
|
|
status.className = 'status-line relation-manage-status is-unavailable';
|
|
status.textContent = 'Не удалось определить пользователя.';
|
|
return screen;
|
|
}
|
|
|
|
refresh().catch((error) => {
|
|
status.className = 'status-line relation-manage-status is-unavailable';
|
|
status.textContent = `Ошибка: ${error?.message || 'Не удалось загрузить связи'}`;
|
|
});
|
|
|
|
return screen;
|
|
}
|