aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/api.ts14
-rw-r--r--src/lib/image.ts10
-rw-r--r--src/lib/thread.ts80
-rw-r--r--src/lib/util.ts23
4 files changed, 127 insertions, 0 deletions
diff --git a/src/lib/api.ts b/src/lib/api.ts
new file mode 100644
index 0000000..221a7b3
--- /dev/null
+++ b/src/lib/api.ts
@@ -0,0 +1,14 @@
+const base = 'https://localhost:5001/api'
+
+export async function api(method: string, resource: string, data?: any) {
+ console.log("API USED", method, resource, data);
+
+
+ return await fetch(`${base}/${method}/${resource}`, {
+ method,
+ headers: {
+ 'content-type': 'application/json'
+ },
+ body: data
+ });
+}
diff --git a/src/lib/image.ts b/src/lib/image.ts
new file mode 100644
index 0000000..53d4c98
--- /dev/null
+++ b/src/lib/image.ts
@@ -0,0 +1,10 @@
+import { api } from './api';
+
+export async function getImg(imgId: string) {
+ if(imgId == null || imgId == '' || imgId == undefined) return null;
+
+ const response = await api('get', `image/${imgId}`)
+
+ if(response.status !== 200) return `Server error: ${response.status}`;
+ return response.text();
+}
diff --git a/src/lib/thread.ts b/src/lib/thread.ts
new file mode 100644
index 0000000..d0a4fce
--- /dev/null
+++ b/src/lib/thread.ts
@@ -0,0 +1,80 @@
+import { writeFileSync, existsSync } from 'fs';
+import { getImg } from './image';
+import type Thread from '../models/Thread'
+
+export async function processThreadIn(board: string, thread: Thread, procComments?: false) {
+ if(!thread || !board) return;
+ let imageId: string = thread.imageId;
+
+ if(existsSync(`public/images/${imageId}`))
+ thread.imageId = `/images/${imageId}`;
+ else
+ thread.imageId = await getImg(imageId);
+
+ thread.threadText = replaceURLs(thread.threadText, board, thread.id);
+
+ if(!procComments) return;
+
+ for(let comment of thread.comments)
+ {
+ let cimageId = comment.imageId;
+ if(existsSync(`public/images/${cimageId}`))
+ comment.imageId = `/images/${cimageId}`;
+ else
+ comment.imageId = await getImg(cimageId);
+
+ comment.commentText = replaceURLs(comment.commentText, board, thread.id);
+ }
+}
+
+function replaceURLs(text: string, board: string, OPtid?: string): string {
+ if(!text) return;
+
+ 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;
+ 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> `;
+ });
+ }
+
+ // 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> '
+ });
+}
diff --git a/src/lib/util.ts b/src/lib/util.ts
new file mode 100644
index 0000000..a0734b9
--- /dev/null
+++ b/src/lib/util.ts
@@ -0,0 +1,23 @@
+export const creatorColor = (creatorid: string) => {
+ let parts = creatorid.split('-')
+ let ints = parts.map(function(d) { return parseInt(d,16) })
+ let code = ints[0]
+
+ let blue = (code >> 16) & 31;
+ let green = (code >> 21) & 31;
+ let red = (code >> 27) & 31;
+ let foreColor = `border:5px solid rgb(${red << 3}, ${green << 3}, ${blue << 3});`;
+ return foreColor;
+};
+
+export function formatTime(timestamp: number): string
+{
+ let date = new Date(timestamp);
+ let year = date.getFullYear();
+ let month = date.getMonth() + 1;
+ let day = date.getDate();
+ let time = date.toTimeString().split(' ')[0];
+ let formattedTime = `${time}, ${day}/${month}/${year}`
+
+ return formattedTime;
+}