mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-31 07:11:33 +00:00
Updated
+ Do not query users table twice when filtering by usernames. + Removed redundant logic.
This commit is contained in:
parent
06ac31d02e
commit
f6cef67d9f
@ -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,30 +813,30 @@ 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
|
||||||
const userids = files
|
let _users = _filters.uploaders
|
||||||
.map(file => file.userid)
|
if (!_users.length) {
|
||||||
.filter((v, i, a) => {
|
const userids = files
|
||||||
return v !== null && v !== undefined && v !== '' && a.indexOf(v) === i
|
.map(file => file.userid)
|
||||||
})
|
.filter((v, i, a) => {
|
||||||
|
return v !== null && v !== undefined && v !== '' && a.indexOf(v) === i
|
||||||
|
})
|
||||||
|
|
||||||
// If there are no uploads attached to a registered user, send response
|
// If there are no uploads attached to a registered user, send response
|
||||||
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 })
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user