mirror of
https://github.com/discourse/discourse.git
synced 2024-12-13 07:33:41 +08:00
35b748e7f4
This commit fixes a bug where the silence button is incorrectly displayed on the admin page of a staff user. It's not actually possible to silence a staff user because the backend correctly prevents it, but the frontend isn't checking if the button should be displayed. Another small bug that this commit fixes is the similar users list not showing up inside the silence/suspend modals due to also a bug in the frontend. I've also changed the way similar users are loaded so that they're not returned by the `admin/users#show` endpoint anymore and moved them into a new endpoint that the penalize modals (suspend and silence) can call directly to retrieve the list of users. This is done because the similar users list is never shown on the admin user page (`/admin/users/:user_id/:username`); they're only needed when the suspend or silence modals are opened. Internal topic: t/130014.
131 lines
3.5 KiB
JavaScript
131 lines
3.5 KiB
JavaScript
import Component from "@glimmer/component";
|
|
import { tracked } from "@glimmer/tracking";
|
|
import { action } from "@ember/object";
|
|
import { service } from "@ember/service";
|
|
import { isEmpty } from "@ember/utils";
|
|
import { extractError } from "discourse/lib/ajax-error";
|
|
import I18n from "discourse-i18n";
|
|
|
|
export default class PenalizeUser extends Component {
|
|
@service dialog;
|
|
@service siteSettings;
|
|
|
|
@tracked penalizeUntil = this.args.model.user.next_penalty;
|
|
@tracked confirmClose = false;
|
|
@tracked otherUserIds = [];
|
|
@tracked postAction = "delete";
|
|
@tracked postEdit = this.args.model.postEdit;
|
|
@tracked flash;
|
|
@tracked reason;
|
|
@tracked message;
|
|
|
|
constructor() {
|
|
super(...arguments);
|
|
if (this.postEdit && this.siteSettings.penalty_include_post_message) {
|
|
this.message = `-------------------\n${this.postEdit}\n-------------------`;
|
|
}
|
|
}
|
|
|
|
get modalTitle() {
|
|
if (this.args.model.penaltyType === "suspend") {
|
|
return "admin.user.suspend_modal_title";
|
|
} else if (this.args.model.penaltyType === "silence") {
|
|
return "admin.user.silence_modal_title";
|
|
}
|
|
}
|
|
|
|
get buttonLabel() {
|
|
if (this.args.model.penaltyType === "suspend") {
|
|
return "admin.user.suspend";
|
|
} else if (this.args.model.penaltyType === "silence") {
|
|
return "admin.user.silence";
|
|
}
|
|
}
|
|
|
|
get penaltyHistory() {
|
|
return I18n.messageFormat("admin.user.penalty_history_MF", {
|
|
SUSPENDED: this.args.model.user.penalty_counts?.suspended,
|
|
SILENCED: this.args.model.user.penalty_counts?.silenced,
|
|
});
|
|
}
|
|
|
|
get canPenalize() {
|
|
if (this.args.model.penaltyType === "suspend") {
|
|
return this.args.model.user.canSuspend;
|
|
} else if (this.args.model.penaltyType === "silence") {
|
|
return this.args.model.user.canSilence;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
get submitDisabled() {
|
|
return (
|
|
this.penalizing ||
|
|
isEmpty(this.penalizeUntil) ||
|
|
!this.reason ||
|
|
this.reason.length < 1
|
|
);
|
|
}
|
|
|
|
@action
|
|
async penalizeUser() {
|
|
if (this.submitDisabled) {
|
|
return;
|
|
}
|
|
this.penalizing = true;
|
|
this.confirmClose = true;
|
|
if (this.before) {
|
|
this.before();
|
|
}
|
|
|
|
let result;
|
|
try {
|
|
const opts = {
|
|
reason: this.reason,
|
|
message: this.message,
|
|
post_id: this.args.model.postId,
|
|
post_action: this.postAction,
|
|
post_edit: this.postEdit,
|
|
other_user_ids: this.otherUserIds,
|
|
};
|
|
|
|
if (this.args.model.penaltyType === "suspend") {
|
|
opts.suspend_until = this.penalizeUntil;
|
|
result = await this.args.model.user.suspend(opts);
|
|
} else if (this.args.model.penaltyType === "silence") {
|
|
opts.silenced_till = this.penalizeUntil;
|
|
result = await this.args.model.user.silence(opts);
|
|
} else {
|
|
// eslint-disable-next-line no-console
|
|
console.error("Unknown penalty type:", this.args.model.penaltyType);
|
|
}
|
|
this.args.closeModal();
|
|
if (this.successCallback) {
|
|
await this.successCallback(result);
|
|
}
|
|
} catch {
|
|
this.flash = extractError(result);
|
|
} finally {
|
|
this.penalizing = false;
|
|
}
|
|
}
|
|
|
|
@action
|
|
warnBeforeClosing() {
|
|
if (!this.confirmClose && (this.reason?.length || this.message?.length)) {
|
|
this.dialog.confirm({
|
|
message: I18n.t("admin.user.confirm_cancel_penalty"),
|
|
didConfirm: () => this.args.closeModal(),
|
|
});
|
|
return false;
|
|
}
|
|
|
|
this.args.closeModal();
|
|
}
|
|
|
|
@action
|
|
similarUsersChanged(userIds) {
|
|
this.otherUserIds = userIds;
|
|
}
|
|
}
|