Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVE] Always shows the exact match first on user's and room's autocomplete for mentions and on sidebar search #16394

Merged
merged 6 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions app/ui-sidenav/client/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,33 @@ const getFromServer = (cb, type) => {
return false;
}

let exactUser = null;
let exactRoom = null;
if (results.users[0] && results.users[0].username === currentFilter) {
exactUser = results.users.shift();
}
if (results.rooms[0] && results.rooms[0].username === currentFilter) {
exactRoom = results.rooms.shift();
}

const resultsFromServer = [];

resultsFromServer.push(...results.users.map((user) => ({
const roomFilter = (room) => !resultsFromClient.find((item) => [item.rid, item._id].includes(room._id));
const userMap = (user) => ({
_id: user._id,
t: 'd',
name: user.username,
fname: user.name,
})));
});

resultsFromServer.push(...results.rooms.filter((room) => !resultsFromClient.find((item) => [item.rid, item._id].includes(room._id))));
resultsFromServer.push(...results.users.map(userMap));
resultsFromServer.push(...results.rooms.filter(roomFilter));

if (resultsFromServer.length) {
cb(resultsFromClient.concat(resultsFromServer));
if (resultsFromServer.length || exactUser || exactRoom) {
exactRoom = exactRoom ? [roomFilter(exactRoom)] : [];
exactUser = exactUser ? [userMap(exactUser)] : [];
const combinedResults = exactUser.concat(exactRoom, resultsFromClient, resultsFromServer);
cb(combinedResults);
}
});
};
Expand Down
17 changes: 14 additions & 3 deletions server/publications/spotlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function fetchRooms(userId, rooms) {
}

Meteor.methods({
spotlight(text, usernames, type = { users: true, rooms: true }, rid) {
spotlight(text, usernames = [], type = { users: true, rooms: true }, rid) {
const searchForChannels = text[0] === '#';
const searchForDMs = text[0] === '@';
if (searchForChannels) {
Expand Down Expand Up @@ -72,7 +72,12 @@ Meteor.methods({

if (hasPermission(userId, 'view-outside-room')) {
if (type.users === true && hasPermission(userId, 'view-d-room')) {
result.users = Users.findByActiveUsersExcept(text, usernames, userOptions).fetch();
const exactUser = Users.findOneByUsernameIgnoringCase(text, userOptions);
if (exactUser && !usernames.includes(exactUser.username)) {
result.users.push(exactUser);
usernames.push(exactUser.username);
}
result.users = result.users.concat(Users.findByActiveUsersExcept(text, usernames, userOptions).fetch());
}

if (type.rooms === true && hasPermission(userId, 'view-c-room')) {
Expand All @@ -81,7 +86,13 @@ Meteor.methods({
.map((roomType) => roomType[0]);

const roomIds = Subscriptions.findByUserIdAndTypes(userId, searchableRoomTypes, { fields: { rid: 1 } }).fetch().map((s) => s.rid);
result.rooms = fetchRooms(userId, Rooms.findByNameAndTypesNotInIds(regex, searchableRoomTypes, roomIds, roomOptions).fetch());
const exactRoom = Rooms.findOneByNameAndType(text, searchableRoomTypes, roomOptions);
if (exactRoom) {
result.exactRoom.push(exactRoom);
roomIds.push(exactRoom.rid);
}

result.rooms = result.rooms.concat(fetchRooms(userId, Rooms.findByNameAndTypesNotInIds(regex, searchableRoomTypes, roomIds, roomOptions).fetch()));
}
} else if (type.users === true && rid) {
const subscriptions = Subscriptions.find({
Expand Down