Seperate dbs

This commit is contained in:
mrfry 2020-11-26 09:16:12 +01:00
parent 728931d56e
commit 4e34267d44
9 changed files with 149 additions and 82 deletions

View file

@ -57,11 +57,13 @@ const idvStatFile = 'stats/idvstats'
const todosFile = 'data/todos.json'
// other constants
const line = '====================================================' // lol
const maxVeteranPwGetCount = 10
const addPWPerDay = 3 // every x day a user can give a pw
const maxPWCount = 6 // maximum pw give opportunities a user can have at once
const addPWCount = 1 // how many pw gen opportunities to add each time
const daysAfterUserGetsPWs = 5 // days after user gets pw-s
const minimumAlowwedSessions = 1 // how many sessions are allowed for a user
// stuff gotten from server.js
let userDB
@ -79,19 +81,27 @@ function GetApp(): ModuleType {
// files in public dirs
const recivedFiles = publicDir + 'recivedfiles'
const uloadFiles = publicDir + 'f'
// FIXME: this to seperate file?
const dataFiles: Array<DataFile> = [
{
path: `${publicDir}oldData.json`,
name: 'oldData',
shouldSave: (recData: RecievedData): boolean => {
return recData.version.includes('2.0.')
return recData.version.startsWith('2.0.')
},
},
{
path: `${publicDir}data.json`,
name: 'newData',
shouldSave: (recData: RecievedData): boolean => {
return recData.version.includes('2.1.')
return recData.version.startsWith('2.1.')
},
},
{
path: `${publicDir}fromwebsiteData.json`,
name: 'fromwebsiteData',
shouldSave: (recData: RecievedData): boolean => {
return recData.version === 'WEBSITE'
},
},
]
@ -99,6 +109,11 @@ function GetApp(): ModuleType {
const userSpecificMotdFile = publicDir + 'userSpecificMotd.json'
const versionFile = publicDir + 'version'
let domain = url.split('.') // [ "https://api", "frylabs", "net" ]
domain.shift() // [ "frylabs", "net" ]
domain = domain.join('.') // "frylabs.net"
logger.DebugLog(`Cookie domain: ${domain}`, 'cookie', 1)
app.use(
bodyParser.urlencoded({
limit: '10mb',
@ -246,7 +261,21 @@ function GetApp(): ModuleType {
// -------------------------------------------------------------
app.get('/getDbs', (req: any, res: any) => {
logger.LogReq(req)
res.json(
dataFiles.map((df) => {
return {
path: df.path.split('/').pop(),
name: df.name,
}
})
)
})
app.get('/updateTodo', (req: any, res: any) => {
logger.LogReq(req)
const userId = req.session.user.id
const id = req.query.id
const todos = utils.ReadJSON(todosFile)
@ -632,13 +661,12 @@ function GetApp(): ModuleType {
if (user) {
const sessionID = uuidv4()
// FIXME: Users now can only log in in one session, this might be too strict.
const existingSessions = dbtools.Select(userDB, 'sessions', {
userID: user.id,
isScript: isScript ? 1 : 0,
})
if (existingSessions.length > 0) {
if (existingSessions.length >= minimumAlowwedSessions) {
logger.Log(
`Multiple ${isScript ? 'script' : 'website'} sessions ( ${
existingSessions.length
@ -675,9 +703,9 @@ function GetApp(): ModuleType {
})
// https://www.npmjs.com/package/cookie
// TODO: cookie age
// FIXME: cookies are not configured coorectly
res.cookie('sessionID', sessionID, {
domain: '.frylabs.net', // TODO: use url. url: "https://api.frylabs.net"
domain: domain,
expires: new Date(
new Date().getTime() + 10 * 365 * 24 * 60 * 60 * 1000
),
@ -724,7 +752,6 @@ function GetApp(): ModuleType {
dbtools.Delete(userDB, 'sessions', {
id: sessionID,
})
// TODO: remove old sessions every once in a while
res.clearCookie('sessionID').json({
result: 'success',
})
@ -829,9 +856,14 @@ function GetApp(): ModuleType {
app.get('/allqr.txt', function(req: any, res: any) {
res.set('Content-Type', 'text/plain')
const stringifiedData = questionDbs.map((qdb) => {
return dataToString(qdb.data)
let result = ''
result += '\n' + line
result += ` Questions in ${qdb.name}: `
result += line + '\n'
result += dataToString(qdb.data)
result += '\n' + line + line + '\n'
return result
})
// TODO: test this
res.send(stringifiedData.join('\n\n'))
res.end()
logger.LogReq(req)
@ -949,26 +981,38 @@ function GetApp(): ModuleType {
app.post('/isAdding', function(req: any, res: any) {
logger.LogReq(req)
const user: User = req.session.user
const dryRun = testUsers.includes(user.id)
processIncomingRequest(req.body, questionDbs, dryRun, user)
.then((resultArray) => {
logResult(req.body, resultArray)
res.json({
success: resultArray.length > 0, // TODO check for -1s
newQuestions: resultArray,
try {
processIncomingRequest(req.body, questionDbs, dryRun, user)
.then((resultArray) => {
logResult(req.body, resultArray, user.id, dryRun)
res.json({
success: resultArray.length > 0,
newQuestions: resultArray,
})
})
.catch((err) => {
logger.Log(
'Error during processing incoming request',
logger.GetColor('redbg')
)
console.error(err)
res.json({
success: false,
})
})
} catch (err) {
logger.Log(
'Error during getting incoming request processor promises ',
logger.GetColor('redbg')
)
console.error(err)
res.json({
success: false,
})
.catch((err) => {
logger.Log(
'Couldnt process incoming request!',
logger.GetColor('redbg')
)
console.error(err)
})
}
})
app.get('/ask', function(req: any, res) {
@ -996,8 +1040,6 @@ function GetApp(): ModuleType {
searchDatas(questionDbs, question, subj, recData)
.then((result) => {
// TODO: test
console.log(result)
res.json({
result: result,
success: true,