Added /react endpoint

This commit is contained in:
mrfry 2021-03-05 20:01:43 +01:00
parent 661d0dd3e4
commit 8ad91239de

View file

@ -724,7 +724,9 @@ function GetApp(): ModuleType {
logger.LogReq(req) logger.LogReq(req)
const user: User = req.session.user const user: User = req.session.user
const news: any = utils.ReadJSON(newsFile) const news: any = utils.ReadJSON(newsFile)
const admins: any = utils.ReadJSON(adminUsersFile) const admins: any = utils.FileExists(adminUsersFile)
? utils.ReadJSON(adminUsersFile)
: []
const { title, content } = req.body const { title, content } = req.body
news[uuidv4()] = { news[uuidv4()] = {
@ -1344,43 +1346,6 @@ function GetApp(): ModuleType {
) )
} }
if (req.body.react && req.body.newsKey) {
const { react, newsKey, isDelete } = req.body
const news: any = utils.ReadJSON(newsFile)
if (news[newsKey]) {
if (isDelete) {
if (news[newsKey].reacts) {
news[newsKey].reacts[react] = news[newsKey].reacts[react].filter(
(uid) => {
return uid !== user.id
}
)
if (news[newsKey].reacts[react].length === 0) {
delete news[newsKey].reacts[react]
}
}
} else {
if (!news[newsKey].reacts) {
news[newsKey].reacts = { [react]: [user.id] }
} else {
if (Array.isArray(news[newsKey].reacts[react])) {
if (!news[newsKey].reacts[react].includes(user.id)) {
news[newsKey].reacts[react].push(user.id)
}
} else {
news[newsKey].reacts[react] = [user.id]
}
}
}
}
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
res.json({ msg: 'done', news: news })
return
}
res.json({ msg: 'done' }) res.json({ msg: 'done' })
}) })
@ -1405,7 +1370,7 @@ function GetApp(): ModuleType {
} }
} }
function addReactionToComment(obj, path, { reaction, isDelete, uid }) { function addReaction(obj, path, { reaction, isDelete, uid }) {
if (path.length === 1) { if (path.length === 1) {
const index = path[0] const index = path[0]
if (!obj[index].reacts) { if (!obj[index].reacts) {
@ -1433,7 +1398,7 @@ function GetApp(): ModuleType {
} }
} else { } else {
const i = path.pop() const i = path.pop()
addReactionToComment(obj[i].subComments, path, { addReaction(obj[i].subComments, path, {
reaction: reaction, reaction: reaction,
isDelete: isDelete, isDelete: isDelete,
uid: uid, uid: uid,
@ -1441,12 +1406,64 @@ function GetApp(): ModuleType {
} }
} }
app.post('/react', (req: Request, res) => {
logger.LogReq(req)
const user: User = req.session.user
const news: any = utils.ReadJSON(newsFile)
const { newsKey, path, reaction, isDelete } = req.body
if (!newsKey || !reaction) {
res.json({ status: 'fail', msg: 'no newskey or reaction' })
return
}
if (!path || path.length === 0) {
if (news[newsKey]) {
if (isDelete) {
if (news[newsKey].reacts) {
news[newsKey].reacts[reaction] = news[newsKey].reacts[
reaction
].filter((uid) => {
return uid !== user.id
})
if (news[newsKey].reacts[reaction].length === 0) {
delete news[newsKey].reacts[reaction]
}
}
} else {
if (!news[newsKey].reacts) {
news[newsKey].reacts = { [reaction]: [user.id] }
} else {
if (Array.isArray(news[newsKey].reacts[reaction])) {
if (!news[newsKey].reacts[reaction].includes(user.id)) {
news[newsKey].reacts[reaction].push(user.id)
}
} else {
news[newsKey].reacts[reaction] = [user.id]
}
}
}
}
} else {
addReaction(news[newsKey].comments, path, {
reaction: reaction,
isDelete: isDelete,
uid: user.id,
})
}
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
res.json({ status: 'ok', news: news })
})
app.post('/comment', (req: Request, res) => { app.post('/comment', (req: Request, res) => {
logger.LogReq(req) logger.LogReq(req)
const user: User = req.session.user const user: User = req.session.user
const news: any = utils.ReadJSON(newsFile) const news: any = utils.ReadJSON(newsFile)
const admins: any = utils.ReadJSON(adminUsersFile) const admins: any = utils.FileExists(adminUsersFile)
? utils.ReadJSON(adminUsersFile)
: []
const { type, path, newsKey } = req.body const { type, path, newsKey } = req.body
if (!type || !path || !newsKey) { if (!type || !path || !newsKey) {
res.json({ status: 'fail', msg: ' type or path or newsKey is undefined' }) res.json({ status: 'fail', msg: ' type or path or newsKey is undefined' })
@ -1469,13 +1486,6 @@ function GetApp(): ModuleType {
if (news[newsKey].comments) { if (news[newsKey].comments) {
deleteComment(news[newsKey].comments, path) deleteComment(news[newsKey].comments, path)
} }
} else if (type === 'reaction') {
const { reaction, isDelete } = req.body
addReactionToComment(news[newsKey].comments, path, {
reaction: reaction,
isDelete: isDelete,
uid: user.id,
})
} else { } else {
res.json({ status: 'fail', msg: 'no such type' }) res.json({ status: 'fail', msg: 'no such type' })
} }