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
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user