2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-06 21:49:26 +02:00
|
|
|
const page = require('page');
|
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-04-01 00:20:34 +02:00
|
|
|
const topNavController = require('../controllers/top_nav_controller.js');
|
2016-06-02 00:07:51 +02:00
|
|
|
const pageController = require('../controllers/page_controller.js');
|
|
|
|
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-05-20 21:35:12 +02:00
|
|
|
page('/upload', (ctx, next) => { this._uploadPostsRoute(); });
|
2016-05-29 12:22:06 +02:00
|
|
|
page('/posts/:query?',
|
|
|
|
(ctx, next) => { misc.parseSearchQueryRoute(ctx, next); },
|
2016-06-02 00:07:51 +02:00
|
|
|
(ctx, next) => { this._listPostsRoute(ctx); });
|
2016-04-06 21:49:26 +02:00
|
|
|
page(
|
|
|
|
'/post/:id',
|
2016-06-06 20:57:22 +02:00
|
|
|
(ctx, next) => { this._showPostRoute(ctx.params.id, false); });
|
2016-04-06 21:49:26 +02:00
|
|
|
page(
|
|
|
|
'/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-04-01 00:20:34 +02:00
|
|
|
topNavController.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-04-01 00:20:34 +02:00
|
|
|
topNavController.activate('posts');
|
2016-06-02 00:07:51 +02:00
|
|
|
|
|
|
|
pageController.run({
|
|
|
|
state: ctx.state,
|
|
|
|
requestPage: page => {
|
2016-06-06 20:57:22 +02:00
|
|
|
const text = this._decorateSearchQuery(ctx.searchQuery.text);
|
2016-06-02 00:07:51 +02:00
|
|
|
return api.get(
|
|
|
|
`/posts/?query=${text}&page=${page}&pageSize=40&_fields=` +
|
2016-06-03 19:12:10 +02:00
|
|
|
`id,type,tags,score,favoriteCount,` +
|
|
|
|
`commentCount,thumbnailUrl`);
|
2016-06-02 00:07:51 +02:00
|
|
|
},
|
|
|
|
clientUrl: '/posts/' + misc.formatSearchQuery({
|
|
|
|
text: ctx.searchQuery.text, page: '{page}'}),
|
|
|
|
searchQuery: ctx.searchQuery,
|
|
|
|
headerRenderer: this._postsHeaderView,
|
|
|
|
pageRenderer: this._postsPageView,
|
|
|
|
});
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 20:57:22 +02:00
|
|
|
_showPostRoute(id, editMode) {
|
2016-04-01 00:20:34 +02:00
|
|
|
topNavController.activate('posts');
|
2016-06-06 20:57:22 +02:00
|
|
|
Promise.all([
|
|
|
|
api.get('/post/' + id),
|
|
|
|
api.get(`/post/${id}/around?_fields=id&query=`
|
|
|
|
+ this._decorateSearchQuery('')),
|
|
|
|
]).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,
|
|
|
|
});
|
|
|
|
}, 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();
|