2020-06-06 00:03:37 +02:00
|
|
|
"use strict";
|
2016-06-13 23:10:53 +02:00
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
const api = require("../api.js");
|
|
|
|
const router = require("../router.js");
|
|
|
|
const uri = require("../util/uri.js");
|
|
|
|
const misc = require("../util/misc.js");
|
|
|
|
const progress = require("../util/progress.js");
|
|
|
|
const topNavigation = require("../models/top_navigation.js");
|
|
|
|
const Post = require("../models/post.js");
|
|
|
|
const Tag = require("../models/tag.js");
|
|
|
|
const PostUploadView = require("../views/post_upload_view.js");
|
|
|
|
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 =
|
2021-05-08 08:02:59 +02:00
|
|
|
"One or more posts needs your attention; " +
|
2017-01-06 14:05:54 +01:00
|
|
|
'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
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
if (!api.hasPrivilege("posts:create")) {
|
2016-08-23 21:18:03 +02:00
|
|
|
this._view = new EmptyView();
|
2020-06-06 00:03:37 +02:00
|
|
|
this._view.showError("You don't have privileges to upload posts.");
|
2016-08-23 21:18:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
topNavigation.activate("upload");
|
|
|
|
topNavigation.setTitle("Upload");
|
2016-08-23 21:50:22 +02:00
|
|
|
this._view = new PostUploadView({
|
2020-06-06 00:03:37 +02:00
|
|
|
canUploadAnonymously: api.hasPrivilege("posts:create:anonymous"),
|
|
|
|
canViewPosts: api.hasPrivilege("posts:view"),
|
2018-06-25 16:47:20 +02:00
|
|
|
enableSafety: api.safetyEnabled(),
|
2016-08-23 21:50:22 +02:00
|
|
|
});
|
2020-06-06 00:03:37 +02:00
|
|
|
this._view.addEventListener("change", (e) => this._evtChange(e));
|
|
|
|
this._view.addEventListener("submit", (e) => this._evtSubmit(e));
|
|
|
|
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();
|
2021-05-08 08:02:59 +02:00
|
|
|
let anyFailures = false;
|
2016-08-20 22:40:25 +02:00
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
e.detail.uploadables
|
|
|
|
.reduce(
|
|
|
|
(promise, uploadable) =>
|
|
|
|
promise.then(() =>
|
|
|
|
this._uploadSinglePost(
|
|
|
|
uploadable,
|
2021-05-08 08:11:29 +02:00
|
|
|
e.detail.skipDuplicates,
|
|
|
|
e.detail.alwaysUploadSimilar
|
2021-09-13 19:26:57 +02:00
|
|
|
).catch((error) => {
|
2021-05-08 08:02:59 +02:00
|
|
|
anyFailures = true;
|
|
|
|
if (error.uploadable) {
|
|
|
|
if (error.similarPosts) {
|
2021-09-13 19:26:57 +02:00
|
|
|
error.uploadable.lookalikes =
|
|
|
|
error.similarPosts;
|
|
|
|
this._view.updateUploadable(
|
|
|
|
error.uploadable
|
|
|
|
);
|
2021-05-08 08:02:59 +02:00
|
|
|
this._view.showInfo(
|
|
|
|
error.message,
|
|
|
|
error.uploadable
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this._view.showError(
|
|
|
|
error.message,
|
|
|
|
error.uploadable
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._view.showError(
|
|
|
|
error.message,
|
2021-05-08 08:11:29 +02:00
|
|
|
uploadable
|
2021-05-08 08:02:59 +02:00
|
|
|
);
|
|
|
|
}
|
2021-11-30 02:06:20 +01:00
|
|
|
if (e.detail.pauseRemainOnError) {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
2021-05-08 08:02:59 +02:00
|
|
|
})
|
2020-06-06 00:03:37 +02:00
|
|
|
),
|
|
|
|
Promise.resolve()
|
|
|
|
)
|
2021-09-13 19:26:57 +02:00
|
|
|
.then(() => {
|
|
|
|
if (anyFailures) {
|
2021-11-30 02:06:20 +01:00
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
() => {
|
2021-09-13 19:26:57 +02:00
|
|
|
this._view.clearMessages();
|
|
|
|
misc.disableExitConfirmation();
|
|
|
|
const ctx = router.show(uri.formatClientLink("posts"));
|
|
|
|
ctx.controller.showSuccess("Posts uploaded.");
|
2021-11-30 02:06:20 +01:00
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this._view.showError(genericErrorMessage);
|
|
|
|
this._view.enableForm();
|
2020-06-04 20:09:35 +02:00
|
|
|
}
|
2021-11-30 02:06:20 +01:00
|
|
|
);
|
2017-01-06 14:05:54 +01:00
|
|
|
}
|
2016-09-29 22:26:37 +02:00
|
|
|
|
2021-05-08 08:11:29 +02:00
|
|
|
_uploadSinglePost(uploadable, skipDuplicates, alwaysUploadSimilar) {
|
2017-01-08 22:24:46 +01:00
|
|
|
progress.start();
|
2017-01-07 11:07:51 +01:00
|
|
|
let reverseSearchPromise = Promise.resolve();
|
2017-01-08 20:57:08 +01:00
|
|
|
if (!uploadable.lookalikesConfirmed) {
|
2020-06-06 00:03:37 +02:00
|
|
|
reverseSearchPromise = Post.reverseSearch(
|
|
|
|
uploadable.url || uploadable.file
|
|
|
|
);
|
2017-01-07 11:07:51 +01:00
|
|
|
}
|
|
|
|
this._lastCancellablePromise = reverseSearchPromise;
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
return reverseSearchPromise
|
|
|
|
.then((searchResult) => {
|
|
|
|
if (searchResult) {
|
|
|
|
// notify about exact duplicate
|
|
|
|
if (searchResult.exactPost) {
|
|
|
|
if (skipDuplicates) {
|
|
|
|
this._view.removeUploadable(uploadable);
|
|
|
|
return Promise.resolve();
|
|
|
|
} else {
|
|
|
|
let error = new Error(
|
|
|
|
"Post already uploaded " +
|
|
|
|
`(@${searchResult.exactPost.id})`
|
|
|
|
);
|
|
|
|
error.uploadable = uploadable;
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify about similar posts
|
2021-09-13 19:26:57 +02:00
|
|
|
if (
|
|
|
|
searchResult.similarPosts.length &&
|
|
|
|
!alwaysUploadSimilar
|
|
|
|
) {
|
2020-06-06 00:03:37 +02:00
|
|
|
let error = new Error(
|
|
|
|
`Found ${searchResult.similarPosts.length} similar ` +
|
|
|
|
"posts.\nYou can resume or discard this upload."
|
|
|
|
);
|
2017-01-15 14:58:29 +01:00
|
|
|
error.uploadable = uploadable;
|
2020-06-06 00:03:37 +02:00
|
|
|
error.similarPosts = searchResult.similarPosts;
|
2017-01-15 14:58:29 +01:00
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
2017-01-07 11:07:51 +01:00
|
|
|
}
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
// no duplicates, proceed with saving
|
|
|
|
let post = this._uploadableToPost(uploadable);
|
|
|
|
let savePromise = post.save(uploadable.anonymous).then(() => {
|
2017-01-07 11:07:51 +01:00
|
|
|
this._view.removeUploadable(uploadable);
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
2020-06-06 00:03:37 +02:00
|
|
|
this._lastCancellablePromise = savePromise;
|
|
|
|
return savePromise;
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
(result) => {
|
|
|
|
progress.done();
|
|
|
|
return Promise.resolve(result);
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
error.uploadable = uploadable;
|
|
|
|
progress.done();
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
);
|
2017-01-07 11:07:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_uploadableToPost(uploadable) {
|
2017-01-06 14:05:54 +01:00
|
|
|
let post = new Post();
|
|
|
|
post.safety = uploadable.safety;
|
|
|
|
post.flags = uploadable.flags;
|
2017-10-01 21:46:53 +02:00
|
|
|
for (let tagName of uploadable.tags) {
|
|
|
|
const tag = new Tag();
|
|
|
|
tag.names = [tagName];
|
|
|
|
post.tags.add(tag);
|
|
|
|
}
|
2017-01-07 11:07:51 +01:00
|
|
|
post.relations = uploadable.relations;
|
2017-01-07 12:33:17 +01:00
|
|
|
post.newContent = uploadable.url || uploadable.file;
|
2019-07-23 01:20:42 +02:00
|
|
|
// if uploadable.source is ever going to be a valid field (e.g when setting source directly in the upload window)
|
|
|
|
// you'll need to change the line below to `post.source = uploadable.source || uploadable.url;`
|
2020-06-04 20:09:35 +02:00
|
|
|
if (uploadable.url) {
|
|
|
|
post.source = uploadable.url;
|
|
|
|
}
|
2017-01-07 11:07:51 +01:00
|
|
|
return post;
|
2016-06-13 23:10:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
module.exports = (router) => {
|
|
|
|
router.enter(["upload"], (ctx, next) => {
|
2016-06-14 10:31:48 +02:00
|
|
|
ctx.controller = new PostUploadController();
|
|
|
|
});
|
|
|
|
};
|