refactor(server/utils): make paths and imports more concise and correct
This commit is contained in:
		
							parent
							
								
									9f0a76e2b0
								
							
						
					
					
						commit
						922807ba6a
					
				| @ -1,21 +0,0 @@ | ||||
| import { H3Event, EventHandlerRequest } from "h3"; | ||||
| export default async function (ev: H3Event<EventHandlerRequest>) { | ||||
| 	const body = await readBody(ev); | ||||
| 
 | ||||
| 	let { data: cres }: { data: any } = await useFetch( | ||||
| 		"https://www.google.com/recaptcha/api/siteverify", | ||||
| 		{ | ||||
| 			method: "post", | ||||
| 			body: { | ||||
| 				secret: useRuntimeConfig().captcha.secret, | ||||
| 				response: body["g-recaptcha-response"], | ||||
| 			}, | ||||
| 		}, | ||||
| 	); | ||||
| 	if (!cres.value.success) { | ||||
| 		throw createError({ | ||||
| 			statusCode: 400, | ||||
| 			message: "bad recaptcha", | ||||
| 		}); | ||||
| 	} | ||||
| } | ||||
| @ -1,6 +1,80 @@ | ||||
| import isIdNan from "./isIdNan"; | ||||
| import isAdmin from "./isAdmin"; | ||||
| import isLoggedIn from "./isLoggedIn"; | ||||
| import storyCheck from "./storyCheck"; | ||||
| import * as storyPrivileges from "./storyPrivileges"; | ||||
| export { isIdNan, isAdmin, isLoggedIn, storyCheck, storyPrivileges }; | ||||
| import { EventHandlerRequest, H3Event } from "h3"; | ||||
| import { messages } from "@server/constants"; | ||||
| import { IStory } from "@models/stories"; | ||||
| import { isFicmasHidden } from "@functions"; | ||||
| import { IDraft } from "@models/stories/draft"; | ||||
| export function isIdNan(ev: H3Event<EventHandlerRequest>) { | ||||
| 	const id = parseInt(getRouterParam(ev, "id")!); | ||||
| 	if (Number.isNaN(id)) { | ||||
| 		throw createError({ | ||||
| 			statusCode: 400, | ||||
| 			message: "Invalid id provided.", | ||||
| 		}); | ||||
| 	} | ||||
| 	return id; | ||||
| } | ||||
| export function isAdmin(ev: H3Event<EventHandlerRequest>) { | ||||
| 	isLoggedIn(ev); | ||||
| 	if (!ev.context.currentUser?.profile.isAdmin) { | ||||
| 		throw createError({ | ||||
| 			statusCode: 403, | ||||
| 			statusMessage: messages[403], | ||||
| 		}); | ||||
| 	} | ||||
| } | ||||
| export function isLoggedIn(ev: H3Event<EventHandlerRequest>) { | ||||
| 	if (!ev.context.currentUser) { | ||||
| 		throw createError({ | ||||
| 			statusCode: 401, | ||||
| 			statusMessage: messages[401], | ||||
| 		}); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| export async function storyCheck( | ||||
| 	event: H3Event<EventHandlerRequest>, | ||||
| 	story: IStory, | ||||
| 	idx: number, | ||||
| ) { | ||||
| 	let ret: any = {}; | ||||
| 	if (!story) { | ||||
| 		ret.statusCode = 404; | ||||
| 		ret.message = messages[404]; | ||||
| 	} else if (story.ficmas != null) { | ||||
| 		if (isFicmasHidden(story)) { | ||||
| 			ret = { | ||||
| 				statusCode: 423, | ||||
| 				message: `TOP SECRET! This story is part of an ongoing challenge. You'll be able to read it after the challenge's reveal date.`, | ||||
| 			}; | ||||
| 		} | ||||
| 	} else if ( | ||||
| 		story.chapters[idx]?.hidden && | ||||
| 		event.context.currentUser?._id !== story.author._id && | ||||
| 		!event.context.currentUser?.profile.isAdmin | ||||
| 	) { | ||||
| 		ret.statusCode = 403; | ||||
| 		ret.message = messages[403]; | ||||
| 	} | ||||
| 	return !!Object.keys(ret).length ? ret : null; | ||||
| } | ||||
| 
 | ||||
| export async function captcha(ev: H3Event<EventHandlerRequest>) { | ||||
| 	const body = await readBody(ev); | ||||
| 
 | ||||
| 	let { data: cres }: { data: any } = await useFetch( | ||||
| 		"https://www.google.com/recaptcha/api/siteverify", | ||||
| 		{ | ||||
| 			method: "post", | ||||
| 			body: { | ||||
| 				secret: useRuntimeConfig().captcha.secret, | ||||
| 				response: body["g-recaptcha-response"], | ||||
| 			}, | ||||
| 		}, | ||||
| 	); | ||||
| 	if (!cres.value.success) { | ||||
| 		throw createError({ | ||||
| 			statusCode: 400, | ||||
| 			message: "bad recaptcha", | ||||
| 		}); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -1,12 +0,0 @@ | ||||
| import { H3Event, EventHandlerRequest } from "h3"; | ||||
| import { messages } from "../constants"; | ||||
| import isLoggedIn from "./isLoggedIn"; | ||||
| export default function (ev: H3Event<EventHandlerRequest>) { | ||||
| 	isLoggedIn(ev); | ||||
| 	if (!ev.context.currentUser?.profile.isAdmin) { | ||||
| 		throw createError({ | ||||
| 			statusCode: 403, | ||||
| 			statusMessage: messages[403], | ||||
| 		}); | ||||
| 	} | ||||
| } | ||||
| @ -1,12 +0,0 @@ | ||||
| import { EventHandlerRequest, H3Event } from "h3"; | ||||
| 
 | ||||
| export default function (ev: H3Event<EventHandlerRequest>) { | ||||
| 	const id = parseInt(getRouterParam(ev, "id")!); | ||||
| 	if (Number.isNaN(id)) { | ||||
| 		throw createError({ | ||||
| 			statusCode: 400, | ||||
| 			message: "Invalid id provided.", | ||||
| 		}); | ||||
| 	} | ||||
| 	return id; | ||||
| } | ||||
| @ -1,10 +0,0 @@ | ||||
| import { H3Event, EventHandlerRequest } from "h3"; | ||||
| import { messages } from "../constants"; | ||||
| export default function (ev: H3Event<EventHandlerRequest>) { | ||||
| 	if (!ev.context.currentUser) { | ||||
| 		throw createError({ | ||||
| 			statusCode: 401, | ||||
| 			statusMessage: messages[401], | ||||
| 		}); | ||||
| 	} | ||||
| } | ||||
| @ -1,30 +0,0 @@ | ||||
| import type { H3Event, EventHandlerRequest } from "h3"; | ||||
| import { isFicmasHidden } from "@functions"; | ||||
| import { IStory } from "@models/stories"; | ||||
| import { messages } from "../constants"; | ||||
| export default async function ( | ||||
| 	event: H3Event<EventHandlerRequest>, | ||||
| 	story: IStory, | ||||
| 	idx: number, | ||||
| ) { | ||||
| 	let ret: any = {}; | ||||
| 	if (!story) { | ||||
| 		ret.statusCode = 404; | ||||
| 		ret.message = messages[404]; | ||||
| 	} else if (story.ficmas != null) { | ||||
| 		if (isFicmasHidden(story)) { | ||||
| 			ret = { | ||||
| 				statusCode: 423, | ||||
| 				message: `TOP SECRET! This story is part of an ongoing challenge. You'll be able to read it after the challenge's reveal date.`, | ||||
| 			}; | ||||
| 		} | ||||
| 	} else if ( | ||||
| 		story.chapters[idx]?.hidden && | ||||
| 		event.context.currentUser?._id !== story.author._id && | ||||
| 		!event.context.currentUser?.profile.isAdmin | ||||
| 	) { | ||||
| 		ret.statusCode = 403; | ||||
| 		ret.message = messages[403]; | ||||
| 	} | ||||
| 	return !!Object.keys(ret).length ? ret : null; | ||||
| } | ||||
| @ -1,6 +1,6 @@ | ||||
| import type { H3Event, EventHandlerRequest } from "h3"; | ||||
| import { IStory } from "@models/stories"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { IDraft } from "@models/stories/draft"; | ||||
| export function canDelete(event: H3Event<EventHandlerRequest>, story: IStory) { | ||||
| 	isLoggedIn(event); | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| import { messages } from "@server/constants"; | ||||
| import isAdmin from "@server/middlewareButNotReally/isAdmin"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isAdmin } from "@server/middlewareButNotReally"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { Band, IBand } from "@models/band"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| import { messages } from "@server/constants"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { Story } from "@models/stories"; | ||||
| import { Review } from "@models/stories/review"; | ||||
| 
 | ||||
| @ -29,7 +29,7 @@ export default eventHandler(async (ev) => { | ||||
| 		}); | ||||
| 	s2v.reviews += 1; | ||||
| 	await s2v!.save(); | ||||
| 	await Review.findByIdAndRemove(revid); | ||||
| 	await Review.findByIdAndDelete(revid); | ||||
| 	return { | ||||
| 		success: true, | ||||
| 	}; | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| import { messages } from "@server/constants"; | ||||
| import { Review } from "@models/stories/review"; | ||||
| import isIdNan from "@server/middlewareButNotReally/isIdNan"; | ||||
| import { isIdNan } from "@server/middlewareButNotReally"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
| 	const revid = isIdNan(ev); | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| import san from "sanitize-html"; | ||||
| import { messages } from "@server/constants"; | ||||
| import { log } from "@server/logger"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { Review } from "@models/stories/review"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| import san from "sanitize-html"; | ||||
| import { messages } from "@server/constants"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { Story } from "@models/stories"; | ||||
| import { Review } from "@models/stories/review"; | ||||
| 
 | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| import { chapterTransformer } from "@server/dbHelpers"; | ||||
| import { storyQuerier } from "@server/dbHelpers"; | ||||
| import storyCheck from "@server/middlewareButNotReally/storyCheck"; | ||||
| import { storyCheck } from "@server/middlewareButNotReally"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
| 	const story = await storyQuerier(ev); | ||||
|  | ||||
| @ -2,7 +2,7 @@ import { FormChapter } from "@client/types/form/story"; | ||||
| import { countWords } from "@functions"; | ||||
| import { messages } from "@server/constants"; | ||||
| import { storyQuerier } from "@server/dbHelpers"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { canModify } from "@server/middlewareButNotReally/storyPrivileges"; | ||||
| import { replaceOrUploadContent, bodyHandler } from "@server/storyHelpers"; | ||||
| import { Story } from "@models/stories"; | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| import san from "sanitize-html"; | ||||
| import { storyQuerier } from "@server/dbHelpers"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { Story } from "@models/stories"; | ||||
| import { Review } from "@models/stories/review"; | ||||
| 
 | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| import { storyQuerier } from "@server/dbHelpers"; | ||||
| import { chapterTransformer } from "@server/dbHelpers"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { messages } from "@server/constants"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| import { storyQuerier } from "@server/dbHelpers"; | ||||
| import storyCheck from "@server/middlewareButNotReally/storyCheck"; | ||||
| import { storyCheck } from "@server/middlewareButNotReally"; | ||||
| export default eventHandler(async (ev) => { | ||||
| 	const story = await storyQuerier(ev); | ||||
| 	let chrs = await storyCheck(ev, story, 0); | ||||
|  | ||||
| @ -3,7 +3,7 @@ import { Document } from "mongoose"; | ||||
| import { IStory, Story } from "@models/stories"; | ||||
| import { FormStory } from "@client/types/form/story"; | ||||
| import { storyQuerier } from "@server/dbHelpers"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { canModify } from "@server/middlewareButNotReally/storyPrivileges"; | ||||
| import { | ||||
| 	bodyHandler, | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| import { User } from "@models/user"; | ||||
| import isIdNan from "@server/middlewareButNotReally/isIdNan"; | ||||
| import { isIdNan } from "@server/middlewareButNotReally"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
| 	const id = isIdNan(ev); | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| import { Readable } from "stream"; | ||||
| import san from "sanitize-html"; | ||||
| import { FormStory } from "@client/types/form/story"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { getBucket, bodyHandler, modelFormChapter } from "@server/storyHelpers"; | ||||
| import { Story } from "@models/stories"; | ||||
| import { sanitizeConf } from "@server/constants"; | ||||
|  | ||||
| @ -1,7 +1,8 @@ | ||||
| import { v4 } from "uuid"; | ||||
| import { resolve } from "path"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import sharp from "sharp"; | ||||
| import { User } from "@models/user"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
| 	isLoggedIn(ev); | ||||
| @ -21,7 +22,13 @@ export default eventHandler(async (ev) => { | ||||
| 			background: { r: 0, g: 0, b: 0, alpha: 0 }, | ||||
| 		}) | ||||
| 		.toFormat("png") | ||||
| 		.toFile(resolve(`public/${nn}.png`)); | ||||
| 		.toFile(resolve(`public/avatars/${nn}.png`)); | ||||
| 	await User.findByIdAndUpdate(ev.context.currentUser!._id, { | ||||
| 		$set: { | ||||
| 			"profile.avatar": nn, | ||||
| 		}, | ||||
| 	}); | ||||
| 
 | ||||
| 	return { | ||||
| 		success: true, | ||||
| 		file: `${nn}.png`, | ||||
|  | ||||
| @ -3,7 +3,7 @@ import { extname } from "path"; | ||||
| import { v4 } from "uuid"; | ||||
| import { ContentFilenameRegex } from "@server/constants"; | ||||
| import { log } from "@server/logger"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
| 	const noMultipart = "no multipart data found!???!?"; | ||||
|  | ||||
| @ -1,5 +1,4 @@ | ||||
| import isAdmin from "@server/middlewareButNotReally/isAdmin"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isAdmin, isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { User } from "@models/user"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| import isAdmin from "@server/middlewareButNotReally/isAdmin"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isAdmin } from "@server/middlewareButNotReally"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { User } from "@models/user"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
|  | ||||
| @ -1,4 +1,4 @@ | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { User } from "@models/user"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| import { messages } from "@server/constants"; | ||||
| import isAdmin from "@server/middlewareButNotReally/isAdmin"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isAdmin } from "@server/middlewareButNotReally"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { User } from "@models/user"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
|  | ||||
| @ -1,4 +1,4 @@ | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { User } from "@models/user"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| import { FavPayload, SubPayload } from "@client/types/form/favSub"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { User } from "@models/user"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| import { HidePayload } from "@client/types/form/favSub"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { User } from "@models/user"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
|  | ||||
| @ -2,7 +2,7 @@ import san from "sanitize-html"; | ||||
| import { weirdToNormalChars } from "weird-to-normal-chars"; | ||||
| import { Profile, MyStuff } from "@client/types/form/myStuff"; | ||||
| import { apiRoot, messages } from "@server/constants"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { Review } from "@models/stories/review"; | ||||
| import { IUser, User } from "@models/user"; | ||||
| import axios from "axios"; | ||||
|  | ||||
| @ -3,7 +3,7 @@ import axios from "axios"; | ||||
| import { Profile } from "@client/types/form/myStuff"; | ||||
| import { apiRoot, h2m } from "@server/constants"; | ||||
| import forumId from "@server/forumId"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { User } from "@models/user"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
| @ -15,10 +15,9 @@ export default eventHandler(async (ev) => { | ||||
| 		"profile.blog": body.blog, | ||||
| 		"profile.bio": san(body.bio), | ||||
| 		"profile.showEmail": !!body.showEmail, | ||||
| 		"profile.avatar": body.avatar, | ||||
| 	}; | ||||
| 	let d = { | ||||
| 		signature: h2m.turndown(body.signature), | ||||
| 		signature: h2m.turndown(body.signature || ""), | ||||
| 		_uid: 1, | ||||
| 	}; | ||||
| 	let lookup = await forumId(ev.context.currentUser!._id!); | ||||
|  | ||||
| @ -1,4 +1,4 @@ | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { Story } from "@models/stories"; | ||||
| import { Review } from "@models/stories/review"; | ||||
| 
 | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| import { SubPayload } from "@client/types/form/favSub"; | ||||
| import isLoggedIn from "@server/middlewareButNotReally/isLoggedIn"; | ||||
| import { isLoggedIn } from "@server/middlewareButNotReally"; | ||||
| import { User } from "@models/user"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user