From 2998239344d804e09eeb13e22f49de3148c72023 Mon Sep 17 00:00:00 2001 From: mrfry Date: Fri, 25 Dec 2020 18:01:45 +0100 Subject: [PATCH] Saving questions --- src/modules/api/api.ts | 105 +++++++++++++++++++++++++++++++--------- src/utils/actions.ts | 2 +- src/utils/workerPool.ts | 6 +-- 3 files changed, 86 insertions(+), 27 deletions(-) diff --git a/src/modules/api/api.ts b/src/modules/api/api.ts index 20d6f96..a3faf5d 100644 --- a/src/modules/api/api.ts +++ b/src/modules/api/api.ts @@ -70,7 +70,6 @@ const todosFile = 'data/todos.json' const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js' const rootRedirectToFile = 'data/apiRootRedirectTo' const recievedQuestionFile = 'stats/recievedQuestions' -const dbsFile = 'data/dbs.json' // other constants const line = '====================================================' // lol @@ -94,6 +93,10 @@ function GetApp(): ModuleType { } // files in public dirs + const questionDbsDir = publicDir + 'questionDbs' + const dbsFile = publicDir + 'questionDbs.json' + const savedQuestionsFile = publicDir + 'savedQuestions.json' + const savedQuestionsDir = publicDir + 'savedQuestions' const recivedFiles = publicDir + 'recivedfiles' const uloadFiles = publicDir + 'f' const dataFiles: Array = utils.ReadJSON(dbsFile) @@ -144,7 +147,7 @@ function GetApp(): ModuleType { }) ) - const questionDbs = loadJSON(dataFiles, publicDir) + const questionDbs = loadJSON(dataFiles, questionDbsDir) let version = '' let rootRedirectURL = '' let motd = '' @@ -782,20 +785,40 @@ function GetApp(): ModuleType { }) app.get('/allqr.txt', function(req: Request, res: any) { - // TODO: if dataId param exists download only that db - res.set('Content-Type', 'text/plain') - const stringifiedData = questionDbs.map((qdb) => { - let result = '' - result += '\n' + line - result += ` Questions in ${qdb.name}: ` - result += line + '\n' - result += dataToString(qdb.data) - result += '\n' + line + line + '\n' - return result - }) - res.send(stringifiedData.join('\n\n')) - res.end() logger.LogReq(req) + const db: any = req.query.db + let stringifiedData = '' + + if (db) { + const requestedDb = questionDbs.find((qdb) => { + return qdb.name === db + }) + + if (!requestedDb) { + res.end(`No such db ${db}`) + return + } + + stringifiedData = '\n' + line + stringifiedData += ` Questions in ${requestedDb.name}: ` + stringifiedData += line + '\n' + stringifiedData += dataToString(requestedDb.data) + stringifiedData += '\n' + line + line + '\n' + } else { + res.set('Content-Type', 'text/plain') + stringifiedData = questionDbs + .map((qdb) => { + let result = '' + result += '\n' + line + result += ` Questions in ${qdb.name}: ` + result += line + '\n' + result += dataToString(qdb.data) + result += '\n' + line + line + '\n' + return result + }) + .join('\n\n') + } + res.end(stringifiedData) }) // ------------------------------------------------------------------------------------------- @@ -1026,7 +1049,32 @@ function GetApp(): ModuleType { } } + function saveQuestion(questions, subj, location, userid) { + const toWrite = { + questions: questions, + subj: subj, + userid: userid, + location: location, + date: new Date(), + } + const fname = `${utils.GetDateString()}_${userid}_${location}.json` + + const savedQuestions = utils.ReadJSON(savedQuestionsFile) + savedQuestions.push({ + fname: fname, + subj: subj, + userid: userid, + location: location, + date: new Date(), + }) + utils.WriteFile(JSON.stringify(savedQuestions), savedQuestionsFile) + + utils.WriteFile(JSON.stringify(toWrite), `${savedQuestionsDir}/${fname}`) + } + app.post('/ask', function(req: Request, res) { + const user: User = req.session.user + if (!req.body.questions) { res.json({ message: @@ -1038,6 +1086,7 @@ function GetApp(): ModuleType { return } const subj: any = req.body.subj || '' + const location = req.body.location.split('/')[2] writeAskData(req.body) @@ -1046,14 +1095,24 @@ function GetApp(): ModuleType { }) Promise.all(resultPromises).then((results) => { - res.json( - results.map((result: any) => { - return { - answers: result.result, - question: result.question, - } - }) - ) + const response = results.map((result: any) => { + return { + answers: result.result, + question: result.question, + } + }) + res.json(response) + + const saveableQuestions = response.reduce((acc, res) => { + // TODO + // if (res.answers.length === 0) { + // acc.push(res.question) + // } + acc.push(res.question) + return acc + }, []) + + saveQuestion(saveableQuestions, subj, location, user.id) }) }) diff --git a/src/utils/actions.ts b/src/utils/actions.ts index 092d4eb..46cf8dc 100755 --- a/src/utils/actions.ts +++ b/src/utils/actions.ts @@ -296,7 +296,7 @@ export function loadJSON( dataDir: string ): Array { return dataFiles.reduce((acc, dataFile, index) => { - const dataPath = dataDir + dataFile.path + const dataPath = dataDir + '/' + dataFile.path if (!utils.FileExists(dataPath)) { utils.WriteFile(JSON.stringify([]), dataPath) diff --git a/src/utils/workerPool.ts b/src/utils/workerPool.ts index 37eef90..49fc119 100644 --- a/src/utils/workerPool.ts +++ b/src/utils/workerPool.ts @@ -93,9 +93,9 @@ function getAWorker(i, initData) { // logger.Log(`[MAIN]: Msg from worker #${i}`, msg) // }) - worker.on('online', () => { - logger.Log(`[THREAD #${i}]: Worker ${i} online`) - }) + // worker.on('online', () => { + // logger.Log(`[THREAD #${i}]: Worker ${i} online`) + // }) worker.on('error', (err) => { logger.Log('Worker error!', logger.GetColor('redbg'))