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:
parent
ea459d05e4
commit
25aa6f3187
2 changed files with 87 additions and 3 deletions
|
@ -42,6 +42,7 @@ interface ForumEntry {
|
||||||
content: string
|
content: string
|
||||||
admin: boolean
|
admin: boolean
|
||||||
comments: Comment[]
|
comments: Comment[]
|
||||||
|
imagePath?: string
|
||||||
reacts: {
|
reacts: {
|
||||||
[key: string]: number[]
|
[key: string]: number[]
|
||||||
}
|
}
|
||||||
|
@ -188,6 +189,7 @@ function setup(data: SubmoduleData): void {
|
||||||
const publicDir = publicdirs[0]
|
const publicDir = publicdirs[0]
|
||||||
|
|
||||||
const forumDir = publicDir + 'forum'
|
const forumDir = publicDir + 'forum'
|
||||||
|
const forumFiles = publicDir + 'forumFiles'
|
||||||
|
|
||||||
if (!utils.FileExists(forumDir)) {
|
if (!utils.FileExists(forumDir)) {
|
||||||
utils.CreatePath(forumDir, true)
|
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(
|
app.post(
|
||||||
'/addPost',
|
'/addPost',
|
||||||
(
|
(
|
||||||
|
@ -252,13 +316,13 @@ function setup(data: SubmoduleData): void {
|
||||||
forumName: string
|
forumName: string
|
||||||
content: string
|
content: string
|
||||||
title: string
|
title: string
|
||||||
|
image?: string
|
||||||
}>,
|
}>,
|
||||||
res
|
res
|
||||||
) => {
|
) => {
|
||||||
logger.LogReq(req)
|
logger.LogReq(req)
|
||||||
|
|
||||||
const { title, content } = req.body
|
const { title, content, forumName, image } = req.body
|
||||||
const forumName = req.body.forumName
|
|
||||||
if (!forumName) {
|
if (!forumName) {
|
||||||
res.json({
|
res.json({
|
||||||
success: false,
|
success: false,
|
||||||
|
@ -282,6 +346,7 @@ function setup(data: SubmoduleData): void {
|
||||||
title: title,
|
title: title,
|
||||||
admin: admins.includes(user.id.toString()),
|
admin: admins.includes(user.id.toString()),
|
||||||
commentCount: 0,
|
commentCount: 0,
|
||||||
|
imagePath: image,
|
||||||
}
|
}
|
||||||
|
|
||||||
contents[newPostKey] = postData
|
contents[newPostKey] = postData
|
||||||
|
|
|
@ -47,6 +47,7 @@ import {
|
||||||
loadJSON,
|
loadJSON,
|
||||||
writeData,
|
writeData,
|
||||||
editDb,
|
editDb,
|
||||||
|
RecievedData,
|
||||||
} from '../../../utils/actions'
|
} from '../../../utils/actions'
|
||||||
import {
|
import {
|
||||||
dataToString,
|
dataToString,
|
||||||
|
@ -81,6 +82,7 @@ const line = '====================================================' // lol
|
||||||
const registeredScriptsFile = 'stats/registeredScripts.json'
|
const registeredScriptsFile = 'stats/registeredScripts.json'
|
||||||
const testUsersFile = 'data/testUsers.json'
|
const testUsersFile = 'data/testUsers.json'
|
||||||
const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js'
|
const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js'
|
||||||
|
const askedQuestionFile = 'stats/recievedQuestions'
|
||||||
const recievedQuestionFile = 'stats/recievedQuestions'
|
const recievedQuestionFile = 'stats/recievedQuestions'
|
||||||
const savedQuestionsFileName = 'savedQuestions.json'
|
const savedQuestionsFileName = 'savedQuestions.json'
|
||||||
const oldMotdFile = 'publicDirs/qminingPublic/oldMotd'
|
const oldMotdFile = 'publicDirs/qminingPublic/oldMotd'
|
||||||
|
@ -259,6 +261,21 @@ function dbExists(location: string, qdbs: Array<QuestionDb>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeAskData(body: QuestionFromScript) {
|
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 {
|
try {
|
||||||
let towrite = utils.GetDateString() + '\n'
|
let towrite = utils.GetDateString() + '\n'
|
||||||
towrite +=
|
towrite +=
|
||||||
|
@ -541,7 +558,7 @@ function setup(data: SubmoduleData): Submodule {
|
||||||
res.end(stringifiedData)
|
res.end(stringifiedData)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post('/isAdding', function (req: Request, res: Response) {
|
app.post('/isAdding', function (req: Request<RecievedData>, res: Response) {
|
||||||
logger.LogReq(req)
|
logger.LogReq(req)
|
||||||
const user: User = req.session.user
|
const user: User = req.session.user
|
||||||
const dryRun = testUsers.includes(user.id)
|
const dryRun = testUsers.includes(user.id)
|
||||||
|
@ -553,6 +570,8 @@ function setup(data: SubmoduleData): Submodule {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
writeIsAddingData(req.body)
|
||||||
|
|
||||||
const location = req.body.location.split('/')[2]
|
const location = req.body.location.split('/')[2]
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue