feat(dm): 私信支持按用户名搜索开聊,并加入首轮发送条数限制
一、私信用户搜索 此前只能先进入对方个人主页、再点「私信」才能发起会话。现在私信窗口 顶部新增搜索框,输入用户名即可搜索并直接开聊。 - 新增 GET /api/users/search:按用户名模糊匹配,前缀匹配优先、其次按 名字长度排序(更接近精确结果)。只返回用户名、头像、角色、签名, 不暴露邮箱等隐私字段(已实测确认响应中不含 email)。 - 关键词转义 % 与 _,与帖子搜索同样处理,避免通配符被当作模式。 - 关键词长度上限 50 字,limit 上限 20。 - 搜索结果中排除自己;输入防抖 300ms,回车立即搜索,Esc 与清除按钮 回到会话列表。 - 搜索框同样带上防密码管理器误填的属性,并在关闭窗口时清空关键词, 避免下次打开残留上次的搜索状态。 二、首轮发送条数限制 对方尚未回复时,发起方最多先发 3 条消息;对方一旦回复过,限制永久 解除。仅对「对方从未回复」的会话生效,已正常交流过的会话不受影响。 - 判定放在事务中并对会话行加 FOR UPDATE 锁:限制依赖「我方已发几条、 对方是否回过」两个计数,不加锁时并发发送可绕过。 - 达到上限返回 429,附带 limit_reached 与 limit 字段,前端据此给出 针对性提示而非笼统的「发送失败」。 - 发送成功时在响应中返回 remaining(对方已回复时为 null)。 - 聊天窗口底部新增提示条,显示「对方回复前还可发送 N 条消息」, 对方回复后自动隐藏。数字与文字分置于不同元素,使固定文案能被 i18n 词典匹配(整句含动态数字时匹配不上)。 实测: - 用户搜索:「dmtester」命中 2 个、「tester2」命中 1 个、无匹配返回 0, 响应中 email 字段出现 0 次 - 连续发送 4 条:前 3 条成功且 remaining 依次为 2/1/0, 第 4 条返回 429 与明确提示 - 对方回复后再发送:成功且 remaining 为 null,限制已解除 - 前端:搜索框正常显示,点击结果直接进入聊天窗口 - 全新会话提示条显示「对方回复前还可发送 3 条消息」, 发送 1 条后更新为 2 条 完整测试套件 25/25 通过。
This commit is contained in:
1 parent
c26e834982
commit
5995efc7a0
6 files changed
+407
-7
No files matched your search
@@ -1789,6 +1789,49 @@ app.delete('/api/admin/custom-css', auth, admin, async (req, res) => {
|
||||
// 私信系统
|
||||
// ============================================================
|
||||
|
||||
// ============================================================
|
||||
// 私信系统
|
||||
// ============================================================
|
||||
|
||||
// 防骚扰限制:对方尚未回复时,发起方最多先发这么多条消息。
|
||||
// 对方一旦回复过,限制永久解除。仅对「对方从未回复」的会话生效,
|
||||
// 因此已经正常交流过的会话不受影响。
|
||||
const NEW_CONVERSATION_MESSAGE_LIMIT = 3;
|
||||
|
||||
// 搜索用户以便发起私信(公开信息:用户名与头像)
|
||||
// 仅返回用户名/头像/角色,不暴露邮箱等隐私字段。
|
||||
app.get('/api/users/search', async (req, res) => {
|
||||
const raw = typeof req.query.q === 'string' ? req.query.q.trim() : '';
|
||||
|
||||
if (raw.length === 0) {
|
||||
return res.status(400).json({ error: '请输入搜索关键词' });
|
||||
}
|
||||
if (raw.length > 50) {
|
||||
return res.status(400).json({ error: '搜索关键词过长(最多 50 字)' });
|
||||
}
|
||||
|
||||
const limit = Math.min(Number(req.query.limit) || 10, 20);
|
||||
const pattern = '%' + escapeLikePattern(raw) + '%';
|
||||
|
||||
try {
|
||||
const r = await pool.query(
|
||||
`SELECT id, username, avatar_url, role, signature
|
||||
FROM users
|
||||
WHERE username ILIKE $1
|
||||
ORDER BY
|
||||
-- 前缀匹配优先,其次按名字长度(更接近精确匹配)
|
||||
CASE WHEN username ILIKE $2 THEN 0 ELSE 1 END,
|
||||
LENGTH(username),
|
||||
username
|
||||
LIMIT $3`,
|
||||
[pattern, escapeLikePattern(raw) + '%', limit]
|
||||
);
|
||||
res.json({ data: r.rows, query: raw });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: '服务器错误' });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取会话列表
|
||||
app.get('/api/conversations', auth, async (req, res) => {
|
||||
try {
|
||||
@@ -1891,38 +1934,78 @@ app.post('/api/conversations/:id/messages', validateUuidId, auth, async (req, re
|
||||
return res.status(400).json({ error: '消息长度应为1到10000个字符' });
|
||||
}
|
||||
|
||||
let client;
|
||||
try {
|
||||
const check = await pool.query(`
|
||||
SELECT id FROM conversations
|
||||
client = await pool.connect();
|
||||
await client.query('BEGIN');
|
||||
|
||||
// 取会话并加行锁:限制判定依赖「我方已发几条、对方是否回过」,
|
||||
// 不加锁的话并发发送可以绕过计数。
|
||||
const check = await client.query(`
|
||||
SELECT id, user1_id, user2_id FROM conversations
|
||||
WHERE id = $1 AND (user1_id = $2 OR user2_id = $2)
|
||||
FOR UPDATE
|
||||
`, [conversationId, req.user.id]);
|
||||
|
||||
if (check.rows.length === 0) {
|
||||
await client.query('ROLLBACK');
|
||||
return res.status(403).json({ error: '无权限访问此会话' });
|
||||
}
|
||||
|
||||
const r = await pool.query(`
|
||||
// 防骚扰限制:对方尚未回复时,我方最多先发 NEW_CONVERSATION_MESSAGE_LIMIT 条。
|
||||
// 对方一旦回复过,限制永久解除,之后不再计数。
|
||||
const conv = check.rows[0];
|
||||
const otherId = conv.user1_id === req.user.id ? conv.user2_id : conv.user1_id;
|
||||
|
||||
const stats = await client.query(`
|
||||
SELECT
|
||||
COUNT(*) FILTER (WHERE sender_id = $2) AS mine,
|
||||
COUNT(*) FILTER (WHERE sender_id = $3) AS theirs
|
||||
FROM messages
|
||||
WHERE conversation_id = $1
|
||||
`, [conversationId, req.user.id, otherId]);
|
||||
|
||||
const mine = parseInt(stats.rows[0].mine || 0);
|
||||
const theirs = parseInt(stats.rows[0].theirs || 0);
|
||||
|
||||
if (theirs === 0 && mine >= NEW_CONVERSATION_MESSAGE_LIMIT) {
|
||||
await client.query('ROLLBACK');
|
||||
return res.status(429).json({
|
||||
error: `对方回复前最多发送 ${NEW_CONVERSATION_MESSAGE_LIMIT} 条消息,请等待对方回复`,
|
||||
limit_reached: true,
|
||||
limit: NEW_CONVERSATION_MESSAGE_LIMIT,
|
||||
});
|
||||
}
|
||||
|
||||
const r = await client.query(`
|
||||
INSERT INTO messages (conversation_id, sender_id, content)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING *
|
||||
`, [conversationId, req.user.id, content.trim()]);
|
||||
|
||||
await pool.query(`
|
||||
await client.query(`
|
||||
UPDATE conversations SET last_message_at = NOW()
|
||||
WHERE id = $1
|
||||
`, [conversationId]);
|
||||
|
||||
const userInfo = await pool.query(`
|
||||
const userInfo = await client.query(`
|
||||
SELECT username, avatar_url FROM users WHERE id = $1
|
||||
`, [req.user.id]);
|
||||
|
||||
await client.query('COMMIT');
|
||||
|
||||
res.json({
|
||||
...r.rows[0],
|
||||
sender_username: userInfo.rows[0].username,
|
||||
sender_avatar_url: userInfo.rows[0].avatar_url
|
||||
sender_avatar_url: userInfo.rows[0].avatar_url,
|
||||
// 告知前端剩余可发条数,便于在界面上提示(对方已回复时为 null)
|
||||
remaining: theirs > 0 ? null : Math.max(0, NEW_CONVERSATION_MESSAGE_LIMIT - mine - 1),
|
||||
});
|
||||
} catch (err) {
|
||||
if (client) await client.query('ROLLBACK').catch(() => {});
|
||||
res.status(500).json({ error: '发送失败,请稍后重试' });
|
||||
} finally {
|
||||
if (client) client.release();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user