 # - userid,
 #
 # or an error.
-exports.sessionInfoRefreshing = (dbConnection, sessid, sess_timeexpire, callback) ->
+exports.sessionInfoRefreshing = ((dbConnection, sessid, sess_timeexpire, asyncUpdate, callback) ->
 	unless sessid?
-		callback null, sessionIsActive: false
-		return
-	async.auto
-		getUser: (callback) ->
-			dbConnection.query("SELECT id, user, permissions FROM uniusers " +
-				"WHERE sessid = ? AND sess_time > NOW() - INTERVAL ? SECOND",
-				[ sessid, sess_timeexpire ], callback)
-			return
-		refresh: [
-			"getUser"
-			(callback, results) ->
-				if results.getUser.rowCount is 0
-					callback null, "session does not exist or expired"
-					return
-				dbConnection.query "UPDATE `uniusers` SET `sess_time` = NOW() WHERE `sessid` = ?",
-					[sessid], callback
-		]
-	, (error, results) ->
-		if error?
-			callback error, null
-			return
-		if results.refresh is "session does not exist or expired"
-			callback null, sessionIsActive: false
-			return
-		callback null,
-			sessionIsActive: true
-			username: results.getUser.rows[0].user
-			admin: (results.getUser.rows[0].permissions is config.PERMISSIONS_ADMIN)
-			userid: results.getUser.rows[0].id
-		return
-	return
+		return sessionIsActive: false
+	userdata = dbConnection.query.sync(dbConnection,
+		"SELECT id, username, permissions FROM uniusers "+
+		"WHERE sessid = $1 AND sess_time > NOW() - $2 * INTERVAL '1 SECOND'",
+		[sessid, sess_timeexpire]
+	)
+
+	if userdata.rows.length is 0
+		return sessionIsActive: false
+
+	if asyncUpdate is true
+		process.nextTick ->
+			dbConnection.query(
+				'UPDATE uniusers SET sess_time = NOW() WHERE id = $1',
+				[userdata.rows[0].id],
+				(error, result) ->
+					if error?
+						console.log 'Async update failed'
+						console.log error.stack
+			)
+	else
+		dbConnection.query.sync(dbConnection,
+			'UPDATE uniusers SET sess_time = NOW() WHERE id = $1',
+			[userdata.rows[0].id]
+		)
+
+	return {
+		sessionIsActive: true
+		username: userdata.rows[0].username
+		admin: (userdata.rows[0].permissions is 'admin')
+		userid: userdata.rows[0].id
+	}
+).async()
 
 
 # Generate an unique sessid with the given length.


crypto = require 'crypto'
async = require 'async'


# Check if a user with the given username exists.
# Returns true or false, or an error.
exports.userExists = (dbConnection, username, callback) ->
	dbConnection.query "SELECT count(*) AS result FROM `uniusers` WHERE user = ?", [username], (error, result) ->
		callback error, error or (result.rows[0].result > 0)


# Check if a user with the given id exists.
# Returns true or false, or an error.
exports.idExists = (dbConnection, id, callback) ->
	dbConnection.query "SELECT count(*) AS result FROM `uniusers` WHERE `id`= ?", [id], (error, result) ->
		callback error, error or (result.rows[0].result > 0)


# Check if a session with the given sessid exists.
# Returns true or false, or an error.
exports.sessionExists = (dbConnection, sess, callback) ->
	dbConnection.query "SELECT count(*) AS result FROM `uniusers` WHERE `sessid` = ?", [sess], (error, result) ->
		callback error, error or (result.rows[0].result > 0)



		(innerCallback) ->
			exports.generateSessId dbConnection, config.sessionLength, innerCallback
		(sessid, innerCallback) ->
			dbConnection.query 'UPDATE uniusers SET sess_time = NOW(), sessid = $1 '+
				'WHERE lower(username) = lower($2)',
				[sessid, username],
				(error, result) ->
					innerCallback error, result, sessid
	], (error, result, sessid) ->
		callback error, sessid
