app moved to next.js
This commit is contained in:
1 parent
dcca513aea
commit
43af2996bf
48 files changed
+1147
-1027
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import Article from '../core/Article'
|
||||
import ImageArtifactType from '../core/ImageArtifactType'
|
||||
import { sortBy } from '../utils/sort'
|
||||
import { timestamp } from '../lib/date'
|
||||
|
||||
export const sortArticleByDate = sortBy(
|
||||
x => timestamp(new Date(x.date))
|
||||
)
|
||||
|
||||
export async function findArticlesNin(nin, skip, limit, sort) {
|
||||
const query = {
|
||||
_id: { $nin: nin }
|
||||
}
|
||||
|
||||
return Article.find(query, {
|
||||
sort,
|
||||
skip,
|
||||
limit,
|
||||
})
|
||||
}
|
||||
|
||||
export async function findArticles(page, pageSize) {
|
||||
const limit = pageSize
|
||||
const skip = (page - 1) * pageSize
|
||||
|
||||
const pinnedArticles = page === 1
|
||||
? await Article.findPinned()
|
||||
: []
|
||||
|
||||
const total = await Article.total()
|
||||
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 items = [
|
||||
...pinnedArticles.sort(sortArticleByDate),
|
||||
...articles
|
||||
]
|
||||
.map(x => x.plain())
|
||||
.map(x => ({
|
||||
...x,
|
||||
preview: !x.preview ? null : (
|
||||
x.preview.getPicture(ImageArtifactType.MEDIUM)
|
||||
)
|
||||
}))
|
||||
|
||||
return {
|
||||
items,
|
||||
prevPage,
|
||||
nextPage,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import ImageArtifactType from '../core/ImageArtifactType'
|
||||
|
||||
const sizes = [
|
||||
ImageArtifactType.MEDIUM,
|
||||
ImageArtifactType.BIG,
|
||||
ImageArtifactType.ORIGIN,
|
||||
]
|
||||
|
||||
export function encodeFilePreview(file, sizes) {
|
||||
const preview = file.preview
|
||||
if (!preview) return null
|
||||
|
||||
const image = preview.findArtifact(sizes)
|
||||
if (!image) return null
|
||||
|
||||
return image.url
|
||||
}
|
||||
|
||||
export function encodeFile(file) {
|
||||
const imageUrl = encodeFilePreview(file, sizes)
|
||||
const category = (file.category || '')
|
||||
.replace('Documents/', '')
|
||||
.replace('Pages/', '')
|
||||
.replace('Albums/', '')
|
||||
.replace('Articles /', '')
|
||||
|
||||
return {
|
||||
imageUrl,
|
||||
category,
|
||||
title: file.title,
|
||||
fileName: file.fileInfo.name,
|
||||
fileSize: file.fileInfo.size,
|
||||
fileUrl: file.url,
|
||||
url: file.viewUrl,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
const { get } = require('koa-route')
|
||||
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
|
||||
|
||||
const Page = require('../core/Page')
|
||||
|
||||
function getPage() {
|
||||
return get('/api/pages/*', async ctx => {
|
||||
const path = getPathWithNoTrailingSlash(ctx.path)
|
||||
const pagePath = path.replace('/api/pages', '')
|
||||
|
||||
const resource = await Page.findByUrl(pagePath)
|
||||
if (!resource) {
|
||||
ctx.status = 404
|
||||
ctx.body = {
|
||||
error: 'not found'
|
||||
}
|
||||
} else {
|
||||
ctx.body = {
|
||||
id: resource.id,
|
||||
title: resource.title,
|
||||
url: resource.url,
|
||||
data: resource.data,
|
||||
preview: resource.preview,
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
exports.getPage = getPage
|
||||
@@ -0,0 +1,11 @@
|
||||
import ImageArtifactType from '../core/ImageArtifactType'
|
||||
const profilePicture = profile => profile.picture.getPicture(ImageArtifactType.BIG)
|
||||
|
||||
export function encodePerson(person) {
|
||||
return {
|
||||
...person,
|
||||
shortName: person.shortName(),
|
||||
name: person.splitName(),
|
||||
picture: profilePicture(person),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user