Files
discourse/plugins/chat/assets/javascripts/discourse/lib/chat-message-flag.js
Krzysztof Kotlarek c3fadc7330 FEATURE: created edit and delete flags (#27484)
Allow admins to create edit and delete flags.
2024-07-03 08:45:37 +10:00

90 lines
2.1 KiB
JavaScript

import { setOwner } from "@ember/application";
import { service } from "@ember/service";
import { popupAjaxError } from "discourse/lib/ajax-error";
import getURL from "discourse-common/lib/get-url";
import I18n from "discourse-i18n";
export default class ChatMessageFlag {
@service chatApi;
constructor(owner) {
setOwner(this, owner);
}
title() {
return "flagging.title";
}
customSubmitLabel() {
return "flagging.notify_action";
}
submitLabel() {
return "chat.flagging.action";
}
targetsTopic() {
return false;
}
editable() {
return false;
}
_rewriteFlagDescriptions(flags) {
return flags.map((flag) => {
flag.set(
"description",
I18n.t(`chat.flags.${flag.name_key}`, {
basePath: getURL(""),
defaultValue: flag.description,
})
);
return flag;
});
}
flagsAvailable(flagModal) {
let flagsAvailable = flagModal.site.flagTypes;
flagsAvailable = flagsAvailable.filter((flag) => {
return (
flagModal.args.model.flagModel.availableFlags.includes(flag.name_key) &&
flag.applies_to.includes("Chat::Message")
);
});
// "message user" option should be at the top
const notifyUserIndex = flagsAvailable.indexOf(
flagsAvailable.filterBy("name_key", "notify_user")[0]
);
if (notifyUserIndex !== -1) {
const notifyUser = flagsAvailable[notifyUserIndex];
flagsAvailable.splice(notifyUserIndex, 1);
flagsAvailable.splice(0, 0, notifyUser);
}
return this._rewriteFlagDescriptions(flagsAvailable);
}
async create(flagModal, opts) {
flagModal.args.closeModal();
const channelId = flagModal.args.model.flagModel.channel.id;
const messageId = flagModal.args.model.flagModel.id;
try {
await this.chatApi.flagMessage(channelId, messageId, {
flag_type_id: flagModal.selected.id,
message: opts.message,
is_warning: opts.isWarning,
take_action: opts.takeAction,
queue_for_review: opts.queue_for_review,
});
} catch (error) {
popupAjaxError(error);
}
}
}