This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/plugins/chat/assets/javascripts/discourse/lib/chat-message-flag.js
Roman Rizzi 0a5f548635
DEV: Move discourse-chat to the core repo. (#18776)
As part of this move, we are also renaming `discourse-chat` to `chat`.
2022-11-02 10:41:30 -03:00

92 lines
2.2 KiB
JavaScript

import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import I18n from "I18n";
import getURL from "discourse-common/lib/get-url";
export default class ChatMessageFlag {
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("") })
);
return flag;
});
}
flagsAvailable(_controller, site, model) {
let flagsAvailable = site.flagTypes;
flagsAvailable = flagsAvailable.filter((flag) => {
return model.available_flags.includes(flag.name_key);
});
// "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);
}
create(controller, opts) {
controller.send("hideModal");
return ajax("/chat/flag", {
method: "PUT",
data: {
chat_message_id: controller.get("model.id"),
flag_type_id: controller.get("selected.id"),
message: opts.message,
is_warning: opts.isWarning,
take_action: opts.takeAction,
queue_for_review: opts.queue_for_review,
},
})
.then(() => {
if (controller.isDestroying || controller.isDestroyed) {
return;
}
if (!opts.skipClose) {
controller.send("closeModal");
}
if (opts.message) {
controller.set("message", "");
}
})
.catch((error) => {
if (!controller.isDestroying && !controller.isDestroyed) {
controller.send("closeModal");
}
popupAjaxError(error);
});
}
}