mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Added comments to news items
This commit is contained in:
parent
7221068cf6
commit
65d4ef3223
1 changed files with 100 additions and 1 deletions
|
@ -1332,7 +1332,9 @@ function GetApp(): ModuleType {
|
|||
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]
|
||||
}
|
||||
|
@ -1350,6 +1352,103 @@ function GetApp(): ModuleType {
|
|||
res.json({ msg: 'done' })
|
||||
})
|
||||
|
||||
function addComment(obj, path, comment) {
|
||||
if (path.length === 0) {
|
||||
obj.push(comment)
|
||||
} else {
|
||||
const i = path.pop()
|
||||
if (!obj[i].subComments) {
|
||||
obj[i].subComments = []
|
||||
}
|
||||
addComment(obj[i].subComments, path, comment)
|
||||
}
|
||||
}
|
||||
|
||||
function deleteComment(obj, path) {
|
||||
if (path.length === 1) {
|
||||
obj.splice(path[0], 1)
|
||||
} else {
|
||||
const i = path.pop()
|
||||
deleteComment(obj[i].subComments, path)
|
||||
}
|
||||
}
|
||||
|
||||
function addReactionToComment(obj, path, { reaction, isDelete, uid }) {
|
||||
if (path.length === 1) {
|
||||
const index = path[0]
|
||||
if (!obj[index].reacts) {
|
||||
obj[index].reacts = {}
|
||||
}
|
||||
if (isDelete) {
|
||||
if (obj[index].reacts[reaction]) {
|
||||
obj[index].reacts[reaction] = obj[index].reacts[reaction].filter(
|
||||
(uid) => {
|
||||
return uid !== uid
|
||||
}
|
||||
)
|
||||
if (obj[index].reacts[reaction].length === 0) {
|
||||
delete obj[index].reacts[reaction]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!obj[index].reacts[reaction]) {
|
||||
obj[index].reacts[reaction] = [uid]
|
||||
} else {
|
||||
if (!obj[index].reacts[reaction].includes(uid)) {
|
||||
obj[index].reacts[reaction].push(uid)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const i = path.pop()
|
||||
addReactionToComment(obj[i].subComments, path, {
|
||||
reaction: reaction,
|
||||
isDelete: isDelete,
|
||||
uid: uid,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
app.post('/comment', (req: Request, res) => {
|
||||
logger.LogReq(req)
|
||||
|
||||
const user: User = req.session.user
|
||||
const news: any = utils.ReadJSON(newsFile)
|
||||
const { type, path, newsKey } = req.body
|
||||
if (!type || !path || !newsKey) {
|
||||
res.json({ status: 'fail', msg: ' type or path or newsKey is undefined' })
|
||||
}
|
||||
|
||||
|
||||
if (type === 'add') {
|
||||
const { text } = req.body
|
||||
const comment = {
|
||||
date: new Date().toLocaleString(),
|
||||
user: user.id,
|
||||
text: text,
|
||||
}
|
||||
if (!news[newsKey].comments) {
|
||||
news[newsKey].comments = []
|
||||
}
|
||||
addComment(news[newsKey].comments, path, comment)
|
||||
} else if (type === 'delete') {
|
||||
if (news[newsKey].comments) {
|
||||
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 {
|
||||
res.json({ status: 'fail', msg: 'no such type' })
|
||||
}
|
||||
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
|
||||
res.json({ status: 'ok', newNews: news })
|
||||
})
|
||||
|
||||
app.post('/registerscript', function(req: Request, res) {
|
||||
logger.LogReq(req)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue