ensure fresh captchas are served by sorting on attempted

Signed-off-by: hrj <harshad.rj@gmail.com>
This commit is contained in:
hrj 2021-04-19 01:02:10 +05:30
parent 7684b68efd
commit 2885decb56

View File

@ -19,7 +19,10 @@ class Statements(dbConn: DBConn, maxAttempts: Int) {
"contentInput varchar, " +
"image blob, " +
"attempted int default 0, " +
"PRIMARY KEY(token))"
"PRIMARY KEY(token));" +
"""
CREATE INDEX IF NOT EXISTS attempted ON challenge(attempted);
"""
)
stmt.execute(
"CREATE TABLE IF NOT EXISTS mapId" +
@ -69,13 +72,13 @@ class Statements(dbConn: DBConn, maxAttempts: Int) {
val tokenPstmt: PreparedStatement = dbConn.con.prepareStatement(
s"""
SELECT token
SELECT token, attempted
FROM challenge
WHERE attempted < $maxAttempts AND
contentLevel = ? AND
contentType = ? AND
contentInput = ?
ORDER BY RAND() LIMIT 1"""
ORDER BY attempted ASC LIMIT 1"""
)
val deleteAnswerPstmt: PreparedStatement = dbConn.con.prepareStatement(