mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-22 03:34:14 +00:00
Improve auto completing
This commit is contained in:
parent
4a66e2fbe0
commit
51f66af320
1 changed files with 58 additions and 17 deletions
|
|
@ -1,5 +1,7 @@
|
|||
import {model as User} from '../../models/user';
|
||||
import {authWithHeaders} from '../../middlewares/auth';
|
||||
import {chatModel as Chat} from '../../models/message';
|
||||
import _ from 'lodash';
|
||||
|
||||
let api = {};
|
||||
|
||||
|
|
@ -9,7 +11,7 @@ api.getUsernameAutocompletes = {
|
|||
url: '/members/find/:username',
|
||||
middlewares: [authWithHeaders()],
|
||||
async handler (req, res) {
|
||||
res.set('Cache-Control', 'public, max-age=300000'); // 5 minutes
|
||||
// res.set('Cache-Control', 'public, max-age=300000'); // 5 minutes
|
||||
req.checkParams('username', res.t('invalidReqParams')).notEmpty();
|
||||
|
||||
let validationErrors = req.validationErrors();
|
||||
|
|
@ -23,25 +25,64 @@ api.getUsernameAutocompletes = {
|
|||
return;
|
||||
}
|
||||
|
||||
let query = {'auth.local.lowerCaseUsername': {$regex: `^${username}.*`}, 'flags.verifiedUsername': true, 'preferences.searchableUsername': {$ne: false}};
|
||||
let commonQuery = {
|
||||
'flags.verifiedUsername': true,
|
||||
'auth.blocked': {$ne: true},
|
||||
'flags.chatRevoked': {$ne: true},
|
||||
'auth.local.lowerCaseUsername': {$regex: `^${username}.*`},
|
||||
};
|
||||
let query = Object.assign({}, commonQuery);
|
||||
|
||||
let context = req.query.context;
|
||||
let id = req.query.id;
|
||||
if (context && id) {
|
||||
if (context === 'party') {
|
||||
query['party._id'] = res.locals.user.party._id;
|
||||
} else if (context === 'privateGuild') {
|
||||
if (res.locals.user.guilds.includes(id)) {
|
||||
query.guilds = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
let members = await User
|
||||
.find(query)
|
||||
.select(['profile.name', 'contributor', 'auth.local.username'])
|
||||
.limit(5)
|
||||
.exec();
|
||||
let groupID = req.query.id;
|
||||
|
||||
let members = [];
|
||||
let isPublicSpace = true;
|
||||
if (context && groupID) {
|
||||
if (context === 'party' && res.locals.user.party._id === groupID) {
|
||||
query['party._id'] = groupID;
|
||||
isPublicSpace = false;
|
||||
} else if (context === 'privateGuild' && res.locals.user.guilds.includes(groupID)) {
|
||||
query.guilds = groupID;
|
||||
isPublicSpace = false;
|
||||
} else if (context !== 'publicGuild' && context !== 'tavern') {
|
||||
res.respond(200, []);
|
||||
return;
|
||||
}
|
||||
|
||||
let recentChats = await Chat
|
||||
.find({groupId: groupID, username: {$regex: `^${username}.*`}})
|
||||
.select(['uuid'])
|
||||
.sort({timestamp: -1})
|
||||
.limit(200)
|
||||
.exec();
|
||||
let recentChatters = _.uniq(recentChats.map((message) => {
|
||||
return message.uuid;
|
||||
}));
|
||||
|
||||
let recentChatQuery = Object.assign({}, commonQuery);
|
||||
recentChatQuery._id = {$in: recentChatters};
|
||||
query._id = {$nin: recentChatters};
|
||||
members = await User
|
||||
.find(recentChatQuery)
|
||||
.select(['profile.name', 'contributor', 'auth.local.username'])
|
||||
.sort({'auth.timestamps.loggedin': -1})
|
||||
.limit(5 - members.length)
|
||||
.exec();
|
||||
}
|
||||
|
||||
if (members.length < 5) {
|
||||
if (isPublicSpace) {
|
||||
query['preferences.searchableUsername'] = {$ne: false};
|
||||
}
|
||||
let secondFetch = await User
|
||||
.find(query)
|
||||
.select(['profile.name', 'contributor', 'auth.local.username'])
|
||||
.sort({'auth.timestamps.loggedin': -1})
|
||||
.limit(5 - members.length)
|
||||
.exec();
|
||||
members = members.concat(secondFetch);
|
||||
}
|
||||
res.respond(200, members);
|
||||
},
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue