2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-12 20:11:43 +02:00
|
|
|
const router = require('../router.js');
|
2016-06-02 00:07:51 +02:00
|
|
|
const api = require('../api.js');
|
2016-06-06 20:57:22 +02:00
|
|
|
const events = require('../events.js');
|
2016-06-13 23:10:53 +02:00
|
|
|
const settings = require('../settings.js');
|
2016-06-13 23:28:55 +02:00
|
|
|
const Post = require('../models/post.js');
|
2016-06-13 22:34:39 +02:00
|
|
|
const TopNavigation = require('../models/top_navigation.js');
|
2016-06-06 20:57:22 +02:00
|
|
|
const PostView = require('../views/post_view.js');
|
2016-05-29 12:21:25 +02:00
|
|
|
const EmptyView = require('../views/empty_view.js');
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-06-13 23:10:53 +02:00
|
|
|
class PostController {
|
2016-06-02 00:07:51 +02:00
|
|
|
constructor() {
|
2016-06-06 20:57:22 +02:00
|
|
|
this._postView = new PostView();
|
2016-06-13 23:10:53 +02:00
|
|
|
this._emptyView = new EmptyView();
|
2016-06-02 00:07:51 +02:00
|
|
|
}
|
|
|
|
|
2016-04-06 21:49:26 +02:00
|
|
|
registerRoutes() {
|
2016-06-12 20:11:43 +02:00
|
|
|
router.enter(
|
2016-04-06 21:49:26 +02:00
|
|
|
'/post/:id',
|
2016-06-06 20:57:22 +02:00
|
|
|
(ctx, next) => { this._showPostRoute(ctx.params.id, false); });
|
2016-06-12 20:11:43 +02:00
|
|
|
router.enter(
|
2016-04-06 21:49:26 +02:00
|
|
|
'/post/:id/edit',
|
2016-06-06 20:57:22 +02:00
|
|
|
(ctx, next) => { this._showPostRoute(ctx.params.id, true); });
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 20:57:22 +02:00
|
|
|
_showPostRoute(id, editMode) {
|
2016-06-13 22:34:39 +02:00
|
|
|
TopNavigation.activate('posts');
|
2016-06-06 20:57:22 +02:00
|
|
|
Promise.all([
|
2016-06-13 23:28:55 +02:00
|
|
|
Post.get(id),
|
2016-06-12 22:10:20 +02:00
|
|
|
api.get(`/post/${id}/around?fields=id&query=` +
|
|
|
|
this._decorateSearchQuery('')),
|
2016-06-06 20:57:22 +02:00
|
|
|
]).then(responses => {
|
2016-06-13 23:28:55 +02:00
|
|
|
const [post, aroundResponse] = responses;
|
2016-06-06 20:57:22 +02:00
|
|
|
this._postView.render({
|
2016-06-13 23:28:55 +02:00
|
|
|
post: post,
|
2016-06-06 20:57:22 +02:00
|
|
|
editMode: editMode,
|
|
|
|
nextPostId: aroundResponse.next ? aroundResponse.next.id : null,
|
|
|
|
prevPostId: aroundResponse.prev ? aroundResponse.prev.id : null,
|
2016-06-11 09:59:29 +02:00
|
|
|
canEditPosts: api.hasPrivilege('posts:edit'),
|
2016-06-12 18:08:50 +02:00
|
|
|
canListComments: api.hasPrivilege('comments:list'),
|
|
|
|
canCreateComments: api.hasPrivilege('comments:create'),
|
2016-06-06 20:57:22 +02:00
|
|
|
});
|
|
|
|
}, response => {
|
|
|
|
this._emptyView.render();
|
|
|
|
events.notify(events.Error, response.description);
|
|
|
|
});
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 20:57:22 +02:00
|
|
|
_decorateSearchQuery(text) {
|
|
|
|
const browsingSettings = settings.getSettings();
|
|
|
|
let disabledSafety = [];
|
|
|
|
for (let key of Object.keys(browsingSettings.listPosts)) {
|
|
|
|
if (browsingSettings.listPosts[key] === false) {
|
|
|
|
disabledSafety.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (disabledSafety.length) {
|
|
|
|
text = `-rating:${disabledSafety.join(',')} ${text}`;
|
|
|
|
}
|
|
|
|
return text.trim();
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 23:10:53 +02:00
|
|
|
module.exports = new PostController();
|