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 通过。
This commit is contained in:
1 parent
5995efc7a0
commit
7c66cb931a
9 files changed
+550
-35
No files matched your search
@@ -281,6 +281,33 @@ const API = {
|
||||
return data.data || [];
|
||||
},
|
||||
|
||||
// ============================================================
|
||||
// 用户屏蔽
|
||||
// ============================================================
|
||||
async blockUser(userId) {
|
||||
const data = await apiFetch('/users/' + userId + '/block', { method: 'POST' });
|
||||
if (data.error) throw new Error(data.error);
|
||||
return data;
|
||||
},
|
||||
|
||||
async unblockUser(userId) {
|
||||
const data = await apiFetch('/users/' + userId + '/block', { method: 'DELETE' });
|
||||
if (data.error) throw new Error(data.error);
|
||||
return data;
|
||||
},
|
||||
|
||||
async getBlockedUsers() {
|
||||
const data = await apiFetch('/blocks');
|
||||
if (data.error) throw new Error(data.error);
|
||||
return data.data || [];
|
||||
},
|
||||
|
||||
async getBlockStatus(userId) {
|
||||
const data = await apiFetch('/users/' + userId + '/block-status');
|
||||
if (data.error) throw new Error(data.error);
|
||||
return data;
|
||||
},
|
||||
|
||||
async getOrCreateConversation(other_user_id) {
|
||||
const data = await apiFetch('/conversations', {
|
||||
method: 'POST',
|
||||
|
||||
@@ -440,10 +440,14 @@ async function updateUnreadBadge() {
|
||||
|
||||
function openMessageList() {
|
||||
document.getElementById('messageListModal').classList.add('active');
|
||||
// 每次打开都回到会话列表状态,避免上次的搜索词残留
|
||||
// 每次打开都回到会话列表状态,避免上次的搜索词或「已屏蔽」视图残留
|
||||
const searchInput = document.getElementById('dmUserSearchInput');
|
||||
if (searchInput) searchInput.value = '';
|
||||
syncDmSearchClear();
|
||||
const tabConv = document.getElementById('dmTabConversations');
|
||||
const tabBlocked = document.getElementById('dmTabBlocked');
|
||||
if (tabConv) tabConv.classList.add('active');
|
||||
if (tabBlocked) tabBlocked.classList.remove('active');
|
||||
renderMessageList();
|
||||
}
|
||||
|
||||
@@ -491,12 +495,38 @@ async function renderDmUserSearch(keyword) {
|
||||
<div class="dm-user-name">${escapeHTML(u.username)}</div>
|
||||
<div class="dm-user-hint">${escapeHTML(u.signature || '点击发送私信')}</div>
|
||||
</div>
|
||||
<button type="button" class="dm-user-block" data-user-id="${escapeHTML(u.id)}"
|
||||
data-username="${escapeHTML(u.username)}" title="屏蔽该用户">屏蔽</button>
|
||||
<span class="dm-user-action">私信</span>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
|
||||
// 屏蔽按钮:需与行点击区分开,因此阻止冒泡
|
||||
container.querySelectorAll('.dm-user-block').forEach(btn => {
|
||||
btn.addEventListener('click', async function(e) {
|
||||
e.stopPropagation();
|
||||
const userId = this.dataset.userId;
|
||||
const username = this.dataset.username;
|
||||
|
||||
const ok = await showConfirm(
|
||||
`确定屏蔽「${username}」?屏蔽后你不再看到 TA 的帖子和回复,TA 也无法给你发私信。`,
|
||||
{ title: '屏蔽用户', confirmText: '屏蔽', danger: true }
|
||||
);
|
||||
if (!ok) return;
|
||||
|
||||
try {
|
||||
await API.blockUser(userId);
|
||||
showToast('已屏蔽该用户', 'success');
|
||||
// 屏蔽后该用户不应再出现于搜索结果中,重新执行一次搜索
|
||||
renderDmUserSearch(document.getElementById('dmUserSearchInput').value);
|
||||
} catch (err) {
|
||||
showToast(err.message, 'error');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
container.querySelectorAll('.dm-user-row').forEach(row => {
|
||||
row.addEventListener('click', async function() {
|
||||
const userId = this.dataset.userId;
|
||||
@@ -558,6 +588,86 @@ async function renderDmUserSearch(keyword) {
|
||||
}
|
||||
})();
|
||||
|
||||
// 渲染「已屏蔽」列表
|
||||
async function renderBlockedUsers() {
|
||||
const container = document.getElementById('messageListContent');
|
||||
container.innerHTML = '<div class="dm-search-status">加载中…</div>';
|
||||
|
||||
try {
|
||||
const users = await API.getBlockedUsers();
|
||||
if (users.length === 0) {
|
||||
container.innerHTML = '<div class="dm-search-status">还没有屏蔽任何人</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
let html = '';
|
||||
users.forEach(u => {
|
||||
const avatar = u.avatar_url ||
|
||||
'https://ui-avatars.com/api/?name=' + encodeURIComponent(u.username) + '&background=6366f1&color=fff&size=64';
|
||||
html += `
|
||||
<div class="dm-user-row" style="cursor:default;">
|
||||
<img src="${escapeHTML(safeURL(avatar, { image: true }))}" alt="" />
|
||||
<div class="dm-user-info">
|
||||
<div class="dm-user-name">${escapeHTML(u.username)}</div>
|
||||
<div class="dm-user-hint">屏蔽于 ${u.created_at ? new Date(u.created_at).toLocaleDateString('zh-CN') : '未知'}</div>
|
||||
</div>
|
||||
<button type="button" class="dm-user-unblock"
|
||||
data-user-id="${escapeHTML(u.id)}" data-username="${escapeHTML(u.username)}">解除屏蔽</button>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
|
||||
container.querySelectorAll('.dm-user-unblock').forEach(btn => {
|
||||
btn.addEventListener('click', async function() {
|
||||
const userId = this.dataset.userId;
|
||||
const username = this.dataset.username;
|
||||
|
||||
const ok = await showConfirm(
|
||||
`解除对「${username}」的屏蔽?解除后你将继续看到 TA 的帖子和回复,TA 也能再次给你发私信。`,
|
||||
{ title: '解除屏蔽', confirmText: '解除屏蔽' }
|
||||
);
|
||||
if (!ok) return;
|
||||
|
||||
try {
|
||||
await API.unblockUser(userId);
|
||||
showToast('已解除屏蔽', 'success');
|
||||
renderBlockedUsers();
|
||||
} catch (err) {
|
||||
showToast(err.message, 'error');
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
container.innerHTML = '<div class="dm-search-status">加载失败</div>';
|
||||
}
|
||||
}
|
||||
|
||||
// 视图切换:会话 / 已屏蔽
|
||||
function switchDmView(view) {
|
||||
const tabConv = document.getElementById('dmTabConversations');
|
||||
const tabBlocked = document.getElementById('dmTabBlocked');
|
||||
if (tabConv) tabConv.classList.toggle('active', view === 'conversations');
|
||||
if (tabBlocked) tabBlocked.classList.toggle('active', view === 'blocked');
|
||||
|
||||
// 切到已屏蔽时清空搜索框,避免搜索状态与列表视图混淆
|
||||
const input = document.getElementById('dmUserSearchInput');
|
||||
if (view === 'blocked' && input) {
|
||||
input.value = '';
|
||||
syncDmSearchClear();
|
||||
}
|
||||
|
||||
if (view === 'blocked') renderBlockedUsers();
|
||||
else renderMessageList();
|
||||
}
|
||||
|
||||
(function initDmTabs() {
|
||||
const tabConv = document.getElementById('dmTabConversations');
|
||||
const tabBlocked = document.getElementById('dmTabBlocked');
|
||||
if (tabConv) tabConv.addEventListener('click', () => switchDmView('conversations'));
|
||||
if (tabBlocked) tabBlocked.addEventListener('click', () => switchDmView('blocked'));
|
||||
})();
|
||||
|
||||
function closeMessageList() {
|
||||
closeModal(document.getElementById('messageListModal'));
|
||||
if (messagePollInterval) {
|
||||
|
||||
+24
@@ -372,6 +372,18 @@ const TRANSLATIONS = {
|
||||
'重新生成恢复码': '重新生成恢复码',
|
||||
'退出登录': '退出登录',
|
||||
|
||||
// ===== 用户屏蔽 =====
|
||||
'屏蔽': '屏蔽',
|
||||
'解除屏蔽': '解除屏蔽',
|
||||
'已屏蔽': '已屏蔽',
|
||||
'会话': '会话',
|
||||
'屏蔽用户': '屏蔽用户',
|
||||
'已屏蔽该用户': '已屏蔽该用户',
|
||||
'已解除屏蔽': '已解除屏蔽',
|
||||
'还没有屏蔽任何人': '还没有屏蔽任何人',
|
||||
'加载中…': '加载中…',
|
||||
'加载失败': '加载失败',
|
||||
|
||||
// ===== 私信用户搜索 =====
|
||||
'搜索用户名开始私信…': '搜索用户名开始私信…',
|
||||
'搜索用户': '搜索用户',
|
||||
@@ -803,6 +815,18 @@ const TRANSLATIONS = {
|
||||
'重新生成恢复码': 'Regenerate Recovery Codes',
|
||||
'退出登录': 'Logout',
|
||||
|
||||
// ===== 用户屏蔽 =====
|
||||
'屏蔽': 'Block',
|
||||
'解除屏蔽': 'Unblock',
|
||||
'已屏蔽': 'Blocked',
|
||||
'会话': 'Chats',
|
||||
'屏蔽用户': 'Block User',
|
||||
'已屏蔽该用户': 'User blocked',
|
||||
'已解除屏蔽': 'User unblocked',
|
||||
'还没有屏蔽任何人': 'You have not blocked anyone',
|
||||
'加载中…': 'Loading…',
|
||||
'加载失败': 'Failed to load',
|
||||
|
||||
// ===== 私信用户搜索 =====
|
||||
'搜索用户名开始私信…': 'Search users to start a chat…',
|
||||
'搜索用户': 'Search users',
|
||||
|
||||
+67
-4
@@ -22,6 +22,16 @@ async function renderUserProfile(username) {
|
||||
|
||||
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);" />
|
||||
@@ -33,10 +43,26 @@ async function renderUserProfile(username) {
|
||||
${user.role === 'admin' ? '<span style="color:var(--primary);font-weight:600;">🛡️ 管理员</span>' : ''}
|
||||
</div>
|
||||
${currentUser && currentUser.id !== user.id ? `
|
||||
<button id="dmBtn" class="btn-primary" style="margin-top:16px;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>
|
||||
<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>
|
||||
@@ -72,6 +98,43 @@ async function renderUserProfile(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>';
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user