2023-10-03 01:22:43 -04:00
|
|
|
import mongoose, { Schema, PopulatedDoc, Document, Model } from "mongoose";
|
2023-09-25 19:41:32 -04:00
|
|
|
import SequenceFactory from "mongoose-sequence";
|
2023-10-03 01:22:43 -04:00
|
|
|
import { hasMigrated } from "~/lib/dbconfig";
|
2023-09-25 19:41:32 -04:00
|
|
|
import { IUser } from "./user";
|
|
|
|
|
|
|
|
const AutoIncrement = SequenceFactory(mongoose);
|
|
|
|
|
|
|
|
export interface IPrivMsg {
|
|
|
|
_id: number;
|
|
|
|
from: PopulatedDoc<IUser & Document>;
|
|
|
|
to: PopulatedDoc<IUser & Document>;
|
|
|
|
subject: string;
|
|
|
|
content: string;
|
|
|
|
sentAt: Date;
|
|
|
|
read: boolean;
|
|
|
|
deletedBy: {
|
|
|
|
sender: boolean;
|
|
|
|
recipient: boolean;
|
2023-10-03 01:22:43 -04:00
|
|
|
};
|
2023-09-25 19:41:32 -04:00
|
|
|
}
|
|
|
|
const PMSchema = new mongoose.Schema<IPrivMsg>({
|
2023-10-03 01:22:43 -04:00
|
|
|
_id: { type: Number },
|
2023-09-25 19:41:32 -04:00
|
|
|
from: {
|
|
|
|
type: Number,
|
2023-10-03 01:22:43 -04:00
|
|
|
ref: "User",
|
2023-09-25 19:41:32 -04:00
|
|
|
},
|
|
|
|
to: {
|
|
|
|
type: Number,
|
2023-10-03 01:22:43 -04:00
|
|
|
ref: "User",
|
2023-09-25 19:41:32 -04:00
|
|
|
},
|
|
|
|
subject: { type: String },
|
|
|
|
content: { type: String },
|
|
|
|
sentAt: {
|
|
|
|
type: Date,
|
2023-10-03 01:22:43 -04:00
|
|
|
default: new Date(),
|
2023-09-25 19:41:32 -04:00
|
|
|
},
|
|
|
|
read: {
|
|
|
|
type: Boolean,
|
2023-10-03 01:22:43 -04:00
|
|
|
default: false,
|
2023-09-25 19:41:32 -04:00
|
|
|
},
|
|
|
|
deletedBy: {
|
|
|
|
sender: {
|
|
|
|
type: Boolean,
|
2023-10-03 01:22:43 -04:00
|
|
|
default: false,
|
2023-09-25 19:41:32 -04:00
|
|
|
},
|
|
|
|
recipient: {
|
|
|
|
type: Boolean,
|
2023-10-03 01:22:43 -04:00
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-09-25 19:41:32 -04:00
|
|
|
|
2023-10-12 22:28:37 -04:00
|
|
|
hasMigrated &&
|
|
|
|
!mongoose.models.PrivMsg &&
|
|
|
|
PMSchema.plugin(AutoIncrement, { id: "private_message" });
|
2023-09-25 19:41:32 -04:00
|
|
|
|
2023-10-03 01:22:43 -04:00
|
|
|
export const PrivMsg: Model<IPrivMsg> =
|
|
|
|
/* mongoose.models.PrivMsg || */ mongoose.model(
|
|
|
|
"PrivMsg",
|
|
|
|
PMSchema,
|
|
|
|
"private_messages",
|
|
|
|
);
|