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-03 19:12:10 +02:00
|
|
|
const settings = require('../settings.js');
|
2016-06-06 20:57:22 +02:00
|
|
|
const events = require('../events.js');
|
2016-06-03 19:12:10 +02:00
|
|
|
const misc = require('../util/misc.js');
|
2016-06-02 00:07:51 +02:00
|
|
|
const pageController = require('../controllers/page_controller.js');
|
2016-06-13 22:34:39 +02:00
|
|
|
const TopNavigation = require('../models/top_navigation.js');
|
2016-06-02 00:07:51 +02:00
|
|
|
const PostsHeaderView = require('../views/posts_header_view.js');
|
|
|
|
const PostsPageView = require('../views/posts_page_view.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-04-01 00:20:34 +02:00
|
|
|
class PostsController {
|
2016-06-02 00:07:51 +02:00
|
|
|
constructor() {
|
|
|
|
this._postsHeaderView = new PostsHeaderView();
|
|
|
|
this._postsPageView = new PostsPageView();
|
2016-06-06 20:57:22 +02:00
|
|
|
this._postView = new PostView();
|
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(
|
|
|
|
'/upload',
|
|
|
|
(ctx, next) => { this._uploadPostsRoute(); });
|
|
|
|
router.enter(
|
|
|
|
'/posts/:query?',
|
2016-05-29 12:22:06 +02:00
|
|
|
(ctx, next) => { misc.parseSearchQueryRoute(ctx, next); },
|
2016-06-02 00:07:51 +02:00
|
|
|
(ctx, next) => { this._listPostsRoute(ctx); });
|
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-05-29 12:21:25 +02:00
|
|
|
this._emptyView = new EmptyView();
|
2016-04-06 21:49:26 +02:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:35:12 +02:00
|
|
|
_uploadPostsRoute() {
|
2016-06-13 22:34:39 +02:00
|
|
|
TopNavigation.activate('upload');
|
2016-05-29 12:21:25 +02:00
|
|
|
this._emptyView.render();
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-06-02 00:07:51 +02:00
|
|
|
_listPostsRoute(ctx) {
|
2016-06-13 22:34:39 +02:00
|
|
|
TopNavigation.activate('posts');
|
2016-06-02 00:07:51 +02:00
|
|
|
|
|
|
|
pageController.run({
|
2016-06-11 09:59:29 +02:00
|
|
|
searchQuery: ctx.searchQuery,
|
|
|
|
clientUrl: '/posts/' + misc.formatSearchQuery({
|
|
|
|
text: ctx.searchQuery.text, page: '{page}'}),
|
2016-06-12 22:02:15 +02:00
|
|
|
requestPage: pageController.createHistoryCacheProxy(
|
|
|
|
ctx,
|
|
|
|
page => {
|
2016-06-12 22:10:20 +02:00
|
|
|
const text
|
|
|
|
= this._decorateSearchQuery(ctx.searchQuery.text);
|
2016-06-12 22:02:15 +02:00
|
|
|
return api.get(
|
|
|
|
`/posts/?query=${text}&page=${page}&pageSize=40` +
|
|
|
|
'&fields=id,type,tags,score,favoriteCount,' +
|
|
|
|
'commentCount,thumbnailUrl');
|
|
|
|
}),
|
2016-06-02 00:07:51 +02:00
|
|
|
headerRenderer: this._postsHeaderView,
|
|
|
|
pageRenderer: this._postsPageView,
|
2016-06-11 09:59:29 +02:00
|
|
|
pageContext: {
|
|
|
|
canViewPosts: api.hasPrivilege('posts:view'),
|
|
|
|
}
|
2016-06-02 00:07:51 +02:00
|
|
|
});
|
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([
|
|
|
|
api.get('/post/' + 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 => {
|
|
|
|
const [postResponse, aroundResponse] = responses;
|
|
|
|
this._postView.render({
|
|
|
|
post: postResponse,
|
|
|
|
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-04-01 00:20:34 +02:00
|
|
|
module.exports = new PostsController();
|