From 8e4163e3f08e29f38fe87c59d8cfb91e0fa98063 Mon Sep 17 00:00:00 2001 From: kartofen Date: Mon, 29 Aug 2022 19:01:20 +0300 Subject: everything works --- src/components/Form.astro | 7 ++- src/components/Image.astro | 21 +++++++ src/components/Post.astro | 28 ++++++++++ src/components/Thread.astro | 82 +++++++++++++++++++++++++++ src/components/Thread.svelte | 105 ----------------------------------- src/layouts/Default.astro | 4 -- src/layouts/ThreadLayout.astro | 22 ++++++++ src/lib/api.ts | 2 +- src/lib/image.ts | 4 +- src/lib/thread.ts | 42 +++++++++----- src/pages/board/[board].astro | 22 ++++---- src/pages/board/[board]/[tid].astro | 28 +++++++--- src/pages/create/[board].astro | 5 +- src/pages/create/[board]/[tid].astro | 27 +++++++++ src/pages/create/comment.ts | 17 ++++++ src/pages/create/thread.ts | 5 +- src/styles/blackbox.css | 4 +- src/styles/thread.css | 4 +- 18 files changed, 273 insertions(+), 156 deletions(-) create mode 100644 src/components/Image.astro create mode 100644 src/components/Post.astro create mode 100644 src/components/Thread.astro delete mode 100644 src/components/Thread.svelte create mode 100644 src/layouts/ThreadLayout.astro create mode 100644 src/pages/create/[board]/[tid].astro create mode 100644 src/pages/create/comment.ts (limited to 'src') diff --git a/src/components/Form.astro b/src/components/Form.astro index 417b569..540a4d4 100644 --- a/src/components/Form.astro +++ b/src/components/Form.astro @@ -1,7 +1,11 @@ --- -import '../styles/blackbox.css'; import { sha256 } from 'js-sha256'; +export interface Props { + board: string; + tid?: string +} + const { board, tid } = Astro.props; const iphash = sha256(Astro.clientAddress); --- @@ -31,6 +35,7 @@ const iphash = sha256(Astro.clientAddress); if(r.status == 200) { alert('Thread Successfuly Posted'); let id = await r.text(); + if(id == 'close') window.top.close(); window.location.assign(`/board/${board}/${id}`); } else alert('An Error has Accured'); diff --git a/src/components/Image.astro b/src/components/Image.astro new file mode 100644 index 0000000..07dbadf --- /dev/null +++ b/src/components/Image.astro @@ -0,0 +1,21 @@ +--- +export interface Props { + image: string; + fileType: string; +} + +const { image, fileType } = Astro.props; +--- + +{(image!= null) && ( + (fileType == 'image') ? ( + + {image}
+
+ ) : ( + (fileType == 'image') && ( + + )) +)} \ No newline at end of file diff --git a/src/components/Post.astro b/src/components/Post.astro new file mode 100644 index 0000000..6805846 --- /dev/null +++ b/src/components/Post.astro @@ -0,0 +1,28 @@ +--- +import { creatorColor, formatTime } from '../lib/util'; + +export interface Props { + id: string; + date: number; + creator: string; + box: string; + board: string +} + +const { id, date, creator, box, board = '' } = Astro.props; +--- +
+ + {(board != '') ? ( + {id} + ) : ( + {id} + )} + + at {formatTime(date)}
+ -> + {creator} +
+

