Files
forumlify/js/user.js
T
Furrium Network 7c66cb931a feat: 新增用户屏蔽功能
屏蔽为单向关系:A 屏蔽 B 之后
  - B 不能再给 A 发私信(含已有会话,以及发起新会话)
  - A 在帖子列表、帖子搜索、帖子详情回复中不再看到 B 的内容
  - A 的用户搜索中不再出现 B
  - B 不会收到任何提示,仍可正常浏览 A 的公开内容
取消屏蔽后全部恢复。

数据层(迁移 006)
- 新增 user_blocks 表,唯一约束 (blocker_id, blocked_id),并加
  CHECK 禁止屏蔽自己;两个方向各建索引(正向用于过滤内容,
  反向用于判断能否发私信)。

后端
- 新增 POST / DELETE /api/users/:id/block、GET /api/blocks、
  GET /api/users/:id/block-status。block-status 返回双向关系,
  便于界面准确提示。
- 新增 optionalAuth 中间件:公开接口(帖子列表、搜索、详情、回复)
  在已登录时解析出 req.user 并应用屏蔽过滤,未登录或令牌无效时
  直接放行。这样未登录访客看到全部内容,登录用户则按自己的屏蔽
  列表过滤。
- 帖子列表的 count 与主查询改为共用同一套 WHERE 条件——此前两者
  分别拼装,加上过滤后若不一致会导致分页总数与实际结果对不上。
- 帖子搜索的关键词条件补上括号:它与屏蔽过滤之间是 AND 关系,
  而 AND 优先级高于 OR,不加括号会让过滤失效。
- 私信发送与会话创建均校验对方是否屏蔽了自己,返回中性提示
  「无法向该用户发送消息」,不透露被屏蔽的事实。

前端
- 用户主页新增屏蔽按钮,按当前状态显示「屏蔽」或「解除屏蔽」;
  已屏蔽时隐藏发私信入口(避免必然失败的尝试),并显示状态说明。
- 私信面板新增「会话 / 已屏蔽」切换,已屏蔽列表可逐个解除。
- 私信搜索结果行新增屏蔽按钮,点击时阻止冒泡以免进入聊天。
- 屏蔽与解除都需要二次确认,文案说明后果。

实测:
- 屏蔽前后对比:对方帖子由可见 1 条变为 0 条,用户搜索由 1 个
  变为 0 个,对方发起会话被拒
- 解除屏蔽后帖子恢复可见
- 不能屏蔽自己
- 界面:用户主页按钮文案与提示随状态变化,私信面板两个 Tab 正常,
  已屏蔽列表显示用户与屏蔽时间

完整测试套件 25/25 通过。
2026-09-22 22:36:42 +08:00

157 lines
7.2 KiB
JavaScript

