akf-forum/models/User.js

50 lines
1.7 KiB
JavaScript
Raw Normal View History

const mongoose = require("mongoose")
2023-03-19 17:14:33 +03:00
const { def_theme, limits, default_user_state } = require("../config.json");
2022-10-09 21:23:31 +03:00
const { userEnum } = require("../lib");
const schema = new mongoose.Schema({
2022-09-17 00:27:38 +03:00
id: { type: String, unique: true },
discordID: { type: String },
2022-09-17 20:25:19 +03:00
name: { type: String, maxlength: limits.names },
2022-09-09 16:29:36 +03:00
avatar: { type: String, default: "/images/avatars/default.jpg" },
time: { type: Date, default: Date.now },
2022-08-29 19:31:59 +03:00
edited: { type: Boolean, default: false },
2022-09-17 20:25:19 +03:00
about: { type: String, default: "", maxlength: limits.desp },
2022-08-27 10:31:16 +03:00
admin: { type: Boolean, default: false },
2022-10-10 00:04:25 +03:00
theme: {
codename: { type: String, default: def_theme.codename },
2022-10-10 00:04:25 +03:00
language: { type: String, default: def_theme.language }
},
2022-09-16 22:26:03 +03:00
lastSeen: { type: Date, default: Date.now, select: false },
hideLastSeen: { type: Boolean, default: false },
ips: { type: [String], default: [], select: false },
password: { type: String, select: false },
discord_code: { type: String, select: false },
2022-10-09 21:23:31 +03:00
state: { type: String, default: default_user_state, enum: userEnum, uppercase: true },
email: { type: String, select: false },
email_code: { type: String, select: false },
});
2022-10-09 21:23:31 +03:00
schema.virtual("deleted").get(function () {
return this.state === "DELETED";
}).set(function (value) {
this.set({ state: value ? "DELETED" : "ACTIVE" });
});
schema.virtual("active").get(function () {
return this.state === "ACTIVE";
})
schema.methods.takeId = async function () {
this.id = String(await model.count() || 0);
return this;
}
schema.methods.getLink = function (id = this.id) {
return "/users/" + id;
}
const model = mongoose.model('user', schema);
2022-10-09 21:23:31 +03:00
model.get = (id, select = "") => model.findOne({ id }, select);
module.exports = model;