Defect #44431
openmentions can't find a user
Description
while using @ for the user mention, redmine can't match input after @ to a real user if number of project users is significant
main reason is that there is a logic flaw
WatchersController is defining users_for_mention method
logic is:
if params[:q].blank? && @project.present?
scope = @project.principals.assignable_watchers.limit(100)
else
scope = Principal.assignable_watchers.limit(10)
end
that mean if you press @ in the issue text editor, first 10 users are being selected and then query filter is applied, so if you user is 11th on a list - there sill be an empty list
I suggest to not limit scope there, but after matching users to a query
Updated by Janusz Harkot 3 months ago
there are performance changes Patch #44190: Improve @mention performance by limiting initial suggestions should be addressed too... is there an option to send quary (`q`) to the database - to limit matched users?
Updated by Holger Just 3 months ago
- Description updated (diff)
- Status changed from New to Needs feedback
WatchersController#users_for_mention is building a SQL query scope. As such, the limit to 10 results is only applied after filtering the list with the supplied query (which in the case of the mentions autocomplete is the text entered after the @ character).
As such, we return either up to 100 users from the project if no further text was entered yet, or we return up to 10 users matching the query. If the desired user is not yet part of the result, the user has to enter a more specific search term. If this does not work as intended for you, please provide a concrete reproducible example demonstrating your specific issue.
In any case, returning an unbound result here is inadvisable since this could result in arbitrarily large responses which would have to be dealt with in the frontend. Otherwise, it could could result in slowdowns or even crashes there.
Updated by Janusz Harkot 3 months ago
you have 11 users: qwer01 till qwer10 and zxc01
if you type 'zxc' it will not find a user, because initial db query will be limited to 10 users
and then filtered for 'zxc'
so zxc01 user will not be on a list - no match - an empty list will be returned
Updated by Janusz Harkot 3 months ago
that is why my suggestion was to put query to the initial db query, not to search on an already limited result
Updated by Alexandre Campos 5 days ago
Here is a concrete, reproducible scenario on 7.0.3 (code is unchanged in current trunk). As noted by Holger, limit(10) is applied after the like filter in SQL, so the description above is not the actual cause. The actual cause is that the limit is applied before the visibility filter, which runs in Ruby:
scope = Principal.assignable_watchers.limit(10) # 10 users from the whole instance
users = scope.sorted.like(params[:q]).to_a
users.reject! {|user| user.is_a?(User) && !object.visible?(user)} # filtered afterwards
As soon as at least one character is typed after @, the candidates are the first 10 matching users of the whole instance (sorted by name), and only then are the users who cannot see the issue removed. On an instance with many users spread across private projects, those 10 users usually belong to other projects, so the list ends up empty even though matching project members exist.
Steps to reproduce
- Create a private project P with member "Zoe Martin" (role with "View issues").
- Create 10 other active users whose name or login matches "m" (e.g. "Marc A01" ... "Marc A10") who are not members of P (or members of another private project).
- As a user allowed to add watchers in P (role with users visibility "All active users"), open an issue of P and type
@min the notes.
Expected: Zoe Martin is suggested.
Actual: the list is empty: the 10 "Marc" users are selected by the SQL query, then all removed because they cannot see the issue.
On our instance (~1,400 active users, mostly in private projects of ~70 members each), typing any one- or two-letter prefix (@a, @fr, @ma, @c...) returns 0 suggestions on most issues, while 3 to 20 project members match. Only typing almost the full login works.
Suggested fix
Search project members first, then fill with other users, and apply the visibility filter before the limit:
def users_for_mention
q = params[:q].to_s.strip
limit = q.blank? ? 100 : 10
object = (@watchables.first if @watchables && @watchables.size == 1)
can_see = lambda do |user|
!(object.respond_to?(:visible?) && user.is_a?(User) && !object.visible?(user))
end
users = []
if @project.present?
users = @project.principals.assignable_watchers.where(:type => ['User']).
sorted.like(q).limit(200).to_a.select(&can_see).first(limit)
end
if users.size < limit && (q.present? || @project.blank?)
Principal.assignable_watchers.where(:type => ['User']).
where.not(:id => users.map(&:id)).sorted.like(q).limit(300).each do |user|
break if users.size >= limit
users << user if can_see.call(user)
end
end
users
end
The candidates are still restricted by Principal.visible and by object.visible?(user), so no additional user is exposed: these are the same users that were reachable before by typing a longer query. We applied this patch on our production instance: suggestions now appear for short prefixes, with response times under 250 ms.