diff options
author | kartofen <mladenovnasko0@gmail.com> | 2022-10-24 23:19:33 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2022-10-24 23:19:33 +0300 |
commit | 9dfd6aa77518b3a1e6bfd926a3b7719c32a8c97f (patch) | |
tree | d10fc76ef816160871e3955aa75821bc5d5088d4 /src/lib/thread.ts | |
parent | 4ca81621e5b31922565b4149ebad1deb5161071a (diff) |
use the new api
Diffstat (limited to 'src/lib/thread.ts')
-rw-r--r-- | src/lib/thread.ts | 169 |
1 files changed, 73 insertions, 96 deletions
diff --git a/src/lib/thread.ts b/src/lib/thread.ts index 2199318..d7460c7 100644 --- a/src/lib/thread.ts +++ b/src/lib/thread.ts @@ -1,115 +1,92 @@ import { existsSync } from 'fs'; import { v4 as uuidV4, v5 as uuidV5, NIL as uuidNIL } from 'uuid'; -import { getImg, postImg, getImgType } from './image'; +import { saveImg, getImgType } from './image'; import type { Thread, Comment } from '../models/Thread' -export async function processThreadIn(board: string, thread: Thread, comments = false) { - if(!thread || !board) return; - let imageId: string = thread.imageId; - - // if(existsSync(`dist/client/images/${imageId}`)) - if(existsSync(`public/images/${imageId}`)) - thread.imageId = `/images/${imageId}`; - else - thread.imageId = await getImg(imageId); - - thread.threadText = replaceURLs(thread.threadText, board, thread.id); +export async function processThreadIn(board: string, thread: Thread) { + for(let comment of thread.comments) { + comment.id += thread.id; + comment.content = replaceURLs(comment.content, board, thread.id); + } +} - if(!comments) return; +export function processThreadOut(form: any): Thread { + let img = form.get('image'); - for(let comment of thread.comments) - { - let cimageId = comment.imageId; - // if(existsSync(`dist/client/images/${cimageId}`)) - if(existsSync(`public/images/${cimageId}`)) - comment.imageId = `/images/${cimageId}`; - else - comment.imageId = await getImg(cimageId); + let t: Thread = { + title: form.get('title'), + content: form.get('content'), + image: saveImg(img), + imagetype: getImgType(img), + } - comment.commentText = replaceURLs(comment.commentText, board, thread.id); - } + return t; } -export async function processThreadOut(form: any): Thread { - let img = form.get('image'); - - let t: Thread = { - id: uuidV4(), - threadName: form.get('ThreadName'), - threadCreator: uuidV5(form.get('iphash'), uuidNIL), - threadText: form.get('ThreadText'), - comments: [], - creationDate: Date.now(), - imageId: await postImg(img), - fileType: getImgType(img), - } - - return t; -} +export function processCommentOut(form: any): Comment { + let img = form.get('image'); -export async function processCommentOut(form: any): Comment { - let img = form.get('image'); + let c: Comment = { + content: form.get('content'), + image: saveImg(img), + imagetype: getImgType(img), + } - let c: Comment = {}; - c.id = uuidV4(); - c.commentCreator = uuidV5(form.get('iphash'), uuidNIL); - c.commentText = form.get('CommentText'); - c.creationDate = Date.now(); - c.imageId = await postImg(img); - c.fileType = getImgType(img); - - return c; + return c; } function replaceURLs(text: string, board: string, OPtid?: string): string { - if(!text) return; + if(!text) return; + + let quoteRegex = />\s.*/g; + text = text.replace(quoteRegex, (q) => { + return ` <span class="quote"> ${q} </span> `; + }); - if(OPtid) { - let replyRegex = /(^|[^>])>>[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g; + let replyRegex = />>\d+/g; text = text.replace(replyRegex, (id) => { - let showid = id; - id = id.replace(/(>|\n| )/g, ''); - if(id == OPtid) showid += "(OP)"; - // this adds the targeted class - // to the target id when its hovered on - // and removes it when it stops hovering - return ` <a class="${id}" href="#${id}" onmouseover="document.getElementById('${id}').classList.add('targeted')" onmouseleave="document.getElementById('${id}').classList.remove('targeted')"> ${showid} </a> `; + let showid = id; + id = id.replace(/>/g, ''); + if(id == OPtid) showid += "(OP)"; + // to highlight referenced id + return ` <a class="${id}" href="#${id}" onmouseover="document.getElementById('${id}').classList.add('targeted')" onmouseleave="document.getElementById('${id}').classList.remove('targeted')"> ${showid} </a> `; + }); + + + + // link to thread on other board + // let otherthreadlinkRegex = />>>\/.*\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g; + // text = text.replace(otherthreadlinkRegex, (id) => { + // let link = id.slice(4); + // let parts = link.split('/'); + // let linkid = parts[1]; let board = parts[0]; + // return ` <a href="${board}/${linkid}#{linkid}"> >>>/${board}/${linkid} </a> ` + // }); + // link to thread on this board + // let threadlinkRegex = />>>[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g; + // text = text.replace(threadlinkRegex, (id) => { + // let linkid = id.slice(3); // remove the first >>> + // return ` <a href="../${board}/${linkid}#${linkid}"> ${id} </a> ` + // }); + // link to another board + // let otherboardRegex = />>>\/.*\/($|[^0-9a-fA-F])/g; + // text = text.replace(otherboardRegex, (id) => { + // let boardid = id.split('/')[1]; + // return ` <a href="../${boardid}"> >>>/${boardid}/ </a> ` + // }); + + let newlineRegex = /(\r\n|\r|\n)/g; + text = text.replace(newlineRegex, () => { + return ' <br> ' + }); + + let urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g; + return text.replace(urlRegex, (url) => { + var hyperlink = url; + if (!hyperlink.match('^https?:\/\/')) { + hyperlink = 'http://' + hyperlink; + } + return ' <a href="' + hyperlink + '" target="_blank" rel="noopener noreferrer">' + url + '</a> ' }); - } - - // link to thread on other board - let otherthreadlinkRegex = />>>\/.*\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g; - text = text.replace(otherthreadlinkRegex, (id) => { - let link = id.slice(4); - let parts = link.split('/'); - let linkid = parts[1]; let board = parts[0]; - return ` <a href="${board}/${linkid}#{linkid}"> >>>/${board}/${linkid} </a> ` - }); - // link to thread on this board - let threadlinkRegex = />>>[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g; - text = text.replace(threadlinkRegex, (id) => { - let linkid = id.slice(3); // remove the first >>> - return ` <a href="../${board}/${linkid}#${linkid}"> ${id} </a> ` - }); - // link to another board - let otherboardRegex = />>>\/.*\/($|[^0-9a-fA-F])/g; - text = text.replace(otherboardRegex, (id) => { - let boardid = id.split('/')[1]; - return ` <a href="../${boardid}"> >>>/${boardid}/ </a> ` - }); - - let newlineRegex = /(\r\n|\r|\n)/g; - text = text.replace(newlineRegex, () => { - return ' <br> ' - }); - - let urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g; - return text.replace(urlRegex, (url) => { - var hyperlink = url; - if (!hyperlink.match('^https?:\/\/')) { - hyperlink = 'http://' + hyperlink; - } - return ' <a href="' + hyperlink + '" target="_blank" rel="noopener noreferrer">' + url + '</a> ' - }); } |