2020-05-14 04:23:41 +08:00
|
|
|
import AdminUser from "admin/models/admin-user";
|
2017-09-13 01:04:53 +08:00
|
|
|
// A service that can act as a bridge between the front end Discourse application
|
|
|
|
// and the admin application. Use this if you need front end code to access admin
|
|
|
|
// modules. Inject it optionally, and if it exists go to town!
|
|
|
|
|
|
|
|
import I18n from "I18n";
|
2019-11-06 00:37:32 +08:00
|
|
|
import { Promise } from "rsvp";
|
2019-10-30 01:29:08 +08:00
|
|
|
import Service from "@ember/service";
|
2017-09-13 01:04:53 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2020-08-27 00:57:13 +08:00
|
|
|
import bootbox from "bootbox";
|
2017-11-15 23:46:09 +08:00
|
|
|
import { getOwner } from "discourse-common/lib/get-owner";
|
2017-09-13 01:04:53 +08:00
|
|
|
import { iconHTML } from "discourse-common/lib/icon-library";
|
2017-09-13 05:07:42 +08:00
|
|
|
import showModal from "discourse/lib/show-modal";
|
2017-09-13 01:04:53 +08:00
|
|
|
|
2019-10-30 01:29:08 +08:00
|
|
|
export default Service.extend({
|
2017-11-15 23:46:09 +08:00
|
|
|
init() {
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2017-11-15 23:46:09 +08:00
|
|
|
|
|
|
|
// TODO: Make `siteSettings` a service that can be injected
|
|
|
|
this.siteSettings = getOwner(this).lookup("site-settings:main");
|
|
|
|
},
|
|
|
|
|
2018-01-27 04:40:03 +08:00
|
|
|
showActionLogs(target, filters) {
|
|
|
|
const controller = getOwner(target).lookup(
|
|
|
|
"controller:adminLogs.staffActionLogs"
|
|
|
|
);
|
|
|
|
target.transitionToRoute("adminLogs.staffActionLogs").then(() => {
|
2020-08-13 03:28:35 +08:00
|
|
|
controller.changeFilters(filters);
|
2018-01-27 04:40:03 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-09-13 01:04:53 +08:00
|
|
|
checkSpammer(userId) {
|
|
|
|
return AdminUser.find(userId).then((au) => this.spammerDetails(au));
|
|
|
|
},
|
|
|
|
|
2021-01-27 00:10:46 +08:00
|
|
|
deleteUser(id, formData) {
|
|
|
|
return AdminUser.find(id).then((user) => user.destroy(formData));
|
2018-01-27 04:40:03 +08:00
|
|
|
},
|
|
|
|
|
2017-09-13 01:04:53 +08:00
|
|
|
spammerDetails(adminUser) {
|
|
|
|
return {
|
|
|
|
deleteUser: () => this._deleteSpammer(adminUser),
|
|
|
|
canDelete:
|
|
|
|
adminUser.get("can_be_deleted") &&
|
|
|
|
adminUser.get("can_delete_all_posts"),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-11-14 02:41:36 +08:00
|
|
|
_showControlModal(type, user, opts) {
|
2017-09-15 02:10:39 +08:00
|
|
|
opts = opts || {};
|
|
|
|
|
2017-11-14 02:41:36 +08:00
|
|
|
let controller = showModal(`admin-${type}-user`, {
|
2017-09-13 05:07:42 +08:00
|
|
|
admin: true,
|
2017-11-14 02:41:36 +08:00
|
|
|
modalClass: `${type}-user-modal`,
|
2017-09-13 05:07:42 +08:00
|
|
|
});
|
2019-01-04 01:03:01 +08:00
|
|
|
controller.setProperties({ postId: opts.postId, postEdit: opts.postEdit });
|
2017-09-15 02:10:39 +08:00
|
|
|
|
2018-01-11 03:48:11 +08:00
|
|
|
return (user.adminUserView
|
2019-11-06 00:37:32 +08:00
|
|
|
? Promise.resolve(user)
|
2018-01-11 03:48:11 +08:00
|
|
|
: AdminUser.find(user.get("id"))
|
|
|
|
).then((loadedUser) => {
|
2017-09-15 02:10:39 +08:00
|
|
|
controller.setProperties({
|
|
|
|
user: loadedUser,
|
|
|
|
loadingUser: false,
|
2018-01-31 05:31:29 +08:00
|
|
|
before: opts.before,
|
2017-09-15 02:10:39 +08:00
|
|
|
successCallback: opts.successCallback,
|
|
|
|
});
|
|
|
|
});
|
2017-09-13 05:07:42 +08:00
|
|
|
},
|
|
|
|
|
2017-11-14 02:41:36 +08:00
|
|
|
showSilenceModal(user, opts) {
|
|
|
|
this._showControlModal("silence", user, opts);
|
|
|
|
},
|
|
|
|
|
|
|
|
showSuspendModal(user, opts) {
|
|
|
|
this._showControlModal("suspend", user, opts);
|
|
|
|
},
|
|
|
|
|
2017-09-13 01:04:53 +08:00
|
|
|
_deleteSpammer(adminUser) {
|
2017-11-15 23:46:09 +08:00
|
|
|
// Try loading the email if the site supports it
|
2019-03-14 05:30:25 +08:00
|
|
|
let tryEmail = this.siteSettings.moderators_view_emails
|
2017-11-15 23:46:09 +08:00
|
|
|
? adminUser.checkEmail()
|
2019-11-06 00:37:32 +08:00
|
|
|
: Promise.resolve();
|
2017-11-15 23:46:09 +08:00
|
|
|
|
|
|
|
return tryEmail.then(() => {
|
2017-09-13 01:04:53 +08:00
|
|
|
let message = I18n.messageFormat("flagging.delete_confirm_MF", {
|
|
|
|
POSTS: adminUser.get("post_count"),
|
|
|
|
TOPICS: adminUser.get("topic_count"),
|
|
|
|
email:
|
|
|
|
adminUser.get("email") || I18n.t("flagging.hidden_email_address"),
|
|
|
|
ip_address:
|
|
|
|
adminUser.get("ip_address") || I18n.t("flagging.ip_address_missing"),
|
|
|
|
});
|
|
|
|
|
|
|
|
let userId = adminUser.get("id");
|
|
|
|
|
2019-11-06 00:37:32 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-09-13 01:04:53 +08:00
|
|
|
const buttons = [
|
|
|
|
{
|
|
|
|
label: I18n.t("composer.cancel"),
|
2017-09-14 01:23:19 +08:00
|
|
|
class: "d-modal-cancel",
|
2017-09-13 01:04:53 +08:00
|
|
|
link: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label:
|
|
|
|
`${iconHTML("exclamation-triangle")} ` +
|
|
|
|
I18n.t("flagging.yes_delete_spammer"),
|
|
|
|
class: "btn btn-danger confirm-delete",
|
|
|
|
callback() {
|
|
|
|
return ajax(`/admin/users/${userId}.json`, {
|
|
|
|
type: "DELETE",
|
|
|
|
data: {
|
|
|
|
delete_posts: true,
|
|
|
|
block_email: true,
|
|
|
|
block_urls: true,
|
|
|
|
block_ip: true,
|
|
|
|
delete_as_spammer: true,
|
|
|
|
context: window.location.pathname,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
if (result.deleted) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
2018-06-05 22:43:45 +08:00
|
|
|
throw new Error("failed to delete");
|
2017-09-13 01:04:53 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
bootbox.alert(I18n.t("admin.user.delete_failed"));
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
bootbox.dialog(message, buttons, {
|
|
|
|
classes: "flagging-delete-spammer",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|