FIX: remove rendering UX from ember models (#11724)

Fix for `admin-user.js` and `backup.js` models.
This commit is contained in:
Krzysztof Kotlarek
2021-01-20 16:04:21 +11:00
committed by GitHub
parent fb184fed06
commit 8b10fc2f8c
11 changed files with 391 additions and 341 deletions

View File

@ -1,16 +1,19 @@
import DiscourseURL, { userPath } from "discourse/lib/url";
import { and, notEmpty } from "@ember/object/computed";
import { fmt, propertyNotEqual, setting } from "discourse/lib/computed";
import AdminUser from "admin/models/admin-user";
import CanCheckEmails from "discourse/mixins/can-check-emails";
import Controller from "@ember/controller";
import I18n from "I18n";
import { ajax } from "discourse/lib/ajax";
import bootbox from "bootbox";
import discourseComputed from "discourse-common/utils/decorators";
import getURL from "discourse-common/lib/get-url";
import { htmlSafe } from "@ember/template";
import { iconHTML } from "discourse-common/lib/icon-library";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { inject as service } from "@ember/service";
import showModal from "discourse/lib/show-modal";
import { userPath } from "discourse/lib/url";
export default Controller.extend(CanCheckEmails, {
adminTools: service(),
@ -141,10 +144,21 @@ export default Controller.extend(CanCheckEmails, {
actions: {
impersonate() {
return this.model.impersonate();
return this.model
.impersonate()
.then(() => DiscourseURL.redirectTo("/"))
.catch((e) => {
if (e.status === 404) {
bootbox.alert(I18n.t("admin.impersonate.not_found"));
} else {
bootbox.alert(I18n.t("admin.impersonate.invalid"));
}
});
},
logOut() {
return this.model.logOut();
return this.model
.logOut()
.then(() => bootbox.alert(I18n.t("admin.user.logged_out")));
},
resetBounceScore() {
return this.model.resetBounceScore();
@ -152,20 +166,56 @@ export default Controller.extend(CanCheckEmails, {
approve() {
return this.model.approve(this.currentUser);
},
_formatError(event) {
return `http: ${event.status} - ${event.body}`;
},
deactivate() {
return this.model.deactivate();
return this.model
.deactivate()
.then(() =>
this.model.setProperties({ active: false, can_activate: true })
)
.catch((e) => {
const error = I18n.t("admin.user.deactivate_failed", {
error: this._formatError(e),
});
bootbox.alert(error);
});
},
sendActivationEmail() {
return this.model.sendActivationEmail();
return this.model
.sendActivationEmail()
.then(() => bootbox.alert(I18n.t("admin.user.activation_email_sent")))
.catch(popupAjaxError);
},
activate() {
return this.model.activate();
return this.model
.activate()
.then(() =>
this.model.setProperties({
active: true,
can_deactivate: !this.model.staff,
})
)
.catch((e) => {
const error = I18n.t("admin.user.activate_failed", {
error: this._formatError(e),
});
bootbox.alert(error);
});
},
revokeAdmin() {
return this.model.revokeAdmin();
},
grantAdmin() {
return this.model.grantAdmin();
return this.model
.grantAdmin()
.then(() => {
bootbox.alert(I18n.t("admin.user.grant_admin_confirm"));
})
.catch(popupAjaxError);
},
revokeModeration() {
return this.model.revokeModeration();
@ -174,13 +224,41 @@ export default Controller.extend(CanCheckEmails, {
return this.model.grantModeration();
},
saveTrustLevel() {
return this.model.saveTrustLevel();
return this.model
.saveTrustLevel()
.then(() => window.location.reload())
.catch((e) => {
let error;
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
error = e.jqXHR.responseJSON.errors[0];
}
error =
error ||
I18n.t("admin.user.trust_level_change_failed", {
error: this._formatError(e),
});
bootbox.alert(error);
});
},
restoreTrustLevel() {
return this.model.restoreTrustLevel();
},
lockTrustLevel(locked) {
return this.model.lockTrustLevel(locked);
return this.model
.lockTrustLevel(locked)
.then(() => window.location.reload())
.catch((e) => {
let error;
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
error = e.jqXHR.responseJSON.errors[0];
}
error =
error ||
I18n.t("admin.user.trust_level_change_failed", {
error: this._formatError(e),
});
bootbox.alert(error);
});
},
unsilence() {
return this.model.unsilence();
@ -189,11 +267,119 @@ export default Controller.extend(CanCheckEmails, {
return this.model.silence();
},
deleteAllPosts() {
return this.model.deleteAllPosts();
let deletedPosts = 0;
let deletedPercentage = 0;
const user = this.model;
const message = I18n.messageFormat(
"admin.user.delete_all_posts_confirm_MF",
{
POSTS: user.get("post_count"),
TOPICS: user.get("topic_count"),
}
);
const performDelete = (progressModal) => {
this.model
.deleteAllPosts()
.then(({ posts_deleted }) => {
if (posts_deleted === 0) {
user.set("post_count", 0);
progressModal.send("closeModal");
} else {
deletedPosts += posts_deleted;
deletedPercentage = Math.floor(
(deletedPosts * 100) / user.get("post_count")
);
progressModal.setProperties({
deletedPercentage: deletedPercentage,
});
performDelete(progressModal);
}
})
.catch((e) => {
progressModal.send("closeModal");
let error;
AdminUser.find(user.get("id")).then((u) => user.setProperties(u));
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
error = e.jqXHR.responseJSON.errors[0];
}
error = error || I18n.t("admin.user.delete_posts_failed");
bootbox.alert(error);
});
};
const buttons = [
{
label: I18n.t("composer.cancel"),
class: "d-modal-cancel",
link: true,
},
{
label:
`${iconHTML("exclamation-triangle")} ` +
I18n.t("admin.user.delete_all_posts"),
class: "btn btn-danger",
callback: () => {
const progressModal = openProgressModal();
performDelete(progressModal);
},
},
];
const openProgressModal = () => {
return showModal("admin-delete-user-posts-progress", {
admin: true,
});
};
bootbox.dialog(message, buttons, { classes: "delete-all-posts" });
},
anonymize() {
return this.model.anonymize();
const user = this.model;
const message = I18n.t("admin.user.anonymize_confirm");
const performAnonymize = () => {
this.model
.anonymize()
.then((data) => {
if (data.success) {
if (data.username) {
document.location = getURL(
`/admin/users/${user.get("id")}/${data.username}`
);
} else {
document.location = getURL("/admin/users/list/active");
}
} else {
bootbox.alert(I18n.t("admin.user.anonymize_failed"));
if (data.user) {
user.setProperties(data.user);
}
}
})
.catch(() => bootbox.alert(I18n.t("admin.user.anonymize_failed")));
};
const buttons = [
{
label: I18n.t("composer.cancel"),
class: "cancel",
link: true,
},
{
label:
`${iconHTML("exclamation-triangle")} ` +
I18n.t("admin.user.anonymize_yes"),
class: "btn btn-danger",
callback: () => {
performAnonymize();
},
},
];
bootbox.dialog(message, buttons, { classes: "delete-user-modal" });
},
disableSecondFactor() {
return this.model.disableSecondFactor();
},
@ -210,11 +396,68 @@ export default Controller.extend(CanCheckEmails, {
destroy() {
const postCount = this.get("model.post_count");
const maxPostCount = this.siteSettings.delete_all_posts_max;
if (postCount <= maxPostCount) {
return this.model.destroy({ deletePosts: true });
} else {
return this.model.destroy();
}
const user = this.model;
const message = I18n.t("admin.user.delete_confirm");
const location = document.location.pathname;
const performDestroy = (block) => {
bootbox.dialog(I18n.t("admin.user.deleting_user"));
let formData = { context: location };
if (block) {
formData["block_email"] = true;
formData["block_urls"] = true;
formData["block_ip"] = true;
}
if (postCount <= maxPostCount) {
formData["delete_posts"] = true;
}
this.model
.destroy(formData)
.then((data) => {
if (data.deleted) {
if (/^\/admin\/users\/list\//.test(location)) {
document.location = location;
} else {
document.location = getURL("/admin/users/list/active");
}
} else {
bootbox.alert(I18n.t("admin.user.delete_failed"));
if (data.user) {
user.setProperties(data.user);
}
}
})
.catch(() => {
AdminUser.find(user.get("id")).then((u) => user.setProperties(u));
bootbox.alert(I18n.t("admin.user.delete_failed"));
});
};
const buttons = [
{
label: I18n.t("composer.cancel"),
class: "btn",
link: true,
},
{
label:
`${iconHTML("exclamation-triangle")} ` +
I18n.t("admin.user.delete_and_block"),
class: "btn btn-danger",
callback: () => {
performDestroy(true);
},
},
{
label: I18n.t("admin.user.delete_dont_block"),
class: "btn btn-primary",
callback: () => {
performDestroy(false);
},
},
];
bootbox.dialog(message, buttons, { classes: "delete-user-modal" });
},
promptTargetUser() {
@ -235,7 +478,31 @@ export default Controller.extend(CanCheckEmails, {
},
merge(targetUsername) {
return this.model.merge({ targetUsername });
const user = this.model;
const location = document.location.pathname;
let formData = { context: location };
if (targetUsername) {
formData["target_username"] = targetUsername;
}
this.model
.merge(formData)
.then((response) => {
if (response.success) {
showModal("admin-merge-users-progress", {
admin: true,
model: this.model,
});
} else {
bootbox.alert(I18n.t("admin.user.merge_failed"));
}
})
.catch(() => {
AdminUser.find(user.id).then((u) => user.setProperties(u));
bootbox.alert(I18n.t("admin.user.merge_failed"));
});
},
viewActionLogs() {