diff --git a/src/modules.json b/src/modules.json index b268626..3659eb3 100644 --- a/src/modules.json +++ b/src/modules.json @@ -1,68 +1,42 @@ { "dataEditor": { "path": "./modules/dataEditor/dataEditor.js", - "publicdirs": [ - "qminingPublic/" - ], + "publicdirs": ["qminingPublic/"], "nextdir": "modules/dataEditor/public/", "name": "dataeditor", - "urls": [ - "dataeditor.frylabs.net" - ], + "urls": ["dataeditor.frylabs.net"], "isNextJs": true }, "qmining": { "path": "./modules/qmining/qmining.js", - "publicdirs": [ - "qminingPublic/" - ], + "publicdirs": ["qminingPublic/"], "nextdir": "modules/qmining/public/", "name": "qmining", - "urls": [ - "qmining.frylabs.net" - ], + "urls": ["qmining.frylabs.net"], "isNextJs": true }, "api": { "path": "./modules/api/api.js", - "publicdirs": [ - "qminingPublic/" - ], + "publicdirs": ["qminingPublic/"], "name": "api", - "urls": [ - "api.frylabs.net", - "localhost" - ] + "urls": ["api.frylabs.net", "localhost"] }, "main": { "path": "./modules/main/main.js", - "publicdirs": [ - "public/" - ], + "publicdirs": ["public/"], "name": "main", - "urls": [ - "frylabs.net", - "www.frylabs.net" - ] + "urls": ["frylabs.net", "www.frylabs.net"] }, "sio": { "path": "./modules/sio/sio.js", - "publicdirs": [ - "sioPublic/" - ], + "publicdirs": ["sioPublic/"], "name": "sio", - "urls": [ - "sio.frylabs.net" - ] + "urls": ["sio.frylabs.net"] }, "stuff": { "path": "./modules/stuff/stuff.js", - "publicdirs": [ - "stuffPublic/" - ], + "publicdirs": ["stuffPublic/"], "name": "stuff", - "urls": [ - "stuff.frylabs.net" - ] + "urls": ["stuff.frylabs.net"] } } diff --git a/src/modules/api/api.js b/src/modules/api/api.js index 0d5d148..16452d9 100644 --- a/src/modules/api/api.js +++ b/src/modules/api/api.js @@ -54,7 +54,7 @@ let userDB let url // eslint-disable-line let publicdirs = [] -function GetApp () { +function GetApp() { const p = publicdirs[0] if (!p) { throw new Error(`No public dir! ( API )`) @@ -67,62 +67,67 @@ function GetApp () { const motdFile = p + 'motd' const versionFile = p + 'version' - app.use(bodyParser.urlencoded({ - limit: '10mb', - extended: true - })) - app.use(bodyParser.json({ - limit: '10mb' - })) + app.use( + bodyParser.urlencoded({ + limit: '10mb', + extended: true, + }) + ) + app.use( + bodyParser.json({ + limit: '10mb', + }) + ) app.set('view engine', 'ejs') - app.set('views', [ - './modules/api/views', - './sharedViews' - ]) - app.use(auth({ - userDB: userDB, - jsonResponse: true, - exceptions: [ - '/favicon.ico', - '/login', - '/getveteranpw', - '/postfeedbackfile', - '/postfeedback', - '/fosuploader', - '/badtestsender' - ] - })) + app.set('views', ['./modules/api/views', './sharedViews']) + app.use( + auth({ + userDB: userDB, + jsonResponse: true, + exceptions: [ + '/favicon.ico', + '/login', + '/getveteranpw', + '/postfeedbackfile', + '/postfeedback', + '/fosuploader', + '/badtestsender', + ], + }) + ) publicdirs.forEach((pdir) => { logger.Log(`Using public dir: ${pdir}`) app.use(express.static(pdir)) }) - app.use(busboy({ - limits: { - fileSize: 50000 * 1024 * 1024 - } - })) + app.use( + busboy({ + limits: { + fileSize: 50000 * 1024 * 1024, + }, + }) + ) var data = actions.LoadJSON(dataFile) var version = '' var motd = '' var testUsers = [] - function LoadVersion () { + function LoadVersion() { version = utils.ReadFile(versionFile) } - function LoadMOTD () { + function LoadMOTD() { motd = utils.ReadFile(motdFile) } - function LoadTestUsers () { + function LoadTestUsers() { testUsers = utils.ReadJSON(testUsersFile) if (testUsers) { testUsers = testUsers.userIds } } - function Load () { + function Load() { utils.WatchFile(motdFile, (newData) => { logger.Log(`Motd changed: ${newData.replace(/\/n/g, '')}`) LoadMOTD() @@ -153,7 +158,7 @@ function GetApp () { if (!key || !val) { res.render('votethank', { results: 'error', - msg: 'no key or val query param!' + msg: 'no key or val query param!', }) return } @@ -162,17 +167,23 @@ function GetApp () { if (utils.FileExists(quickVotes)) { votes = utils.ReadJSON(quickVotes) } else { - logger.Log(`No such vote "${key}", and quickVotes.json is missing ( #${user.id}: ${key}-${val} )`, logger.GetColor('blue')) + logger.Log( + `No such vote "${key}", and quickVotes.json is missing ( #${user.id}: ${key}-${val} )`, + logger.GetColor('blue') + ) res.render('votethank', { - result: 'no such pool' + result: 'no such pool', }) return } if (!votes.voteNames.includes(key)) { - logger.Log(`No such vote "${key}" ( #${user.id}: ${key}-${val} )`, logger.GetColor('blue')) + logger.Log( + `No such vote "${key}" ( #${user.id}: ${key}-${val} )`, + logger.GetColor('blue') + ) res.render('votethank', { - result: 'no such pool' + result: 'no such pool', }) return } @@ -181,7 +192,7 @@ function GetApp () { let voteData = { votes: {}, - users: [] + users: [], } if (utils.FileExists(voteFile)) { @@ -198,16 +209,22 @@ function GetApp () { } voteData.users.push(user.id) - logger.Log(`Vote from #${user.id}: ${key}: ${val}`, logger.GetColor('blue')) + logger.Log( + `Vote from #${user.id}: ${key}: ${val}`, + logger.GetColor('blue') + ) res.render('votethank', { result: 'success', - msg: 'vote added' + msg: 'vote added', }) } else { - logger.Log(`#${user.id} already voted for: ${key}: ${val}`, logger.GetColor('blue')) + logger.Log( + `#${user.id} already voted for: ${key}: ${val}`, + logger.GetColor('blue') + ) res.render('votethank', { result: 'already voted', - msg: 'already voted' + msg: 'already voted', }) } @@ -226,11 +243,11 @@ function GetApp () { requestedPWS: user.pwRequestCount, maxPWCount: maxPWCount, // daysAfterUserGetsPWs: daysAfterUserGetsPWs, - addPWPerDay: addPWPerDay + addPWPerDay: addPWPerDay, }) }) - app.post('/getpw', function (req, res) { + app.post('/getpw', function(req, res) { logger.LogReq(req) const requestingUser = req.session.user @@ -238,63 +255,83 @@ function GetApp () { if (requestingUser.avaiblePWRequests <= 0) { res.json({ result: 'error', - msg: 'Too many passwords requested or cant request password yet, try later' + msg: + 'Too many passwords requested or cant request password yet, try later', }) - logger.Log(`User #${requestingUser.id} requested too much passwords`, logger.GetColor('cyan')) + logger.Log( + `User #${requestingUser.id} requested too much passwords`, + logger.GetColor('cyan') + ) return } - dbtools.Update(userDB, 'users', { - avaiblePWRequests: requestingUser.avaiblePWRequests - 1, - pwRequestCount: requestingUser.pwRequestCount + 1 - }, { - id: requestingUser.id - }) + dbtools.Update( + userDB, + 'users', + { + avaiblePWRequests: requestingUser.avaiblePWRequests - 1, + pwRequestCount: requestingUser.pwRequestCount + 1, + }, + { + id: requestingUser.id, + } + ) const pw = uuidv4() const insertRes = dbtools.Insert(userDB, 'users', { pw: pw, avaiblePWRequests: 0, - created: utils.GetDateString() + created: utils.GetDateString(), }) - logger.Log(`User #${requestingUser.id} created new user #${insertRes.lastInsertRowid}`, logger.GetColor('cyan')) + logger.Log( + `User #${requestingUser.id} created new user #${insertRes.lastInsertRowid}`, + logger.GetColor('cyan') + ) res.json({ result: 'success', pw: pw, requestedPWS: requestingUser.pwRequestCount + 1, - remaining: requestingUser.avaiblePWRequests - 1 + remaining: requestingUser.avaiblePWRequests - 1, }) }) - app.post('/getveteranpw', function (req, res) { + app.post('/getveteranpw', function(req, res) { logger.LogReq(req) const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const tries = dbtools.Select(userDB, 'veteranPWRequests', { - ip: ip + ip: ip, })[0] if (tries) { if (tries.count > maxVeteranPwGetCount) { res.json({ result: 'error', - msg: 'Too many tries from this IP' + msg: 'Too many tries from this IP', }) - logger.Log(`Too many veteran PW requests from ${ip}!`, logger.GetColor('cyan')) + logger.Log( + `Too many veteran PW requests from ${ip}!`, + logger.GetColor('cyan') + ) return } else { - dbtools.Update(userDB, 'veteranPWRequests', { - count: tries.count + 1, - lastDate: utils.GetDateString() - }, { - id: tries.id - }) + dbtools.Update( + userDB, + 'veteranPWRequests', + { + count: tries.count + 1, + lastDate: utils.GetDateString(), + }, + { + id: tries.id, + } + ) } } else { dbtools.Insert(userDB, 'veteranPWRequests', { ip: ip, - lastDate: utils.GetDateString() + lastDate: utils.GetDateString(), }) } @@ -303,41 +340,55 @@ function GetApp () { if (!oldUserID) { res.json({ result: 'error', - msg: 'No Client ID recieved' + msg: 'No Client ID recieved', }) logger.Log(`No client ID recieved`, logger.GetColor('cyan')) return } const user = dbtools.Select(userDB, 'users', { - oldCID: oldUserID + oldCID: oldUserID, })[0] if (user) { if (user.pwGotFromCID === 0) { - logger.Log(`Sent password to veteran user #${user.id}`, logger.GetColor('cyan')) - dbtools.Update(userDB, 'users', { - pwGotFromCID: 1 - }, { - id: user.id - }) + logger.Log( + `Sent password to veteran user #${user.id}`, + logger.GetColor('cyan') + ) + dbtools.Update( + userDB, + 'users', + { + pwGotFromCID: 1, + }, + { + id: user.id, + } + ) res.json({ result: 'success', - pw: user.pw + pw: user.pw, }) } else { - logger.Log(`Veteran user #${user.id} already requested password`, logger.GetColor('cyan')) + logger.Log( + `Veteran user #${user.id} already requested password`, + logger.GetColor('cyan') + ) res.json({ result: 'error', - msg: 'Password already requested' + msg: 'Password already requested', }) } } else { - logger.Log(`Invalid password request with CID: ${oldUserID}`, logger.GetColor('cyan')) + logger.Log( + `Invalid password request with CID: ${oldUserID}`, + logger.GetColor('cyan') + ) res.json({ result: 'error', - msg: 'No such Client ID' + msg: 'No such Client ID', }) } }) @@ -349,7 +400,7 @@ function GetApp () { const isScript = req.body.script const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const user = dbtools.Select(userDB, 'users', { - pw: pw + pw: pw, })[0] if (user) { @@ -358,59 +409,83 @@ function GetApp () { // 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 + isScript: isScript ? 1 : 0, }) if (existingSessions.length > 0) { - logger.Log(`Multiple ${isScript ? 'script' : 'website'} sessions ( ${existingSessions.length} ) for #${user.id}, deleting olds`, logger.GetColor('cyan')) + logger.Log( + `Multiple ${isScript ? 'script' : 'website'} sessions ( ${ + existingSessions.length + } ) for #${user.id}, deleting olds`, + logger.GetColor('cyan') + ) existingSessions.forEach((sess) => { dbtools.Delete(userDB, 'sessions', { id: sess.id, - isScript: isScript ? 1 : 0 + isScript: isScript ? 1 : 0, }) }) } - dbtools.Update(userDB, 'users', { - loginCount: user.loginCount + 1, - lastIP: ip, - lastLogin: utils.GetDateString() - }, { - id: user.id - }) + dbtools.Update( + userDB, + 'users', + { + loginCount: user.loginCount + 1, + lastIP: ip, + lastLogin: utils.GetDateString(), + }, + { + id: user.id, + } + ) dbtools.Insert(userDB, 'sessions', { id: sessionID, ip: ip, userID: user.id, isScript: isScript ? 1 : 0, - createDate: utils.GetDateString() + createDate: utils.GetDateString(), }) // https://www.npmjs.com/package/cookie // TODO: cookie age res.cookie('sessionID', sessionID, { domain: '.frylabs.net', // TODO: use url. url: "https://api.frylabs.net" - expires: new Date(new Date().getTime() + (10 * 365 * 24 * 60 * 60 * 1000)), + expires: new Date( + new Date().getTime() + 10 * 365 * 24 * 60 * 60 * 1000 + ), sameSite: 'none', - secure: true + secure: true, }) res.cookie('sessionID', sessionID, { - expires: new Date(new Date().getTime() + (10 * 365 * 24 * 60 * 60 * 1000)), + expires: new Date( + new Date().getTime() + 10 * 365 * 24 * 60 * 60 * 1000 + ), sameSite: 'none', - secure: true + secure: true, }) res.json({ result: 'success', - msg: 'you are now logged in' + msg: 'you are now logged in', }) - logger.Log(`Successfull login to ${isScript ? 'script' : 'website'} with user ID: #${user.id}`, logger.GetColor('cyan')) + logger.Log( + `Successfull login to ${ + isScript ? 'script' : 'website' + } with user ID: #${user.id}`, + logger.GetColor('cyan') + ) } else { - logger.Log(`Login attempt with invalid pw: ${pw} to ${isScript ? 'script' : 'website'}${cid ? ', CID:' + cid : ''}`, logger.GetColor('cyan')) + logger.Log( + `Login attempt with invalid pw: ${pw} to ${ + isScript ? 'script' : 'website' + }${cid ? ', CID:' + cid : ''}`, + logger.GetColor('cyan') + ) res.json({ result: 'error', - msg: 'Invalid password' + msg: 'Invalid password', }) } }) @@ -421,22 +496,22 @@ function GetApp () { // removing session from db dbtools.Delete(userDB, 'sessions', { - id: sessionID + id: sessionID, }) // TODO: remove old sessions every once in a while res.clearCookie('sessionID').json({ - result: 'success' + result: 'success', }) }) // -------------------------------------------------------------- - app.get('/', function (req, res) { + app.get('/', function(req, res) { logger.LogReq(req) res.redirect('https://www.youtube.com/watch?v=ieqGJgqiXFk') }) - app.post('/postfeedbackfile', function (req, res) { + app.post('/postfeedbackfile', function(req, res) { UploadFile(req, res, uloadFiles, (fn) => { res.json({ success: true }) }) @@ -445,43 +520,66 @@ function GetApp () { logger.Log('New feedback file', logger.GetColor('bluebg'), true) }) - app.post('/postfeedback', function (req, res) { + app.post('/postfeedback', function(req, res) { logger.LogReq(req) if (req.body.fromLogin) { - logger.Log('New feedback message from Login page', logger.GetColor('bluebg'), true) + logger.Log( + 'New feedback message from Login page', + logger.GetColor('bluebg'), + true + ) } else { - logger.Log('New feedback message from feedback page', logger.GetColor('bluebg'), true) + logger.Log( + 'New feedback message from feedback page', + logger.GetColor('bluebg'), + true + ) } const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const user = req.session.user - utils.AppendToFile(utils.GetDateString() + ':\n' + JSON.stringify({ - ...req.body, - userID: user ? user.id : 'no user', - ip: ip - }), msgFile) + utils.AppendToFile( + utils.GetDateString() + + ':\n' + + JSON.stringify({ + ...req.body, + userID: user ? user.id : 'no user', + ip: ip, + }), + msgFile + ) res.json({ success: true }) }) - function UploadFile (req, res, path, next) { + function UploadFile(req, res, path, next) { try { var fstream req.pipe(req.busboy) - req.busboy.on('file', function (fieldname, file, filename) { + req.busboy.on('file', function(fieldname, file, filename) { logger.Log('Uploading: ' + filename, logger.GetColor('blue')) utils.CreatePath(path, true) let d = new Date() - let fn = d.getHours() + '' + d.getMinutes() + '' + d.getSeconds() + '_' + filename + let fn = + d.getHours() + + '' + + d.getMinutes() + + '' + + d.getSeconds() + + '_' + + filename fstream = fs.createWriteStream(path + '/' + fn) file.pipe(fstream) - fstream.on('close', function () { - logger.Log('Upload Finished of ' + path + '/' + fn, logger.GetColor('blue')) + fstream.on('close', function() { + logger.Log( + 'Upload Finished of ' + path + '/' + fn, + logger.GetColor('blue') + ) next(fn) }) - fstream.on('error', function (err) { + fstream.on('error', function(err) { console.log(err) res.end('something bad happened :s') }) @@ -492,20 +590,20 @@ function GetApp () { } } - app.route('/fosuploader').post(function (req, res, next) { + app.route('/fosuploader').post(function(req, res, next) { UploadFile(req, res, uloadFiles, (fn) => { res.redirect('/f/' + fn) }) }) - app.route('/badtestsender').post(function (req, res, next) { + app.route('/badtestsender').post(function(req, res, next) { UploadFile(req, res, recivedFiles, (fn) => { res.redirect('back') }) logger.LogReq(req) }) - app.get('/allqr.txt', function (req, res) { + app.get('/allqr.txt', function(req, res) { res.set('Content-Type', 'text/plain') res.send(data.toString()) res.end() @@ -516,25 +614,25 @@ function GetApp () { // API app.post('/uploaddata', (req, res) => { - // body: JSON.stringify({ - // newData: data, - // count: getCount(data), - // initialCount: initialCount, - // password: password, - // editedQuestions: editedQuestions - // }) + // body: JSON.stringify({ + // newData: data, + // count: getCount(data), + // initialCount: initialCount, + // password: password, + // editedQuestions: editedQuestions + // }) const { count, initialCount, editedQuestions, password, newData } = req.body const respStatuses = { invalidPass: 'invalidPass', ok: 'ok', - error: 'error' + error: 'error', } logger.LogReq(req) try { - // finding user + // finding user const pwds = JSON.parse(utils.ReadFile(passwordFile)) let user = Object.keys(pwds).find((key) => { const u = pwds[key] @@ -547,19 +645,53 @@ function GetApp () { logger.Log(`PWD: ${password}`, logger.GetColor('bluebg')) // returning if user password is not ok if (!user) { - logger.Log(`Data upload: invalid password ${password}`, logger.GetColor('red')) - utils.AppendToFile(utils.GetDateString() + '\n' + password + '(FAILED PASSWORD)\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog) + logger.Log( + `Data upload: invalid password ${password}`, + logger.GetColor('red') + ) + utils.AppendToFile( + utils.GetDateString() + + '\n' + + password + + '(FAILED PASSWORD)\n' + + JSON.stringify(editedQuestions) + + '\n\n', + dataEditsLog + ) res.json({ status: respStatuses.invalidPass }) return } - logger.Log(`Password accepted for ${user.name}`, logger.GetColor('bluebg')) - logger.Log(`Old Subjects/Questions: ${initialCount.subjectCount} / ${initialCount.questionCount} | New: ${count.subjectCount} / ${count.questionCount} | Edited question count: ${Object.keys(editedQuestions).length}`, logger.GetColor('bluebg')) + logger.Log( + `Password accepted for ${user.name}`, + logger.GetColor('bluebg') + ) + logger.Log( + `Old Subjects/Questions: ${initialCount.subjectCount} / ${ + initialCount.questionCount + } | New: ${count.subjectCount} / ${ + count.questionCount + } | Edited question count: ${Object.keys(editedQuestions).length}`, + logger.GetColor('bluebg') + ) // saving detailed editedCount - utils.AppendToFile(utils.GetDateString() + '\n' + JSON.stringify(user) + '\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog) + utils.AppendToFile( + utils.GetDateString() + + '\n' + + JSON.stringify(user) + + '\n' + + JSON.stringify(editedQuestions) + + '\n\n', + dataEditsLog + ) // making backup - utils.CopyFile('./' + dataFile, `./qminingPublic/backs/data_before_${user.name}_${utils.GetDateString().replace(/ /g, '_')}`) // TODO: rewrite to dinamyc public!!! + utils.CopyFile( + './' + dataFile, + `./qminingPublic/backs/data_before_${ + user.name + }_${utils.GetDateString().replace(/ /g, '_')}` + ) // TODO: rewrite to dinamyc public!!! logger.Log('Backup made') // writing data utils.WriteFile(JSON.stringify(newData), dataFile) @@ -571,7 +703,7 @@ function GetApp () { res.json({ status: respStatuses.ok, - user: user.name + user: user.name, }) logger.Log('Data updating done!', logger.GetColor('bluebg')) } catch (e) { @@ -581,7 +713,7 @@ function GetApp () { } }) - app.post('/isAdding', function (req, res) { + app.post('/isAdding', function(req, res) { logger.LogReq(req) const user = req.session.user @@ -590,27 +722,29 @@ function GetApp () { // automatically saves to dataFile every n write // FIXME: req.body.datatoadd is for backwards compatibility, remove this sometime in the future - actions.ProcessIncomingRequest( - req.body.datatoadd || req.body, - data, - { motd, version }, - dryRun - ).then((r) => { - res.json({ - success: r !== -1, - newQuestions: r + actions + .ProcessIncomingRequest( + req.body.datatoadd || req.body, + data, + { motd, version }, + dryRun + ) + .then((r) => { + res.json({ + success: r !== -1, + newQuestions: r, + }) }) - }) }) - app.get('/ask', function (req, res) { + app.get('/ask', function(req, res) { if (Object.keys(req.query).length === 0) { logger.DebugLog(`No query params`, 'ask', 1) res.json({ message: `ask something! ?q=[question]&subj=[subject]&data=[question data]. 'subj' is optimal for faster result`, result: [], recievedData: JSON.stringify(req.query), - success: false + success: false, }) } else { if (req.query.q && req.query.data) { @@ -620,13 +754,16 @@ function GetApp () { try { recData = JSON.parse(req.query.data) } catch (e) { - logger.Log(`Unable to parse recieved question data! '${req.query.data}'`, logger.GetColor('redbg')) + logger.Log( + `Unable to parse recieved question data! '${req.query.data}'`, + logger.GetColor('redbg') + ) } let r = data.Search(question, subj, recData) res.json({ result: r, - success: true + success: true, }) logger.DebugLog(`Question result length: ${r.length}`, 'ask', 1) logger.DebugLog(r, 'ask', 2) @@ -636,35 +773,35 @@ function GetApp () { message: `Invalid question :(`, result: [], recievedData: JSON.stringify(req.query), - success: false + success: false, }) } } }) - function getSimplreRes () { + function getSimplreRes() { return { subjects: data.length, questions: data.Subjects.reduce((acc, subj) => { return acc + subj.length - }, 0) + }, 0), } } - function getDetailedRes () { + function getDetailedRes() { return data.Subjects.map((subj) => { return { name: subj.Name, - count: subj.length + count: subj.length, } }) } - app.get('/datacount', function (req, res) { + app.get('/datacount', function(req, res) { logger.LogReq(req) if (req.query.detailed === 'all') { res.json({ detailed: getDetailedRes(), - simple: getSimplreRes() + simple: getSimplreRes(), }) } else if (req.query.detailed) { res.json(getDetailedRes()) @@ -673,12 +810,12 @@ function GetApp () { } }) - app.get('/infos', function (req, res) { + app.get('/infos', function(req, res) { const user = req.session.user let result = { result: 'success', - uid: user.id + uid: user.id, } if (req.query.subjinfo) { result.subjinfo = getSimplreRes() @@ -694,30 +831,38 @@ function GetApp () { // ------------------------------------------------------------------------------------------- - app.get('*', function (req, res) { + app.get('*', function(req, res) { res.status(404).render('404') }) - app.post('*', function (req, res) { + app.post('*', function(req, res) { res.status(404).render('404') }) - function ExportDailyDataCount () { + function ExportDailyDataCount() { logger.Log('Saving daily data count ...') - utils.AppendToFile(JSON.stringify({ - date: utils.GetDateString(), - subjectCount: data.Subjects.length, - questionCOunt: data.Subjects.reduce((acc, subj) => { - return acc + subj.Questions.length - }, 0), - userCount: dbtools.TableInfo(userDB, 'users').dataCount - }), dailyDataCountFile) + utils.AppendToFile( + JSON.stringify({ + date: utils.GetDateString(), + subjectCount: data.Subjects.length, + questionCOunt: data.Subjects.reduce((acc, subj) => { + return acc + subj.Questions.length + }, 0), + userCount: dbtools.TableInfo(userDB, 'users').dataCount, + }), + dailyDataCountFile + ) } - function BackupDB () { + function BackupDB() { logger.Log('Backing up auth DB ...') utils.CreatePath(usersDbBackupPath, true) - userDB.backup(`${usersDbBackupPath}/users.${utils.GetDateString().replace(/ /g, '_')}.db`) + userDB + .backup( + `${usersDbBackupPath}/users.${utils + .GetDateString() + .replace(/ /g, '_')}.db` + ) .then(() => { logger.Log('Auth DB backup complete!') }) @@ -727,7 +872,7 @@ function GetApp () { }) } - function IncrementAvaiblePWs () { + function IncrementAvaiblePWs() { // FIXME: check this if this is legit and works logger.Log('Incrementing avaible PW-s ...') const users = dbtools.SelectAll(userDB, 'users') @@ -749,17 +894,27 @@ function GetApp () { // } if (dayDiff % addPWPerDay === 0) { - logger.Log(`Incrementing avaible PW-s for user #${u.id}: ${u.avaiblePWRequests} -> ${u.avaiblePWRequests + 1}`, logger.GetColor('cyan')) - dbtools.Update(userDB, 'users', { - avaiblePWRequests: u.avaiblePWRequests + 1 - }, { - id: u.id - }) + logger.Log( + `Incrementing avaible PW-s for user #${u.id}: ${ + u.avaiblePWRequests + } -> ${u.avaiblePWRequests + 1}`, + logger.GetColor('cyan') + ) + dbtools.Update( + userDB, + 'users', + { + avaiblePWRequests: u.avaiblePWRequests + 1, + }, + { + id: u.id, + } + ) } }) } - function DailyAction () { + function DailyAction() { ExportDailyDataCount() BackupDB() IncrementAvaiblePWs() @@ -767,7 +922,7 @@ function GetApp () { return { dailyAction: DailyAction, - app: app + app: app, } } diff --git a/src/modules/api/apiDBStruct.json b/src/modules/api/apiDBStruct.json index 381dfd7..ea321ad 100644 --- a/src/modules/api/apiDBStruct.json +++ b/src/modules/api/apiDBStruct.json @@ -52,13 +52,9 @@ "sessions": { "foreignKey": [ { - "keysFrom": [ - "userID" - ], + "keysFrom": ["userID"], "table": "users", - "keysTo": [ - "id" - ] + "keysTo": ["id"] } ], "tableStruct": { @@ -91,13 +87,9 @@ "accesses": { "foreignKey": [ { - "keysFrom": [ - "userID" - ], + "keysFrom": ["userID"], "table": "users", - "keysTo": [ - "id" - ] + "keysTo": ["id"] } ], "tableStruct": { diff --git a/src/modules/main/main.js b/src/modules/main/main.js index 3c4d070..3e53fc5 100644 --- a/src/modules/main/main.js +++ b/src/modules/main/main.js @@ -31,48 +31,51 @@ const logger = require('../../utils/logger.js') let publicdirs = [] let url = '' // http(s)//asd.basd -function GetApp () { +function GetApp() { app.set('view engine', 'ejs') - app.set('views', [ - './modules/main/views', - './sharedViews' - ]) + app.set('views', ['./modules/main/views', './sharedViews']) publicdirs.forEach((pdir) => { logger.Log(`Using public dir: ${pdir}`) app.use(express.static(pdir)) }) - app.use(busboy({ - limits: { - fileSize: 10000 * 1024 * 1024 - } - })) + app.use( + busboy({ + limits: { + fileSize: 10000 * 1024 * 1024, + }, + }) + ) app.use(bodyParser.json()) - app.use(bodyParser.urlencoded({ - limit: '5mb', - extended: true - })) - app.use(bodyParser.json({ - limit: '5mb' - })) + app.use( + bodyParser.urlencoded({ + limit: '5mb', + extended: true, + }) + ) + app.use( + bodyParser.json({ + limit: '5mb', + }) + ) // -------------------------------------------------------------- - app.get('/', function (req, res) { + app.get('/', function(req, res) { res.render('main', { - siteurl: url + siteurl: url, }) }) - app.get('*', function (req, res) { + app.get('*', function(req, res) { res.status(404).render('404') }) - app.post('*', function (req, res) { + app.post('*', function(req, res) { res.status(404).render('404') }) return { - app: app + app: app, } } diff --git a/src/modules/qmining/qmining.js b/src/modules/qmining/qmining.js index cfc1173..91c670f 100644 --- a/src/modules/qmining/qmining.js +++ b/src/modules/qmining/qmining.js @@ -41,117 +41,125 @@ try { logger.Log('Couldnt read donate URL file!', logger.GetColor('red')) } -function GetApp () { - app.use(bodyParser.urlencoded({ - limit: '5mb', - extended: true - })) - app.use(bodyParser.json({ - limit: '5mb' - })) +function GetApp() { + app.use( + bodyParser.urlencoded({ + limit: '5mb', + extended: true, + }) + ) + app.use( + bodyParser.json({ + limit: '5mb', + }) + ) app.set('view engine', 'ejs') - app.set('views', [ - './modules/qmining/views', - './sharedViews' - ]) - app.use(auth({ - userDB: userDB, - jsonResponse: false, - exceptions: [ - '/thanks', - '/thanks.html', - '/img/thanks.gif', - '/install', - '/favicon.ico', - '/getVeteranPw', - '/moodle-test-userscript/stable.user.js', - '/donate', - '/irc' - ] - })) + app.set('views', ['./modules/qmining/views', './sharedViews']) + app.use( + auth({ + userDB: userDB, + jsonResponse: false, + exceptions: [ + '/thanks', + '/thanks.html', + '/img/thanks.gif', + '/install', + '/favicon.ico', + '/getVeteranPw', + '/moodle-test-userscript/stable.user.js', + '/donate', + '/irc', + ], + }) + ) publicdirs.forEach((pdir) => { logger.Log(`Using public dir: ${pdir}`) app.use(express.static(pdir)) }) app.use(express.static(nextdir)) - app.use(busboy({ - limits: { - fileSize: 10000 * 1024 * 1024 - } - })) + app.use( + busboy({ + limits: { + fileSize: 10000 * 1024 * 1024, + }, + }) + ) // -------------------------------------------------------------- // REDIRECTS // -------------------------------------------------------------- // to be backwards compatible - app.get('/ask', function (req, res) { + app.get('/ask', function(req, 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}`) + res.redirect( + `http://api.frylabs.net/ask?q=${req.query.q}&subj=${req.query.subj}&data=${req.query.data}` + ) }) const simpleRedirects = [ { from: '/dataeditor', - to: 'https://dataeditor.frylabs.net' + to: 'https://dataeditor.frylabs.net', }, { from: '/install', - to: 'https://qmining.frylabs.net/moodle-test-userscript/stable.user.js' + to: 'https://qmining.frylabs.net/moodle-test-userscript/stable.user.js', }, { from: '/servergit', - to: 'https://gitlab.com/MrFry/mrfrys-node-server' + to: 'https://gitlab.com/MrFry/mrfrys-node-server', }, { from: '/scriptgit', - to: 'https://gitlab.com/MrFry/moodle-test-userscript' + to: 'https://gitlab.com/MrFry/moodle-test-userscript', }, { from: '/qminingSite', - to: 'https://gitlab.com/MrFry/qmining-page' + to: 'https://gitlab.com/MrFry/qmining-page', }, { from: '/classesgit', - to: 'https://gitlab.com/MrFry/question-classes' + to: 'https://gitlab.com/MrFry/question-classes', }, { from: '/menuClick', - to: '/' + to: '/', }, { from: '/lred', - to: '/allQuestions.html' + to: '/allQuestions.html', }, { from: '/donate', - to: donateURL + to: donateURL, }, - { // to be backwards compatible + { + // to be backwards compatible from: '/legacy', - to: '/allQuestions.html' + to: '/allQuestions.html', }, { from: '/allqr', - to: 'http://api.frylabs.net/allqr.txt' + to: 'http://api.frylabs.net/allqr.txt', }, { from: '/allqr.txt', - to: 'http://api.frylabs.net/allqr.txt' + to: 'http://api.frylabs.net/allqr.txt', }, { from: '/infos', to: 'http://api.frylabs.net/infos?version=true&motd=true&subjinfo=true', - nolog: true + nolog: true, }, { from: '/irc', - to: 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining' - } + to: 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining', + }, ] simpleRedirects.forEach((redirect) => { - app.get(redirect.from, function (req, res) { + app.get(redirect.from, function(req, res) { if (!redirect.nolog) { logger.LogReq(req) } @@ -162,7 +170,7 @@ function GetApp () { // -------------------------------------------------------------- - function AddHtmlRoutes (files) { + function AddHtmlRoutes(files) { const routes = files.reduce((acc, f) => { if (f.includes('html')) { acc.push(f.split('.')[0]) @@ -173,7 +181,7 @@ function GetApp () { routes.forEach((route) => { logger.DebugLog(`Added route /${route}`, 'Qmining routes', 1) - app.get(`/${route}`, function (req, res) { + app.get(`/${route}`, function(req, res) { logger.LogReq(req) res.redirect(`${route}.html`) }) @@ -183,29 +191,29 @@ function GetApp () { // -------------------------------------------------------------- - app.get('/', function (req, res) { + app.get('/', function(req, res) { res.end('hai') logger.LogReq(req) }) - app.get('/getVeteranPw', function (req, res) { + app.get('/getVeteranPw', function(req, res) { res.render('veteranPw', { cid: req.query.cid || '', - devel: process.env.NS_DEVEL + devel: process.env.NS_DEVEL, }) logger.LogReq(req) }) - app.get('*', function (req, res) { + app.get('*', function(req, res) { res.status(404).render('404') }) - app.post('*', function (req, res) { + app.post('*', function(req, res) { res.status(404).render('404') }) return { - app: app + app: app, } } diff --git a/src/modules/sio/sio.js b/src/modules/sio/sio.js index f64b3f8..378a838 100644 --- a/src/modules/sio/sio.js +++ b/src/modules/sio/sio.js @@ -32,7 +32,7 @@ const utils = require('../../utils/utils.js') // stuff gotten from server.js let publicdirs = [] -function GetApp () { +function GetApp() { const p = publicdirs[0] if (!p) { throw new Error(`No public dir! ( SIO )`) @@ -42,73 +42,86 @@ function GetApp () { const uloadFiles = p + 'f' app.set('view engine', 'ejs') - app.set('views', [ - './modules/sio/views', - './sharedViews' - ]) + app.set('views', ['./modules/sio/views', './sharedViews']) publicdirs.forEach((pdir) => { logger.Log(`Using public dir: ${pdir}`) app.use(express.static(pdir)) }) - app.use(busboy({ - limits: { - fileSize: 10000 * 1024 * 1024 - } - })) + app.use( + busboy({ + limits: { + fileSize: 10000 * 1024 * 1024, + }, + }) + ) app.use(bodyParser.json()) - app.use(bodyParser.urlencoded({ - limit: '5mb', - extended: true - })) - app.use(bodyParser.json({ - limit: '5mb' - })) + app.use( + bodyParser.urlencoded({ + limit: '5mb', + extended: true, + }) + ) + app.use( + bodyParser.json({ + limit: '5mb', + }) + ) // -------------------------------------------------------------- - app.get('/', function (req, res) { + app.get('/', function(req, res) { res.render('uload') res.end() }) - function UploadFile (req, res, path, next) { + function UploadFile(req, res, path, next) { var fstream req.pipe(req.busboy) - req.busboy.on('file', function (fieldname, file, filename) { + req.busboy.on('file', function(fieldname, file, filename) { logger.Log('Uploading: ' + filename, logger.GetColor('blue')) utils.CreatePath(path, true) let d = new Date() - let fn = d.getHours() + '' + d.getMinutes() + '' + d.getSeconds() + '_' + filename + let fn = + d.getHours() + + '' + + d.getMinutes() + + '' + + d.getSeconds() + + '_' + + filename fstream = fs.createWriteStream(path + '/' + fn) file.pipe(fstream) - fstream.on('close', function () { - logger.Log('Upload Finished of ' + path + '/' + fn, logger.GetColor('blue')) + fstream.on('close', function() { + logger.Log( + 'Upload Finished of ' + path + '/' + fn, + logger.GetColor('blue') + ) next(fn) }) - fstream.on('error', function (err) { + fstream.on('error', function(err) { console.log(err) res.end('something bad happened :s') }) }) } - app.route('/fosuploader').post(function (req, res, next) { + app.route('/fosuploader').post(function(req, res, next) { UploadFile(req, res, uloadFiles, (fn) => { res.redirect('/f/' + fn) }) }) - app.get('*', function (req, res) { + app.get('*', function(req, res) { res.status(404).render('404') }) - app.post('*', function (req, res) { + app.post('*', function(req, res) { res.status(404).render('404') }) return { - app: app + app: app, } } diff --git a/src/modules/stuff/stuff.js b/src/modules/stuff/stuff.js index 96d75ce..b385236 100644 --- a/src/modules/stuff/stuff.js +++ b/src/modules/stuff/stuff.js @@ -32,7 +32,7 @@ const logger = require('../../utils/logger.js') let publicdirs = [] let url = '' -function GetApp () { +function GetApp() { const p = publicdirs[0] if (!p) { throw new Error(`No public dir! ( Stuff )`) @@ -42,33 +42,36 @@ function GetApp () { const listedFiles = './' + p + 'files' app.set('view engine', 'ejs') - app.set('views', [ - './modules/stuff/views', - './sharedViews' - ]) + app.set('views', ['./modules/stuff/views', './sharedViews']) publicdirs.forEach((pdir) => { logger.Log(`Using public dir: ${pdir}`) app.use(express.static(pdir)) }) - app.use(busboy({ - limits: { - fileSize: 10000 * 1024 * 1024 - } - })) + app.use( + busboy({ + limits: { + fileSize: 10000 * 1024 * 1024, + }, + }) + ) app.use(bodyParser.json()) - app.use(bodyParser.urlencoded({ - limit: '5mb', - extended: true - })) - app.use(bodyParser.json({ - limit: '5mb' - })) + app.use( + bodyParser.urlencoded({ + limit: '5mb', + extended: true, + }) + ) + app.use( + bodyParser.json({ + limit: '5mb', + }) + ) // -------------------------------------------------------------- // app, '/*.mp4', 'video/mp4', 'stuff/video' - function appGetFileType (app, wildcard, contentType, pageToRender) { - app.get(wildcard, function (req, res) { + function appGetFileType(app, wildcard, contentType, pageToRender) { + app.get(wildcard, function(req, res) { let p = decodeURI(req.url) let fp = p if (p.includes('?')) { @@ -80,7 +83,7 @@ function GetApp () { if (!fs.existsSync(fpath)) { res.render('nofile', { missingFile: fpath, - url + url, }) return } @@ -91,23 +94,21 @@ function GetApp () { if (range) { const parts = range.replace(/bytes=/, '').split('-') const start = parseInt(parts[0], 10) - const end = parts[1] - ? parseInt(parts[1], 10) - : fileSize - 1 - const chunksize = (end - start) + 1 + const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1 + const chunksize = end - start + 1 const file = fs.createReadStream(fpath, { start, end }) const head = { 'Content-Range': `bytes ${start}-${end}/${fileSize}`, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, - 'Content-Type': contentType + 'Content-Type': contentType, } res.writeHead(206, head) file.pipe(res) } else { const head = { 'Content-Length': fileSize, - 'Content-Type': contentType + 'Content-Type': contentType, } res.writeHead(200, head) fs.createReadStream(fpath).pipe(res) @@ -121,7 +122,7 @@ function GetApp () { fname, url, contentType, - albumArt: GetAlbumArt(p) + albumArt: GetAlbumArt(p), }) } }) @@ -132,10 +133,10 @@ function GetApp () { ['/*.mkv', 'audio/x-matroska', 'video'], ['/*.mp3', 'audio/mpeg', 'audio'], ['/*.pdf', 'application/pdf'], - ['/*.zip', 'application/zip'] + ['/*.zip', 'application/zip'], ] - function GetAlbumArt (path) { + function GetAlbumArt(path) { let tmp = path.split('.') tmp.pop() tmp = tmp.join('.').split('/') @@ -148,16 +149,23 @@ function GetApp () { appGetFileType(app, t[0], t[1], t[2]) }) - app.get('/*', function (req, res) { + app.get('/*', function(req, res) { let parsedUrl = decodeURI(req.url) - let curr = listedFiles + '/' + parsedUrl.substring('/'.length, parsedUrl.length).split('?')[0] + let curr = + listedFiles + + '/' + + parsedUrl.substring('/'.length, parsedUrl.length).split('?')[0] let relPath = curr.substring(listedFiles.length, curr.length) - if (relPath[relPath.length - 1] !== '/') { relPath += '/' } + if (relPath[relPath.length - 1] !== '/') { + relPath += '/' + } let t = relPath.split('/') let prevDir = '' - for (let i = 0; i < t.length - 2; i++) { prevDir += t[i] + '/' } + for (let i = 0; i < t.length - 2; i++) { + prevDir += t[i] + '/' + } // curr = curr.replace(/\//g, "/"); // relPath = relPath.replace(/\//g, "/"); @@ -167,14 +175,18 @@ function GetApp () { try { const stat = fs.lstatSync(curr) if (stat.isDirectory() || stat.isSymbolicLink()) { - if (curr[curr.length - 1] !== '/') { curr += '/' } + if (curr[curr.length - 1] !== '/') { + curr += '/' + } let f = [] let files = fs.readdirSync(curr) - files.sort(function (a, b) { - return fs.statSync(curr + b).mtime.getTime() - - fs.statSync(curr + a).mtime.getTime() + files.sort(function(a, b) { + return ( + fs.statSync(curr + b).mtime.getTime() - + fs.statSync(curr + a).mtime.getTime() + ) }) files.forEach((item) => { @@ -186,7 +198,9 @@ function GetApp () { res.size = Math.round(fileSizeInBytes / 1000000) res.path = relPath - if (res.path[res.path.length - 1] !== '/') { res.path += '/' } + if (res.path[res.path.length - 1] !== '/') { + res.path += '/' + } res.path += item res.mtime = stat['mtime'].toLocaleString() @@ -200,7 +214,7 @@ function GetApp () { folders: f, dirname: relPath, prevDir, - url + url, }) } else { let fileStream = fs.createReadStream(curr) @@ -209,23 +223,23 @@ function GetApp () { } catch (e) { res.render('nofile', { missingFile: curr, - url + url, }) } }) // ----------------------------------------------------------------------------------------------- - app.get('*', function (req, res) { + app.get('*', function(req, res) { res.status(404).render('404') }) - app.post('*', function (req, res) { + app.post('*', function(req, res) { res.status(404).render('404') }) return { - app: app + app: app, } } diff --git a/src/utils/actions.js b/src/utils/actions.js index 2201180..82ec66d 100755 --- a/src/utils/actions.js +++ b/src/utils/actions.js @@ -19,7 +19,7 @@ Question Server module.exports = { ProcessIncomingRequest: ProcessIncomingRequest, - LoadJSON: LoadJSON + LoadJSON: LoadJSON, } const dataFile = './qminingPublic/data.json' @@ -38,7 +38,7 @@ const minMatchAmmountToAdd = 90 // FIXME: test this value const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate var currWrites = 0 -function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) { +function ProcessIncomingRequest(recievedData, qdb, infos, dryRun) { return new Promise((resolve, reject) => { logger.DebugLog('Processing incoming request', 'actions', 1) if (recievedData === undefined) { @@ -48,13 +48,15 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) { try { let towrite = logger.GetDateString() + '\n' - towrite += '------------------------------------------------------------------------------\n' + towrite += + '------------------------------------------------------------------------------\n' if (typeof recievedData === 'object') { towrite += JSON.stringify(recievedData) } else { towrite += recievedData } - towrite += '\n------------------------------------------------------------------------------\n' + towrite += + '\n------------------------------------------------------------------------------\n' utils.AppendToFile(towrite, recDataFile) logger.DebugLog('recDataFile written', 'actions', 1) } catch (e) { @@ -62,7 +64,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) { } try { - // recievedData: { version: "", id: "", subj: "" quiz: {} } + // recievedData: { version: "", id: "", subj: "" quiz: {} } let d = recievedData // FIXME: if is for backwards compatibility, remove this sometime in the future if (typeof d !== 'object') { @@ -78,7 +80,11 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) { logger.DebugLog('Question:', 'actions', 2) logger.DebugLog(question, 'actions', 2) let q = new classes.Question(question.Q, question.A, question.data) - logger.DebugLog('Searching for question in subj ' + d.subj, 'actions', 3) + logger.DebugLog( + 'Searching for question in subj ' + d.subj, + 'actions', + 3 + ) logger.DebugLog(q, 'actions', 3) let sames = qdb.Search(q, d.subj) @@ -86,9 +92,11 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) { logger.DebugLog('Length: ' + sames.length, 'actions', 2) logger.DebugLog(sames, 'actions', 3) // if it didnt find any question, or every found questions match is lower thatn 80 - let isNew = sames.length === 0 || sames.every(searchResItem => { - return searchResItem.match < minMatchAmmountToAdd - }) + let isNew = + sames.length === 0 || + sames.every((searchResItem) => { + return searchResItem.match < minMatchAmmountToAdd + }) logger.DebugLog('isNew: ' + isNew, 'actions', 2) if (isNew) { allQuestions.push(q) @@ -102,7 +110,11 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) { msg += `New questions: ${allQuestions.length} ( All: ${allQLength} )` allQuestions.forEach((q) => { const sName = classes.SUtils.GetSubjNameWithoutYear(d.subj) - logger.DebugLog('Adding question with subjName: ' + sName + ' :', 'actions', 3) + logger.DebugLog( + 'Adding question with subjName: ' + sName + ' :', + 'actions', + 3 + ) logger.DebugLog(q, 'actions', 3) qdb.AddQuestion(sName, q) }) @@ -134,7 +146,9 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) { idStats.LogId(d.id, d.subj) } logger.Log(subjRow) - if (d.version !== undefined) { msg += '. Version: ' + d.version } + if (d.version !== undefined) { + msg += '. Version: ' + d.version + } logger.Log('\t' + msg, color) logger.DebugLog('New Questions:', 'actions', 2) @@ -151,7 +165,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) { } // loading stuff -function LoadJSON (dataFile) { +function LoadJSON(dataFile) { try { var d = JSON.parse(utils.ReadFile(dataFile)) var r = new classes.QuestionDB() @@ -166,7 +180,7 @@ function LoadJSON (dataFile) { } rt.push({ name: d.Subjects[i].Name, - count: j + count: j, }) r.AddSubject(s) } diff --git a/src/utils/changedataversion.js b/src/utils/changedataversion.js index 94582e2..333a917 100755 --- a/src/utils/changedataversion.js +++ b/src/utils/changedataversion.js @@ -26,6 +26,6 @@ console.log(parsed.version) utils.WriteFile(JSON.stringify(parsed), dataFile) utils.WriteFile(parsed.version, versionFile) -function GetParams () { +function GetParams() { return process.argv.splice(2) } diff --git a/src/utils/classes.js b/src/utils/classes.js index d173010..8dac37d 100755 --- a/src/utils/classes.js +++ b/src/utils/classes.js @@ -1,10 +1,10 @@ var debugLogger = null -function initLogger (logger) { +function initLogger(logger) { debugLogger = logger } -function debugLog (msg, name, lvl) { +function debugLog(msg, name, lvl) { if (debugLogger) { debugLogger(msg, name, lvl) } @@ -18,26 +18,21 @@ const commonUselessAnswerParts = [ 'A helyes válasz: ', 'A helyes válasz:', 'The correct answer is:', - '\'' + "'", ] -const commonUselessStringParts = [ - ',', - '\\.', - ':', - '!', - '\\+', - '\\s*\\.' -] -const specialChars = [ '&', '\\+' ] +const commonUselessStringParts = [',', '\\.', ':', '!', '\\+', '\\s*\\.'] +const specialChars = ['&', '\\+'] const lengthDiffMultiplier = 10 /* Percent minus for length difference */ const minMatchAmmount = 60 /* Minimum ammount to consider that two questions match during answering */ const assert = (val) => { - if (!val) { throw new Error('Assertion failed') } + if (!val) { + throw new Error('Assertion failed') + } } class StringUtils { - GetSubjNameWithoutYear (subjName) { + GetSubjNameWithoutYear(subjName) { let t = subjName.split(' - ') if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) { return t[1] || subjName @@ -46,7 +41,7 @@ class StringUtils { } } - RemoveStuff (value, removableStrings, toReplace) { + RemoveStuff(value, removableStrings, toReplace) { removableStrings.forEach((x) => { var regex = new RegExp(x, 'g') value = value.replace(regex, toReplace || '') @@ -54,14 +49,14 @@ class StringUtils { return value } - SimplifyQuery (q) { + SimplifyQuery(q) { assert(q) var result = q.replace(/\n/g, ' ').replace(/\s/g, ' ') return this.RemoveUnnecesarySpaces(result) } - ShortenString (toShorten, ammount) { + ShortenString(toShorten, ammount) { assert(toShorten) var result = '' @@ -73,7 +68,7 @@ class StringUtils { return result } - ReplaceCharsWithSpace (val, char) { + ReplaceCharsWithSpace(val, char) { assert(val) assert(char) @@ -86,7 +81,7 @@ class StringUtils { } // removes whitespace from begining and and, and replaces multiple spaces with one space - RemoveUnnecesarySpaces (toremove) { + RemoveUnnecesarySpaces(toremove) { assert(toremove) toremove = this.NormalizeSpaces(toremove) @@ -97,34 +92,41 @@ class StringUtils { } // simplifies a string for easier comparison - SimplifyStringForComparison (value) { + SimplifyStringForComparison(value) { assert(value) value = this.RemoveUnnecesarySpaces(value).toLowerCase() return this.RemoveStuff(value, commonUselessStringParts) } - RemoveSpecialChars (value) { + RemoveSpecialChars(value) { assert(value) return this.RemoveStuff(value, specialChars, ' ') } // if the value is empty, or whitespace - EmptyOrWhiteSpace (value) { + EmptyOrWhiteSpace(value) { // replaces /n-s with "". then replaces spaces with "". if it equals "", then its empty, or only consists of white space - if (value === undefined) { return true } - return (value.replace(/\n/g, '').replace(/ /g, '').replace(/\s/g, ' ') === '') + if (value === undefined) { + return true + } + return ( + value + .replace(/\n/g, '') + .replace(/ /g, '') + .replace(/\s/g, ' ') === '' + ) } // damn nonbreaking space - NormalizeSpaces (input) { + NormalizeSpaces(input) { assert(input) return input.replace(/\s/g, ' ') } - CompareString (s1, s2) { + CompareString(s1, s2) { if (!s1 || !s2) { if (!s1 && !s2) { return 100 @@ -137,24 +139,27 @@ class StringUtils { s2 = this.SimplifyStringForComparison(s2).split(' ') var match = 0 for (var i = 0; i < s1.length; i++) { - if (s2.includes(s1[i])) { match++ } + if (s2.includes(s1[i])) { + match++ + } } var percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent var lengthDifference = Math.abs(s2.length - s1.length) percent -= lengthDifference * lengthDiffMultiplier - if (percent < 0) { percent = 0 } + if (percent < 0) { + percent = 0 + } return percent } - AnswerPreProcessor (value) { + AnswerPreProcessor(value) { assert(value) - return this.RemoveStuff( - value, commonUselessAnswerParts) + return this.RemoveStuff(value, commonUselessAnswerParts) } // 'a. pécsi sör' -> 'pécsi sör' - RemoveAnswerLetters (value) { + RemoveAnswerLetters(value) { assert(value) let s = value.split('. ') @@ -166,8 +171,10 @@ class StringUtils { } } - SimplifyQA (value, mods) { - if (!value) { return } + SimplifyQA(value, mods) { + if (!value) { + return + } const reducer = (res, fn) => { return fn(res) @@ -176,27 +183,23 @@ class StringUtils { return mods.reduce(reducer, value) } - SimplifyAnswer (value) { - return this.SimplifyQA( - value, - [ - this.RemoveSpecialChars.bind(this), - this.RemoveUnnecesarySpaces.bind(this), - this.AnswerPreProcessor.bind(this), - this.RemoveAnswerLetters.bind(this) - ]) + SimplifyAnswer(value) { + return this.SimplifyQA(value, [ + this.RemoveSpecialChars.bind(this), + this.RemoveUnnecesarySpaces.bind(this), + this.AnswerPreProcessor.bind(this), + this.RemoveAnswerLetters.bind(this), + ]) } - SimplifyQuestion (value) { - return this.SimplifyQA( - value, - [ - this.RemoveSpecialChars.bind(this), - this.RemoveUnnecesarySpaces.bind(this) - ]) + SimplifyQuestion(value) { + return this.SimplifyQA(value, [ + this.RemoveSpecialChars.bind(this), + this.RemoveUnnecesarySpaces.bind(this), + ]) } - SimplifyStack (stack) { + SimplifyStack(stack) { return this.SimplifyQuery(stack) } } @@ -204,13 +207,13 @@ class StringUtils { const SUtils = new StringUtils() class Question { - constructor (q, a, data) { + constructor(q, a, data) { this.Q = SUtils.SimplifyQuestion(q) this.A = SUtils.SimplifyAnswer(a) this.data = { ...data } } - toString () { + toString() { if (this.data.type !== 'simple') { return '?' + this.Q + '\n!' + this.A + '\n>' + JSON.stringify(this.data) } else { @@ -218,28 +221,31 @@ class Question { } } - HasQuestion () { + HasQuestion() { return this.Q !== undefined } - HasAnswer () { + HasAnswer() { return this.A !== undefined } - HasImage () { + HasImage() { return this.data.type === 'image' } - IsComplete () { + IsComplete() { return this.HasQuestion() && this.HasAnswer() } - CompareImage (data2) { - return SUtils.CompareString(this.data.images.join(' '), data2.images.join(' ')) + CompareImage(data2) { + return SUtils.CompareString( + this.data.images.join(' '), + data2.images.join(' ') + ) } // returns -1 if botth is simple - CompareData (qObj) { + CompareData(qObj) { try { if (qObj.data.type === this.data.type) { let dataType = qObj.data.type @@ -248,7 +254,11 @@ class Question { } else if (dataType === 'image') { return this.CompareImage(qObj.data) } else { - debugLog(`Unhandled data type ${dataType}`, 'Compare question data', 1) + debugLog( + `Unhandled data type ${dataType}`, + 'Compare question data', + 1 + ) debugLog(qObj, 'Compare question data', 2) } } else { @@ -262,22 +272,22 @@ class Question { return 0 } - CompareQuestion (qObj) { + CompareQuestion(qObj) { return SUtils.CompareString(this.Q, qObj.Q) } - CompareAnswer (qObj) { + CompareAnswer(qObj) { return SUtils.CompareString(this.A, qObj.A) } - Compare (q2, data) { + Compare(q2, data) { assert(q2) let qObj if (typeof q2 === 'string') { qObj = { Q: q2, - data: data + data: data, } } else { qObj = q2 @@ -307,42 +317,42 @@ class Question { qMatch: qMatch, aMatch: aMatch, dMatch: dMatch, - avg: avg + avg: avg, } } } class Subject { - constructor (n) { + constructor(n) { assert(n) this.Name = n this.Questions = [] } - setIndex (i) { + setIndex(i) { this.index = i } - getIndex () { + getIndex() { return this.index } - get length () { + get length() { return this.Questions.length } - AddQuestion (q) { + AddQuestion(q) { assert(q) this.Questions.push(q) } - getSubjNameWithoutYear () { + getSubjNameWithoutYear() { return SUtils.GetSubjNameWithoutYear(this.Name) } - getYear () { + getYear() { let t = this.Name.split(' - ')[0] if (t.match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) { return t @@ -351,7 +361,7 @@ class Subject { } } - Search (q, data) { + Search(q, data) { assert(q) var r = [] @@ -361,7 +371,7 @@ class Subject { r.push({ q: this.Questions[i], match: percent.avg, - detailedMatch: percent + detailedMatch: percent, }) } } @@ -379,30 +389,36 @@ class Subject { return r } - toString () { + toString() { var r = [] - for (var i = 0; i < this.Questions.length; i++) { r.push(this.Questions[i].toString()) } + for (var i = 0; i < this.Questions.length; i++) { + r.push(this.Questions[i].toString()) + } return '+' + this.Name + '\n' + r.join('\n') } } class QuestionDB { - constructor () { + constructor() { this.Subjects = [] } - get length () { + get length() { return this.Subjects.length } - AddQuestion (subj, q) { + AddQuestion(subj, q) { debugLog('Adding new question with subjName: ' + subj, 'qdb add', 1) debugLog(q, 'qdb add', 3) assert(subj) var i = 0 - while (i < this.Subjects.length && - !subj.toLowerCase().includes(this.Subjects[i].getSubjNameWithoutYear().toLowerCase())) { + while ( + i < this.Subjects.length && + !subj + .toLowerCase() + .includes(this.Subjects[i].getSubjNameWithoutYear().toLowerCase()) + ) { i++ } @@ -417,7 +433,7 @@ class QuestionDB { } } - SimplifyQuestion (q) { + SimplifyQuestion(q) { if (typeof q === 'string') { return SUtils.SimplifyQuestion(q) } else { @@ -427,7 +443,7 @@ class QuestionDB { } } - Search (q, subjName, data) { + Search(q, subjName, data) { assert(q) debugLog('Searching for question', 'qdb search', 1) debugLog('Question:', 'qdb search', 2) @@ -447,7 +463,11 @@ class QuestionDB { var r = [] this.Subjects.forEach((subj) => { - if (subjName.toLowerCase().includes(subj.getSubjNameWithoutYear().toLowerCase())) { + if ( + subjName + .toLowerCase() + .includes(subj.getSubjNameWithoutYear().toLowerCase()) + ) { debugLog(`Searching in ${subj.Name} `, 2) r = r.concat(subj.Search(q, data)) } @@ -455,12 +475,20 @@ class QuestionDB { // FIXME: try to remove this? but this is also a good backup plan so idk if (r.length === 0) { - debugLog('Reqults length is zero when comparing names, trying all subjects', 'qdb search', 1) + debugLog( + 'Reqults length is zero when comparing names, trying all subjects', + 'qdb search', + 1 + ) this.Subjects.forEach((subj) => { r = r.concat(subj.Search(q, data)) }) if (r.length > 0) { - debugLog(`FIXME: '${subjName}' gave no result but '' did!`, 'qdb search', 1) + debugLog( + `FIXME: '${subjName}' gave no result but '' did!`, + 'qdb search', + 1 + ) console.error(`FIXME: '${subjName}' gave no result but '' did!`) } } @@ -479,11 +507,13 @@ class QuestionDB { return r } - AddSubject (subj) { + AddSubject(subj) { assert(subj) var i = 0 - while (i < this.length && subj.Name !== this.Subjects[i].Name) { i++ } + while (i < this.length && subj.Name !== this.Subjects[i].Name) { + i++ + } if (i < this.length) { this.Subjects.concat(subj.Questions) @@ -492,9 +522,11 @@ class QuestionDB { } } - toString () { + toString() { var r = [] - for (var i = 0; i < this.Subjects.length; i++) { r.push(this.Subjects[i].toString()) } + for (var i = 0; i < this.Subjects.length; i++) { + r.push(this.Subjects[i].toString()) + } return r.join('\n\n') } } diff --git a/src/utils/dbSetup.js b/src/utils/dbSetup.js index a00b4c7..c879f25 100644 --- a/src/utils/dbSetup.js +++ b/src/utils/dbSetup.js @@ -12,7 +12,7 @@ CreateDB() authDB.close() -function CreateDB () { +function CreateDB() { const dbStruct = utils.ReadJSON(dbStructPath) // authDB = dbtools.GetDB(':memory:') authDB = dbtools.GetDB(usersDBPath) @@ -20,7 +20,12 @@ function CreateDB () { Object.keys(dbStruct).forEach((tableName) => { const tableData = dbStruct[tableName] - dbtools.CreateTable(authDB, tableName, tableData.tableStruct, tableData.foreignKey) + dbtools.CreateTable( + authDB, + tableName, + tableData.tableStruct, + tableData.foreignKey + ) }) try { @@ -28,14 +33,16 @@ function CreateDB () { const uids = utils.ReadFile('./ids').split('\n') uids.forEach((cid, i) => { - if (!cid) { return } + if (!cid) { + return + } logger.Log(`[ ${i} / ${uids.length} ]`) try { dbtools.Insert(authDB, 'users', { pw: uuidv4(), oldCID: cid, avaiblePWRequests: 4, - created: utils.GetDateString() + created: utils.GetDateString(), }) } catch (e) { logger.Log('Error during inserting', logger.GetColor('redbg')) @@ -52,10 +59,13 @@ function CreateDB () { Object.keys(dbStruct).forEach((key) => { const path = `${dir}/${key}.json` logger.Log(`Writing ${path}...`) - utils.WriteFile(JSON.stringify({ - tableInfo: dbtools.TableInfo(authDB, key), - tableRows: dbtools.SelectAll(authDB, key) - }), path) + utils.WriteFile( + JSON.stringify({ + tableInfo: dbtools.TableInfo(authDB, key), + tableRows: dbtools.SelectAll(authDB, key), + }), + path + ) }) logger.Log('Done') diff --git a/src/utils/dbtools.js b/src/utils/dbtools.js index b745427..5b69a41 100644 --- a/src/utils/dbtools.js +++ b/src/utils/dbtools.js @@ -11,7 +11,7 @@ module.exports = { SelectAll, Select, Insert, - CloseDB + CloseDB, } const Sqlite = require('better-sqlite3') @@ -21,7 +21,7 @@ const utils = require('../utils/utils.js') const debugLog = process.env.NS_SQL_DEBUG_LOG // { asd: 'asd', basd: 4 } => asd = 'asd', basd = 4 -function GetSqlQuerry (conditions, type) { +function GetSqlQuerry(conditions, type) { const res = Object.keys(conditions).reduce((acc, key) => { const item = conditions[key] if (typeof item === 'string') { @@ -40,20 +40,20 @@ function GetSqlQuerry (conditions, type) { // ------------------------------------------------------------------------- -function GetDB (path) { +function GetDB(path) { utils.CreatePath(path) const res = new Sqlite(path) res.pragma('synchronous = OFF') return res } -function DebugLog (msg) { +function DebugLog(msg) { if (debugLog) { logger.DebugLog(msg, 'sql', 0) } } -function AddColumn (db, table, col) { +function AddColumn(db, table, col) { try { const colName = Object.keys(col)[0] const colType = col.type @@ -67,9 +67,8 @@ function AddColumn (db, table, col) { } } -function TableInfo (db, table) { +function TableInfo(db, table) { try { - } catch (e) { console.error(e) } @@ -85,13 +84,16 @@ function TableInfo (db, table) { return { columns: infoRes, - dataCount: countRes[Object.keys(countRes)[0]] + dataCount: countRes[Object.keys(countRes)[0]], } } -function Update (db, table, newData, conditions) { +function Update(db, table, newData, conditions) { try { - const s = `UPDATE ${table} SET ${GetSqlQuerry(newData, 'set')} WHERE ${GetSqlQuerry(conditions, 'where')}` + const s = `UPDATE ${table} SET ${GetSqlQuerry( + newData, + 'set' + )} WHERE ${GetSqlQuerry(conditions, 'where')}` const stmt = PrepareStatement(db, s) return stmt.run() @@ -100,7 +102,7 @@ function Update (db, table, newData, conditions) { } } -function Delete (db, table, conditions) { +function Delete(db, table, conditions) { try { const s = `DELETE FROM ${table} WHERE ${GetSqlQuerry(conditions, 'where')}` const stmt = PrepareStatement(db, s) @@ -111,39 +113,45 @@ function Delete (db, table, conditions) { } } -function CreateTable (db, name, columns, foreignKeys) { +function CreateTable(db, name, columns, foreignKeys) { // CREATE TABLE users(pw text PRIMARY KEY NOT NULL, id number, lastIP text, notes text, loginCount // number, lastLogin text, lastAccess text // // FOREIGN KEY(songartist, songalbum) REFERENCES album(albumartist, albumname) ) try { - const cols = Object.keys(columns).reduce((acc, key) => { - const item = columns[key] - // FIXME: array, and push stuff, then join() - const flags = [] - const toCheck = { - primary: 'PRIMARY KEY', - notNull: 'NOT NULL', - unique: 'UNIQUE', - autoIncrement: 'AUTOINCREMENT', - defaultZero: 'DEFAULT 0' - } - Object.keys(toCheck).forEach((key) => { - if (item[key]) { - flags.push(toCheck[key]) + const cols = Object.keys(columns) + .reduce((acc, key) => { + const item = columns[key] + // FIXME: array, and push stuff, then join() + const flags = [] + const toCheck = { + primary: 'PRIMARY KEY', + notNull: 'NOT NULL', + unique: 'UNIQUE', + autoIncrement: 'AUTOINCREMENT', + defaultZero: 'DEFAULT 0', } - }) + Object.keys(toCheck).forEach((key) => { + if (item[key]) { + flags.push(toCheck[key]) + } + }) - acc.push(`${key} ${item.type} ${flags.join(' ')}`) - return acc - }, []).join(', ') + acc.push(`${key} ${item.type} ${flags.join(' ')}`) + return acc + }, []) + .join(', ') let fKeys = [] if (foreignKeys) { foreignKeys.forEach((f) => { const { keysFrom, table, keysTo } = f - fKeys.push(`, FOREIGN KEY(${keysFrom.join(', ')}) REFERENCES ${table}(${keysTo.join(', ')})`) + fKeys.push( + `, FOREIGN KEY(${keysFrom.join( + ', ' + )}) REFERENCES ${table}(${keysTo.join(', ')})` + ) }) } @@ -156,7 +164,7 @@ function CreateTable (db, name, columns, foreignKeys) { } } -function SelectAll (db, from) { +function SelectAll(db, from) { try { const s = `SELECT * from ${from}` @@ -167,7 +175,7 @@ function SelectAll (db, from) { } } -function Select (db, from, conditions) { +function Select(db, from, conditions) { try { const s = `SELECT * from ${from} WHERE ${GetSqlQuerry(conditions, 'where')}` @@ -178,22 +186,26 @@ function Select (db, from, conditions) { } } -function Insert (db, table, data) { +function Insert(db, table, data) { try { - const cols = Object.keys(data).reduce((acc, key) => { - acc.push(`${key}`) - return acc - }, []).join(', ') + const cols = Object.keys(data) + .reduce((acc, key) => { + acc.push(`${key}`) + return acc + }, []) + .join(', ') - const values = Object.keys(data).reduce((acc, key) => { - const item = data[key] - if (typeof item === 'string') { - acc.push(`'${item}'`) - } else { - acc.push(`${item}`) - } - return acc - }, []).join(', ') + const values = Object.keys(data) + .reduce((acc, key) => { + const item = data[key] + if (typeof item === 'string') { + acc.push(`'${item}'`) + } else { + acc.push(`${item}`) + } + return acc + }, []) + .join(', ') const s = `INSERT INTO ${table} (${cols}) VALUES (${values})` const stmt = PrepareStatement(db, s) @@ -204,7 +216,7 @@ function Insert (db, table, data) { } } -function CloseDB (db) { +function CloseDB(db) { db.close((err) => { if (err) { return console.error(err.message) @@ -215,9 +227,11 @@ function CloseDB (db) { // ------------------------------------------------------------------------- -function PrepareStatement (db, s) { +function PrepareStatement(db, s) { if (!db) { - throw new Error('DB is undefined in prepare statement! DB action called with undefined db') + throw new Error( + 'DB is undefined in prepare statement! DB action called with undefined db' + ) } DebugLog(s) return db.prepare(s) diff --git a/src/utils/ids.js b/src/utils/ids.js index 44aef84..9052285 100755 --- a/src/utils/ids.js +++ b/src/utils/ids.js @@ -20,7 +20,7 @@ module.exports = { LogId: LogId, - Load: Load + Load: Load, } const utils = require('../utils/utils.js') @@ -34,12 +34,15 @@ let data = {} let vData = {} let writes = 0 -function Load () { +function Load() { try { var prevData = utils.ReadFile(idStatFile) data = JSON.parse(prevData) } catch (e) { - logger.Log('Error at loading id logs! (@ first run its normal)', logger.GetColor('redbg')) + logger.Log( + 'Error at loading id logs! (@ first run its normal)', + logger.GetColor('redbg') + ) console.log(e) } @@ -47,50 +50,60 @@ function Load () { var prevVData = utils.ReadFile(idVStatFile) vData = JSON.parse(prevVData) } catch (e) { - logger.Log('Error at loading id logs! (@ first run its normal)', logger.GetColor('redbg')) + logger.Log( + 'Error at loading id logs! (@ first run its normal)', + logger.GetColor('redbg') + ) console.log(e) } } -function LogId (id, subj) { +function LogId(id, subj) { Inc(id, subj) AddVisitStat(id, subj) Save() } -function AddSubjToList (list, subj) { +function AddSubjToList(list, subj) { if (!list[subj]) { list[subj] = 0 } list[subj]++ } -function Inc (value, subj) { +function Inc(value, subj) { if (data[value] === undefined) { data[value] = { count: 0, - subjs: {} + subjs: {}, } } data[value].count++ AddSubjToList(data[value].subjs, subj) } -function AddVisitStat (name, subj) { +function AddVisitStat(name, subj) { var m = new Date() - const now = m.getFullYear() + '/' + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + ('0' + m.getDate()).slice(-2) - if (vData[now] === undefined) { vData[now] = {} } + const now = + m.getFullYear() + + '/' + + ('0' + (m.getMonth() + 1)).slice(-2) + + '/' + + ('0' + m.getDate()).slice(-2) + if (vData[now] === undefined) { + vData[now] = {} + } if (vData[now][name] === undefined) { vData[now][name] = { count: 0, - subjs: {} + subjs: {}, } } vData[now][name].count++ AddSubjToList(vData[now][name].subjs, subj) } -function Save () { +function Save() { writes++ if (writes === writeInterval) { try { diff --git a/src/utils/logger.js b/src/utils/logger.js index 3149bae..a5e6167 100755 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -18,7 +18,8 @@ ------------------------------------------------------------------------- */ -const hr = '---------------------------------------------------------------------------------' +const hr = + '---------------------------------------------------------------------------------' module.exports = { GetDateString: GetDateString, @@ -30,7 +31,7 @@ module.exports = { Load: Load, logHashed: logHashed, hr: hr, - C: C + C: C, } const DELIM = C('green') + '|' + C() @@ -45,14 +46,7 @@ const uStatsFile = 'stats/ustats' const uvStatsFile = 'stats/uvstats' const nologFile = './nolog' -const colors = [ - 'green', - 'red', - 'yellow', - 'blue', - 'magenta', - 'cyan' -] +const colors = ['green', 'red', 'yellow', 'blue', 'magenta', 'cyan'] const writeInterval = 10 const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0 @@ -66,19 +60,21 @@ let writes = 0 let noLogips = [] -function GetDateString () { +function GetDateString() { const m = new Date() const d = utils.GetDateString() return GetRandomColor(m.getHours().toString()) + d + C() } -function DebugLog (msg, name, lvl) { +function DebugLog(msg, name, lvl) { if (lvl <= debugLevel) { if (msg === 'hr') { msg = hr } let s = msg - let header = `${C('red')}#DEBUG${lvl}#${C('yellow')}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}` + let header = `${C('red')}#DEBUG${lvl}#${C( + 'yellow' + )}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}` if (typeof msg !== 'object') { s = header + msg } else { @@ -89,7 +85,7 @@ function DebugLog (msg, name, lvl) { } } -function Log (s, c) { +function Log(s, c) { let log = s if (typeof s !== 'object') { let dl = DELIM + C(c) @@ -100,7 +96,7 @@ function Log (s, c) { utils.AppendToFile(log, logFile) } -function LogReq (req, toFile, sc) { +function LogReq(req, toFile, sc) { try { let ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress @@ -122,12 +118,13 @@ function LogReq (req, toFile, sc) { hostname = req.hostname.replace('www.', '').split('.')[0] } else { hostname = 'NOHOST' - Log('req.hostname is undefined! req.hostname: ' + req.hostname, GetColor('redbg')) + Log( + 'req.hostname is undefined! req.hostname: ' + req.hostname, + GetColor('redbg') + ) } - logEntry += dl + - hostname + dl + - req.headers['user-agent'] + dl + - req.method + dl + logEntry += + dl + hostname + dl + req.headers['user-agent'] + dl + req.method + dl if (req.session && req.session.user) { logEntry += C('cyan') + req.session.user.id + C() + dl @@ -139,7 +136,9 @@ function LogReq (req, toFile, sc) { logEntry += GetRandomColor(req.url.split('?')[0]) + req.url - if (sc !== undefined) { logEntry += dl + sc } + if (sc !== undefined) { + logEntry += dl + sc + } logEntry += C() if (!toFile) { @@ -156,7 +155,7 @@ function LogReq (req, toFile, sc) { } } -function parseNoLogFile (newData) { +function parseNoLogFile(newData) { noLogips = newData.split('\n') if (noLogips[noLogips.length - 1] === '') { noLogips.pop() @@ -167,7 +166,7 @@ function parseNoLogFile (newData) { Log('\tNo Log IP-s changed: ' + noLogips.join(', ')) } -function setNoLogReadInterval () { +function setNoLogReadInterval() { utils.WatchFile(nologFile, (newData) => { parseNoLogFile(newData) }) @@ -175,7 +174,7 @@ function setNoLogReadInterval () { parseNoLogFile(utils.ReadFile(nologFile)) } -function Load () { +function Load() { Log('Loading logger...') try { uData = JSON.parse(utils.ReadFile(uStatsFile)) @@ -203,13 +202,16 @@ function Load () { var prevVData = utils.ReadFile(vStatFile) vData = JSON.parse(prevVData) } catch (e) { - Log('Error at loading visit logs! (@ first run its normal)', GetColor('redbg')) + Log( + 'Error at loading visit logs! (@ first run its normal)', + GetColor('redbg') + ) console.log(e) } setNoLogReadInterval() } -function LogStat (url, ip, hostname, userId) { +function LogStat(url, ip, hostname, userId) { let nolog = noLogips.some((x) => { return x.includes(ip) }) @@ -225,9 +227,11 @@ function LogStat (url, ip, hostname, userId) { Save() } -function IncUserStat (userId) { +function IncUserStat(userId) { try { - if (uData[userId] === undefined) { uData[userId] = 0 } + if (uData[userId] === undefined) { + uData[userId] = 0 + } uData[userId]++ } catch (e) { Log('Error at making user ID stats!', GetColor('redbg')) @@ -235,12 +239,21 @@ function IncUserStat (userId) { } } -function AddUserIdStat (userId) { +function AddUserIdStat(userId) { try { var m = new Date() - const now = m.getFullYear() + '/' + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + ('0' + m.getDate()).slice(-2) - if (uvData[now] === undefined) { uvData[now] = {} } - if (uvData[now][userId] === undefined) { uvData[now][userId] = 0 } + const now = + m.getFullYear() + + '/' + + ('0' + (m.getMonth() + 1)).slice(-2) + + '/' + + ('0' + m.getDate()).slice(-2) + if (uvData[now] === undefined) { + uvData[now] = {} + } + if (uvData[now][userId] === undefined) { + uvData[now][userId] = 0 + } uvData[now][userId]++ } catch (e) { Log('Error at making user ID stats!', GetColor('redbg')) @@ -248,21 +261,34 @@ function AddUserIdStat (userId) { } } -function Inc (value) { - if (value.startsWith('/?')) { value = '/' } - if (data[value] === undefined) { data[value] = 0 } +function Inc(value) { + if (value.startsWith('/?')) { + value = '/' + } + if (data[value] === undefined) { + data[value] = 0 + } data[value]++ } -function AddVisitStat (name) { +function AddVisitStat(name) { var m = new Date() - const now = m.getFullYear() + '/' + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + ('0' + m.getDate()).slice(-2) - if (vData[now] === undefined) { vData[now] = {} } - if (vData[now][name] === undefined) { vData[now][name] = 0 } + const now = + m.getFullYear() + + '/' + + ('0' + (m.getMonth() + 1)).slice(-2) + + '/' + + ('0' + m.getDate()).slice(-2) + if (vData[now] === undefined) { + vData[now] = {} + } + if (vData[now][name] === undefined) { + vData[now][name] = 0 + } vData[now][name]++ } -function Save () { +function Save() { writes++ if (writes === writeInterval) { try { @@ -295,11 +321,11 @@ function Save () { } } -function logHashed (x) { +function logHashed(x) { return GetRandomColor(x.toString()) + x + C() } -function GetRandomColor (ip) { +function GetRandomColor(ip) { if (!ip) { return 'red' } @@ -310,20 +336,38 @@ function GetRandomColor (ip) { return C(colors[res % colors.length]) } -function GetColor (c) { +function GetColor(c) { return c } -function C (c) { - if (c !== undefined) { c = c.toLowerCase() } +function C(c) { + if (c !== undefined) { + c = c.toLowerCase() + } - if (c === 'redbg') { return '\x1b[41m' } - if (c === 'bluebg') { return '\x1b[44m' } - if (c === 'green') { return '\x1b[32m' } - if (c === 'red') { return '\x1b[31m' } - if (c === 'yellow') { return '\x1b[33m' } - if (c === 'blue') { return '\x1b[34m' } - if (c === 'magenta') { return '\x1b[35m' } - if (c === 'cyan') { return '\x1b[36m' } + if (c === 'redbg') { + return '\x1b[41m' + } + if (c === 'bluebg') { + return '\x1b[44m' + } + if (c === 'green') { + return '\x1b[32m' + } + if (c === 'red') { + return '\x1b[31m' + } + if (c === 'yellow') { + return '\x1b[33m' + } + if (c === 'blue') { + return '\x1b[34m' + } + if (c === 'magenta') { + return '\x1b[35m' + } + if (c === 'cyan') { + return '\x1b[36m' + } return '\x1b[0m' } diff --git a/src/utils/motd.js b/src/utils/motd.js index 2b2e011..c289ee5 100755 --- a/src/utils/motd.js +++ b/src/utils/motd.js @@ -17,6 +17,6 @@ utils.WriteFile(JSON.stringify(parsed), dataFile) utils.WriteFile(parsed.motd, motdFile) -function GetParams () { +function GetParams() { return process.argv.splice(2) } diff --git a/src/utils/rmDuplicates.js b/src/utils/rmDuplicates.js index e725e3e..aa6b2b1 100644 --- a/src/utils/rmDuplicates.js +++ b/src/utils/rmDuplicates.js @@ -30,7 +30,7 @@ const logPath = './mergeLogs/mergelog_' + GetDateString().replace(/ /g, '_') Main() -function Main () { +function Main() { const params = GetParams() console.log(params) if (params.length === 0) { @@ -59,7 +59,7 @@ function Main () { console.log(C('green') + 'Done' + C()) } -function LogStats (stats, oldData, newData) { +function LogStats(stats, oldData, newData) { const maxSubjNameLength = MaxLengthOf(stats, 'name') const maxPrevLength = MaxLengthOf(stats, 'prevQuestions') const maxAddedLength = MaxLengthOf(stats, 'addedQuestions') @@ -97,16 +97,25 @@ function LogStats (stats, oldData, newData) { LogDataCount(newData) } -function LogDataCount (data) { +function LogDataCount(data) { const subjLength = data.Subjects.length const qLength = data.Subjects.reduce((acc, subj) => { return acc + subj.Questions.length }, 0) - console.log('Subjects: ' + C('green') + subjLength + C() + ', Questions: ' + C('green') + qLength + C()) + console.log( + 'Subjects: ' + + C('green') + + subjLength + + C() + + ', Questions: ' + + C('green') + + qLength + + C() + ) } -function PrintDB (data) { +function PrintDB(data) { const maxSubjNameLength = MaxLengthOf(data.Subjects, 'Name') data.Subjects.forEach((subj) => { @@ -127,7 +136,7 @@ function PrintDB (data) { console.log(hr()) } -function GetExactLength (s, length) { +function GetExactLength(s, length) { let toLog = s.toString() const lengthDiff = length - toLog.length for (let i = 0; i < lengthDiff; i++) { @@ -137,7 +146,7 @@ function GetExactLength (s, length) { return toLog } -function MaxLengthOf (prop, key) { +function MaxLengthOf(prop, key) { return prop.reduce((acc, currStat) => { if (acc < currStat[key].toString().length) { acc = currStat[key].toString().length @@ -146,13 +155,14 @@ function MaxLengthOf (prop, key) { }, 0) } -function RemoveDuplicates (data) { +function RemoveDuplicates(data) { console.log(C('yellow') + 'Removing duplicates' + C()) const res = new classes.QuestionDB() const stats = [] data.Subjects.forEach((subj, i) => { - const logFile = logPath + '/' + subj.Name.replace(/ /g, '_').replace(/\//g, '-') + const logFile = + logPath + '/' + subj.Name.replace(/ /g, '_').replace(/\//g, '-') LogSubjProgress(i, subj, data.Subjects.length) let addedQuestions = 0 let removedQuestions = 0 @@ -185,53 +195,53 @@ function RemoveDuplicates (data) { name: subj.Name, prevQuestions: subj.Questions.length, addedQuestions: addedQuestions, - removedQuestions: removedQuestions + removedQuestions: removedQuestions, }) }) return { res, stats } } -function LogSubjProgress (i, subj, subjCount) { +function LogSubjProgress(i, subj, subjCount) { log( '[ ' + - C('cyan') + - (i + 1) + - C() + - ' / ' + - C('green') + - subjCount + - C() + - ' ] ' + - C('yellow') + - subj.Name + - C() + - ': ' + - C('green') + - subj.Questions.length + C('cyan') + + (i + 1) + + C() + + ' / ' + + C('green') + + subjCount + + C() + + ' ] ' + + C('yellow') + + subj.Name + + C() + + ': ' + + C('green') + + subj.Questions.length ) } -function LogResultProgress (subj, addedQuestions, removedQuestions) { +function LogResultProgress(subj, addedQuestions, removedQuestions) { log( ' ' + - C('cyan') + - '-> ' + - C('green') + - addedQuestions + - C() + - ', removed: ' + - C('red') + - removedQuestions + - C() + - '\n' + C('cyan') + + '-> ' + + C('green') + + addedQuestions + + C() + + ', removed: ' + + C('red') + + removedQuestions + + C() + + '\n' ) } -function log (msg) { +function log(msg) { process.stdout.write(msg) } -function hr (char) { +function hr(char) { let h = '' const cols = process.stdout.columns || 20 for (let i = 0; i < cols; i++) { @@ -240,21 +250,27 @@ function hr (char) { return h } -function C (color) { +function C(color) { return logger.C(color) } -function GetParams () { +function GetParams() { return process.argv.splice(2) } -function GetDateString () { +function GetDateString() { const m = new Date() - const d = m.getFullYear() + '-' + - ('0' + (m.getMonth() + 1)).slice(-2) + '-' + - ('0' + m.getDate()).slice(-2) + ' ' + - ('0' + m.getHours()).slice(-2) + ':' + - ('0' + m.getMinutes()).slice(-2) + ':' + + const d = + m.getFullYear() + + '-' + + ('0' + (m.getMonth() + 1)).slice(-2) + + '-' + + ('0' + m.getDate()).slice(-2) + + ' ' + + ('0' + m.getHours()).slice(-2) + + ':' + + ('0' + m.getMinutes()).slice(-2) + + ':' + ('0' + m.getSeconds()).slice(-2) return d } diff --git a/src/utils/utils.js b/src/utils/utils.js index f23b230..b566982 100755 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -11,7 +11,7 @@ module.exports = { WatchFile: WatchFile, ReadDir: ReadDir, CopyFile: CopyFile, - GetDateString: GetDateString + GetDateString: GetDateString, } var fs = require('fs') @@ -20,26 +20,33 @@ var logger = require('../utils/logger.js') const dataFile = './qminingPublic/data.json' -function GetDateString () { +function GetDateString() { const m = new Date() - return m.getFullYear() + '-' + - ('0' + (m.getMonth() + 1)).slice(-2) + '-' + - ('0' + m.getDate()).slice(-2) + ' ' + - ('0' + m.getHours()).slice(-2) + ':' + - ('0' + m.getMinutes()).slice(-2) + ':' + + return ( + m.getFullYear() + + '-' + + ('0' + (m.getMonth() + 1)).slice(-2) + + '-' + + ('0' + m.getDate()).slice(-2) + + ' ' + + ('0' + m.getHours()).slice(-2) + + ':' + + ('0' + m.getMinutes()).slice(-2) + + ':' + ('0' + m.getSeconds()).slice(-2) + ) } -function CopyFile (from, to) { +function CopyFile(from, to) { CreatePath(to) fs.copyFileSync(from, to) } -function ReadDir (path) { +function ReadDir(path) { return fs.readdirSync(path) } -function ReadJSON (name) { +function ReadJSON(name) { try { return JSON.parse(ReadFile(name)) } catch (e) { @@ -48,16 +55,18 @@ function ReadJSON (name) { } } -function ReadFile (name) { - if (!FileExists(name)) { throw new Error('No such file: ' + name) } +function ReadFile(name) { + if (!FileExists(name)) { + throw new Error('No such file: ' + name) + } return fs.readFileSync(name, 'utf8') } -function FileExists (path) { +function FileExists(path) { return fs.existsSync(path) } -function WatchFile (file, callback) { +function WatchFile(file, callback) { if (FileExists(file)) { fs.watchFile(file, (curr, prev) => { fs.readFile(file, 'utf8', (err, data) => { @@ -76,8 +85,10 @@ function WatchFile (file, callback) { } } -function CreatePath (path, onlyPath) { - if (FileExists(path)) { return } +function CreatePath(path, onlyPath) { + if (FileExists(path)) { + return + } var p = path.split('/') var currDir = p[0] @@ -85,7 +96,9 @@ function CreatePath (path, onlyPath) { if (currDir !== '' && !fs.existsSync(currDir)) { try { fs.mkdirSync(currDir) - } catch (e) { console.log('Failed to make ' + currDir + ' directory... ') } + } catch (e) { + console.log('Failed to make ' + currDir + ' directory... ') + } } currDir += '/' + p[i] } @@ -95,32 +108,38 @@ function CreatePath (path, onlyPath) { } } -function WriteFile (content, path) { +function WriteFile(content, path) { CreatePath(path) fs.writeFileSync(path, content) } -function WriteFileAsync (content, path) { +function WriteFileAsync(content, path) { CreatePath(path) - fs.writeFile(path, content, function (err) { + fs.writeFile(path, content, function(err) { if (err) { - logger.Log('Error writing file: ' + path + ' (sync)', logger.GetColor('redbg')) + logger.Log( + 'Error writing file: ' + path + ' (sync)', + logger.GetColor('redbg') + ) } }) } -function AppendToFile (data, file) { +function AppendToFile(data, file) { CreatePath(file) try { fs.appendFileSync(file, '\n' + data) } catch (e) { - logger.Log('Error appendig to file log file: ' + file + ' (sync)', logger.GetColor('redbg')) + logger.Log( + 'Error appendig to file log file: ' + file + ' (sync)', + logger.GetColor('redbg') + ) logger.Log(data) console.log(e) } } -function Beep () { +function Beep() { try { process.stdout.write('\x07') } catch (e) { @@ -128,9 +147,12 @@ function Beep () { } } -function WriteBackup () { +function WriteBackup() { try { - WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString()) + WriteFileAsync( + ReadFile(dataFile), + 'public/backs/data_' + new Date().toString() + ) } catch (e) { logger.Log('Error backing up data json file!', logger.GetColor('redbg')) console.log(e)