// ============================================================
// 👤 用户主页
// ============================================================
async function renderUserProfile(username) {
const container = document.getElementById('userProfileContent');
container.innerHTML = Skeleton.userProfile();
try {
// 使用公开接口 /users/profile/:username
const user = await apiFetch('/users/profile/' + encodeURIComponent(username));
if (user.error || !user.id) {
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">用户不存在</div>';
return;
}
document.getElementById('userPageTitle').textContent = '👤 ' + user.username;
const result = await apiFetch('/posts?user_id=' + user.id + '&page=1&limit=100');
const posts = result.data || [];
const avatar = user.avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(user.username) + '&background=6366f1&color=fff&size=128';
// 已登录且不是本人时,查一次屏蔽关系,用于决定按钮文案
let blockStatus = { blocked_by_me: false, blocked_me: false };
if (currentUser && currentUser.id !== user.id) {
try {
blockStatus = await API.getBlockStatus(user.id);
} catch (err) {
// 查询失败时按未屏蔽处理,不阻断页面渲染
}
}
let html = `
<div style="background:var(--surface);border:1px solid var(--border);border-radius:8px;padding:32px;text-align:center;">
<img src="${escapeHTML(safeURL(avatar, { image: true }))}" style="width:96px;height:96px;border-radius:50%;object-fit:cover;border:3px solid var(--primary);" />
<h2 style="margin:16px 0 4px;font-size:24px;">${escapeHTML(user.username)}</h2>
<p style="color:var(--text-secondary);font-size:14px;">${escapeHTML(user.bio || '这个人很懒,什么都没写')}</p>
<div style="display:flex;justify-content:center;gap:32px;margin-top:16px;font-size:14px;color:var(--text-secondary);flex-wrap:wrap;">
<span>📅 加入于 ${user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'}</span>
<span>📝 发了 ${posts.length} 个帖子</span>
${user.role === 'admin' ? '<span style="color:var(--primary);font-weight:600;">🛡️ 管理员</span>' : ''}
</div>
${currentUser && currentUser.id !== user.id ? `
<div style="display:flex;justify-content:center;gap:10px;margin-top:16px;flex-wrap:wrap;">
<!-- 被我屏蔽时不给出发私信入口,避免必然失败的尝试 -->
${blockStatus.blocked_by_me ? '' : `
<button id="dmBtn" class="btn-primary" style="padding:8px 24px;">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:inline;vertical-align:middle;margin-right:6px;"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>
发私信
</button>
`}
<button id="blockBtn" class="btn-secondary ${blockStatus.blocked_by_me ? 'is-blocked' : ''}"
data-userid="${escapeHTML(user.id)}" data-username="${escapeHTML(user.username)}"
data-blocked="${blockStatus.blocked_by_me ? '1' : '0'}"
style="padding:8px 20px;">
${blockStatus.blocked_by_me ? '解除屏蔽' : '屏蔽'}
</button>
</div>
${blockStatus.blocked_by_me ? `
<p style="margin-top:10px;font-size:12px;color:var(--text-light);">
已屏蔽该用户:你不再看到 TA 的帖子与回复,TA 也无法给你发私信
</p>
` : ''}
` : ''}
</div>
<h3 style="margin:24px 0 16px;font-size:18px;">📝 发布的帖子</h3>
`;
if (posts.length === 0) {
html += '<div style="color:#94a3b8;padding:20px 0;text-align:center;">还没有发帖</div>';
} else {
posts.forEach(p => {
const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : '';
html += `
<div class="post-card" data-postid="${escapeHTML(p.id)}" style="cursor:pointer;">
<div class="post-title" style="font-size:16px;font-weight:600;">${escapeHTML(p.title || '无标题')}</div>
<div class="post-content" style="font-size:14px;color:var(--text-secondary);">${escapeHTML((p.content || '').substring(0, 100))}${(p.content || '').length > 100 ? '...' : ''}</div>
<div style="font-size:12px;color:var(--text-light);margin-top:8px;">${time}</div>
</div>
`;
});
}
container.innerHTML = sanitizeHTML(html);
container.querySelectorAll('.post-card[data-postid]').forEach(card => {
card.addEventListener('click', function() {
switchToPost(this.dataset.postid);
});
});
const dmBtn = document.getElementById('dmBtn');
if (dmBtn) {
dmBtn.addEventListener('click', function() {
openPrivateChat(user.id, user.username);
});
}
// 屏蔽 / 解除屏蔽。两者都需要确认——屏蔽会影响双方的内容可见性,
// 属于不容易察觉后果的操作。
const blockBtn = document.getElementById('blockBtn');
if (blockBtn) {
blockBtn.addEventListener('click', async function() {
const targetId = this.dataset.userid;
const targetName = this.dataset.username;
const isBlocked = this.dataset.blocked === '1';
const ok = await showConfirm(
isBlocked
? `解除对「${targetName}」的屏蔽?解除后你将继续看到 TA 的帖子和回复,TA 也能再次给你发私信。`
: `确定屏蔽「${targetName}」?屏蔽后你不再看到 TA 的帖子和回复,TA 也无法给你发私信。`,
{
title: isBlocked ? '解除屏蔽' : '屏蔽用户',
confirmText: isBlocked ? '解除屏蔽' : '屏蔽',
danger: !isBlocked,
}
);
if (!ok) return;
try {
if (isBlocked) {
await API.unblockUser(targetId);
showToast('已解除屏蔽', 'success');
} else {
await API.blockUser(targetId);
showToast('已屏蔽该用户', 'success');
}
// 重新渲染以更新按钮状态与提示
renderUserProfile(user.username);
} catch (err) {
showToast(err.message, 'error');
}
});
}
} catch (err) {
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">加载失败:' + escapeHTML(err.message) + '</div>';
}
}
function showUserPage(username) {
const goingBack = typeof window.isNavigatingBack === 'function' && window.isNavigatingBack();
const el = document.getElementById('pageUser');
// 先让其他覆盖页滑出(例如从帖子详情跳到用户主页)
document.querySelectorAll('.page-slide.active').forEach(other => {
if (other !== el) hideSlide(other, { back: goingBack });
});
// 首页留在背景中下沉,新页面从屏幕外滑入
showSlide(el, { back: goingBack });
currentPage = 'user';
renderUserProfile(username);
}