refactor(db/models): update populatedDoc types

- remove `& Document` qualifier
- cast `T | number` to T with the `as` keyword
This commit is contained in:
parent 02ed969132
commit 1447960b1a
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
17 changed files with 56 additions and 46 deletions

@ -2,6 +2,7 @@ import { SingleChapterResult } from "./types/slightlyDifferentStory";
import { IChapter } from "@models/stories/chapter"; import { IChapter } from "@models/stories/chapter";
import { IStory } from "@models/stories"; import { IStory } from "@models/stories";
import { messages } from "@server/constants"; import { messages } from "@server/constants";
import { IUser } from "@models/user";
export const storyMiddleware = defineNuxtRouteMiddleware(async (to, from) => { export const storyMiddleware = defineNuxtRouteMiddleware(async (to, from) => {
const { getSession } = useAuth(); const { getSession } = useAuth();
@ -33,8 +34,8 @@ export const storyEditMiddleware = defineNuxtRouteMiddleware(
return showError({ statusCode: 404, message: messages[404] }); return showError({ statusCode: 404, message: messages[404] });
} }
if ( if (
curU.value?.user?._id !== storyInfo.value?.author._id && curU.value?.user?._id !== (storyInfo.value?.author as IUser)._id &&
curU.value?.user?._id !== storyInfo.value?.coAuthor?._id curU.value?.user?._id !== (storyInfo.value?.coAuthor as IUser)?._id
) { ) {
return showError({ return showError({
statusCode: 403, statusCode: 403,

@ -5,8 +5,9 @@ import { resolve } from "path";
import { GridFSBucketReadStream } from "mongodb"; import { GridFSBucketReadStream } from "mongodb";
import { stripHtml } from "string-strip-html"; import { stripHtml } from "string-strip-html";
import { IStory } from "@models/stories"; import { IStory } from "@models/stories";
import { ficsHidden } from "./server/ficmas"; import { ficsHidden } from "@server/ficmas";
import { PreMiddlewareFunction, Query } from "mongoose"; import { PreMiddlewareFunction, Query } from "mongoose";
import { IFicmas } from "@models/challenges/ficmas";
// const { encode, decode } = iconv; // const { encode, decode } = iconv;
@ -36,11 +37,11 @@ export function populateSelected<T>(
export function isFicmasHidden(story: IStory): boolean { export function isFicmasHidden(story: IStory): boolean {
return ( return (
(story.ficmas.year == new Date().getFullYear() && ((story.ficmas as IFicmas)?.year == new Date().getFullYear() &&
story.ficmas.anniversary && (story.ficmas as IFicmas)?.anniversary &&
new Date() < new Date(Date.parse("Aug 1 " + new Date().getFullYear()))) || new Date() < new Date(Date.parse("Aug 1 " + new Date().getFullYear()))) ||
(story.ficmas.year == new Date().getFullYear() && ((story.ficmas as IFicmas)?.year == new Date().getFullYear() &&
!story.ficmas.anniversary && !(story.ficmas as IFicmas)?.anniversary &&
ficsHidden(Date.now())) ficsHidden(Date.now()))
); );
} }

@ -4,6 +4,7 @@ import { IStory } from "@models/stories";
import { isFicmasHidden } from "@functions"; import { isFicmasHidden } from "@functions";
import { IDraft } from "@models/stories/draft"; import { IDraft } from "@models/stories/draft";
import axios from "axios"; import axios from "axios";
import { IUser } from "@models/user";
export function isIdNan(ev: H3Event<EventHandlerRequest>) { export function isIdNan(ev: H3Event<EventHandlerRequest>) {
const id = parseInt(getRouterParam(ev, "id")!); const id = parseInt(getRouterParam(ev, "id")!);
if (Number.isNaN(id)) { if (Number.isNaN(id)) {
@ -50,7 +51,7 @@ export async function storyCheck(
} }
} else if ( } else if (
story.chapters[idx]?.hidden && 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 !event.context.currentUser?.profile.isAdmin
) { ) {
ret.statusCode = 403; ret.statusCode = 403;

@ -2,11 +2,12 @@ import type { H3Event, EventHandlerRequest } from "h3";
import { IStory } from "@models/stories"; import { IStory } from "@models/stories";
import { isLoggedIn } from "@server/middlewareButNotReally"; import { isLoggedIn } from "@server/middlewareButNotReally";
import { IDraft } from "@models/stories/draft"; import { IDraft } from "@models/stories/draft";
import { IUser } from "@models/user";
export function canDelete(event: H3Event<EventHandlerRequest>, story: IStory) { export function canDelete(event: H3Event<EventHandlerRequest>, story: IStory) {
isLoggedIn(event); isLoggedIn(event);
return ( return (
event.context.currentUser?.profile.isAdmin || event.context.currentUser?.profile.isAdmin ||
story.author._id === event.context.currentUser?._id (story.author as IUser)._id === event.context.currentUser?._id
); );
} }
export function canDeleteDraft( export function canDeleteDraft(
@ -22,7 +23,7 @@ export function canModify(
) { ) {
isLoggedIn(event); isLoggedIn(event);
return ( return (
event.context.currentUser?._id === story.author._id || event.context.currentUser?._id === (story.author as IUser)._id ||
story.coAuthor?._id === event.context.currentUser?._id (story.coAuthor as IUser)?._id === event.context.currentUser?._id
); );
} }

@ -16,7 +16,7 @@ export interface IBiffno {
genre: string; genre: string;
cover: string; cover: string;
year: number; year: number;
author: PopulatedDoc<IUser & Document>; author: PopulatedDoc<IUser>;
} }
const biffnoschema = new mongoose.Schema<IBiffno>({ const biffnoschema = new mongoose.Schema<IBiffno>({

@ -16,10 +16,10 @@ const AutoIncrement = SequenceFactory(mongoose);
export interface IFicmas { export interface IFicmas {
_id: number; _id: number;
kink: string; kink: string;
year: string; year: number;
bands: PopulatedDoc<IBand & Document>[]; bands: PopulatedDoc<IBand>[];
relationship: string; relationship: string;
wisher: PopulatedDoc<IUser & Document>; wisher: PopulatedDoc<IUser>;
anniversary: boolean; anniversary: boolean;
} }
@ -30,7 +30,7 @@ export const FicmasSchema = new mongoose.Schema<IFicmas>({
kink: { kink: {
type: String, type: String,
}, },
year: { type: String }, year: { type: Number },
bands: [ bands: [
{ {
type: Number, type: Number,

@ -10,10 +10,10 @@ import SequenceFactory from "mongoose-sequence";
import { IPrivMsg } from "./privMsg"; import { IPrivMsg } from "./privMsg";
import { IUser } from "./user"; import { IUser } from "./user";
export interface IInbox { export interface IInbox {
owningUser: PopulatedDoc<IUser & Document>; owningUser: PopulatedDoc<IUser>;
saved: PopulatedDoc<IPrivMsg & Document>[]; saved: PopulatedDoc<IPrivMsg>[];
received: PopulatedDoc<IPrivMsg & Document>[]; received: PopulatedDoc<IPrivMsg>[];
sent: PopulatedDoc<IPrivMsg & Document>[]; sent: PopulatedDoc<IPrivMsg>[];
} }
const InboxSchema = new Schema<IInbox>({ const InboxSchema = new Schema<IInbox>({

@ -7,8 +7,8 @@ const AutoIncrement = SequenceFactory(mongoose);
export interface IPrivMsg { export interface IPrivMsg {
_id: number; _id: number;
from: PopulatedDoc<IUser & Document>; from: PopulatedDoc<IUser>;
to: PopulatedDoc<IUser & Document>; to: PopulatedDoc<IUser>;
subject: string; subject: string;
content: string; content: string;
sentAt: Date; sentAt: Date;

@ -8,7 +8,7 @@ export interface IChapter {
words?: number; words?: number;
notes: string; notes: string;
genre: string[]; genre: string[];
bands: PopulatedDoc<IBand & Document>[]; bands: PopulatedDoc<IBand>[];
characters: string[]; characters: string[];
relationships: string[][]; relationships: string[][];
nsfw: boolean; nsfw: boolean;

@ -12,19 +12,19 @@ import { hasMigrated } from "@dbconfig";
export interface IStory { export interface IStory {
_id?: number; _id?: number;
title: string; title: string;
author: PopulatedDoc<IUser & Document>; author: PopulatedDoc<IUser>;
chapters: IChapter[]; chapters: IChapter[];
recs: number; recs: number;
favs: number; favs: number;
reviews: number; reviews: number;
views: number; views: number;
completed: boolean; completed: boolean;
challenge: PopulatedDoc<IChallenge & Document> | null; challenge: PopulatedDoc<IChallenge> | null;
ficmas: PopulatedDoc<IFicmas & Document> | null; ficmas: PopulatedDoc<IFicmas> | null;
downloads: number; downloads: number;
lastUpdate: Date; lastUpdate: Date;
posted: Date; posted: Date;
coAuthor: PopulatedDoc<IUser & Document> | null; coAuthor: PopulatedDoc<IUser> | null;
} }
const StorySchema = new mongoose.Schema<IStory>({ const StorySchema = new mongoose.Schema<IStory>({

@ -11,10 +11,10 @@ export interface IReview {
text: string; text: string;
leftOn: number; leftOn: number;
whichChapter: number; whichChapter: number;
author: PopulatedDoc<IUser & Document>; author: PopulatedDoc<IUser>;
datePosted: Date; datePosted: Date;
replyingTo: PopulatedDoc<IReview & Document> | null; replyingTo: PopulatedDoc<IReview> | null;
replies: PopulatedDoc<IReview & Document>[]; replies: PopulatedDoc<IReview>[];
} }
const CommentSchema = new mongoose.Schema<IReview>({ const CommentSchema = new mongoose.Schema<IReview>({
_id: { _id: {

@ -57,19 +57,19 @@ export interface IUser extends Document {
wins: number; wins: number;
}; };
favs: { favs: {
authors: PopulatedDoc<IUser & Document>[]; authors: PopulatedDoc<IUser>[];
stories: PopulatedDoc<IStory & Document>[]; stories: PopulatedDoc<IStory>[];
}; };
subscriptions: { subscriptions: {
authors: PopulatedDoc<IUser & Document>[]; authors: PopulatedDoc<IUser>[];
bands: PopulatedDoc<IBand & Document>[]; bands: PopulatedDoc<IBand>[];
stories: PopulatedDoc<IStory & Document>[]; stories: PopulatedDoc<IStory>[];
}; };
//@ts-ignore SHUT UP //@ts-ignore SHUT UP
hiddenAuthors: PopulatedDoc<IUser & Document>[]; hiddenAuthors: PopulatedDoc<IUser>[];
hiddenBands: PopulatedDoc<IBand & Document>[]; hiddenBands: PopulatedDoc<IBand>[];
//@ts-ignore SHUT UP //@ts-ignore SHUT UP
blocked: PopulatedDoc<IUser & Document>[]; blocked: PopulatedDoc<IUser>[];
sessionId: string | null; sessionId: string | null;
banned: boolean; banned: boolean;
quickMenuConfig: QuickMenuItem[]; quickMenuConfig: QuickMenuItem[];

@ -3,6 +3,7 @@ import { messages } from "@server/constants";
import { log } from "@server/logger"; import { log } from "@server/logger";
import { isLoggedIn } from "@server/middlewareButNotReally"; import { isLoggedIn } from "@server/middlewareButNotReally";
import { Review } from "@models/stories/review"; import { Review } from "@models/stories/review";
import { IUser } from "@models/user";
export default eventHandler(async (ev) => { export default eventHandler(async (ev) => {
isLoggedIn(ev); isLoggedIn(ev);
@ -17,7 +18,7 @@ export default eventHandler(async (ev) => {
log.silly(`${ev.context.currentUser!._id!} || ${c.author}`, { log.silly(`${ev.context.currentUser!._id!} || ${c.author}`, {
label: "what the fuck", label: "what the fuck",
}); });
if (c?.author._id != ev.context.currentUser?._id) { if ((c?.author as IUser)?._id != ev.context.currentUser?._id) {
throw createError({ throw createError({
message: messages[403], message: messages[403],
statusCode: 403, statusCode: 403,
@ -32,7 +33,7 @@ export default eventHandler(async (ev) => {
return { return {
success: true, success: true,
data: await Review.findById(revid) data: await Review.findById(revid)
.populate("author", "username _id") .populate("author", "username profile _id")
.exec(), .exec(),
}; };
}); });

@ -3,6 +3,7 @@ import { messages } from "@server/constants";
import { isLoggedIn } from "@server/middlewareButNotReally"; import { isLoggedIn } from "@server/middlewareButNotReally";
import { Story } from "@models/stories"; import { Story } from "@models/stories";
import { Review } from "@models/stories/review"; import { Review } from "@models/stories/review";
import { IUser } from "@models/user";
export default eventHandler(async (ev) => { export default eventHandler(async (ev) => {
isLoggedIn(ev); isLoggedIn(ev);
@ -19,8 +20,10 @@ export default eventHandler(async (ev) => {
}); });
} }
if ( if (
replyingTo?.author.blocked.includes(ev.context.currentUser!._id) || (replyingTo?.author as IUser).blocked.includes(
ev.context.currentUser!.blocked.includes(replyingTo?.author._id) ev.context.currentUser!._id,
) ||
ev.context.currentUser!.blocked.includes((replyingTo?.author as IUser)._id)
) { ) {
throw createError({ throw createError({
statusCode: 403, statusCode: 403,

@ -34,11 +34,11 @@ export default eventHandler(async (ev) => {
"chapters.$.characters": cc.characters, "chapters.$.characters": cc.characters,
"chapters.$.relationships": Array.from(new Set(cc.relationships)), "chapters.$.relationships": Array.from(new Set(cc.relationships)),
"chapters.$.bands": cc.bands, "chapters.$.bands": cc.bands,
"chapters.$.nsfw": !!cc.nsfw, "chapters.$.nsfw": cc.nsfw,
"chapters.$.notes": cc.notes, "chapters.$.notes": cc.notes,
"chapters.$.words": countWords(content), "chapters.$.words": countWords(content),
"chapters.$.genre": cc.genre, "chapters.$.genre": cc.genre,
"chapters.$.loggedInOnly": !!cc.loggedInOnly, "chapters.$.loggedInOnly": cc.loggedInOnly,
}, },
}, },
{ new: true }, { new: true },

@ -2,6 +2,7 @@ import { storyQuerier } from "@server/dbHelpers";
import { chapterTransformer } from "@server/dbHelpers"; import { chapterTransformer } from "@server/dbHelpers";
import { isLoggedIn } from "@server/middlewareButNotReally"; import { isLoggedIn } from "@server/middlewareButNotReally";
import { messages } from "@server/constants"; import { messages } from "@server/constants";
import { IUser } from "@models/user";
export default eventHandler(async (ev) => { export default eventHandler(async (ev) => {
isLoggedIn(ev); isLoggedIn(ev);
@ -9,7 +10,7 @@ export default eventHandler(async (ev) => {
const hidden = s.chapters.some((a) => a.hidden); const hidden = s.chapters.some((a) => a.hidden);
if ( if (
hidden && hidden &&
ev.context.currentUser?._id !== s.author._id && ev.context.currentUser?._id !== (s.author as IUser)._id &&
!ev.context.currentUser?.profile.isAdmin !ev.context.currentUser?.profile.isAdmin
) { ) {
throw createError({ throw createError({

@ -2,6 +2,7 @@
"extends": "../.nuxt/tsconfig.server.json", "extends": "../.nuxt/tsconfig.server.json",
"compilerOptions": { "compilerOptions": {
"allowJs": true, "allowJs": true,
"outDir": "../out" "outDir": "../out",
"noImplicitAny": false
} }
} }