Skip to content

Commit 178379a

Browse files
committed
New form for forum post creation
1 parent 41a0cda commit 178379a

3 files changed

Lines changed: 52 additions & 56 deletions

File tree

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
import { error, redirect } from "@sveltejs/kit"
2-
import { type } from "arktype"
3-
import { authorise } from "$lib/server/auth"
4-
import createCommentQuery from "$lib/server/createComment.surql"
1+
import { error } from "@sveltejs/kit"
52
import exclude from "$lib/server/exclude"
6-
import filter from "$lib/server/filter"
7-
import formError from "$lib/server/formError"
8-
import ratelimit from "$lib/server/ratelimit"
9-
import { db, Record } from "$lib/server/surreal"
10-
import { arktype, superValidate } from "$lib/server/validate"
11-
12-
const schema = type({
13-
// title: "1 <= string <= 50",
14-
content: "(string <= 3000) | undefined",
15-
})
3+
import { db } from "$lib/server/surreal"
164

175
export async function load({ url }) {
186
exclude("Forum")
@@ -29,43 +17,5 @@ export async function load({ url }) {
2917

3018
return {
3119
categoryName: category.name,
32-
form: await superValidate(null, arktype(schema)),
3320
}
3421
}
35-
36-
export const actions: import("./$types").Actions = {}
37-
actions.default = async ({ locals, request, url, getClientAddress }) => {
38-
exclude("Forum")
39-
const { user } = await authorise(locals)
40-
const form = await superValidate(request, arktype(schema))
41-
if (!form.valid) return formError(form)
42-
43-
// const title = form.data.title.trim()
44-
// if (!title) return formError(form, ["title"], ["Post must have a title"])
45-
46-
const unfiltered = form.data.content?.trim()
47-
if (!unfiltered)
48-
return formError(form, ["content"], ["Post must have content"])
49-
50-
const category = url.searchParams.get("category")
51-
if (!category) error(400, "Missing category")
52-
53-
const limit = ratelimit(form, "comment", getClientAddress, 5)
54-
if (limit) return limit
55-
56-
const [[getCategory]] = await db.query<{ id: string }[][]>(
57-
`
58-
SELECT record::id(id) AS id FROM forumCategory
59-
WHERE string::lowercase(name) = string::lowercase($category)`,
60-
{ category }
61-
)
62-
if (!getCategory) error(404, "Category not found")
63-
64-
const [, newCommentId] = await db.query<string[]>(createCommentQuery, {
65-
content: filter(unfiltered),
66-
type: ["forum", getCategory.id],
67-
user: Record("user", user.id),
68-
})
69-
70-
redirect(302, `/comment/${newCommentId}`)
71-
}

Site/src/routes/(main)/forum/create/+page.svelte

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
import Form from "$components/forms/Form.svelte"
33
import Textarea from "$components/forms/Textarea.svelte"
44
import Head from "$components/Head.svelte"
5-
import { superForm } from "$lib/validate"
5+
import { formData } from "./create.remote"
66
77
const { data } = $props()
8-
9-
let formData = $derived(superForm(data.form))
10-
export const snapshot = formData
118
</script>
129

1310
<Head name={data.siteName} title="Create a post in {data.categoryName}" />
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { error, redirect } from "@sveltejs/kit"
2+
import { type } from "arktype"
3+
import { form, getRequestEvent } from "$app/server"
4+
import { authorise } from "$lib/server/auth"
5+
import createCommentQuery from "$lib/server/createComment.surql"
6+
import exclude from "$lib/server/exclude"
7+
import filter from "$lib/server/filter"
8+
import { ratelimitRemote } from "$lib/server/ratelimit"
9+
import { db, Record } from "$lib/server/surreal"
10+
import type { ClientForm } from "$lib/validate"
11+
12+
const schema = type({
13+
// title: "1 <= string <= 50",
14+
content: "string <= 3000",
15+
})
16+
17+
export const formData = form(schema, async ({ content }, issues) => {
18+
exclude("Forum")
19+
const { locals, url, getClientAddress } = getRequestEvent()
20+
const { user } = await authorise(locals)
21+
22+
// const title = form.data.title.trim()
23+
// if (!title) return issues.title("Post must have a title")
24+
25+
const unfiltered = content.trim()
26+
if (!unfiltered) return issues.content("Post must have content")
27+
28+
const category = url.searchParams.get("category")
29+
if (!category) error(400, "Missing category")
30+
31+
const limit = ratelimitRemote(issues, "comment", getClientAddress, 5)
32+
if (limit) return limit
33+
34+
const [[getCategory]] = await db.query<{ id: string }[][]>(
35+
`
36+
SELECT record::id(id) AS id FROM forumCategory
37+
WHERE string::lowercase(name) = string::lowercase($category)`,
38+
{ category }
39+
)
40+
if (!getCategory) error(404, "Category not found")
41+
42+
const [, newCommentId] = await db.query<string[]>(createCommentQuery, {
43+
content: filter(unfiltered),
44+
type: ["forum", getCategory.id],
45+
user: Record("user", user.id),
46+
})
47+
48+
redirect(302, `/comment/${newCommentId}`)
49+
}) as ClientForm<typeof schema.infer>

0 commit comments

Comments
 (0)