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
@@ -440,9 +440,124 @@ async function updateUnreadBadge() {
|
||||
|
||||
function openMessageList() {
|
||||
document.getElementById('messageListModal').classList.add('active');
|
||||
// 每次打开都回到会话列表状态,避免上次的搜索词残留
|
||||
const searchInput = document.getElementById('dmUserSearchInput');
|
||||
if (searchInput) searchInput.value = '';
|
||||
syncDmSearchClear();
|
||||
renderMessageList();
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// ✉️ 私信:按用户名搜索并直接开聊
|
||||
// ============================================================
|
||||
// 此前只能进入对方个人主页再点「私信」,这里提供直接搜索的入口。
|
||||
|
||||
function syncDmSearchClear() {
|
||||
const input = document.getElementById('dmUserSearchInput');
|
||||
const btn = document.getElementById('dmUserSearchClear');
|
||||
if (input && btn) btn.style.display = input.value.trim() ? 'flex' : 'none';
|
||||
}
|
||||
|
||||
async function renderDmUserSearch(keyword) {
|
||||
const container = document.getElementById('messageListContent');
|
||||
const kw = (keyword || '').trim();
|
||||
|
||||
if (!kw) {
|
||||
renderMessageList();
|
||||
return;
|
||||
}
|
||||
|
||||
container.innerHTML = '<div class="dm-search-status">搜索中…</div>';
|
||||
|
||||
try {
|
||||
const users = await API.searchUsers(kw, 10);
|
||||
|
||||
// 排除自己
|
||||
const others = users.filter(u => !currentUser || u.id !== currentUser.id);
|
||||
|
||||
if (others.length === 0) {
|
||||
container.innerHTML = '<div class="dm-search-status">没有找到匹配的用户</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
let html = '';
|
||||
others.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" data-user-id="${escapeHTML(u.id)}" data-username="${escapeHTML(u.username)}">
|
||||
<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">${escapeHTML(u.signature || '点击发送私信')}</div>
|
||||
</div>
|
||||
<span class="dm-user-action">私信</span>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
|
||||
container.querySelectorAll('.dm-user-row').forEach(row => {
|
||||
row.addEventListener('click', async function() {
|
||||
const userId = this.dataset.userId;
|
||||
const username = this.dataset.username;
|
||||
try {
|
||||
const conv = await API.getOrCreateConversation(userId);
|
||||
// 关闭搜索状态,进入聊天窗口
|
||||
const searchInput = document.getElementById('dmUserSearchInput');
|
||||
if (searchInput) searchInput.value = '';
|
||||
syncDmSearchClear();
|
||||
// openChat 需要三个参数:会话 ID、对方用户 ID、对方用户名
|
||||
openChat(conv.id, userId, username);
|
||||
} catch (err) {
|
||||
showToast(err.message, 'error');
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
container.innerHTML = '<div class="dm-search-status">搜索失败:' + escapeHTML(err.message) + '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
// 绑定搜索框事件(防抖 300ms)
|
||||
(function initDmUserSearch() {
|
||||
const input = document.getElementById('dmUserSearchInput');
|
||||
const clearBtn = document.getElementById('dmUserSearchClear');
|
||||
if (!input) return;
|
||||
|
||||
let timer = null;
|
||||
|
||||
function run() {
|
||||
syncDmSearchClear();
|
||||
if (timer) clearTimeout(timer);
|
||||
timer = setTimeout(() => renderDmUserSearch(input.value), 300);
|
||||
}
|
||||
|
||||
input.addEventListener('input', run);
|
||||
|
||||
input.addEventListener('keydown', e => {
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
if (timer) clearTimeout(timer);
|
||||
renderDmUserSearch(input.value);
|
||||
} else if (e.key === 'Escape') {
|
||||
e.preventDefault();
|
||||
input.value = '';
|
||||
if (timer) clearTimeout(timer);
|
||||
renderDmUserSearch('');
|
||||
}
|
||||
});
|
||||
|
||||
if (clearBtn) {
|
||||
clearBtn.addEventListener('click', () => {
|
||||
input.value = '';
|
||||
if (timer) clearTimeout(timer);
|
||||
renderDmUserSearch('');
|
||||
input.focus();
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
function closeMessageList() {
|
||||
closeModal(document.getElementById('messageListModal'));
|
||||
if (messagePollInterval) {
|
||||
@@ -525,6 +640,38 @@ function closeChat() {
|
||||
updateUnreadBadge();
|
||||
}
|
||||
|
||||
// 更新聊天窗口底部的发送限制提示。
|
||||
// 规则:对方从未回复时,我方最多先发 3 条(与后端
|
||||
// NEW_CONVERSATION_MESSAGE_LIMIT 对应)。对方回复过就不再限制,
|
||||
// 此时隐藏提示条。
|
||||
const DM_FIRST_ROUND_LIMIT = 3;
|
||||
|
||||
function updateChatLimitNote(messages) {
|
||||
const note = document.getElementById('chatLimitNote');
|
||||
if (!note || !currentUser) return;
|
||||
|
||||
const mine = messages.filter(m => m.sender_id === currentUser.id).length;
|
||||
const theirs = messages.filter(m => m.sender_id !== currentUser.id).length;
|
||||
|
||||
if (theirs > 0) {
|
||||
// 对方已回复,限制解除
|
||||
note.style.display = 'none';
|
||||
return;
|
||||
}
|
||||
|
||||
const remaining = Math.max(0, DM_FIRST_ROUND_LIMIT - mine);
|
||||
note.style.display = 'block';
|
||||
// 数字与文字分开放进不同元素:整句含动态数字时 i18n 匹配不到词典,
|
||||
// 拆开后固定文案可被翻译。
|
||||
if (remaining > 0) {
|
||||
note.innerHTML = '<span>对方回复前还可发送</span> '
|
||||
+ '<strong>' + remaining + '</strong> '
|
||||
+ '<span>条消息</span>';
|
||||
} else {
|
||||
note.textContent = '对方回复前无法继续发送,请等待对方回复';
|
||||
}
|
||||
}
|
||||
|
||||
async function renderMessages(conversationId, silent = false) {
|
||||
const container = document.getElementById('chatMessages');
|
||||
if (!silent) {
|
||||
@@ -533,6 +680,7 @@ async function renderMessages(conversationId, silent = false) {
|
||||
|
||||
try {
|
||||
const messages = await API.getMessages(conversationId);
|
||||
updateChatLimitNote(messages);
|
||||
if (messages.length === 0) {
|
||||
container.innerHTML = '<div style="text-align:center;color:#94a3b8;padding:40px 0;">还没有消息,打个招呼吧</div>';
|
||||
return;
|
||||
@@ -579,7 +727,13 @@ async function sendMessage() {
|
||||
renderMessageList();
|
||||
}
|
||||
} catch (err) {
|
||||
showToast('发送失败:' + err.message, 'error');
|
||||
// 达到首轮发送上限时给出更明确的提示,而不是笼统的「发送失败」
|
||||
if (/最多发送/.test(err.message)) {
|
||||
showToast(err.message, 'warning');
|
||||
renderMessages(currentConversationId, true);
|
||||
} else {
|
||||
showToast('发送失败:' + err.message, 'error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user