mirror of
				https://gitlab.com/MrFry/mrfrys-node-server
				synced 2025-04-01 20:24:18 +02:00 
			
		
		
		
	Added images to forum, saving recieved data on isAdding
This commit is contained in:
		| @@ -42,6 +42,7 @@ interface ForumEntry { | ||||
|   content: string | ||||
|   admin: boolean | ||||
|   comments: Comment[] | ||||
|   imagePath?: string | ||||
|   reacts: { | ||||
|     [key: string]: number[] | ||||
|   } | ||||
| @@ -188,6 +189,7 @@ function setup(data: SubmoduleData): void { | ||||
|   const publicDir = publicdirs[0] | ||||
|  | ||||
|   const forumDir = publicDir + 'forum' | ||||
|   const forumFiles = publicDir + 'forumFiles' | ||||
|  | ||||
|   if (!utils.FileExists(forumDir)) { | ||||
|     utils.CreatePath(forumDir, true) | ||||
| @@ -245,6 +247,68 @@ function setup(data: SubmoduleData): void { | ||||
|     }) | ||||
|   }) | ||||
|  | ||||
|   app.get('/forumRanklist', (req: Request, res) => { | ||||
|     const forumName: string = req.query.forumName | ||||
|     if (!forumName) { | ||||
|       res.json({ success: false, msg: 'forumName required' }) | ||||
|       return | ||||
|     } | ||||
|  | ||||
|     const { forumPath, contents } = getForumData(forumName, forumDir) | ||||
|  | ||||
|     const forumEntries = Object.keys(contents).map((key) => { | ||||
|       const entry = utils.ReadJSON(forumPath + '/' + key) | ||||
|       return entry | ||||
|     }) | ||||
|  | ||||
|     const leaderBoard = forumEntries.reduce((acc, forumEntry) => { | ||||
|       const { user, reacts } = forumEntry | ||||
|       const ups = reacts?.['thumbs up']?.length || 0 | ||||
|       const downs = reacts?.['thumbs down']?.length || 0 | ||||
|  | ||||
|       if (!acc[user]) { | ||||
|         acc[user] = { | ||||
|           up: ups, | ||||
|           down: downs, | ||||
|         } | ||||
|       } else { | ||||
|         acc[user] = { | ||||
|           up: acc[user].up + ups, | ||||
|           down: acc[user].down + downs, | ||||
|         } | ||||
|       } | ||||
|       return acc | ||||
|     }, {}) | ||||
|  | ||||
|     res.json({ | ||||
|       success: true, | ||||
|       leaderBoard: Object.keys(leaderBoard) | ||||
|         .map((key) => { | ||||
|           const val = leaderBoard[key] | ||||
|           return { | ||||
|             ...val, | ||||
|             user: key, | ||||
|             sum: val.up - val.down, | ||||
|           } | ||||
|         }) | ||||
|         .sort((a, b) => { | ||||
|           return b.sum - a.sum | ||||
|         }), | ||||
|     }) | ||||
|   }) | ||||
|  | ||||
|   app.post('/postMeme', (req: Request, res) => { | ||||
|     utils | ||||
|       .uploadFile(req, forumFiles) | ||||
|       .then(() => { | ||||
|         res.json({ success: true }) | ||||
|       }) | ||||
|       .catch(() => { | ||||
|         res.json({ success: false, msg: 'error during uploading' }) | ||||
|         return | ||||
|       }) | ||||
|   }) | ||||
|  | ||||
|   app.post( | ||||
|     '/addPost', | ||||
|     ( | ||||
| @@ -252,13 +316,13 @@ function setup(data: SubmoduleData): void { | ||||
|         forumName: string | ||||
|         content: string | ||||
|         title: string | ||||
|         image?: string | ||||
|       }>, | ||||
|       res | ||||
|     ) => { | ||||
|       logger.LogReq(req) | ||||
|  | ||||
|       const { title, content } = req.body | ||||
|       const forumName = req.body.forumName | ||||
|       const { title, content, forumName, image } = req.body | ||||
|       if (!forumName) { | ||||
|         res.json({ | ||||
|           success: false, | ||||
| @@ -282,6 +346,7 @@ function setup(data: SubmoduleData): void { | ||||
|         title: title, | ||||
|         admin: admins.includes(user.id.toString()), | ||||
|         commentCount: 0, | ||||
|         imagePath: image, | ||||
|       } | ||||
|  | ||||
|       contents[newPostKey] = postData | ||||
|   | ||||
| @@ -47,6 +47,7 @@ import { | ||||
|   loadJSON, | ||||
|   writeData, | ||||
|   editDb, | ||||
|   RecievedData, | ||||
| } from '../../../utils/actions' | ||||
| import { | ||||
|   dataToString, | ||||
| @@ -81,6 +82,7 @@ const line = '====================================================' // lol | ||||
| const registeredScriptsFile = 'stats/registeredScripts.json' | ||||
| const testUsersFile = 'data/testUsers.json' | ||||
| const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js' | ||||
| const askedQuestionFile = 'stats/recievedQuestions' | ||||
| const recievedQuestionFile = 'stats/recievedQuestions' | ||||
| const savedQuestionsFileName = 'savedQuestions.json' | ||||
| const oldMotdFile = 'publicDirs/qminingPublic/oldMotd' | ||||
| @@ -259,6 +261,21 @@ function dbExists(location: string, qdbs: Array<QuestionDb>) { | ||||
| } | ||||
|  | ||||
| function writeAskData(body: QuestionFromScript) { | ||||
|   try { | ||||
|     let towrite = utils.GetDateString() + '\n' | ||||
|     towrite += | ||||
|       '------------------------------------------------------------------------------\n' | ||||
|     towrite += JSON.stringify(body) | ||||
|     towrite += | ||||
|       '\n------------------------------------------------------------------------------\n' | ||||
|     utils.AppendToFile(towrite, askedQuestionFile) | ||||
|   } catch (err) { | ||||
|     logger.Log('Error writing revieved /ask POST data') | ||||
|     console.error(err) | ||||
|   } | ||||
| } | ||||
|  | ||||
| function writeIsAddingData(body: RecievedData) { | ||||
|   try { | ||||
|     let towrite = utils.GetDateString() + '\n' | ||||
|     towrite += | ||||
| @@ -541,7 +558,7 @@ function setup(data: SubmoduleData): Submodule { | ||||
|     res.end(stringifiedData) | ||||
|   }) | ||||
|  | ||||
|   app.post('/isAdding', function (req: Request, res: Response) { | ||||
|   app.post('/isAdding', function (req: Request<RecievedData>, res: Response) { | ||||
|     logger.LogReq(req) | ||||
|     const user: User = req.session.user | ||||
|     const dryRun = testUsers.includes(user.id) | ||||
| @@ -553,6 +570,8 @@ function setup(data: SubmoduleData): Submodule { | ||||
|       return | ||||
|     } | ||||
|  | ||||
|     writeIsAddingData(req.body) | ||||
|  | ||||
|     const location = req.body.location.split('/')[2] | ||||
|  | ||||
|     try { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user