2016-06-13 23:10:53 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-23 21:18:03 +02:00
|
|
|
const api = require('../api.js');
|
2016-08-20 22:40:25 +02:00
|
|
|
const router = require('../router.js');
|
|
|
|
const misc = require('../util/misc.js');
|
2016-06-14 10:31:48 +02:00
|
|
|
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');
|
2016-08-23 21:18:03 +02:00
|
|
|
const EmptyView = require('../views/empty_view.js');
|
2016-06-13 23:10:53 +02:00
|
|
|
|
2017-01-06 14:05:54 +01: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() {
|
2017-01-06 14:05:54 +01:00
|
|
|
this._lastCancellablePromise = null;
|
2016-09-29 21:36:59 +02:00
|
|
|
|
2016-08-23 21:18:03 +02:00
|
|
|
if (!api.hasPrivilege('posts:create')) {
|
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError('You don\'t have privileges to upload posts.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
topNavigation.activate('upload');
|
2016-07-13 21:50:07 +02:00
|
|
|
topNavigation.setTitle('Upload');
|
2016-08-23 21:50:22 +02:00
|
|
|
this._view = new PostUploadView({
|
|
|
|
canUploadAnonymously: api.hasPrivilege('posts:create:anonymous'),
|
2017-01-06 14:05:54 +01:00
|
|
|
canViewPosts: api.hasPrivilege('posts:view'),
|
2016-08-23 21:50:22 +02:00
|
|
|
});
|
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();
|
2017-01-06 14:05:54 +01:00
|
|
|
this._view.clearMessages();
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 21:36:59 +02:00
|
|
|
_evtCancel(e) {
|
2017-01-06 14:05:54 +01:00
|
|
|
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();
|
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
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.');
|
|
|
|
}, errorContext => {
|
|
|
|
if (errorContext.constructor === Array) {
|
|
|
|
const [errorMessage, uploadable] = errorContext;
|
|
|
|
this._view.showError(genericErrorMessage);
|
|
|
|
this._view.showError(errorMessage, uploadable);
|
|
|
|
} else {
|
|
|
|
this._view.showError(errorContext);
|
|
|
|
}
|
|
|
|
this._view.enableForm();
|
|
|
|
return Promise.reject();
|
|
|
|
});
|
|
|
|
}
|
2016-09-29 22:26:37 +02:00
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
_uploadSinglePost(uploadable, skipDuplicates) {
|
|
|
|
let post = new Post();
|
|
|
|
post.safety = uploadable.safety;
|
|
|
|
post.flags = uploadable.flags;
|
2016-09-29 22:26:37 +02:00
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
if (uploadable.url) {
|
|
|
|
post.newContentUrl = uploadable.url;
|
|
|
|
} else {
|
|
|
|
post.newContent = uploadable.file;
|
|
|
|
}
|
2016-09-29 22:26:37 +02:00
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
let savePromise = post.save(uploadable.anonymous)
|
2016-09-29 22:26:37 +02:00
|
|
|
.then(() => {
|
2017-01-06 14:05:54 +01:00
|
|
|
this._view.removeUploadable(uploadable);
|
|
|
|
return Promise.resolve();
|
2016-08-20 22:40:25 +02:00
|
|
|
}, errorMessage => {
|
2017-01-06 14:05:54 +01:00
|
|
|
// XXX:
|
|
|
|
// lame, API eats error codes so we need to match
|
|
|
|
// messages instead
|
|
|
|
if (skipDuplicates &&
|
|
|
|
errorMessage.match(/already uploaded/)) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return Promise.reject([errorMessage, uploadable, null]);
|
2016-08-20 22:40:25 +02:00
|
|
|
});
|
2017-01-06 14:05:54 +01:00
|
|
|
this._lastCancellablePromise = savePromise;
|
|
|
|
return savePromise;
|
2016-06-13 23:10:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
module.exports = router => {
|
|
|
|
router.enter('/upload', (ctx, next) => {
|
|
|
|
ctx.controller = new PostUploadController();
|
|
|
|
});
|
|
|
|
};
|