diff --git a/src/modules/api/api.ts b/src/modules/api/api.ts index 69f0931..994059f 100644 --- a/src/modules/api/api.ts +++ b/src/modules/api/api.ts @@ -41,7 +41,7 @@ import auth from '../../middlewares/auth.middleware' import { dataToString, searchDatas } from '../../utils/classes' import { SetupData } from '../../server' -import { ModuleType, User, DataFile } from '../../types/basicTypes' +import { ModuleType, User, DataFile, Request } from '../../types/basicTypes' // files const msgFile = 'stats/msgs' @@ -63,7 +63,7 @@ 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 +const minimumAlowwedSessions = 2 // how many sessions are allowed for a user // stuff gotten from server.js let userDB @@ -261,7 +261,7 @@ function GetApp(): ModuleType { // ------------------------------------------------------------- - app.get('/getDbs', (req: any, res: any) => { + app.get('/getDbs', (req: Request, res: any) => { logger.LogReq(req) res.json( @@ -274,10 +274,10 @@ function GetApp(): ModuleType { ) }) - app.get('/voteTodo', (req: any, res: any) => { + app.get('/voteTodo', (req: Request, res: any) => { logger.LogReq(req) const userId = req.session.user.id - const id = req.query.id + const id: any = req.query.id const todos = utils.ReadJSON(todosFile) if (!id) { @@ -314,7 +314,7 @@ function GetApp(): ModuleType { }) }) - app.get('/todos', (req: any, res: any) => { + app.get('/todos', (req: Request, res: any) => { logger.LogReq(req) const userId = req.session.user.id const todos = utils.ReadJSON(todosFile) @@ -326,17 +326,17 @@ function GetApp(): ModuleType { }) }) - app.get('/ranklist', (req: any, res: any) => { + app.get('/ranklist', (req: Request, res: any) => { logger.LogReq(req) let result - let since = req.query.since + const querySince: any = req.query.since const user: User = req.session.user - if (!since) { + if (!querySince) { result = utils.ReadJSON(idStatFile) } else { try { - since = new Date(since) + const since = new Date(querySince) if (!(since instanceof Date) || isNaN(since.getTime())) { throw new Error('Not a date') } @@ -396,16 +396,16 @@ function GetApp(): ModuleType { } res.json({ - since: since, + since: querySince, sum: sum, list: list, selfuserId: user.id, }) }) - app.get('/quickvote', (req: any, res: any) => { + app.get('/quickvote', (req: Request, res: any) => { const key = req.query.key - const val = req.query.val + const val: any = req.query.val const user: User = req.session.user if (!key || !val) { @@ -479,7 +479,7 @@ function GetApp(): ModuleType { utils.WriteFile(JSON.stringify(voteData), voteFile) }) - app.get('/avaiblePWS', (req: any, res: any) => { + app.get('/avaiblePWS', (req: Request, res: any) => { logger.LogReq(req) const user: User = req.session.user @@ -497,7 +497,7 @@ function GetApp(): ModuleType { }) }) - app.post('/getpw', function(req: any, res: any) { + app.post('/getpw', function(req: Request, res: any) { logger.LogReq(req) const requestingUser = req.session.user @@ -547,7 +547,7 @@ function GetApp(): ModuleType { }) }) - app.post('/getveteranpw', function(req: any, res: any) { + app.post('/getveteranpw', function(req: Request, res: any) { logger.LogReq(req) const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const tries = dbtools.Select(userDB, 'veteranPWRequests', { @@ -643,7 +643,7 @@ function GetApp(): ModuleType { } }) - app.post('/login', (req: any, res: any) => { + app.post('/login', (req: Request, res: any) => { logger.LogReq(req) const pw = req.body.pw ? req.body.pw @@ -744,7 +744,7 @@ function GetApp(): ModuleType { } }) - app.post('/logout', (req: any, res: any) => { + app.post('/logout', (req: Request, res: any) => { logger.LogReq(req) const sessionID = req.cookies.sessionID @@ -759,12 +759,12 @@ function GetApp(): ModuleType { // -------------------------------------------------------------- - app.get('/', function(req: any, res: any) { + app.get('/', function(req: Request, res: any) { logger.LogReq(req) - res.redirect('https://www.youtube.com/watch?v=ieqGJgqiXFk') + res.redirect('https://www.youtube.com/watch?v=qLrnkK2YEcE') }) - app.post('/postfeedbackfile', function(req: any, res: any) { + app.post('/postfeedbackfile', function(req: Request, res: any) { UploadFile(req, res, uloadFiles, () => { res.json({ success: true }) }) @@ -773,7 +773,7 @@ function GetApp(): ModuleType { logger.Log('New feedback file', logger.GetColor('bluebg')) }) - app.post('/postfeedback', function(req: any, res: any) { + app.post('/postfeedback', function(req: Request, res: any) { logger.LogReq(req) if (req.body.fromLogin) { logger.Log( @@ -803,7 +803,7 @@ function GetApp(): ModuleType { res.json({ success: true }) }) - function UploadFile(req: any, res: any, path, next) { + function UploadFile(req: Request, res: any, path, next) { try { req.pipe(req.busboy) req.busboy.on('file', function(fieldname, file, filename) { @@ -840,20 +840,21 @@ function GetApp(): ModuleType { } } - app.route('/fosuploader').post(function(req: any, res: any) { + app.route('/fosuploader').post(function(req: Request, res: any) { UploadFile(req, res, uloadFiles, (fn) => { res.redirect('/f/' + fn) }) }) - app.route('/badtestsender').post(function(req: any, res: any) { + app.route('/badtestsender').post(function(req: Request, res: any) { UploadFile(req, res, recivedFiles, () => { res.redirect('back') }) logger.LogReq(req) }) - app.get('/allqr.txt', function(req: any, res: any) { + 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 = '' @@ -872,7 +873,7 @@ function GetApp(): ModuleType { // ------------------------------------------------------------------------------------------- // API - app.post('/uploaddata', (req: any, res: any) => { + app.post('/uploaddata', (req: Request, res: any) => { // body: JSON.stringify({ // newData: data, // count: getCount(data), @@ -979,7 +980,7 @@ function GetApp(): ModuleType { } }) - app.post('/isAdding', function(req: any, res: any) { + app.post('/isAdding', function(req: Request, res: any) { logger.LogReq(req) const user: User = req.session.user const dryRun = testUsers.includes(user.id) @@ -1015,7 +1016,7 @@ function GetApp(): ModuleType { } }) - app.get('/ask', function(req: any, res) { + app.get('/ask', function(req: Request, res) { if (Object.keys(req.query).length === 0) { logger.DebugLog(`No query params`, 'ask', 1) res.json({ @@ -1026,7 +1027,7 @@ function GetApp(): ModuleType { }) } else { if (req.query.q && req.query.data) { - const subj = req.query.subj || '' + const subj: any = req.query.subj || '' const question = req.query.q const recData: any = req.query.data @@ -1124,7 +1125,7 @@ function GetApp(): ModuleType { }) } - app.get('/datacount', function(req: any, res: any) { + app.get('/datacount', function(req: Request, res: any) { logger.LogReq(req) if (req.query.detailed === 'all') { res.json({ @@ -1138,7 +1139,7 @@ function GetApp(): ModuleType { } }) - app.get('/infos', function(req: any, res) { + app.get('/infos', function(req: Request, res) { const user: User = req.session.user const result: any = { @@ -1163,11 +1164,11 @@ function GetApp(): ModuleType { // ------------------------------------------------------------------------------------------- - app.get('*', function(req: any, res: any) { + app.get('*', function(req: Request, res: any) { res.status(404).render('404') }) - app.post('*', function(req: any, res: any) { + app.post('*', function(req: Request, res: any) { res.status(404).render('404') }) diff --git a/src/modules/dataEditor/dataEditor.ts b/src/modules/dataEditor/dataEditor.ts index f127609..1427f08 100644 --- a/src/modules/dataEditor/dataEditor.ts +++ b/src/modules/dataEditor/dataEditor.ts @@ -29,7 +29,7 @@ import utils from '../../utils/utils' import logger from '../../utils/logger' import auth from '../../middlewares/auth.middleware' import { SetupData } from '../../server' -import { ModuleType } from '../../types/basicTypes' +import { ModuleType, Request } from '../../types/basicTypes' // stuff gotten from server.js let userDB @@ -83,7 +83,7 @@ function GetApp(): ModuleType { routes.forEach((route) => { logger.DebugLog(`Added route /${route}`, 'DataEditor routes', 1) - app.get(`/${route}`, function(req, res) { + app.get(`/${route}`, function(req: Request, res) { logger.LogReq(req) res.redirect(`${route}.html`) }) @@ -93,16 +93,16 @@ function GetApp(): ModuleType { // -------------------------------------------------------------- - app.get('/', function(req, res) { + app.get('/', function(req: Request, res) { res.end('hai') logger.LogReq(req) }) - app.get('*', function(req, res) { + app.get('*', function(req: Request, res) { res.status(404).render('404') }) - app.post('*', function(req, res) { + app.post('*', function(req: Request, res) { res.status(404).render('404') }) diff --git a/src/modules/qmining/qmining.ts b/src/modules/qmining/qmining.ts index 6d1d383..0ea3055 100644 --- a/src/modules/qmining/qmining.ts +++ b/src/modules/qmining/qmining.ts @@ -29,7 +29,7 @@ import utils from '../../utils/utils' import logger from '../../utils/logger' import auth from '../../middlewares/auth.middleware' import { SetupData } from '../../server' -import { ModuleType } from '../../types/basicTypes' +import { ModuleType, Request } from '../../types/basicTypes' // stuff gotten from server.js let publicdirs = [] @@ -104,7 +104,7 @@ function GetApp(): ModuleType { // -------------------------------------------------------------- // to be backwards compatible - app.get('/ask', function(req, res) { + app.get('/ask', function(req: Request, res) { logger.DebugLog(`Qmining module ask redirect`, 'ask', 1) res.redirect( `http://api.frylabs.net/ask?q=${req.query.q}&subj=${req.query.subj}&data=${req.query.data}` @@ -173,7 +173,7 @@ function GetApp(): ModuleType { ] simpleRedirects.forEach((redirect) => { - app.get(redirect.from, function(req, res) { + app.get(redirect.from, function(req: Request, res) { if (!redirect.nolog) { logger.LogReq(req) } @@ -201,7 +201,7 @@ function GetApp(): ModuleType { routes.forEach((route) => { logger.DebugLog(`Added route /${route}`, 'Qmining routes', 1) - app.get(`/${route}`, function(req: express.Request, res) { + app.get(`/${route}`, function(req: Request, res) { logger.LogReq(req) res.redirect( utils.formatUrl({ @@ -216,12 +216,12 @@ function GetApp(): ModuleType { // -------------------------------------------------------------- - app.get('/', function(req, res) { + app.get('/', function(req: Request, res) { res.end('hai') logger.LogReq(req) }) - app.get('/getVeteranPw', function(req, res) { + app.get('/getVeteranPw', function(req: Request, res) { res.render('veteranPw', { cid: req.query.cid || '', devel: process.env.NS_DEVEL, @@ -229,11 +229,11 @@ function GetApp(): ModuleType { logger.LogReq(req) }) - app.get('*', function(req, res) { + app.get('*', function(req: Request, res) { res.status(404).render('404') }) - app.post('*', function(req, res) { + app.post('*', function(req: Request, res) { res.status(404).render('404') }) diff --git a/src/modules/stuff/stuff.ts b/src/modules/stuff/stuff.ts index 8f9ba4e..ea25136 100644 --- a/src/modules/stuff/stuff.ts +++ b/src/modules/stuff/stuff.ts @@ -28,7 +28,7 @@ const app = express() // other requires import logger from '../../utils/logger' import { SetupData } from '../../server' -import { ModuleType } from '../../types/basicTypes' +import { ModuleType, Request } from '../../types/basicTypes' // stuff gotten from server.js let publicdirs = [] @@ -150,7 +150,7 @@ function GetApp(): ModuleType { appGetFileType(app, t[0], t[1], t[2]) }) - app.get('/*', function(req, res) { + app.get('/*', function(req: Request, res) { const parsedUrl = decodeURIComponent(req.url) let curr = listedFiles + @@ -230,11 +230,11 @@ function GetApp(): ModuleType { // ----------------------------------------------------------------------------------------------- - app.get('*', function(req, res) { + app.get('*', function(req: Request, res) { res.status(404).render('404') }) - app.post('*', function(req, res) { + app.post('*', function(req: Request, res) { res.status(404).render('404') }) diff --git a/src/sharedViews/404.ejs b/src/sharedViews/404.ejs index 1bb6149..4126305 100755 --- a/src/sharedViews/404.ejs +++ b/src/sharedViews/404.ejs @@ -18,7 +18,7 @@

