refactor(db/models): add notifyOnReviewReply flag to user schema

This commit is contained in:
parent 2c282b6bcd
commit 6598dfd3d6
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

@ -1,11 +1,17 @@
import mongoose, { Schema, connect, PopulatedDoc, Document, Model } from "mongoose"; import mongoose, {
Schema,
connect,
PopulatedDoc,
Document,
Model,
} from "mongoose";
import SequenceFactory from "mongoose-sequence"; import SequenceFactory from "mongoose-sequence";
import bcrypt from "bcryptjs"; import bcrypt from "bcryptjs";
import md5 from "blueimp-md5"; import md5 from "blueimp-md5";
import jwt from "jsonwebtoken" import jwt from "jsonwebtoken";
import { hasMigrated } from "../lib/dbconfig"; import { hasMigrated } from "~/lib/dbconfig";
import { IBand } from "./band"; import { IBand } from "./band";
import {IStory} from "./stories/index" import { IStory } from "./stories/index";
import { QuickMenuItem, QuickMenuSchema } from "./quickMenu"; import { QuickMenuItem, QuickMenuSchema } from "./quickMenu";
const AutoIncrement = SequenceFactory(mongoose); const AutoIncrement = SequenceFactory(mongoose);
@ -23,11 +29,11 @@ export interface IUser extends Document {
emailVerified: boolean; emailVerified: boolean;
activationKey: string | null; activationKey: string | null;
passwordResetToken: string | null; passwordResetToken: string | null;
} };
ts: { ts: {
created: Date; created: Date;
updated: Date; updated: Date;
} };
ipLog: IIPLogEntry[]; ipLog: IIPLogEntry[];
lastLogin: Date; lastLogin: Date;
lastVisit: Date; lastVisit: Date;
@ -45,20 +51,20 @@ export interface IUser extends Document {
hidden: boolean; hidden: boolean;
disclaimer: string; disclaimer: string;
showEmail: boolean; showEmail: boolean;
} };
biffno: { biffno: {
years: string[]; years: string[];
wins: number; wins: number;
} };
favs: { favs: {
authors: PopulatedDoc<IUser & Document>[]; authors: PopulatedDoc<IUser & Document>[];
stories: PopulatedDoc<IStory & Document>[]; stories: PopulatedDoc<IStory & Document>[];
} };
subscriptions: { subscriptions: {
authors: PopulatedDoc<IUser & Document>[]; authors: PopulatedDoc<IUser & Document>[];
bands: PopulatedDoc<IBand & Document>[]; bands: PopulatedDoc<IBand & Document>[];
stories: PopulatedDoc<IStory & Document>[]; stories: PopulatedDoc<IStory & Document>[];
} };
//@ts-ignore SHUT UP //@ts-ignore SHUT UP
hiddenAuthors: PopulatedDoc<IUser & Document>[]; hiddenAuthors: PopulatedDoc<IUser & Document>[];
hiddenBands: PopulatedDoc<IBand & Document>[]; hiddenBands: PopulatedDoc<IBand & Document>[];
@ -66,7 +72,8 @@ export interface IUser extends Document {
blocked: PopulatedDoc<IUser & Document>[]; blocked: PopulatedDoc<IUser & Document>[];
sessionId: string | null; sessionId: string | null;
banned: boolean; banned: boolean;
quickMenuConfig: QuickMenuItem[] quickMenuConfig: QuickMenuItem[];
notifyOnReviewReply?: boolean;
validPassword(password: string): boolean; validPassword(password: string): boolean;
generateToken(jwtSecret: string): string; generateToken(jwtSecret: string): string;
} }
@ -75,183 +82,216 @@ interface UModel extends Model<IUser> {
generateHash(pwd: string): string; generateHash(pwd: string): string;
} }
const UserSchema = new mongoose.Schema<IUser, UModel>({ const UserSchema = new mongoose.Schema<IUser>({
_id: { _id: {
type: Number type: Number,
}, },
username: { username: {
type: String type: String,
}, },
email: { email: {
type: String type: String,
}, },
password: { password: {
type: String type: String,
}, },
auth: { auth: {
emailVerified: { emailVerified: {
type: Boolean, type: Boolean,
default: false default: false,
}, },
activationKey: { activationKey: {
type: String type: String,
}, },
passwordResetToken: { passwordResetToken: {
type: String, type: String,
default: null default: null,
} },
}, },
ts: { ts: {
created: { created: {
type: Date, type: Date,
default: new Date() default: new Date(),
}, },
updated: { updated: {
type: Date, type: Date,
default: new Date() default: new Date(),
}
}, },
ipLog: [{ },
ipLog: [
{
ip: { ip: {
type: String type: String,
}, },
lastAccess: { lastAccess: {
type: Date, type: Date,
default: new Date() default: new Date(),
} },
}], },
],
lastLogin: { lastLogin: {
type: Date, type: Date,
default: null default: null,
}, },
lastVisit: { lastVisit: {
type: Date, type: Date,
default: null default: null,
}, },
profile: { profile: {
avatar: { avatar: {
type: String, type: String,
default: "" default: "",
}, },
isAdmin: { isAdmin: {
type: Boolean, type: Boolean,
default: false default: false,
}, },
nightMode: { nightMode: {
type: Boolean, type: Boolean,
default: false default: false,
}, },
bio: { bio: {
type: String, type: String,
default: "" default: "",
}, },
location: { location: {
type: String, type: String,
default: "" default: "",
}, },
occupation: { occupation: {
type: String, type: String,
default: "" default: "",
}, },
website: { website: {
type: String, type: String,
default: "" default: "",
}, },
blog: { blog: {
type: String, type: String,
default: "" default: "",
}, },
views: { views: {
type: Number, type: Number,
min: 0, min: 0,
default: 0 default: 0,
}, },
lastWhere: { lastWhere: {
type: String, type: String,
default: null default: null,
}, },
hidden: { hidden: {
type: Boolean, type: Boolean,
default: false default: false,
}, },
disclaimer: { disclaimer: {
type: String, type: String,
default: "" default: "",
}, },
showEmail: { showEmail: {
type: Boolean, type: Boolean,
default: false default: false,
} },
}, },
biffno: { biffno: {
years: [{ years: [
type: String {
}], type: String,
},
],
wins: { wins: {
type: Number, type: Number,
default: 0 default: 0,
} },
}, },
favs: { favs: {
authors: [{ authors: [
{
type: Number, type: Number,
ref: "User" ref: "User",
}], },
stories: [{ ],
stories: [
{
type: Number, type: Number,
ref: "Story" ref: "Story",
}] },
],
}, },
subscriptions: { subscriptions: {
authors: [{ authors: [
{
type: Number, type: Number,
ref: "User" ref: "User",
}],
stories: [{
type: Number,
ref: "Story"
}],
bands: [{
type: Number,
ref: "Band"
}]
}, },
hiddenBands: [{ ],
stories: [
{
type: Number, type: Number,
ref: "Band" ref: "Story",
}], },
hiddenAuthors: [{ ],
bands: [
{
type: Number, type: Number,
ref: "User" ref: "Band",
}], },
blocked: [{ ],
},
hiddenBands: [
{
type: Number, type: Number,
ref: "User" ref: "Band",
}], },
],
hiddenAuthors: [
{
type: Number,
ref: "User",
},
],
notifyOnReviewReply: {
type: Boolean,
default: true,
},
blocked: [
{
type: Number,
ref: "User",
},
],
sessionId: { sessionId: {
type: String, type: String,
default: null default: null,
}, },
banned: { banned: {
type: Boolean, type: Boolean,
default: false default: false,
}, },
quickMenuConfig: [QuickMenuSchema] quickMenuConfig: [QuickMenuSchema],
}) });
UserSchema.static("generateHash", function(password: string): string { UserSchema.static("generateHash", function (password: string): string {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8)); return bcrypt.hashSync(password, bcrypt.genSaltSync(8));
}); });
UserSchema.methods.validPassword = function(password: string): boolean { UserSchema.methods.validPassword = function (password: string): boolean {
return md5(password) === this.password || bcrypt.compareSync(password, this.password) || false; return (
} md5(password) === this.password ||
bcrypt.compareSync(password, this.password) ||
false
);
};
UserSchema.methods.generateToken = function(jwtKey: string): string { UserSchema.methods.generateToken = function (jwtKey: string): string {
let token = jwt.sign({id: this._id, isAdmin: this.profile.isAdmin}, jwtKey, { let token = jwt.sign(
expiresIn: '14 days' { id: this._id, isAdmin: this.profile.isAdmin },
}) jwtKey,
return token {
} expiresIn: "14 days",
},
);
return token;
};
hasMigrated && UserSchema.plugin(AutoIncrement, {id: "userid"}) hasMigrated &&
export const User = mongoose.model<IUser, UModel>("User", UserSchema, "users") UserSchema.plugin(AutoIncrement, { id: "userid", inc_field: "_id" });
export const User = mongoose.model<IUser, UModel>("User", UserSchema, "users");