akf-forum/src/models/cache.js

17 lines
448 B
JavaScript
Raw Normal View History

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)
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;
}