+ Do not query users table twice when filtering by usernames.
+ Removed redundant logic.
This commit is contained in:
Bobby Wibowo 2019-06-18 02:48:42 +07:00
parent 06ac31d02e
commit f6cef67d9f
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -731,8 +731,7 @@ uploadsController.list = async (req, res) => {
}) })
_filters.uploaders = await db.table('users') _filters.uploaders = await db.table('users')
.whereIn('username', usernames) .whereIn('username', usernames)
.select('id') .select('id', 'username')
.then(rows => rows.map(v => v.id))
} }
function filter () { function filter () {
@ -742,11 +741,11 @@ uploadsController.list = async (req, res) => {
this.where('userid', user.id) this.where('userid', user.id)
} else { } else {
// Fisrt, look for uploads matching ANY of the supplied 'user' OR 'ip' filters // Fisrt, look for uploads matching ANY of the supplied 'user' OR 'ip' filters
// Then, refined the matches using the supplied 'name' filters // Then, refine the matches using the supplied 'name' filters
const raw = [] const raw = []
const source = [] const source = []
if (_filters.uploaders.length) if (_filters.uploaders.length)
source.push(`\`userid\` in (${_filters.uploaders.map(v => `'${v}'`).join(', ')})`) source.push(`\`userid\` in (${_filters.uploaders.map(v => `'${v.id}'`).join(', ')})`)
if (_filters.ips.length) if (_filters.ips.length)
source.push(`\`ip\` in (${_filters.ips.map(v => `'${v}'`).join(', ')})`) source.push(`\`ip\` in (${_filters.ips.map(v => `'${v}'`).join(', ')})`)
if (_filters.flags.nouser) if (_filters.flags.nouser)
@ -814,11 +813,12 @@ uploadsController.list = async (req, res) => {
}) })
} }
// If we are a regular user, or we are not listing all uploads, send response // If we are not listing all uploads, send response
// TODO: !ismoderator is probably redundant (?) if (!all) return res.json({ success: true, files, count, albums, basedomain })
if (!ismoderator || !all) return res.json({ success: true, files, count, albums, basedomain })
// Otherwise proceed to querying usernames // Otherwise proceed to querying usernames
let _users = _filters.uploaders
if (!_users.length) {
const userids = files const userids = files
.map(file => file.userid) .map(file => file.userid)
.filter((v, i, a) => { .filter((v, i, a) => {
@ -829,15 +829,14 @@ uploadsController.list = async (req, res) => {
if (userids.length === 0) return res.json({ success: true, files, count, basedomain }) if (userids.length === 0) return res.json({ success: true, files, count, basedomain })
// Query usernames of user IDs from currently selected files // Query usernames of user IDs from currently selected files
const users = await db.table('users') _users = await db.table('users')
.whereIn('id', userids) .whereIn('id', userids)
.select('id', 'username') .select('id', 'username')
.then(rows => { }
// Build Object indexed by their IDs
const obj = {} const users = {}
for (const row of rows) obj[row.id] = row.username for (const user of _users)
return obj users[user.id] = user.username
})
return res.json({ success: true, files, count, users, basedomain }) return res.json({ success: true, files, count, users, basedomain })
} }