2022-08-31 14:44:28 +03:00
|
|
|
const UserModel = require("./User");
|
2023-08-27 20:22:28 +03:00
|
|
|
const UserCache = new Map();
|
2022-08-31 14:44:28 +03:00
|
|
|
module.exports.getAuthor = async function () {
|
|
|
|
const id = this.authorID || this.author?.id;
|
2023-08-27 20:22:28 +03:00
|
|
|
let user = UserCache.get(id);
|
2022-10-09 20:58:55 +03:00
|
|
|
if (!user)
|
2024-02-10 23:51:42 +03:00
|
|
|
UserCache.set(id, user = await UserModel.findOne({ id }));
|
2022-10-09 20:58:55 +03:00
|
|
|
|
|
|
|
if (!this.get('authorID', null, { getters: false })) {
|
|
|
|
this.authorID = user.id;
|
|
|
|
await this.save();
|
2022-08-31 14:44:28 +03:00
|
|
|
}
|
2022-10-09 20:58:55 +03:00
|
|
|
|
2022-08-31 14:44:28 +03:00
|
|
|
this.author = user;
|
2022-10-09 20:58:55 +03:00
|
|
|
|
2022-08-31 14:44:28 +03:00
|
|
|
return this;
|
|
|
|
}
|