akf-forum/models/cache.js

18 lines
458 B
JavaScript
Raw Normal View History

2022-08-31 14:44:28 +03:00
const UserModel = require("./User");
const UserCache = [];
module.exports.getAuthor = async function () {
const id = this.authorID || this.author?.id;
let user = UserCache.find(user => user?.id == id)
2022-10-09 20:58:55 +03:00
if (!user)
UserCache.push(user = await UserModel.findOne({ id }))
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;
}