2017-08-24 10:52:12 +00:00
|
|
|
import API from './index'
|
|
|
|
import Models from './models'
|
|
|
|
|
|
|
|
const perPage = 8
|
|
|
|
|
|
|
|
function slugify (title) {
|
2017-09-10 09:42:12 +00:00
|
|
|
return title.toLowerCase().replace(/\W/g, '-').substring(0, 32)
|
2017-08-24 10:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanArticle (entry, shortenContent = false) {
|
|
|
|
let poster = await API.User.get(entry.user_id)
|
|
|
|
let article = {
|
|
|
|
id: entry.id,
|
|
|
|
slug: slugify(entry.title),
|
|
|
|
title: entry.title,
|
|
|
|
content: entry.content,
|
|
|
|
tags: entry.tags.split(','),
|
|
|
|
created_at: entry.created_at,
|
|
|
|
updated_at: entry.updated_at
|
|
|
|
}
|
|
|
|
|
|
|
|
if (poster) {
|
|
|
|
article.author = {
|
|
|
|
id: poster.id,
|
|
|
|
display_name: poster.display_name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shortenContent) {
|
|
|
|
article.content = article.content.replace(/(<([^>]+)>)/ig, '').substring(0, 128) + '...'
|
|
|
|
}
|
|
|
|
|
|
|
|
return article
|
|
|
|
}
|
|
|
|
|
|
|
|
const News = {
|
|
|
|
preview: async () => {
|
|
|
|
// Fetch 3 latest stories
|
|
|
|
let news = await Models.News.query().orderBy('created_at', 'desc').limit(3)
|
|
|
|
|
|
|
|
if (!news.length) return []
|
|
|
|
|
|
|
|
let articles = []
|
|
|
|
for (let i in news) {
|
|
|
|
let entry = news[i]
|
|
|
|
articles.push(await cleanArticle(entry, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
return articles
|
|
|
|
},
|
|
|
|
listNews: async (page) => {
|
|
|
|
let count = await Models.News.query().count('id as ids')
|
|
|
|
if (page < 1) page = 1
|
|
|
|
|
|
|
|
if (!count.length || !count[0]['ids'] || isNaN(page)) {
|
2017-11-30 21:13:14 +00:00
|
|
|
return { error: 'No news' }
|
2017-08-24 10:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
count = count[0].ids
|
2017-08-28 15:42:16 +00:00
|
|
|
let paginated = API.Pagination(perPage, parseInt(count), page)
|
2017-08-24 10:52:12 +00:00
|
|
|
let news = await Models.News.query().orderBy('created_at', 'desc').offset(paginated.offset).limit(perPage)
|
|
|
|
|
|
|
|
let articles = []
|
|
|
|
for (let i in news) {
|
|
|
|
let entry = news[i]
|
|
|
|
|
|
|
|
articles.push(await cleanArticle(entry))
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
page: paginated,
|
|
|
|
articles: articles
|
|
|
|
}
|
|
|
|
},
|
|
|
|
article: async (id) => {
|
|
|
|
let article = await Models.News.query().where('id', id)
|
|
|
|
if (!article.length) return {}
|
|
|
|
article = article[0]
|
|
|
|
|
2017-08-24 16:23:03 +00:00
|
|
|
return cleanArticle(article)
|
2017-08-29 12:00:36 +00:00
|
|
|
},
|
|
|
|
compose: async (user, body) => {
|
|
|
|
let article = {
|
|
|
|
title: body.title,
|
|
|
|
content: body.content,
|
|
|
|
tags: body.tags || '',
|
|
|
|
user_id: user.id,
|
|
|
|
created_at: new Date()
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = await Models.News.query().insert(article)
|
|
|
|
result.slug = slugify(result.title)
|
|
|
|
|
|
|
|
return result
|
|
|
|
},
|
|
|
|
edit: async (id, body) => {
|
|
|
|
let patch = {
|
|
|
|
content: body.content,
|
|
|
|
updated_at: new Date()
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = await Models.News.query().patchAndFetchById(id, patch)
|
2017-11-30 21:13:14 +00:00
|
|
|
if (!result) throw new Error('Something went wrong.')
|
2017-08-29 12:00:36 +00:00
|
|
|
return {}
|
2017-08-24 10:52:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = News
|