Article server based
Required Sync changes!
This commit is contained in:
1 parent
6acb7ba64d
commit
be84058aba
18 files changed
+475
-114
No files matched your search
+131
-23
@@ -1,28 +1,136 @@
|
||||
import route from 'koa-route';
|
||||
import {c} from '../core/db';
|
||||
import {accepts, index} from './';
|
||||
const React = require('react')
|
||||
import {get} from 'koa-route'
|
||||
import {c} from '../core/db'
|
||||
const Article = require('../components/Article')
|
||||
const ArticleList = require('../components/ArticleList')
|
||||
const Paginator = require('../components/Paginator')
|
||||
const {render} = require('../lib/render')
|
||||
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
|
||||
const timestamp = require('../lib/date').timestamp
|
||||
const {sortBy} = require('../utils/sort')
|
||||
const accepts = require('./index').accepts
|
||||
|
||||
async function exists(ctx, id) {
|
||||
let i = await article(id);
|
||||
return i ? true : false;
|
||||
const sortArticleByDate = sortBy(
|
||||
i => timestamp(new Date(i.date))
|
||||
)
|
||||
|
||||
const articleUrl = article => `/article/${article.id}`
|
||||
|
||||
async function findArticle(id) {
|
||||
return c('articles').findOne({id: id})
|
||||
}
|
||||
|
||||
async function article(id){
|
||||
let i = await c('events').findOne({id: id});
|
||||
if (!i) {
|
||||
i = await c('timeline').findOne({id: id});
|
||||
}
|
||||
return i;
|
||||
async function findArticlesNin(nin, skip, limit, sort) {
|
||||
return c('articles')
|
||||
.find({
|
||||
_id: {$nin: nin}
|
||||
})
|
||||
.sort(sort)
|
||||
.skip(skip)
|
||||
.limit(limit)
|
||||
.toArray()
|
||||
}
|
||||
|
||||
export default function () {
|
||||
return route.get('/article/:id', accepts({
|
||||
'text/html': index(exists),
|
||||
'text/plain': index(exists),
|
||||
'application/json': async (ctx, id) => {
|
||||
let i = await article(id);
|
||||
if (!i) ctx.status = 404;
|
||||
else ctx.body = i;
|
||||
}
|
||||
}))
|
||||
};
|
||||
async function totalArticles() {
|
||||
return c('articles')
|
||||
.find({})
|
||||
.count()
|
||||
}
|
||||
|
||||
async function findPinned(page) {
|
||||
const now = new Date()
|
||||
return page !== 1
|
||||
? []
|
||||
: await c('articles')
|
||||
.find({until: {$gte: now}})
|
||||
.sort({date: -1})
|
||||
.toArray()
|
||||
}
|
||||
|
||||
function getMeta(article) {
|
||||
return {
|
||||
title: article.title,
|
||||
}
|
||||
}
|
||||
|
||||
function getArticles(pageSize) {
|
||||
return get('/articles/:page?', async (ctx, page) => {
|
||||
const path = getPathWithNoTrailingSlash(ctx.path)
|
||||
page = parseInt(page) || 1
|
||||
|
||||
const limit = pageSize
|
||||
const skip = (page - 1) * pageSize
|
||||
|
||||
const pinnedArticles = await findPinned(page)
|
||||
const total = await totalArticles()
|
||||
const totalPages = total / pageSize
|
||||
|
||||
const id = i => i._id
|
||||
const pinnedIds = pinnedArticles.map(id)
|
||||
const articles = await findArticlesNin(pinnedIds, skip, limit, {date: -1})
|
||||
|
||||
const prevPage = page > 1
|
||||
? page - 1
|
||||
: null
|
||||
|
||||
const nextPage = page < totalPages
|
||||
? page + 1
|
||||
: null
|
||||
|
||||
const content = [
|
||||
...pinnedArticles.sort(sortArticleByDate),
|
||||
...articles
|
||||
]
|
||||
.map(article => ({
|
||||
...article,
|
||||
url: articleUrl(article),
|
||||
}))
|
||||
const Component = (
|
||||
<ArticleList
|
||||
articles={content}
|
||||
prevPage={prevPage}
|
||||
nextPage={nextPage}
|
||||
/>
|
||||
)
|
||||
|
||||
ctx.body = await render(path, Component, getMeta({title: `Статьи`}))
|
||||
})
|
||||
}
|
||||
|
||||
function getArticle() {
|
||||
return get('/article/:id', accepts({
|
||||
'text/html': async (ctx, id) => {
|
||||
const path = getPathWithNoTrailingSlash(ctx.path)
|
||||
const article = await findArticle(id)
|
||||
|
||||
if (article) {
|
||||
const Component = (
|
||||
<div className="content content_thin">
|
||||
<Article
|
||||
title={article.title}
|
||||
date={article.date}
|
||||
data={article.post}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
|
||||
ctx.type = 'text/html'
|
||||
ctx.body = await render(path, Component, getMeta(article))
|
||||
} else {
|
||||
ctx.status = 404
|
||||
}
|
||||
},
|
||||
'application/json': async (ctx, id) => {
|
||||
const article = await findArticle(id)
|
||||
|
||||
if (article) {
|
||||
ctx.body = article
|
||||
} else {
|
||||
ctx.status = 404
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
exports.getArticles = getArticles
|
||||
exports.getArticle = getArticle
|
||||
@@ -0,0 +1,88 @@
|
||||
const React = require('react')
|
||||
const Article = require('../components/Article')
|
||||
const {get} = require('koa-route')
|
||||
const {c} = require('../core/db')
|
||||
const {render} = require('../lib/render')
|
||||
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
|
||||
const timestamp = require('../lib/date').timestamp
|
||||
const {sortBy} = require('../utils/sort')
|
||||
|
||||
const sortArticleByDate = sortBy(
|
||||
i => timestamp(new Date(i.date))
|
||||
)
|
||||
|
||||
const articleUrl = article => `/article/${article.id}`
|
||||
|
||||
async function findArticlesNin(nin, skip, limit, sort) {
|
||||
return c('articles')
|
||||
.find({
|
||||
_id: {$nin: nin}
|
||||
})
|
||||
.sort(sort)
|
||||
.skip(skip)
|
||||
.limit(limit)
|
||||
.toArray()
|
||||
}
|
||||
|
||||
async function findPinned(page) {
|
||||
const now = new Date()
|
||||
return page !== 1
|
||||
? []
|
||||
: await c('articles')
|
||||
.find({until: {$gte: now}})
|
||||
.sort({date: -1})
|
||||
.toArray()
|
||||
}
|
||||
|
||||
function getComponent(path, article) {
|
||||
return (
|
||||
<Article
|
||||
url={path}
|
||||
title={article.title}
|
||||
date={article.date}
|
||||
data={article.post}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function getMeta(article) {
|
||||
return {
|
||||
title: article.title,
|
||||
}
|
||||
}
|
||||
|
||||
function getHome(pageSize) {
|
||||
return get('/', async (ctx, page) => {
|
||||
const path = getPathWithNoTrailingSlash(ctx.path) || '/'
|
||||
page = parseInt(page) || 1
|
||||
|
||||
const limit = pageSize
|
||||
const skip = (page - 1) * pageSize
|
||||
|
||||
const pinnedArticles = await findPinned(page)
|
||||
|
||||
const id = i => i._id
|
||||
const pinnedIds = pinnedArticles.map(id)
|
||||
const articles = await findArticlesNin(pinnedIds, skip, limit, {date: -1})
|
||||
|
||||
const content = [
|
||||
...pinnedArticles.sort(sortArticleByDate),
|
||||
...articles
|
||||
]
|
||||
.map(article => getComponent(articleUrl(article), article))
|
||||
const Component = (
|
||||
<div className="content content_thin">
|
||||
<div className="hudozka-title">
|
||||
<p>МБУДО</p>
|
||||
<h1>Шлиссельбургская детская художественная школа</h1>
|
||||
</div>
|
||||
|
||||
{content}
|
||||
</div>
|
||||
)
|
||||
|
||||
ctx.body = await render(path, Component, getMeta({title: 'Шлиссельбургская ДХШ'}))
|
||||
})
|
||||
}
|
||||
|
||||
exports.getHome = getHome
|
||||
@@ -1,6 +1,5 @@
|
||||
import {readFile} from 'async-file'
|
||||
import handlebars from 'handlebars'
|
||||
import route from 'koa-route'
|
||||
import compose from 'koa-compose'
|
||||
|
||||
import {map, assign} from '../utils/common'
|
||||
@@ -8,20 +7,15 @@ import {index as indexFile} from '../config'
|
||||
|
||||
import sitemap from './sitemap'
|
||||
import schedule from './schedule'
|
||||
import {getPageUrls} from '../core/pages'
|
||||
import news from './news'
|
||||
import gallery from './gallery'
|
||||
import documents from './documents'
|
||||
import teachers from './teachers'
|
||||
import events from './events'
|
||||
import articles from './articles'
|
||||
import pages from './pages'
|
||||
|
||||
let main = () => route.get('/', index())
|
||||
|
||||
export function routes(store) {
|
||||
return compose([
|
||||
main(),
|
||||
sitemap(),
|
||||
schedule(),
|
||||
news(),
|
||||
@@ -29,7 +23,6 @@ export function routes(store) {
|
||||
documents(store),
|
||||
teachers(),
|
||||
events(),
|
||||
articles(),
|
||||
pages(),
|
||||
])
|
||||
}
|
||||
|
||||
+13
-53
@@ -1,55 +1,25 @@
|
||||
const {readFile} = require('async-file')
|
||||
const handlebars = require('handlebars')
|
||||
const React = require('react')
|
||||
const Page = require('../components/Page')
|
||||
|
||||
const {get} = require('koa-route')
|
||||
const {c} = require('../core/db')
|
||||
|
||||
const url = require('url')
|
||||
const renderApp = require('../lib/component').renderApp
|
||||
const isEqualPath = require('../lib/url').isEqualPath
|
||||
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
|
||||
const {compose, any} = require('../lib/common')
|
||||
const {render} = require('../lib/render')
|
||||
|
||||
const menuModel = require('../models/menu').default
|
||||
const config = require('../config')
|
||||
|
||||
async function getPage(path) {
|
||||
return await c('pages').findOne({url: path})
|
||||
}
|
||||
|
||||
function isActive(path, menuItem) {
|
||||
return any(flatMenuItem(menuItem)
|
||||
.map(i => isEqualPath(path, i.url, true))
|
||||
)
|
||||
}
|
||||
|
||||
function flatMenuItem(item) {
|
||||
const rootUrl = item.url
|
||||
const flatUrls = items => {
|
||||
return items.map(item => ({
|
||||
...item,
|
||||
url: url.resolve(rootUrl, item.url)
|
||||
}))
|
||||
function getPageMeta(page) {
|
||||
return {
|
||||
title: page.title,
|
||||
}
|
||||
|
||||
return !item.items
|
||||
? [item]
|
||||
: item.items.reduce((acc, i) => [...acc, ...compose(flatUrls, flatMenuItem)(i)], [item])
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes plain menu model and sample path and defines is an item of menu should be active
|
||||
*
|
||||
* @param path
|
||||
* @param menu
|
||||
* @return {{items}}
|
||||
*/
|
||||
function buildMenu(path, menu) {
|
||||
const items = menu.map(item => ({
|
||||
...item,
|
||||
active: isActive(path, item)
|
||||
}))
|
||||
return {items}
|
||||
function getComponent(page) {
|
||||
return <Page>{page.data}</Page>
|
||||
}
|
||||
|
||||
module.exports = function () {
|
||||
@@ -57,22 +27,12 @@ module.exports = function () {
|
||||
const path = getPathWithNoTrailingSlash(ctx.path)
|
||||
const page = await getPage(path)
|
||||
if (page) {
|
||||
const menu = buildMenu(path, menuModel)
|
||||
const content = renderApp({menu, page})
|
||||
|
||||
const source = await readFile(config.viewMain, 'utf-8')
|
||||
const template = handlebars.compile(source)
|
||||
|
||||
const title = page.title
|
||||
const meta = {
|
||||
title,
|
||||
description: title,
|
||||
image: 'https://art.shlisselburg.org/entrance.jpg',
|
||||
url: `https://art.shlisselburg.org${path}`,
|
||||
}
|
||||
|
||||
ctx.type = 'text/html'
|
||||
ctx.body = template({content, title, meta})
|
||||
ctx.body = await render(
|
||||
path,
|
||||
getComponent(page),
|
||||
getPageMeta(page),
|
||||
)
|
||||
} else {
|
||||
ctx.status = 404
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user