From 50b3f7fbd56b878fc02a7ad6e2ddf9ef912b229e Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Sun, 15 Oct 2017 02:09:26 +0300 Subject: [PATCH] [www] added Article model --- src/core/Article.js | 107 ++++++++++++++++++++++++++++++++++++++++++++ src/core/db.js | 2 + 2 files changed, 109 insertions(+) create mode 100644 src/core/Article.js diff --git a/src/core/Article.js b/src/core/Article.js new file mode 100644 index 00000000..3090a5a0 --- /dev/null +++ b/src/core/Article.js @@ -0,0 +1,107 @@ +const Data = require('./Data') +const Image = require('./Image') +const Config = require('./Config') +const {find, findOne, total} = require('../lib/store') + +const store = () => Data.getStore(Article) + +class Article { + static async find(query, options = {}) { + const items = await find(store(), query, options) + + return Promise.all(items.map(processArticle)) + } + + static async findById(id) { + const item = await findOne(store(), {id}) + + return !item + ? null + : processArticle(item) + } + + static async total(query = {}) { + return total(store(), query) + } + + static async findPinned() { + const now = new Date() + const query = {until: {$gte: now}} + + return Article.find(query, { + sort: {date: -1}, + }) + } + + constructor(data) { + this.id = data.id + this.images = data.images + this.post = data.post + this.folder = data.folder + this.date = data.date + this.title = data.title + this.until = data.until + this.tags = data.tags + this.content = data.content + this.hash = data.hash + this.type = data.type + this.origin = data.origin + this.file = data.file + this.version = data.version + this.preview = data.preview + + this.url = `/article/${this.id}` + } + + plain() { + return { + id: this.id, + images: this.images, + post: this.post, + folder: this.folder, + date: this.date, + title: this.title, + until: this.until, + tags: this.tags, + content: this.content, + hash: this.hash, + type: this.type, + origin: this.origin, + file: this.file, + version: this.version, + preview: this.preview, + url: this.url, + } + } +} + +const previewFromImage = (imgs = []) => imgs.length + ? imgs[0] + : null + +async function resolveDefaultPreview() { + const config = await Config.findConfig() + if (!config) return null + + return Image.findByFile(config.articleCardDefaultPreview) +} + +async function processArticle(article) { + let preview = null + const previewId = article.preview + ? article.preview + : previewFromImage(article.images) + + if (previewId) { + preview = await Image.findById(previewId) + } else { + preview = await resolveDefaultPreview() + } + + return new Article({ + ...article, + preview, + }) +} + +module.exports = Article diff --git a/src/core/db.js b/src/core/db.js index 24927016..9528a1de 100644 --- a/src/core/db.js +++ b/src/core/db.js @@ -1,4 +1,5 @@ const Data = require('./Data') +const Article = require('./Article') const Album = require('./Album') const Teacher = require('./Teacher') const Document = require('./Document') @@ -29,6 +30,7 @@ export function id(i) { } function init(db) { + Data.setStore(Article, db.collection('articles')) Data.setStore(Album, db.collection('albums')) Data.setStore(Image, db.collection('images')) Data.setStore(Teacher, db.collection('collective'))