Files
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

573 lines
17 KiB
JavaScript

// ============================================================
// API 请求封装
// ============================================================
let token = localStorage.getItem('forumlify-token') || null;
function getAuthHeaders() {
return token ? { 'Authorization': 'Bearer ' + token } : {};
}
function apiFetch(path, options = {}) {
const url = CONFIG.API_BASE_URL + path;
const headers = {
'Content-Type': 'application/json',
...getAuthHeaders(),
...(options.headers || {})
};
return fetch(url, {
...options,
headers
}).then(res => res.json());
}
const API = {
// ============================================================
// 认证
// ============================================================
async login(email, password) {
const data = await apiFetch('/auth/login', {
method: 'POST',
body: JSON.stringify({ email, password })
});
if (data.error) throw new Error(data.error);
if (data.token) {
token = data.token;
localStorage.setItem('forumlify-token', token);
}
return data;
},
async register(email, password, username, bootstrapToken = '', emailCode = '') {
const data = await apiFetch('/auth/register', {
method: 'POST',
body: JSON.stringify({
email,
password,
username,
bootstrap_token: bootstrapToken || undefined,
email_code: emailCode || undefined
})
});
if (data.error) throw new Error(data.error);
return data;
},
async logout() {
token = null;
localStorage.removeItem('forumlify-token');
currentUser = null;
},
async getMe() {
const data = await apiFetch('/auth/me');
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 帖子(含分页)
// ============================================================
async getPosts(sort, page = 1, limit = 20) {
const data = await apiFetch('/posts?sort=' + (sort || 'latest') + '&page=' + page + '&limit=' + limit);
if (data.error) throw new Error(data.error);
return data;
},
// 搜索帖子(标题与正文)
async searchPosts(keyword, page = 1, limit = 20) {
const data = await apiFetch(
'/posts/search?q=' + encodeURIComponent(keyword) + '&page=' + page + '&limit=' + limit
);
if (data.error) throw new Error(data.error);
return data;
},
async getPost(postId) {
const data = await apiFetch('/posts/' + postId);
if (data.error) throw new Error(data.error);
return data;
},
async createPost(title, content, images) {
const data = await apiFetch('/posts', {
method: 'POST',
body: JSON.stringify({ title, content, images: images || [] })
});
if (data.error) throw new Error(data.error);
return data;
},
async uploadImage(file) {
const formData = new FormData();
formData.append('file', file);
const response = await fetch(CONFIG.API_BASE_URL + '/upload', {
method: 'POST',
headers: getAuthHeaders(),
body: formData
});
const data = await response.json().catch(() => ({ error: '上传响应无效' }));
if (!response.ok || data.error) throw new Error(data.error || '图片上传失败');
return data;
},
async updatePost(postId, title, content) {
const data = await apiFetch('/posts/' + postId, {
method: 'PUT',
body: JSON.stringify({ title, content })
});
if (data.error) throw new Error(data.error);
return data;
},
async deletePost(postId) {
const data = await apiFetch('/posts/' + postId, { method: 'DELETE' });
if (data.error) throw new Error(data.error);
return data;
},
async togglePinPost(postId) {
const data = await apiFetch('/posts/' + postId + '/pin', {
method: 'PUT'
});
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 回复
// ============================================================
async getReplies(postId) {
const data = await apiFetch('/posts/' + postId + '/replies');
if (data.error) throw new Error(data.error);
return data || [];
},
async createReply(postId, content) {
const data = await apiFetch('/posts/' + postId + '/replies', {
method: 'POST',
body: JSON.stringify({ content })
});
if (data.error) throw new Error(data.error);
return data;
},
async deleteReply(replyId) {
const data = await apiFetch('/replies/' + replyId, { method: 'DELETE' });
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 用户管理(支持分页和搜索)
// ============================================================
async getUsers(page = 1, limit = 20, search = '') {
const data = await apiFetch('/users?page=' + page + '&limit=' + limit + '&search=' + encodeURIComponent(search));
if (data.error) throw new Error(data.error);
return data;
},
async updateUserRole(userId, role) {
const data = await apiFetch('/users/' + userId + '/role', {
method: 'PUT',
body: JSON.stringify({ role })
});
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 举报
// ============================================================
async getReports() {
const data = await apiFetch('/reports');
if (data.error) throw new Error(data.error);
return data || [];
},
async createReport(postId, reason) {
const data = await apiFetch('/reports', {
method: 'POST',
body: JSON.stringify({ post_id: postId, reason })
});
if (data.error) throw new Error(data.error);
return data;
},
async updateReport(reportId, status, note) {
const data = await apiFetch('/reports/' + reportId, {
method: 'PUT',
body: JSON.stringify({ status, note })
});
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 统计
// ============================================================
async getStats() {
const data = await apiFetch('/stats');
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 友情链接
// ============================================================
async getLinks() {
const data = await apiFetch('/links');
if (data.error) throw new Error(data.error);
return data || [];
},
async addLink(title, url) {
const data = await apiFetch('/links', {
method: 'POST',
body: JSON.stringify({ title, url })
});
if (data.error) throw new Error(data.error);
return data;
},
async deleteLink(id) {
const data = await apiFetch('/links/' + id, { method: 'DELETE' });
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 事件日志(支持分页)
// ============================================================
async getEventLogs(page = 1, limit = 20) {
const data = await apiFetch('/event-logs?page=' + page + '&limit=' + limit);
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 论坛设置
// ============================================================
async getSettings() {
const data = await apiFetch('/settings');
if (data.error) throw new Error(data.error);
return data;
},
async updateSettings(forum_name) {
const data = await apiFetch('/settings', {
method: 'PUT',
body: JSON.stringify({ forum_name })
});
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 私信
// ============================================================
async getConversations() {
const data = await apiFetch('/conversations');
if (data.error) throw new Error(data.error);
return data || [];
},
// 搜索用户(用于发起私信)。仅返回用户名、头像等公开信息。
async searchUsers(keyword, limit = 10) {
const data = await apiFetch(
'/users/search?q=' + encodeURIComponent(keyword) + '&limit=' + limit
);
if (data.error) throw new Error(data.error);
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',
body: JSON.stringify({ other_user_id })
});
if (data.error) throw new Error(data.error);
return data;
},
async getMessages(conversationId) {
const data = await apiFetch('/conversations/' + conversationId + '/messages');
if (data.error) throw new Error(data.error);
return data || [];
},
async sendMessage(conversationId, content) {
const data = await apiFetch('/conversations/' + conversationId + '/messages', {
method: 'POST',
body: JSON.stringify({ content })
});
if (data.error) throw new Error(data.error);
return data;
},
async markMessageRead(messageId) {
const data = await apiFetch('/messages/' + messageId + '/read', {
method: 'PUT'
});
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 自定义页面
// ============================================================
async getCustomPages() {
const data = await apiFetch('/custom-pages');
if (data.error) throw new Error(data.error);
return data || [];
},
async getCustomPage(name) {
const data = await apiFetch('/custom-pages/' + encodeURIComponent(name));
if (data.error) throw new Error(data.error);
return data;
},
async getAdminCustomPages() {
const data = await apiFetch('/admin/custom-pages');
if (data.error) throw new Error(data.error);
return data || [];
},
async createCustomPage(name, title, content) {
const data = await apiFetch('/admin/custom-pages', {
method: 'POST',
body: JSON.stringify({ name, title, content })
});
if (data.error) throw new Error(data.error);
return data;
},
async updateCustomPage(id, title, content, enabled) {
const data = await apiFetch('/admin/custom-pages/' + id, {
method: 'PUT',
body: JSON.stringify({ title, content, enabled })
});
if (data.error) throw new Error(data.error);
return data;
},
async deleteCustomPage(id) {
const data = await apiFetch('/admin/custom-pages/' + id, { method: 'DELETE' });
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 头像
// ============================================================
async updateAvatar(userId, avatar_url) {
const data = await apiFetch('/users/' + userId + '/avatar', {
method: 'PUT',
body: JSON.stringify({ avatar_url })
});
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 恢复码
// ============================================================
async generateRecoveryCodes() {
const data = await apiFetch('/auth/recovery-codes/generate', {
method: 'POST'
});
if (data.error) throw new Error(data.error);
return data;
},
async getRecoveryCodesCount() {
const data = await apiFetch('/auth/recovery-codes/count');
if (data.error) throw new Error(data.error);
return data;
},
async resetPassword(email, recoveryCode, newPassword) {
const data = await apiFetch('/auth/reset-password', {
method: 'POST',
body: JSON.stringify({ email, recoveryCode, newPassword })
});
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 修改密码和邮箱
// ============================================================
// 这两个接口会递增 token_version 使旧令牌失效,服务端随即补发一个
// 新令牌。这里必须接住它,否则用户操作成功后会被立刻登出。
_persistRefreshedToken(data) {
if (data && data.token) {
token = data.token;
localStorage.setItem('forumlify-token', token);
}
return data;
},
async changePassword(oldPassword, newPassword) {
const data = await apiFetch('/users/' + currentUser.id + '/password', {
method: 'PUT',
body: JSON.stringify({ oldPassword, newPassword })
});
if (data.error) throw new Error(data.error);
return this._persistRefreshedToken(data);
},
async changeEmail(password, newEmail) {
const data = await apiFetch('/users/' + currentUser.id + '/email', {
method: 'PUT',
body: JSON.stringify({ password, newEmail })
});
if (data.error) throw new Error(data.error);
return this._persistRefreshedToken(data);
},
// ============================================================
// 更新用户资料(含签名)
// ============================================================
async updateProfile(userId, username, bio, signature) {
const data = await apiFetch('/users/' + userId, {
method: 'PUT',
body: JSON.stringify({ username, bio, signature })
});
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 自定义 CSS
// ============================================================
async uploadCustomCSS(file) {
const formData = new FormData();
formData.append('file', file);
const url = CONFIG.API_BASE_URL + '/admin/custom-css';
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + localStorage.getItem('forumlify-token')
},
body: formData
});
const data = await response.json();
if (data.error) throw new Error(data.error);
return data;
},
async deleteCustomCSS() {
const data = await apiFetch('/admin/custom-css', {
method: 'DELETE'
});
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// SMTP 配置(管理员)
// ============================================================
async getSmtpConfig() {
const data = await apiFetch('/admin/smtp');
if (data.error) throw new Error(data.error);
return data;
},
// password 留空表示保持原有密码不变
async saveSmtpConfig(config) {
const data = await apiFetch('/admin/smtp', {
method: 'PUT',
body: JSON.stringify(config)
});
if (data.error) throw new Error(data.error);
return data;
},
async clearSmtpPassword() {
const data = await apiFetch('/admin/smtp/password', { method: 'DELETE' });
if (data.error) throw new Error(data.error);
return data;
},
async sendTestMail(to) {
const data = await apiFetch('/admin/smtp/test', {
method: 'POST',
body: JSON.stringify(to ? { to } : {})
});
if (data.error) throw new Error(data.error);
return data;
},
async setEmailVerifyRequired(required) {
const data = await apiFetch('/admin/email-verify', {
method: 'PUT',
body: JSON.stringify({ required })
});
if (data.error) throw new Error(data.error);
return data;
},
// ============================================================
// 邮箱验证码
// ============================================================
async sendRegisterCode(email) {
const data = await apiFetch('/auth/send-code', {
method: 'POST',
body: JSON.stringify({ email })
});
if (data.error) throw new Error(data.error);
return data;
},
async forgotPassword(email) {
const data = await apiFetch('/auth/forgot-password', {
method: 'POST',
body: JSON.stringify({ email })
});
if (data.error) throw new Error(data.error);
return data;
},
async resetPasswordByEmail(email, code, newPassword) {
const data = await apiFetch('/auth/reset-password-by-email', {
method: 'POST',
body: JSON.stringify({ email, code, newPassword })
});
if (data.error) throw new Error(data.error);
return data;
}
};