szurubooru/client/js/controllers/post_upload_controller.js

152 lines
5.5 KiB
JavaScript
Raw Normal View History

2016-06-13 23:10:53 +02:00
'use strict';
const api = require('../api.js');
2016-08-20 22:40:25 +02:00
const router = require('../router.js');
const misc = require('../util/misc.js');
const topNavigation = require('../models/top_navigation.js');
2016-08-20 22:40:25 +02:00
const Post = require('../models/post.js');
const PostUploadView = require('../views/post_upload_view.js');
const EmptyView = require('../views/empty_view.js');
2016-06-13 23:10:53 +02:00
const genericErrorMessage =
'One of the posts needs your attention; ' +
'click "resume upload" when you\'re ready.';
2016-06-13 23:10:53 +02:00
class PostUploadController {
constructor() {
this._lastCancellablePromise = null;
2016-09-29 21:36:59 +02:00
if (!api.hasPrivilege('posts:create')) {
this._view = new EmptyView();
this._view.showError('You don\'t have privileges to upload posts.');
return;
}
topNavigation.activate('upload');
topNavigation.setTitle('Upload');
this._view = new PostUploadView({
canUploadAnonymously: api.hasPrivilege('posts:create:anonymous'),
canViewPosts: api.hasPrivilege('posts:view'),
});
2016-08-20 22:40:25 +02:00
this._view.addEventListener('change', e => this._evtChange(e));
this._view.addEventListener('submit', e => this._evtSubmit(e));
2016-09-29 21:36:59 +02:00
this._view.addEventListener('cancel', e => this._evtCancel(e));
2016-08-20 22:40:25 +02:00
}
_evtChange(e) {
if (e.detail.uploadables.length) {
misc.enableExitConfirmation();
} else {
misc.disableExitConfirmation();
this._view.clearMessages();
2016-08-20 22:40:25 +02:00
}
}
2016-09-29 21:36:59 +02:00
_evtCancel(e) {
if (this._lastCancellablePromise) {
this._lastCancellablePromise.abort();
2016-09-29 21:36:59 +02:00
}
}
2016-08-20 22:40:25 +02:00
_evtSubmit(e) {
this._view.disableForm();
this._view.clearMessages();
e.detail.uploadables.reduce(
(promise, uploadable) =>
promise.then(() => this._uploadSinglePost(
uploadable, e.detail.skipDuplicates)),
Promise.resolve())
.then(() => {
this._view.clearMessages();
misc.disableExitConfirmation();
const ctx = router.show('/posts');
ctx.controller.showSuccess('Posts uploaded.');
}, ([errorMessage, uploadable, similarPostResults]) => {
if (uploadable) {
if (similarPostResults) {
uploadable.lookalikes = similarPostResults;
this._view.updateUploadable(uploadable);
this._view.showInfo(genericErrorMessage);
this._view.showInfo(errorMessage, uploadable);
} else {
this._view.showError(genericErrorMessage);
this._view.showError(errorMessage, uploadable);
}
} else {
this._view.showError(errorMessage);
}
this._view.enableForm();
return Promise.reject();
});
}
_uploadSinglePost(uploadable, skipDuplicates) {
let reverseSearchPromise = Promise.resolve();
if (!uploadable.lookalikesConfirmed &&
['image'].includes(uploadable.type)) {
reverseSearchPromise =
Post.reverseSearch(uploadable.url || uploadable.file);
}
this._lastCancellablePromise = reverseSearchPromise;
return reverseSearchPromise.then(searchResult => {
if (searchResult) {
// notify about exact duplicate
if (searchResult.exactPost && !skipDuplicates) {
return Promise.reject([
`Post already uploaded (@${searchResult.exactPost.id})`,
uploadable,
null]);
}
// notify about similar posts
if (!searchResult.exactPost
&& searchResult.similarPosts.length) {
return Promise.reject([
`Found ${searchResult.similarPosts.length} similar ` +
'posts.\nYou can resume or discard this upload.',
uploadable,
searchResult.similarPosts]);
}
}
// no duplicates, proceed with saving
let post = this._uploadableToPost(uploadable);
let apiSavePromise = post.save(uploadable.anonymous);
let returnedSavePromise = apiSavePromise
.then(() => {
this._view.removeUploadable(uploadable);
return Promise.resolve();
}, errorMessage => {
return Promise.reject([errorMessage, uploadable, null]);
});
returnedSavePromise.abort = () => {
apiSavePromise.abort();
};
this._lastCancellablePromise = returnedSavePromise;
return returnedSavePromise;
}, errorMessage => {
return Promise.reject([errorMessage, uploadable, null]);
});
}
_uploadableToPost(uploadable) {
let post = new Post();
post.safety = uploadable.safety;
post.flags = uploadable.flags;
post.tags = uploadable.tags;
post.relations = uploadable.relations;
post.newContent = uploadable.url || uploadable.file;
return post;
2016-06-13 23:10:53 +02:00
}
}
module.exports = router => {
router.enter('/upload', (ctx, next) => {
ctx.controller = new PostUploadController();
});
};