From 1447960b1a3ca2959502862add2dc644ad84e80e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Fri, 29 Dec 2023 19:06:55 -0500 Subject: [PATCH] refactor(db/models): update populatedDoc types - remove `& Document` qualifier - cast `T | number` to T with the `as` keyword --- lib/client/middleware.ts | 5 +++-- lib/functions.ts | 11 ++++++----- lib/server/middlewareButNotReally/index.ts | 3 ++- .../middlewareButNotReally/storyPrivileges.ts | 7 ++++--- models/challenges/biffno.ts | 2 +- models/challenges/ficmas.ts | 8 ++++---- models/inbox.ts | 8 ++++---- models/privMsg.ts | 4 ++-- models/stories/chapter.ts | 2 +- models/stories/index.ts | 8 ++++---- models/stories/review.ts | 6 +++--- models/user.ts | 16 ++++++++-------- server/api/review/[id]/index.put.ts | 5 +++-- server/api/review/[id]/reply.post.ts | 7 +++++-- server/api/story/[id]/[chapter]/index.put.ts | 4 ++-- server/api/story/[id]/full.get.ts | 3 ++- server/tsconfig.json | 3 ++- 17 files changed, 56 insertions(+), 46 deletions(-) diff --git a/lib/client/middleware.ts b/lib/client/middleware.ts index de96098..d27ed97 100644 --- a/lib/client/middleware.ts +++ b/lib/client/middleware.ts @@ -2,6 +2,7 @@ import { SingleChapterResult } from "./types/slightlyDifferentStory"; import { IChapter } from "@models/stories/chapter"; import { IStory } from "@models/stories"; import { messages } from "@server/constants"; +import { IUser } from "@models/user"; export const storyMiddleware = defineNuxtRouteMiddleware(async (to, from) => { const { getSession } = useAuth(); @@ -33,8 +34,8 @@ export const storyEditMiddleware = defineNuxtRouteMiddleware( return showError({ statusCode: 404, message: messages[404] }); } if ( - curU.value?.user?._id !== storyInfo.value?.author._id && - curU.value?.user?._id !== storyInfo.value?.coAuthor?._id + curU.value?.user?._id !== (storyInfo.value?.author as IUser)._id && + curU.value?.user?._id !== (storyInfo.value?.coAuthor as IUser)?._id ) { return showError({ statusCode: 403, diff --git a/lib/functions.ts b/lib/functions.ts index 6171873..1462e4b 100644 --- a/lib/functions.ts +++ b/lib/functions.ts @@ -5,8 +5,9 @@ import { resolve } from "path"; import { GridFSBucketReadStream } from "mongodb"; import { stripHtml } from "string-strip-html"; import { IStory } from "@models/stories"; -import { ficsHidden } from "./server/ficmas"; +import { ficsHidden } from "@server/ficmas"; import { PreMiddlewareFunction, Query } from "mongoose"; +import { IFicmas } from "@models/challenges/ficmas"; // const { encode, decode } = iconv; @@ -36,11 +37,11 @@ export function populateSelected( export function isFicmasHidden(story: IStory): boolean { return ( - (story.ficmas.year == new Date().getFullYear() && - story.ficmas.anniversary && + ((story.ficmas as IFicmas)?.year == new Date().getFullYear() && + (story.ficmas as IFicmas)?.anniversary && new Date() < new Date(Date.parse("Aug 1 " + new Date().getFullYear()))) || - (story.ficmas.year == new Date().getFullYear() && - !story.ficmas.anniversary && + ((story.ficmas as IFicmas)?.year == new Date().getFullYear() && + !(story.ficmas as IFicmas)?.anniversary && ficsHidden(Date.now())) ); } diff --git a/lib/server/middlewareButNotReally/index.ts b/lib/server/middlewareButNotReally/index.ts index 417268a..416602b 100644 --- a/lib/server/middlewareButNotReally/index.ts +++ b/lib/server/middlewareButNotReally/index.ts @@ -4,6 +4,7 @@ import { IStory } from "@models/stories"; import { isFicmasHidden } from "@functions"; import { IDraft } from "@models/stories/draft"; import axios from "axios"; +import { IUser } from "@models/user"; export function isIdNan(ev: H3Event) { const id = parseInt(getRouterParam(ev, "id")!); if (Number.isNaN(id)) { @@ -50,7 +51,7 @@ export async function storyCheck( } } else if ( story.chapters[idx]?.hidden && - event.context.currentUser?._id !== story.author._id && + event.context.currentUser?._id !== (story.author as IUser)._id && !event.context.currentUser?.profile.isAdmin ) { ret.statusCode = 403; diff --git a/lib/server/middlewareButNotReally/storyPrivileges.ts b/lib/server/middlewareButNotReally/storyPrivileges.ts index 0f7b3d9..bc5aafc 100644 --- a/lib/server/middlewareButNotReally/storyPrivileges.ts +++ b/lib/server/middlewareButNotReally/storyPrivileges.ts @@ -2,11 +2,12 @@ import type { H3Event, EventHandlerRequest } from "h3"; import { IStory } from "@models/stories"; import { isLoggedIn } from "@server/middlewareButNotReally"; import { IDraft } from "@models/stories/draft"; +import { IUser } from "@models/user"; export function canDelete(event: H3Event, story: IStory) { isLoggedIn(event); return ( event.context.currentUser?.profile.isAdmin || - story.author._id === event.context.currentUser?._id + (story.author as IUser)._id === event.context.currentUser?._id ); } export function canDeleteDraft( @@ -22,7 +23,7 @@ export function canModify( ) { isLoggedIn(event); return ( - event.context.currentUser?._id === story.author._id || - story.coAuthor?._id === event.context.currentUser?._id + event.context.currentUser?._id === (story.author as IUser)._id || + (story.coAuthor as IUser)?._id === event.context.currentUser?._id ); } diff --git a/models/challenges/biffno.ts b/models/challenges/biffno.ts index 18372f9..c587b50 100644 --- a/models/challenges/biffno.ts +++ b/models/challenges/biffno.ts @@ -16,7 +16,7 @@ export interface IBiffno { genre: string; cover: string; year: number; - author: PopulatedDoc; + author: PopulatedDoc; } const biffnoschema = new mongoose.Schema({ diff --git a/models/challenges/ficmas.ts b/models/challenges/ficmas.ts index 10f8a76..fec25f0 100644 --- a/models/challenges/ficmas.ts +++ b/models/challenges/ficmas.ts @@ -16,10 +16,10 @@ const AutoIncrement = SequenceFactory(mongoose); export interface IFicmas { _id: number; kink: string; - year: string; - bands: PopulatedDoc[]; + year: number; + bands: PopulatedDoc[]; relationship: string; - wisher: PopulatedDoc; + wisher: PopulatedDoc; anniversary: boolean; } @@ -30,7 +30,7 @@ export const FicmasSchema = new mongoose.Schema({ kink: { type: String, }, - year: { type: String }, + year: { type: Number }, bands: [ { type: Number, diff --git a/models/inbox.ts b/models/inbox.ts index ed46ea6..9bcd3db 100644 --- a/models/inbox.ts +++ b/models/inbox.ts @@ -10,10 +10,10 @@ import SequenceFactory from "mongoose-sequence"; import { IPrivMsg } from "./privMsg"; import { IUser } from "./user"; export interface IInbox { - owningUser: PopulatedDoc; - saved: PopulatedDoc[]; - received: PopulatedDoc[]; - sent: PopulatedDoc[]; + owningUser: PopulatedDoc; + saved: PopulatedDoc[]; + received: PopulatedDoc[]; + sent: PopulatedDoc[]; } const InboxSchema = new Schema({ diff --git a/models/privMsg.ts b/models/privMsg.ts index 025937b..d363fdb 100644 --- a/models/privMsg.ts +++ b/models/privMsg.ts @@ -7,8 +7,8 @@ const AutoIncrement = SequenceFactory(mongoose); export interface IPrivMsg { _id: number; - from: PopulatedDoc; - to: PopulatedDoc; + from: PopulatedDoc; + to: PopulatedDoc; subject: string; content: string; sentAt: Date; diff --git a/models/stories/chapter.ts b/models/stories/chapter.ts index a130d06..c265293 100644 --- a/models/stories/chapter.ts +++ b/models/stories/chapter.ts @@ -8,7 +8,7 @@ export interface IChapter { words?: number; notes: string; genre: string[]; - bands: PopulatedDoc[]; + bands: PopulatedDoc[]; characters: string[]; relationships: string[][]; nsfw: boolean; diff --git a/models/stories/index.ts b/models/stories/index.ts index 222f400..cd91753 100644 --- a/models/stories/index.ts +++ b/models/stories/index.ts @@ -12,19 +12,19 @@ import { hasMigrated } from "@dbconfig"; export interface IStory { _id?: number; title: string; - author: PopulatedDoc; + author: PopulatedDoc; chapters: IChapter[]; recs: number; favs: number; reviews: number; views: number; completed: boolean; - challenge: PopulatedDoc | null; - ficmas: PopulatedDoc | null; + challenge: PopulatedDoc | null; + ficmas: PopulatedDoc | null; downloads: number; lastUpdate: Date; posted: Date; - coAuthor: PopulatedDoc | null; + coAuthor: PopulatedDoc | null; } const StorySchema = new mongoose.Schema({ diff --git a/models/stories/review.ts b/models/stories/review.ts index 8d0cc5a..c20cf1f 100644 --- a/models/stories/review.ts +++ b/models/stories/review.ts @@ -11,10 +11,10 @@ export interface IReview { text: string; leftOn: number; whichChapter: number; - author: PopulatedDoc; + author: PopulatedDoc; datePosted: Date; - replyingTo: PopulatedDoc | null; - replies: PopulatedDoc[]; + replyingTo: PopulatedDoc | null; + replies: PopulatedDoc[]; } const CommentSchema = new mongoose.Schema({ _id: { diff --git a/models/user.ts b/models/user.ts index b53c548..62467b2 100644 --- a/models/user.ts +++ b/models/user.ts @@ -57,19 +57,19 @@ export interface IUser extends Document { wins: number; }; favs: { - authors: PopulatedDoc[]; - stories: PopulatedDoc[]; + authors: PopulatedDoc[]; + stories: PopulatedDoc[]; }; subscriptions: { - authors: PopulatedDoc[]; - bands: PopulatedDoc[]; - stories: PopulatedDoc[]; + authors: PopulatedDoc[]; + bands: PopulatedDoc[]; + stories: PopulatedDoc[]; }; //@ts-ignore SHUT UP - hiddenAuthors: PopulatedDoc[]; - hiddenBands: PopulatedDoc[]; + hiddenAuthors: PopulatedDoc[]; + hiddenBands: PopulatedDoc[]; //@ts-ignore SHUT UP - blocked: PopulatedDoc[]; + blocked: PopulatedDoc[]; sessionId: string | null; banned: boolean; quickMenuConfig: QuickMenuItem[]; diff --git a/server/api/review/[id]/index.put.ts b/server/api/review/[id]/index.put.ts index f3ccc58..f3d3905 100644 --- a/server/api/review/[id]/index.put.ts +++ b/server/api/review/[id]/index.put.ts @@ -3,6 +3,7 @@ import { messages } from "@server/constants"; import { log } from "@server/logger"; import { isLoggedIn } from "@server/middlewareButNotReally"; import { Review } from "@models/stories/review"; +import { IUser } from "@models/user"; export default eventHandler(async (ev) => { isLoggedIn(ev); @@ -17,7 +18,7 @@ export default eventHandler(async (ev) => { log.silly(`${ev.context.currentUser!._id!} || ${c.author}`, { label: "what the fuck", }); - if (c?.author._id != ev.context.currentUser?._id) { + if ((c?.author as IUser)?._id != ev.context.currentUser?._id) { throw createError({ message: messages[403], statusCode: 403, @@ -32,7 +33,7 @@ export default eventHandler(async (ev) => { return { success: true, data: await Review.findById(revid) - .populate("author", "username _id") + .populate("author", "username profile _id") .exec(), }; }); diff --git a/server/api/review/[id]/reply.post.ts b/server/api/review/[id]/reply.post.ts index cbea629..3a3babf 100644 --- a/server/api/review/[id]/reply.post.ts +++ b/server/api/review/[id]/reply.post.ts @@ -3,6 +3,7 @@ import { messages } from "@server/constants"; import { isLoggedIn } from "@server/middlewareButNotReally"; import { Story } from "@models/stories"; import { Review } from "@models/stories/review"; +import { IUser } from "@models/user"; export default eventHandler(async (ev) => { isLoggedIn(ev); @@ -19,8 +20,10 @@ export default eventHandler(async (ev) => { }); } if ( - replyingTo?.author.blocked.includes(ev.context.currentUser!._id) || - ev.context.currentUser!.blocked.includes(replyingTo?.author._id) + (replyingTo?.author as IUser).blocked.includes( + ev.context.currentUser!._id, + ) || + ev.context.currentUser!.blocked.includes((replyingTo?.author as IUser)._id) ) { throw createError({ statusCode: 403, diff --git a/server/api/story/[id]/[chapter]/index.put.ts b/server/api/story/[id]/[chapter]/index.put.ts index ea49006..2709ec7 100644 --- a/server/api/story/[id]/[chapter]/index.put.ts +++ b/server/api/story/[id]/[chapter]/index.put.ts @@ -34,11 +34,11 @@ export default eventHandler(async (ev) => { "chapters.$.characters": cc.characters, "chapters.$.relationships": Array.from(new Set(cc.relationships)), "chapters.$.bands": cc.bands, - "chapters.$.nsfw": !!cc.nsfw, + "chapters.$.nsfw": cc.nsfw, "chapters.$.notes": cc.notes, "chapters.$.words": countWords(content), "chapters.$.genre": cc.genre, - "chapters.$.loggedInOnly": !!cc.loggedInOnly, + "chapters.$.loggedInOnly": cc.loggedInOnly, }, }, { new: true }, diff --git a/server/api/story/[id]/full.get.ts b/server/api/story/[id]/full.get.ts index b205aa4..a13d499 100644 --- a/server/api/story/[id]/full.get.ts +++ b/server/api/story/[id]/full.get.ts @@ -2,6 +2,7 @@ import { storyQuerier } from "@server/dbHelpers"; import { chapterTransformer } from "@server/dbHelpers"; import { isLoggedIn } from "@server/middlewareButNotReally"; import { messages } from "@server/constants"; +import { IUser } from "@models/user"; export default eventHandler(async (ev) => { isLoggedIn(ev); @@ -9,7 +10,7 @@ export default eventHandler(async (ev) => { const hidden = s.chapters.some((a) => a.hidden); if ( hidden && - ev.context.currentUser?._id !== s.author._id && + ev.context.currentUser?._id !== (s.author as IUser)._id && !ev.context.currentUser?.profile.isAdmin ) { throw createError({ diff --git a/server/tsconfig.json b/server/tsconfig.json index 78394c7..65dce3f 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -2,6 +2,7 @@ "extends": "../.nuxt/tsconfig.server.json", "compilerOptions": { "allowJs": true, - "outDir": "../out" + "outDir": "../out", + "noImplicitAny": false } }