404

- +
diff --git a/src/types/basicTypes.ts b/src/types/basicTypes.ts index acffeed..df45cb7 100644 --- a/src/types/basicTypes.ts +++ b/src/types/basicTypes.ts @@ -46,3 +46,9 @@ export interface User { avaiblePWRequests: number loginCount: number } + +export interface Request extends express.Request { + cookies: any + session: any + busboy: any +} diff --git a/src/utils/classes.ts b/src/utils/classes.ts index a0d051a..f07a8fa 100755 --- a/src/utils/classes.ts +++ b/src/utils/classes.ts @@ -215,16 +215,20 @@ function createQuestion( ): Question { return { Q: simplifyQuestion(question), - A: simplifyAnswer(answer), + A: answer ? simplifyAnswer(answer) : null, data, } } function compareImage(data: QuestionData, data2: QuestionData) { - // TODO: img comparing (hashed images vs images) - const imgs1 = data.hashedImages ? data.hashedImages : data.images - const imgs2 = data2.hashedImages ? data2.hashedImages : data2.images - return compareString(imgs1.join(' '), imgs2.join(' ')) + if (data.hashedImages && data2.hashedImages) { + return compareString( + data.hashedImages.join(' '), + data.hashedImages.join(' ') + ) + } else { + return compareString(data.images.join(' '), data2.images.join(' ')) - 10 + } } function compareData(q1: Question, q2: Question) { @@ -273,24 +277,15 @@ function compareQuestionObj( assert(q1) assert(typeof q1 === 'object') assert(q2) - let qObj + assert(typeof q2 === 'object') - if (typeof q2 === 'string') { - qObj = { - Q: q2, - data: data, - } - } else { - qObj = q2 - } - - const qMatch = compareQuestion(q1, qObj) - const aMatch = compareAnswer(q1, qObj) + const qMatch = compareQuestion(q1, q2) + const aMatch = q2.A ? compareAnswer(q1, q2) : 0 // -1 if botth questions are simple - const dMatch = compareData(q1, qObj) + const dMatch = compareData(q1, q2) let avg = -1 - if (qObj.A) { + if (q2.A) { if (dMatch === -1) { avg = (qMatch + aMatch) / 2 } else { @@ -427,33 +422,46 @@ function searchDatas( ) } -// FIXME: remove questionData, make question only Question type +function prepareQuestion( + question: string | Question, + data: string | QuestionData +): Question { + let preparedQuestion: Question + if (typeof question === 'object') { + preparedQuestion = question + } else { + // FIXME data was checkedif its null, it should be never null. check if its really never null + const parsedData = typeof data === 'object' ? data : JSON.parse(data) + preparedQuestion = createQuestion(question, null, parsedData) + } + + return simplifyQuestion(preparedQuestion) +} + function searchData( qdb: QuestionDb, - question: any, + question: Question | string, subjName: string, - questionData?: QuestionData + questionData?: QuestionData | string ): Promise { + // FIXME subjName was checkedif its null, it should be never null. check if its really never null return new Promise((resolve, reject) => { assert(question) logger.DebugLog('Searching for question', 'qdb search', 1) - logger.DebugLog('Question:', 'qdb search', 2) - logger.DebugLog(question, 'qdb search', 2) - logger.DebugLog(`Subject name: ${subjName}`, 'qdb search', 2) - logger.DebugLog('Data:', 'qdb search', 2) - logger.DebugLog(questionData || question.data, 'qdb search', 2) - if (!questionData) { - questionData = question.data || { type: 'simple' } - } - if (!subjName) { - subjName = '' - logger.DebugLog('No subject name as param!', 'qdb search', 1) - } - question = simplifyQuestion(question) + const preparedQuestion = prepareQuestion(question, questionData) + + logger.DebugLog('Question:', 'qdb search', 2) + logger.DebugLog(preparedQuestion, 'qdb search', 2) + logger.DebugLog(`Subject name: ${subjName}`, 'qdb search', 2) const worker = workerTs(searchDataWorkerFile, { - workerData: { data: qdb.data, subjName, question, questionData }, + workerData: { + data: qdb.data, + subjName, + question: preparedQuestion, + questionData, + }, }) worker.on('error', (err) => { @@ -474,6 +482,14 @@ function searchData( }) worker.on('message', (result) => { + // TODO: remove (?) + if (typeof result === 'string') { + try { + console.log(JSON.parse(result)) + } catch (err) { + console.log(result) + } + } logger.DebugLog(`Worker message arrived`, 'worker', 2) logger.DebugLog(result, 'worker', 3) logger.DebugLog(`Question result length: ${result.length}`, 'ask', 1) diff --git a/src/utils/logger.ts b/src/utils/logger.ts index a0f2b23..f6107e2 100755 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -39,6 +39,8 @@ const DELIM = C('green') + '|' + C() // import express from 'express' import utils from '../utils/utils' +import { Request } from '../types/basicTypes' + const vlogDir = './stats/vlogs/' const logDir = './stats/logs/' const statFile = 'stats/stats' @@ -109,13 +111,10 @@ function Log(msg: string | object, color?: string): void { ) } -function LogReq( - req: any /*express.Request*/, - toFile?: boolean, - statusCode?: string -): void { +function LogReq(req: Request, toFile?: boolean, statusCode?: string): void { try { - const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress + const ip: any = + req.headers['cf-connecting-ip'] || req.connection.remoteAddress const nolog = noLogips.some((noLogip) => { return ip.includes(noLogip) diff --git a/submodules/qmining-page b/submodules/qmining-page index 309436b..e935de7 160000 --- a/submodules/qmining-page +++ b/submodules/qmining-page @@ -1 +1 @@ -Subproject commit 309436b2270ebf2398945b2cd707d1f133fe8ec1 +Subproject commit e935de7e7896ca5207de84672ecdaa89a003827a