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"
52import 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
175export 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- }
0 commit comments