From 5ab4a1cbfc2cb98ba891b4e9d7209f64ae3abc12 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Mon, 18 Sep 2017 00:57:27 +0300 Subject: [PATCH] [www] Article metadata updated --- src/core/ImageArtifactType.js | 2 ++ src/routes/articles.js | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/core/ImageArtifactType.js b/src/core/ImageArtifactType.js index 2f6e75fd..5a8722c4 100644 --- a/src/core/ImageArtifactType.js +++ b/src/core/ImageArtifactType.js @@ -9,6 +9,8 @@ ImageArtifactType.MEDIUM = 'medium' ImageArtifactType.BIG = 'big' ImageArtifactType.ORIGIN = 'origin' +ImageArtifactType.FACEBOOK = 'fb' + ImageArtifactType.RETINA_DENSITY = 2 ImageArtifactType.retina = type => `${type}@${ImageArtifactType.RETINA_DENSITY}` diff --git a/src/routes/articles.js b/src/routes/articles.js index 4c3e132c..84a71487 100644 --- a/src/routes/articles.js +++ b/src/routes/articles.js @@ -7,6 +7,8 @@ const Paginator = require('../components/Paginator') const {render} = require('../lib/render') const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash const timestamp = require('../lib/date').timestamp +const ImageArtifactType = require('../core/ImageArtifactType') +const Image = require('../core/Image') const {sortBy} = require('../utils/sort') const accepts = require('./index').accepts @@ -88,9 +90,25 @@ async function getArticleListComponent(path, page, pageSize) { } function getMeta(article) { - return { + const types = [ + ImageArtifactType.FACEBOOK, + ImageArtifactType.MEDIUM, + ImageArtifactType.BIG, + ImageArtifactType.ORIGIN, + ] + const meta = { title: article.title, } + + if (article.preview) { + try { + const artifact = article.preview.findArtifact(types) + meta.image = artifact.url + } catch (error) { + meta.image = 'https://art.shlisselburg.org/entrance.jpg' + } + } + return meta } function getArticles(pageSize) { @@ -120,6 +138,11 @@ function getArticle() { const path = getPathWithNoTrailingSlash(ctx.path) const article = await findArticle(id) + const previewId = article.preview + ? article.preview + : article.images[0] + article.preview = await Image.findById(previewId) + if (article) { const Component = (