<%= comment.content %>+
<%- comment.content %>
diff --git a/client/comment.js b/client/comment.js index db44cc9..59105a9 100644 --- a/client/comment.js +++ b/client/comment.js @@ -5,14 +5,37 @@ import initDb from "../db.js"; let db = await initDb(); let main = new Route([auth], async function (req, res, input) { - let {route} = req.params; - let {id} = req.query; + let { route } = req.params; + let { id } = req.query; let comments = await db.all('SELECT * FROM comment WHERE targetType = ? AND targetId = ? ORDER BY date DESC', [ route, id ]); + for (let comment of comments) { + let { content } = comment; + content = content.replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); + + content = content.split(' '); + + for (let elem in content) { + let e = content[elem]; + if (e.startsWith('@')) { + let f = e.slice(1); + content[elem] = `${e}` + } + } + + content = content.join(' '); + + comment.content = content; + } + return { ...input, route, diff --git a/form/comment.js b/form/comment.js index 53e197a..b826560 100644 --- a/form/comment.js +++ b/form/comment.js @@ -14,6 +14,9 @@ let main = new Route([auth], async function (req, res, input) { if (!targetType || !targetId || !content || username == '!nobody') return { 'success': false, 'message': 'Some fields are missing' }; // probably should not re-use these strings + if (content.length > 8192) + return { 'success': false, 'message': 'Comment is too long' }; + await db.run('INSERT INTO comment (username, targetType, targetId, date, content, id) VALUES (?,?,?,?,?,?)', [ username, targetType, @@ -25,6 +28,23 @@ let main = new Route([auth], async function (req, res, input) { let u = `/client/${targetType}?id=${targetId}`; + let a = content.split(' '); + + for (let b of a) { + if (b.startsWith('@')) { + let f = b.slice(1); + + await db.run('INSERT INTO message (username, targetType, targetId, date, content, read) VALUES (?,?,?,?,?,?)', [ + f, + 'mention', + u, + +new Date(), + content, + 'false' + ]); + } + } + if (targetType == 'user') { await db.run('INSERT INTO message (username, targetType, targetId, date, content, read) VALUES (?,?,?,?,?,?)', [ targetId, diff --git a/views/comments.ejs b/views/comments.ejs index d9ac2f4..1ca91cf 100644 --- a/views/comments.ejs +++ b/views/comments.ejs @@ -1,7 +1,6 @@ <%- include('form_bare.ejs', { data: [ { label: "" , type: "text" , name: "targetId" , hidden: id }, { label: "" , type: "text" , name: "targetType" , hidden: route }, { label: "Your thoughts..." , type: "textarea" , - name: "content" , hidden: route }, ], 'route' : '/api/form/comment' , 'title' : 'Comments' , noCaptcha: true }) - -%> + name: "content" , hidden: route }, ], 'route' : '/api/form/comment' , 'title' : 'Comments' , noCaptcha: true }) -%>
<%= comment.content %>+
<%- comment.content %>