Files
wekan/models/lib/memberAutocomplete.js
T
Lauri Ojansivu 2328bcd0f1 Case-insensitive '@' member autocomplete (shared, null-safe).
Follow-up to the #5116 label fix: the add-card-title '@' member mention in client/components/lists/listBody.js matched with user.username.indexOf(term) === 0 -- case-sensitive, prefix-only, username-only, and it threw when ReactiveCache.getUser() returned null. So '@ann' did not suggest 'Anna', '@Anna' did not match username 'anna', and a full-name search never matched. The card description/comment editor (client/components/main/editor.js) already matched case-insensitively on username OR full name but shared no code and had the same null hazard. Both now route through a pure, unit-tested models/lib/memberAutocomplete.js (memberMatchesTerm / filterMembersByTerm) that lowercases the term and matches username or profile.fullname as a substring, and is null-safe.

Tests: tests/memberAutocompleteMatch.test.cjs (11 checks) covers case-insensitive + substring + full-name matching, empty-term-shows-all, null/undefined user (the old crash), missing profile, order preservation, and source guards that both '@' call sites use the shared matcher.

Thanks to rjl9 and xet7 !
2026-07-22 10:15:50 +03:00

43 lines
1.9 KiB
JavaScript

'use strict';
// Pure helper for the "@" member autocomplete (issue #5116 follow-up): typing
// "@ann" should suggest the board member "Anna" / username "anna.smith". The
// add-card-title strategy in client/components/lists/listBody.js used a
// case-SENSITIVE, prefix-only, username-only match
// (user.username.indexOf(term) === 0), so "@ann" did not match "Anna", "@Anna"
// did not match username "anna", and a full-name-only search never matched. It
// also threw if ReactiveCache.getUser() returned null. The card description /
// comment editor (client/components/main/editor.js) already matched
// case-insensitively on username OR full name, but shared no code and had the
// same null hazard.
//
// Extracted here (mirrors models/lib/labelAutocomplete.js and the other
// models/lib pure helpers) so both call sites match members the same way and it
// is unit-testable in plain Node without Meteor.
// Does `user` match the typed `term`? Case-insensitive substring match against
// the username and the profile full name. A null/undefined user, or a missing
// username/fullname, is treated safely (no throw). An empty term matches every
// user (the menu shows all members before anything is typed).
function memberMatchesTerm(user, term) {
if (!user) return false;
const needle = String(term == null ? '' : term).toLowerCase();
const username = String(user.username == null ? '' : user.username).toLowerCase();
const fullname = String(
(user.profile && user.profile.fullname) == null ? '' : user.profile.fullname,
).toLowerCase();
return username.indexOf(needle) > -1 || fullname.indexOf(needle) > -1;
}
// Filter a list of user documents by the typed term, preserving input order.
// Does not mutate the input; skips null/undefined entries.
function filterMembersByTerm(users, term) {
if (!Array.isArray(users)) return [];
return users.filter(user => memberMatchesTerm(user, term));
}
module.exports = {
memberMatchesTerm,
filterMembersByTerm,
};