REFACTOR: admin-user (#7475)

This commit is contained in:
Joffrey JAFFEUX 2019-05-07 10:53:21 +02:00 committed by GitHub
parent f73ed45429
commit 4bbb34f7cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -52,7 +52,7 @@ const AdminUser = Discourse.User.extend({
canResetBounceScore: Ember.computed.gt("bounce_score", 0), canResetBounceScore: Ember.computed.gt("bounce_score", 0),
resetBounceScore() { resetBounceScore() {
return ajax(`/admin/users/${this.get("id")}/reset_bounce_score`, { return ajax(`/admin/users/${this.id}/reset_bounce_score`, {
type: "POST" type: "POST"
}).then(() => }).then(() =>
this.setProperties({ this.setProperties({
@ -63,25 +63,24 @@ const AdminUser = Discourse.User.extend({
}, },
generateApiKey() { generateApiKey() {
const self = this; return ajax(`/admin/users/${this.id}/generate_api_key`, {
return ajax("/admin/users/" + this.get("id") + "/generate_api_key", {
type: "POST" type: "POST"
}).then(function(result) { }).then(result => {
const apiKey = ApiKey.create(result.api_key); const apiKey = ApiKey.create(result.api_key);
self.set("api_key", apiKey); this.set("api_key", apiKey);
return apiKey; return apiKey;
}); });
}, },
groupAdded(added) { groupAdded(added) {
return ajax("/admin/users/" + this.get("id") + "/groups", { return ajax(`/admin/users/${this.id}/groups`, {
type: "POST", type: "POST",
data: { group_id: added.id } data: { group_id: added.id }
}).then(() => this.get("groups").pushObject(added)); }).then(() => this.get("groups").pushObject(added));
}, },
groupRemoved(groupId) { groupRemoved(groupId) {
return ajax("/admin/users/" + this.get("id") + "/groups/" + groupId, { return ajax(`/admin/users/${this.id}/groups/${groupId}`, {
type: "DELETE" type: "DELETE"
}).then(() => { }).then(() => {
this.set("groups.[]", this.get("groups").rejectBy("id", groupId)); this.set("groups.[]", this.get("groups").rejectBy("id", groupId));
@ -92,80 +91,84 @@ const AdminUser = Discourse.User.extend({
}, },
revokeApiKey() { revokeApiKey() {
return ajax("/admin/users/" + this.get("id") + "/revoke_api_key", { return ajax(`/admin/users/${this.id}/revoke_api_key`, {
type: "DELETE" type: "DELETE"
}).then(() => this.set("api_key", null)); }).then(() => this.set("api_key", null));
}, },
deleteAllPosts() { deleteAllPosts() {
let deletedPosts = 0; let deletedPosts = 0;
const user = this, const user = this;
message = I18n.messageFormat("admin.user.delete_all_posts_confirm_MF", { const message = I18n.messageFormat(
"admin.user.delete_all_posts_confirm_MF",
{
POSTS: user.get("post_count"), POSTS: user.get("post_count"),
TOPICS: user.get("topic_count") TOPICS: user.get("topic_count")
}), }
buttons = [ );
{ const buttons = [
label: I18n.t("composer.cancel"), {
class: "d-modal-cancel", label: I18n.t("composer.cancel"),
link: true class: "d-modal-cancel",
}, link: true
{ },
label: {
`${iconHTML("exclamation-triangle")} ` + label:
I18n.t("admin.user.delete_all_posts"), `${iconHTML("exclamation-triangle")} ` +
class: "btn btn-danger", I18n.t("admin.user.delete_all_posts"),
callback: () => { class: "btn btn-danger",
openProgressModal(); callback: () => {
openProgressModal();
performDelete();
}
}
];
const openProgressModal = () => {
bootbox.dialog(
`<p>${I18n.t(
"admin.user.delete_posts_progress"
)}</p><div class='progress-bar'><span></span></div>`,
[],
{ classes: "delete-posts-progress" }
);
};
const performDelete = () => {
let deletedPercentage = 0;
return ajax(`/admin/users/${user.get("id")}/delete_posts_batch`, {
type: "PUT"
})
.then(({ posts_deleted }) => {
if (posts_deleted === 0) {
user.set("post_count", 0);
bootbox.hideAll();
} else {
deletedPosts += posts_deleted;
deletedPercentage = Math.floor(
(deletedPosts * 100) / user.get("post_count")
);
$(".delete-posts-progress .progress-bar > span").css({
width: `${deletedPercentage}%`
});
performDelete(); performDelete();
} }
}
],
openProgressModal = () => {
bootbox.dialog(
`<p>${I18n.t(
"admin.user.delete_posts_progress"
)}</p><div class='progress-bar'><span></span></div>`,
[],
{ classes: "delete-posts-progress" }
);
},
performDelete = () => {
let deletedPercentage = 0;
return ajax(`/admin/users/${user.get("id")}/delete_posts_batch`, {
type: "PUT"
}) })
.then(({ posts_deleted }) => { .catch(e => {
if (posts_deleted === 0) { bootbox.hideAll();
user.set("post_count", 0); let error;
bootbox.hideAll(); AdminUser.find(user.get("id")).then(u => user.setProperties(u));
} else { if (e.responseJSON && e.responseJSON.errors) {
deletedPosts += posts_deleted; error = e.responseJSON.errors[0];
deletedPercentage = Math.floor( }
(deletedPosts * 100) / user.get("post_count") error = error || I18n.t("admin.user.delete_posts_failed");
); bootbox.alert(error);
$(".delete-posts-progress .progress-bar > span").css({ });
width: `${deletedPercentage}%` };
});
performDelete();
}
})
.catch(e => {
bootbox.hideAll();
let error;
AdminUser.find(user.get("id")).then(u => user.setProperties(u));
if (e.responseJSON && e.responseJSON.errors) {
error = e.responseJSON.errors[0];
}
error = error || I18n.t("admin.user.delete_posts_failed");
bootbox.alert(error);
});
};
bootbox.dialog(message, buttons, { classes: "delete-all-posts" }); bootbox.dialog(message, buttons, { classes: "delete-all-posts" });
}, },
revokeAdmin() { revokeAdmin() {
return ajax(`/admin/users/${this.get("id")}/revoke_admin`, { return ajax(`/admin/users/${this.id}/revoke_admin`, {
type: "PUT" type: "PUT"
}).then(() => { }).then(() => {
this.setProperties({ this.setProperties({
@ -177,7 +180,7 @@ const AdminUser = Discourse.User.extend({
}, },
grantAdmin() { grantAdmin() {
return ajax(`/admin/users/${this.get("id")}/grant_admin`, { return ajax(`/admin/users/${this.id}/grant_admin`, {
type: "PUT" type: "PUT"
}) })
.then(() => { .then(() => {
@ -187,12 +190,11 @@ const AdminUser = Discourse.User.extend({
}, },
revokeModeration() { revokeModeration() {
const self = this; return ajax(`/admin/users/${this.id}/revoke_moderation`, {
return ajax("/admin/users/" + this.get("id") + "/revoke_moderation", {
type: "PUT" type: "PUT"
}) })
.then(function() { .then(() => {
self.setProperties({ this.setProperties({
moderator: false, moderator: false,
can_grant_moderation: true, can_grant_moderation: true,
can_revoke_moderation: false can_revoke_moderation: false
@ -202,12 +204,11 @@ const AdminUser = Discourse.User.extend({
}, },
grantModeration() { grantModeration() {
const self = this; return ajax(`/admin/users/${this.id}/grant_moderation`, {
return ajax("/admin/users/" + this.get("id") + "/grant_moderation", {
type: "PUT" type: "PUT"
}) })
.then(function() { .then(() => {
self.setProperties({ this.setProperties({
moderator: true, moderator: true,
can_grant_moderation: false, can_grant_moderation: false,
can_revoke_moderation: true can_revoke_moderation: true
@ -217,7 +218,7 @@ const AdminUser = Discourse.User.extend({
}, },
disableSecondFactor() { disableSecondFactor() {
return ajax(`/admin/users/${this.get("id")}/disable_second_factor`, { return ajax(`/admin/users/${this.id}/disable_second_factor`, {
type: "PUT" type: "PUT"
}) })
.then(() => { .then(() => {
@ -227,11 +228,10 @@ const AdminUser = Discourse.User.extend({
}, },
approve() { approve() {
const self = this; return ajax(`/admin/users/${this.id}/approve`, {
return ajax("/admin/users/" + this.get("id") + "/approve", {
type: "PUT" type: "PUT"
}).then(function() { }).then(() => {
self.setProperties({ this.setProperties({
can_approve: false, can_approve: false,
approved: true, approved: true,
approved_by: Discourse.User.current() approved_by: Discourse.User.current()
@ -246,14 +246,12 @@ const AdminUser = Discourse.User.extend({
dirty: propertyNotEqual("originalTrustLevel", "trustLevel.id"), dirty: propertyNotEqual("originalTrustLevel", "trustLevel.id"),
saveTrustLevel() { saveTrustLevel() {
return ajax("/admin/users/" + this.id + "/trust_level", { return ajax(`/admin/users/${this.id}/trust_level`, {
type: "PUT", type: "PUT",
data: { level: this.get("trustLevel.id") } data: { level: this.get("trustLevel.id") }
}) })
.then(function() { .then(() => window.location.reload())
window.location.reload(); .catch(e => {
})
.catch(function(e) {
let error; let error;
if (e.responseJSON && e.responseJSON.errors) { if (e.responseJSON && e.responseJSON.errors) {
error = e.responseJSON.errors[0]; error = e.responseJSON.errors[0];
@ -261,7 +259,7 @@ const AdminUser = Discourse.User.extend({
error = error =
error || error ||
I18n.t("admin.user.trust_level_change_failed", { I18n.t("admin.user.trust_level_change_failed", {
error: "http: " + e.status + " - " + e.body error: this._formatError(e)
}); });
bootbox.alert(error); bootbox.alert(error);
}); });
@ -272,14 +270,12 @@ const AdminUser = Discourse.User.extend({
}, },
lockTrustLevel(locked) { lockTrustLevel(locked) {
return ajax("/admin/users/" + this.id + "/trust_level_lock", { return ajax(`/admin/users/${this.id}/trust_level_lock`, {
type: "PUT", type: "PUT",
data: { locked: !!locked } data: { locked: !!locked }
}) })
.then(function() { .then(() => window.location.reload())
window.location.reload(); .catch(e => {
})
.catch(function(e) {
let error; let error;
if (e.responseJSON && e.responseJSON.errors) { if (e.responseJSON && e.responseJSON.errors) {
error = e.responseJSON.errors[0]; error = e.responseJSON.errors[0];
@ -287,16 +283,13 @@ const AdminUser = Discourse.User.extend({
error = error =
error || error ||
I18n.t("admin.user.trust_level_change_failed", { I18n.t("admin.user.trust_level_change_failed", {
error: "http: " + e.status + " - " + e.body error: this._formatError(e)
}); });
bootbox.alert(error); bootbox.alert(error);
}); });
}, },
@computed("trust_level") canLockTrustLevel: Ember.computed.lt("trust_level", 4),
canLockTrustLevel(trustLevel) {
return trustLevel < 4;
},
canSuspend: Ember.computed.not("staff"), canSuspend: Ember.computed.not("staff"),
@ -324,9 +317,7 @@ const AdminUser = Discourse.User.extend({
return ajax("/admin/users/" + this.id + "/log_out", { return ajax("/admin/users/" + this.id + "/log_out", {
type: "POST", type: "POST",
data: { username_or_email: this.get("username") } data: { username_or_email: this.get("username") }
}).then(function() { }).then(() => bootbox.alert(I18n.t("admin.user.logged_out")));
bootbox.alert(I18n.t("admin.user.logged_out"));
});
}, },
impersonate() { impersonate() {
@ -334,10 +325,8 @@ const AdminUser = Discourse.User.extend({
type: "POST", type: "POST",
data: { username_or_email: this.get("username") } data: { username_or_email: this.get("username") }
}) })
.then(function() { .then(() => (document.location = Discourse.getURL("/")))
document.location = Discourse.getURL("/"); .catch(e => {
})
.catch(function(e) {
if (e.status === 404) { if (e.status === 404) {
bootbox.alert(I18n.t("admin.impersonate.not_found")); bootbox.alert(I18n.t("admin.impersonate.not_found"));
} else { } else {
@ -347,31 +336,27 @@ const AdminUser = Discourse.User.extend({
}, },
activate() { activate() {
return ajax("/admin/users/" + this.id + "/activate", { return ajax(`/admin/users/${this.id}/activate`, {
type: "PUT" type: "PUT"
}) })
.then(function() { .then(() => window.location.reload())
window.location.reload(); .catch(e => {
}) const error = I18n.t("admin.user.activate_failed", {
.catch(function(e) { error: this._formatError(e)
var error = I18n.t("admin.user.activate_failed", {
error: "http: " + e.status + " - " + e.body
}); });
bootbox.alert(error); bootbox.alert(error);
}); });
}, },
deactivate() { deactivate() {
return ajax("/admin/users/" + this.id + "/deactivate", { return ajax(`/admin/users/${this.id}/deactivate`, {
type: "PUT", type: "PUT",
data: { context: document.location.pathname } data: { context: document.location.pathname }
}) })
.then(function() { .then(() => window.location.reload())
window.location.reload(); .catch(e => {
}) const error = I18n.t("admin.user.deactivate_failed", {
.catch(function(e) { error: this._formatError(e)
var error = I18n.t("admin.user.deactivate_failed", {
error: "http: " + e.status + " - " + e.body
}); });
bootbox.alert(error); bootbox.alert(error);
}); });
@ -383,18 +368,14 @@ const AdminUser = Discourse.User.extend({
return ajax(`/admin/users/${this.id}/unsilence`, { return ajax(`/admin/users/${this.id}/unsilence`, {
type: "PUT" type: "PUT"
}) })
.then(result => { .then(result => this.setProperties(result.unsilence))
this.setProperties(result.unsilence);
})
.catch(e => { .catch(e => {
let error = I18n.t("admin.user.unsilence_failed", { const error = I18n.t("admin.user.unsilence_failed", {
error: `http: ${e.status} - ${e.body}` error: this._formatError(e)
}); });
bootbox.alert(error); bootbox.alert(error);
}) })
.finally(() => { .finally(() => this.set("silencingUser", false));
this.set("silencingUser", false);
});
}, },
silence(data) { silence(data) {
@ -403,18 +384,14 @@ const AdminUser = Discourse.User.extend({
type: "PUT", type: "PUT",
data data
}) })
.then(result => { .then(result => this.setProperties(result.silence))
this.setProperties(result.silence);
})
.catch(e => { .catch(e => {
let error = I18n.t("admin.user.silence_failed", { const error = I18n.t("admin.user.silence_failed", {
error: `http: ${e.status} - ${e.body}` error: this._formatError(e)
}); });
bootbox.alert(error); bootbox.alert(error);
}) })
.finally(() => { .finally(() => this.set("silencingUser", false));
this.set("silencingUser", false);
});
}, },
sendActivationEmail() { sendActivationEmail() {
@ -422,25 +399,23 @@ const AdminUser = Discourse.User.extend({
type: "POST", type: "POST",
data: { username: this.get("username") } data: { username: this.get("username") }
}) })
.then(function() { .then(() => bootbox.alert(I18n.t("admin.user.activation_email_sent")))
bootbox.alert(I18n.t("admin.user.activation_email_sent"));
})
.catch(popupAjaxError); .catch(popupAjaxError);
}, },
anonymize() { anonymize() {
const user = this, const user = this;
message = I18n.t("admin.user.anonymize_confirm"); const message = I18n.t("admin.user.anonymize_confirm");
const performAnonymize = function() { const performAnonymize = function() {
return ajax("/admin/users/" + user.get("id") + "/anonymize.json", { return ajax(`/admin/users/${user.get("id")}/anonymize.json`, {
type: "PUT" type: "PUT"
}) })
.then(function(data) { .then(function(data) {
if (data.success) { if (data.success) {
if (data.username) { if (data.username) {
document.location = Discourse.getURL( document.location = Discourse.getURL(
"/admin/users/" + user.get("id") + "/" + data.username `/admin/users/${user.get("id")}/${data.username}`
); );
} else { } else {
document.location = Discourse.getURL("/admin/users/list/active"); document.location = Discourse.getURL("/admin/users/list/active");
@ -452,9 +427,7 @@ const AdminUser = Discourse.User.extend({
} }
} }
}) })
.catch(function() { .catch(() => bootbox.alert(I18n.t("admin.user.anonymize_failed")));
bootbox.alert(I18n.t("admin.user.anonymize_failed"));
});
}; };
const buttons = [ const buttons = [
@ -478,9 +451,9 @@ const AdminUser = Discourse.User.extend({
}, },
destroy(opts) { destroy(opts) {
const user = this, const user = this;
message = I18n.t("admin.user.delete_confirm"), const message = I18n.t("admin.user.delete_confirm");
location = document.location.pathname; const location = document.location.pathname;
const performDestroy = function(block) { const performDestroy = function(block) {
bootbox.dialog(I18n.t("admin.user.deleting_user")); bootbox.dialog(I18n.t("admin.user.deleting_user"));
@ -493,7 +466,7 @@ const AdminUser = Discourse.User.extend({
if (opts && opts.deletePosts) { if (opts && opts.deletePosts) {
formData["delete_posts"] = true; formData["delete_posts"] = true;
} }
return ajax("/admin/users/" + user.get("id") + ".json", { return ajax(`/admin/users/${user.get("id")}.json`, {
type: "DELETE", type: "DELETE",
data: formData data: formData
}) })
@ -545,15 +518,13 @@ const AdminUser = Discourse.User.extend({
}, },
loadDetails() { loadDetails() {
const user = this; if (this.get("loadedDetails")) {
return Ember.RSVP.resolve(this);
if (user.get("loadedDetails")) {
return Ember.RSVP.resolve(user);
} }
return AdminUser.find(user.get("id")).then(result => { return AdminUser.find(this.id).then(result => {
user.setProperties(result); const userProperties = Object.assign(result, { loadedDetails: true });
user.set("loadedDetails", true); this.setProperties(userProperties);
}); });
}, },
@ -571,23 +542,25 @@ const AdminUser = Discourse.User.extend({
silencedBy: wrapAdmin, silencedBy: wrapAdmin,
@computed("approved_by") @computed("approved_by")
approvedBy: wrapAdmin approvedBy: wrapAdmin,
_formatError(event) {
return `http: ${event.status} - ${event.body}`;
}
}); });
AdminUser.reopenClass({ AdminUser.reopenClass({
find(user_id) { find(user_id) {
return ajax("/admin/users/" + user_id + ".json").then(result => { return ajax(`/admin/users/${user_id}.json`).then(result => {
result.loadedDetails = true; result.loadedDetails = true;
return AdminUser.create(result); return AdminUser.create(result);
}); });
}, },
findAll(query, filter) { findAll(query, filter) {
return ajax("/admin/users/list/" + query + ".json", { return ajax(`/admin/users/list/${query}.json`, {
data: filter data: filter
}).then(function(users) { }).then(users => users.map(u => AdminUser.create(u)));
return users.map(u => AdminUser.create(u));
});
} }
}); });