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');
|
|
|
|
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-03-19 21:37:04 +01:00
|
|
|
|
2016-06-13 23:10:53 +02:00
|
|
|
class PostListController {
|
2016-06-02 00:07:51 +02:00
|
|
|
constructor() {
|
|
|
|
this._postsHeaderView = new PostsHeaderView();
|
|
|
|
this._postsPageView = new PostsPageView();
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:49:26 +02:00
|
|
|
registerRoutes() {
|
2016-06-12 20:11:43 +02:00
|
|
|
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-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
|
|
|
_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 PostListController();
|