mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-26 13:20:41 +03:00
75 lines
No EOL
1.8 KiB
JavaScript
75 lines
No EOL
1.8 KiB
JavaScript
const { UserModel } = require("../models");
|
|
|
|
module.exports = class User {
|
|
|
|
|
|
constructor(name = "guest", avatar = "/images/guest.png", time = Date.now(), admin = false, deleted = false) {
|
|
|
|
this.name = name;
|
|
this.avatar = avatar;
|
|
this.time = time;
|
|
this.admin = admin;
|
|
this.deleted = deleted;
|
|
|
|
}
|
|
|
|
async getById(id = this.id) {
|
|
try {
|
|
this.id = Number(id);
|
|
const user = await UserModel.findOne({ id });
|
|
if (!user) return null;
|
|
|
|
const { name = "guest", avatar = "/images/guest.png", time = Date.now(), admin = false, deleted = false } = user;
|
|
this.name = name;
|
|
this.avatar = avatar;
|
|
this.time = time;
|
|
this.admin = admin;
|
|
this.deleted = deleted;
|
|
return this;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
async getByName(Name = this.name) {
|
|
try {
|
|
const user = await UserModel.findOne({ name: Name });
|
|
|
|
if (!user) return null;
|
|
|
|
const { id, name = "guest", avatar = "/images/guest.png", time = Date.now(), admin = false, deleted = false } = user;
|
|
|
|
this.id = Number(id);
|
|
this.name = name;
|
|
this.avatar = avatar;
|
|
this.time = time;
|
|
this.admin = admin;
|
|
this.deleted = deleted;
|
|
return this;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
async takeId() {
|
|
this.id = await UserModel.count({}) || 0;
|
|
return this;
|
|
}
|
|
|
|
async write(id = this.id) {
|
|
const writing = await UserModel.findOneAndUpdate({ id }, this);
|
|
|
|
if (!writing)
|
|
await UserModel.create(this);
|
|
|
|
return this;
|
|
}
|
|
|
|
getLink(id = this.id) {
|
|
return "/users/" + id;
|
|
}
|
|
|
|
} |