commit
a2a868bf42
@ -0,0 +1,97 @@
|
|||||||
|
// post.js
|
||||||
|
// Copyright (C) 2021 Digital Telepresence, LLC
|
||||||
|
// License: Apache-2.0
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const express = require('express');
|
||||||
|
// const multer = require('multer');
|
||||||
|
|
||||||
|
const { SiteController, SiteError } = require('../../lib/site-lib');
|
||||||
|
|
||||||
|
class TagController extends SiteController {
|
||||||
|
|
||||||
|
constructor (dtp) {
|
||||||
|
super(dtp, module.exports);
|
||||||
|
}
|
||||||
|
|
||||||
|
async start ( ) {
|
||||||
|
const { dtp } = this;
|
||||||
|
// const {
|
||||||
|
// post: postService,
|
||||||
|
// limiter: limiterService,
|
||||||
|
// session: sessionService,
|
||||||
|
// } = dtp.services;
|
||||||
|
|
||||||
|
// const authRequired = sessionService.authCheckMiddleware({ requiredLogin: true });
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
dtp.app.use('/tag', router);
|
||||||
|
|
||||||
|
router.use(async (req, res, next) => {
|
||||||
|
res.locals.currentView = 'home';
|
||||||
|
return next();
|
||||||
|
});
|
||||||
|
|
||||||
|
router.param('tagSlug', this.populateTagSlug.bind(this));
|
||||||
|
|
||||||
|
router.get('/:tagSlug', this.getSearchView.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
async populateTagSlug (req, res, next, tagSlug) {
|
||||||
|
const { post: postService } = this.dtp.services;
|
||||||
|
try {
|
||||||
|
var allPosts = false;
|
||||||
|
var statusArray = ['published'];
|
||||||
|
if (req.user) {
|
||||||
|
if (req.user.flags.isAdmin) {
|
||||||
|
statusArray.push('draft');
|
||||||
|
allPosts = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.locals.allPosts = allPosts;
|
||||||
|
res.locals.tagSlug = tagSlug;
|
||||||
|
tagSlug = tagSlug.replace("_", " ");
|
||||||
|
res.locals.pagination = this.getPaginationParameters(req, 20);
|
||||||
|
res.locals.posts = await postService.getByTags(tagSlug, res.locals.pagination, statusArray);
|
||||||
|
res.locals.tag = tagSlug;
|
||||||
|
return next();
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
this.log.error('failed to populate tagSlug', { tagSlug, error });
|
||||||
|
return next(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getSearchView (req, res) {
|
||||||
|
try {
|
||||||
|
res.locals.pageTitle = `Tag ${res.locals.tag} on ${this.dtp.config.site.name}`;
|
||||||
|
|
||||||
|
res.render('tag/view');
|
||||||
|
} catch (error) {
|
||||||
|
this.log.error('failed to service post view', { postId: res.locals.post._id, error });
|
||||||
|
throw SiteError("Error getting tag view:", error );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async getIndex (req, res, next) {
|
||||||
|
const { post: postService } = this.dtp.services;
|
||||||
|
try {
|
||||||
|
res.locals.pagination = this.getPaginationParameters(req, 20);
|
||||||
|
res.locals.posts = await postService.getPosts(res.locals.pagination);
|
||||||
|
res.render('tag/index');
|
||||||
|
} catch (error) {
|
||||||
|
return next(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
slug: 'tag',
|
||||||
|
name: 'tag',
|
||||||
|
create: async (dtp) => {
|
||||||
|
let controller = new TagController(dtp);
|
||||||
|
return controller;
|
||||||
|
},
|
||||||
|
};
|
@ -0,0 +1,19 @@
|
|||||||
|
mixin renderPostSummaryFull (post)
|
||||||
|
div(uk-grid).uk-grid-small
|
||||||
|
if post.image
|
||||||
|
.uk-width-auto
|
||||||
|
img(src= `/image/${post.image._id}`).uk-width-medium
|
||||||
|
else
|
||||||
|
.uk-width-auto
|
||||||
|
img(src="/img/default-poster.jpg").uk-width-medium
|
||||||
|
.uk-width-expand
|
||||||
|
.uk-text-large.uk-text-bold(style="line-height: 1em;")
|
||||||
|
a(href=`/post/${post.slug}`)= `${post.title}`
|
||||||
|
.uk-text-small.uk-text-muted
|
||||||
|
div
|
||||||
|
div= moment(post.created).fromNow()
|
||||||
|
span by
|
||||||
|
a(href=`/user/${post.author.username}`)=` ${post.author.username}`
|
||||||
|
if user && allPosts
|
||||||
|
div= `Status: ${post.status}`
|
||||||
|
div= post.summary
|
@ -0,0 +1,37 @@
|
|||||||
|
extends ../layouts/main-sidebar
|
||||||
|
|
||||||
|
include components/list
|
||||||
|
|
||||||
|
include ../components/pagination-bar
|
||||||
|
|
||||||
|
block content
|
||||||
|
|
||||||
|
|
||||||
|
if Array.isArray(posts) && (posts.length > 0)
|
||||||
|
h3= `Posts with the tag ${tag}.`
|
||||||
|
ul.uk-list.uk-list-divider
|
||||||
|
each post in posts
|
||||||
|
li
|
||||||
|
+renderPostSummaryFull(post)
|
||||||
|
|
||||||
|
.uk-card-footer
|
||||||
|
+renderPaginationBar(`/tag/${tagSlug}`, posts.length )
|
||||||
|
//- li
|
||||||
|
if post.image
|
||||||
|
img(src= `/image/${post.image._id}`, href=`/post/${post.slug}`, style="max-height: 350px; object-fit: cover; vertical-align:middle;margin:0px 20px;").responsive
|
||||||
|
else
|
||||||
|
img(src="/img/default-poster.jpg", href=`/post/${post.slug}`, style="max-height: 350px; object-fit: cover; vertical-align:middle;margin:0px 20px;").responsive
|
||||||
|
a(href=`/post/${post.slug}`).uk-display-block
|
||||||
|
div.h2= post.title
|
||||||
|
|
||||||
|
.uk-article-meta
|
||||||
|
div(uk-grid).uk-grid-small.uk-text-small
|
||||||
|
.uk-width-expand
|
||||||
|
a(href=`/post/${post.slug}`)= moment(post.created).fromNow()
|
||||||
|
span by
|
||||||
|
a(href=`/user/${post.author.username}`)=` ${post.author.username}`
|
||||||
|
.uk-width-expand
|
||||||
|
div= post.summary
|
||||||
|
|
||||||
|
else
|
||||||
|
h3= `There are no posts with the tag ${tag}.`
|
Loading…
Reference in new issue