+ +
diff --git a/src/components/Thread.astro b/src/components/Thread.astro new file mode 100644 index 0000000..6fae3de --- /dev/null +++ b/src/components/Thread.astro @@ -0,0 +1,82 @@ +--- +import type Thread from '../models/Thread'; +import Post from './Post.astro'; +import Image from './Image.astro'; + +export interface Props { + thread: Thread; + board: string; + comments: boolean; +} + +const { thread, board, comments = false } = Astro.props; + +let replies: string[] = []; +const listReplies = (id: string, getReplies: boolean = false): any => { + if(getReplies) return replies; + + replies = []; + thread.comments.forEach(comment => { + if(comment.commentText.includes(id)) + replies.push(comment.id); + }) + + if(replies.length <= 0) return false; + return true; +} +--- + +{(thread.id != "rules") && ( <> + + + {comments && (listReplies(thread.id) && ( + + {listReplies(thread.id, true).map((id) => ( <> + >>{id} + ))} +
+ ))} + + +

{thread.threadName}

+ +

+ + {comments && ( <> + {thread.comments.map((comment) => ( <> + + + {listReplies(comment.id) && ( + + {listReplies(comment.id, true).map((id) => ( <> + >>{id} + ))} +


+ )} + + +

+ + + ))} + )} + + + )} + +{comments && ( <> + + + + )} diff --git a/src/components/Thread.svelte b/src/components/Thread.svelte deleted file mode 100644 index 0321b8d..0000000 --- a/src/components/Thread.svelte +++ /dev/null @@ -1,105 +0,0 @@ - - -{#if thread.id != "rules"} -

- - {thread.id} - at {formatTime(thread.creationDate)}
- - -> - {thread.threadCreator} -
-
- - {#if comments} - {#if listReplies(thread.id) } - - {#each listReplies(thread.id, true) as id} - >>{id}  - {/each} -
- {/if} - {/if} - -
- -

{thread.threadName}

- - {#if thread.imageId!= null && thread.imageId != undefined} - {#if thread.fileType == 'image'} - - {thread.imageId}
-
- {:else if thread.fileType == 'video'} - - {/if} - {/if} - -

{@html thread.threadText}

- -{#if comments} - {#each thread.comments as comment} -
- - {comment.id} - at {formatTime(comment.creationDate)}
- - -> - {comment.commentCreator} -
-
- - {#if listReplies(comment.id)} - - {#each listReplies(comment.id, true) as id} - >>{id}  - {/each} -
- {/if} - -
- - {#if comment.imageId!= null && comment.imageId != undefined} - {#if comment.fileType == 'image'} - - {comment.imageId}
-
- {:else if comment.fileType == 'video'} - - {/if} - {/if} - -

{@html comment.commentText}

- -
- {/each} -{/if} - -
-{/if} diff --git a/src/layouts/Default.astro b/src/layouts/Default.astro index 3c0b6ff..ae59edb 100644 --- a/src/layouts/Default.astro +++ b/src/layouts/Default.astro @@ -2,13 +2,10 @@ const { title } = Astro.props as Props; --- - - - {title} @@ -19,6 +16,5 @@ const { title } = Astro.props as Props; diff --git a/src/layouts/ThreadLayout.astro b/src/layouts/ThreadLayout.astro new file mode 100644 index 0000000..24aba41 --- /dev/null +++ b/src/layouts/ThreadLayout.astro @@ -0,0 +1,22 @@ +--- +const { title } = Astro.props as Props; +--- + + + + + + {title} + + +
+ +
+ + + + diff --git a/src/lib/api.ts b/src/lib/api.ts index 645fc23..e6ac0f1 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,7 +1,7 @@ const base = 'https://localhost:5001/api' export async function api(method: string, resource: string, data?: any) { - console.log("API USED", method, resource, data); + // console.log("API USED", method, resource, data); return await fetch(`${base}/${method}/${resource}`, { method, headers: { diff --git a/src/lib/image.ts b/src/lib/image.ts index c7d4d49..6ab20a5 100644 --- a/src/lib/image.ts +++ b/src/lib/image.ts @@ -20,8 +20,8 @@ export async function postImg(img: string) { let filename = `${uuidV4()}.${fileExt}`; //save file in this server - writeFileSync(`dist/client/images/${filename}`, img_b64, "base64"); - // writeFileSync(`public/images/${filename}`, img_b64, "base64"); + // writeFileSync(`dist/client/images/${filename}`, img_b64, "base64"); + writeFileSync(`public/images/${filename}`, img_b64, "base64"); let data = { 'name': filename, diff --git a/src/lib/thread.ts b/src/lib/thread.ts index e0f0aa4..2199318 100644 --- a/src/lib/thread.ts +++ b/src/lib/thread.ts @@ -8,8 +8,8 @@ export async function processThreadIn(board: string, thread: Thread, comments = if(!thread || !board) return; let imageId: string = thread.imageId; - if(existsSync(`dist/client/images/${imageId}`)) - // if(existsSync(`public/images/${imageId}`)) + // if(existsSync(`dist/client/images/${imageId}`)) + if(existsSync(`public/images/${imageId}`)) thread.imageId = `/images/${imageId}`; else thread.imageId = await getImg(imageId); @@ -21,8 +21,8 @@ export async function processThreadIn(board: string, thread: Thread, comments = for(let comment of thread.comments) { let cimageId = comment.imageId; - if(existsSync(`dist/client/images/${cimageId}`)) - // if(existsSync(`public/images/${cimageId}`)) + // if(existsSync(`dist/client/images/${cimageId}`)) + if(existsSync(`public/images/${cimageId}`)) comment.imageId = `/images/${cimageId}`; else comment.imageId = await getImg(cimageId); @@ -32,22 +32,34 @@ export async function processThreadIn(board: string, thread: Thread, comments = } export async function processThreadOut(form: any): Thread { - let img = form.get('image').toString(); - - let t: Thread = {}; - t.id = uuidV4(); - t.ThreadName = form.get('ThreadName').toString(); - t.ThreadCreator = uuidV5(form.get('iphash'), uuidNIL); - t.ThreadText = form.get('ThreadText').toString(); - t.Comments = []; - t.CreationDate = Date.now(); - t.ImageId = await postImg(img); - t.FileType = getImgType(img); + 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 async function processCommentOut(form: any): Comment { + let img = form.get('image'); + + 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; } function replaceURLs(text: string, board: string, OPtid?: string): string { diff --git a/src/pages/board/[board].astro b/src/pages/board/[board].astro index a529d3c..eaea03e 100644 --- a/src/pages/board/[board].astro +++ b/src/pages/board/[board].astro @@ -1,8 +1,9 @@ --- -import Default from '../../layouts/Default.astro'; -import Thread from '../../components/Thread.svelte' import '../../styles/thread.css' -import '../../styles/blackbox.css?' +import '../../styles/blackbox.css' + +import ThreadLayout from '../../layouts/ThreadLayout.astro'; +import Thread from '../../components/Thread.astro' import type Thread from '../../models/Thread'; import { api } from '../../lib/api.ts'; @@ -18,8 +19,11 @@ for(let thread of threads) await processThreadIn(board, thread); --- - -

{board}

+ +

+ {board} +

+
@@ -27,10 +31,4 @@ for(let thread of threads) {threads.map((thread) => ( ))} -
- - + diff --git a/src/pages/board/[board]/[tid].astro b/src/pages/board/[board]/[tid].astro index 4aa34ef..e9b345a 100644 --- a/src/pages/board/[board]/[tid].astro +++ b/src/pages/board/[board]/[tid].astro @@ -1,14 +1,16 @@ --- -import Default from '../../../layouts/Default.astro'; -import Thread from '../../../components/Thread.svelte' -import '../../../styles/thread.css' -import type Thread from '../../../models/Thread'; +import '../../../styles/thread.css'; +import '../../../styles/blackbox.css'; + +import ThreadLayout from '../../../layouts/ThreadLayout.astro'; +import Thread from '../../../components/Thread.astro' +import type Thread from '../../../models/Thread';; import { api } from '../../../lib/api'; import { processThreadIn } from '../../../lib/thread'; -const { board } = Astro.params; -const data = await api('get', `thread/${board}/${Astro.params.tid}`); +const { board, tid } = Astro.params; +const data = await api('get', `thread/${board}/${tid}`); if(data.status === 404) return Astro.redirect('/404'); @@ -16,6 +18,16 @@ const thread: Thread = await data.json(); await processThreadIn(board, thread, true); --- - + +

+ {board} +

+ +
+ +
+ -
\ No newline at end of file + \ No newline at end of file diff --git a/src/pages/create/[board].astro b/src/pages/create/[board].astro index 9c2308f..152277c 100644 --- a/src/pages/create/[board].astro +++ b/src/pages/create/[board].astro @@ -1,4 +1,6 @@ --- +import '../../styles/blackbox.css'; + import Default from '../../layouts/Default.astro'; import Form from '../../components/Form.astro'; @@ -8,7 +10,7 @@ const { board } = Astro.params;

{board}

-
+
@@ -21,5 +23,6 @@ const { board } = Astro.params; diff --git a/src/pages/create/[board]/[tid].astro b/src/pages/create/[board]/[tid].astro new file mode 100644 index 0000000..ccc95ca --- /dev/null +++ b/src/pages/create/[board]/[tid].astro @@ -0,0 +1,27 @@ +--- +import '../../../styles/blackbox.css'; + +import Default from '../../../layouts/Default.astro'; +import Form from '../../../components/Form.astro'; + +const { board, tid } = Astro.params; +--- + + +

Comment

+ + + + +
+ + + +
+ + diff --git a/src/pages/create/comment.ts b/src/pages/create/comment.ts new file mode 100644 index 0000000..f8d4c82 --- /dev/null +++ b/src/pages/create/comment.ts @@ -0,0 +1,17 @@ +import { api } from '../../lib/api'; +import { processCommentOut } from '../../lib/thread'; +import Comment from '../../models/Thread'; + +export async function post({ request }) { + const form = await request.formData(); + + let c: Comment = await processCommentOut(form); + + console.log(c); + + await api('post', `comment/${form.get('board')}/${form.get('tid')}`, JSON.stringify(c)); + + return new Response('close', { + status: 200 + }); +} diff --git a/src/pages/create/thread.ts b/src/pages/create/thread.ts index 0a0b7d1..b3a02be 100644 --- a/src/pages/create/thread.ts +++ b/src/pages/create/thread.ts @@ -1,13 +1,14 @@ import { api } from '../../lib/api'; import { processThreadOut } from '../../lib/thread'; +import Thread from '../../models/Thread'; export async function post({ request }) { const form = await request.formData(); - console.log(form); - let t: Thread = await processThreadOut(form); + console.log(t); + await api('post', `thread/${form.get('board')}`, JSON.stringify(t)); return new Response(t.id, { diff --git a/src/styles/blackbox.css b/src/styles/blackbox.css index 4a0e63f..4ddcb85 100644 --- a/src/styles/blackbox.css +++ b/src/styles/blackbox.css @@ -1,8 +1,8 @@ .blackbox { - width: var(--wdt); + max-width: var(--wdt, 100%); border: 10px solid black; padding: 10px; margin: 10px; - margin-left: 0px; + margin-left: var(--ml, 10px); background-color: white; } diff --git a/src/styles/thread.css b/src/styles/thread.css index 14997de..2667d6f 100644 --- a/src/styles/thread.css +++ b/src/styles/thread.css @@ -1,13 +1,11 @@ .threadbox { - width: 600px; border: 10px solid green; padding: 10px; margin: 10px; - margin-left: 0px; background-color: white; } .commentbox { - width: 500px; + max-width: 90%; border: 5px solid green; padding: 10px; margin: 10px; -- cgit v1.2.3