szurubooru/client/js/controllers/posts_controller.js

100 lines
3.5 KiB
JavaScript
Raw Normal View History

'use strict';
const page = require('page');
const api = require('../api.js');
2016-06-03 19:12:10 +02:00
const settings = require('../settings.js');
const events = require('../events.js');
2016-06-03 19:12:10 +02:00
const misc = require('../util/misc.js');
const topNavController = require('../controllers/top_nav_controller.js');
const pageController = require('../controllers/page_controller.js');
const PostsHeaderView = require('../views/posts_header_view.js');
const PostsPageView = require('../views/posts_page_view.js');
const PostView = require('../views/post_view.js');
const EmptyView = require('../views/empty_view.js');
class PostsController {
constructor() {
this._postsHeaderView = new PostsHeaderView();
this._postsPageView = new PostsPageView();
this._postView = new PostView();
}
registerRoutes() {
page('/upload', (ctx, next) => { this._uploadPostsRoute(); });
2016-05-29 12:22:06 +02:00
page('/posts/:query?',
(ctx, next) => { misc.parseSearchQueryRoute(ctx, next); },
(ctx, next) => { this._listPostsRoute(ctx); });
page(
'/post/:id',
(ctx, next) => { this._showPostRoute(ctx.params.id, false); });
page(
'/post/:id/edit',
(ctx, next) => { this._showPostRoute(ctx.params.id, true); });
this._emptyView = new EmptyView();
}
_uploadPostsRoute() {
topNavController.activate('upload');
this._emptyView.render();
}
_listPostsRoute(ctx) {
topNavController.activate('posts');
pageController.run({
2016-06-11 09:59:29 +02:00
searchQuery: ctx.searchQuery,
clientUrl: '/posts/' + misc.formatSearchQuery({
text: ctx.searchQuery.text, page: '{page}'}),
requestPage: page => {
const text = this._decorateSearchQuery(ctx.searchQuery.text);
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`);
},
headerRenderer: this._postsHeaderView,
pageRenderer: this._postsPageView,
2016-06-11 09:59:29 +02:00
pageContext: {
canViewPosts: api.hasPrivilege('posts:view'),
}
});
}
_showPostRoute(id, editMode) {
topNavController.activate('posts');
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,
2016-06-11 09:59:29 +02:00
canEditPosts: api.hasPrivilege('posts:edit'),
});
}, response => {
this._emptyView.render();
events.notify(events.Error, response.description);
});
}
_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();
}
}
module.exports = new PostsController();