2020-05-13 16:23:41 -04:00
|
|
|
import I18n from "I18n";
|
2019-11-07 16:38:28 -05:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2019-10-30 16:28:29 -04:00
|
|
|
import { or } from "@ember/object/computed";
|
2019-10-30 09:48:24 -04:00
|
|
|
import { schedule } from "@ember/runloop";
|
2020-03-06 11:41:41 -05:00
|
|
|
import Controller, { inject as controller } from "@ember/controller";
|
2017-06-28 16:56:44 -04:00
|
|
|
import WatchedWord from "admin/models/watched-word";
|
2019-07-22 07:59:56 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { fmt } from "discourse/lib/computed";
|
2019-08-02 05:53:03 -04:00
|
|
|
import showModal from "discourse/lib/show-modal";
|
2020-08-26 12:57:13 -04:00
|
|
|
import bootbox from "bootbox";
|
2017-06-28 16:56:44 -04:00
|
|
|
|
2019-10-23 13:06:54 -04:00
|
|
|
export default Controller.extend({
|
2020-03-06 11:41:41 -05:00
|
|
|
adminWatchedWords: controller(),
|
2017-06-28 16:56:44 -04:00
|
|
|
actionNameKey: null,
|
2019-10-30 16:28:29 -04:00
|
|
|
showWordsList: or(
|
2017-06-28 16:56:44 -04:00
|
|
|
"adminWatchedWords.filtered",
|
|
|
|
"adminWatchedWords.showWords"
|
|
|
|
),
|
2019-07-22 07:59:56 -04:00
|
|
|
downloadLink: fmt(
|
|
|
|
"actionNameKey",
|
|
|
|
"/admin/logs/watched_words/action/%@/download"
|
|
|
|
),
|
2017-06-28 16:56:44 -04:00
|
|
|
|
|
|
|
findAction(actionName) {
|
|
|
|
return (this.get("adminWatchedWords.model") || []).findBy(
|
|
|
|
"nameKey",
|
|
|
|
actionName
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("actionNameKey", "adminWatchedWords.model")
|
2019-07-22 07:59:56 -04:00
|
|
|
currentAction(actionName) {
|
|
|
|
return this.findAction(actionName);
|
|
|
|
},
|
2017-06-28 16:56:44 -04:00
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("currentAction.words.[]", "adminWatchedWords.model")
|
2019-07-22 07:59:56 -04:00
|
|
|
filteredContent(words) {
|
|
|
|
return words || [];
|
2017-06-28 16:56:44 -04:00
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("actionNameKey")
|
2017-06-28 16:56:44 -04:00
|
|
|
actionDescription(actionNameKey) {
|
|
|
|
return I18n.t("admin.watched_words.action_descriptions." + actionNameKey);
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("currentAction.count")
|
2019-07-22 07:59:56 -04:00
|
|
|
wordCount(count) {
|
|
|
|
return count || 0;
|
2018-03-27 17:26:53 -04:00
|
|
|
},
|
|
|
|
|
2017-06-28 16:56:44 -04:00
|
|
|
actions: {
|
|
|
|
recordAdded(arg) {
|
2019-05-27 04:15:39 -04:00
|
|
|
const a = this.findAction(this.actionNameKey);
|
2017-06-28 16:56:44 -04:00
|
|
|
if (a) {
|
|
|
|
a.words.unshiftObject(arg);
|
|
|
|
a.incrementProperty("count");
|
2019-10-30 09:48:24 -04:00
|
|
|
schedule("afterRender", () => {
|
2017-06-28 16:56:44 -04:00
|
|
|
// remove from other actions lists
|
|
|
|
let match = null;
|
|
|
|
this.get("adminWatchedWords.model").forEach((action) => {
|
2020-09-22 10:28:28 -04:00
|
|
|
if (match) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-28 16:56:44 -04:00
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (action.nameKey !== this.actionNameKey) {
|
2017-06-28 16:56:44 -04:00
|
|
|
match = action.words.findBy("id", arg.id);
|
|
|
|
if (match) {
|
|
|
|
action.words.removeObject(match);
|
|
|
|
action.decrementProperty("count");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
recordRemoved(arg) {
|
2019-07-22 07:59:56 -04:00
|
|
|
if (this.currentAction) {
|
|
|
|
this.currentAction.words.removeObject(arg);
|
|
|
|
this.currentAction.decrementProperty("count");
|
2017-06-28 16:56:44 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
uploadComplete() {
|
|
|
|
WatchedWord.findAll().then((data) => {
|
|
|
|
this.set("adminWatchedWords.model", data);
|
|
|
|
});
|
2019-07-22 07:59:56 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
clearAll() {
|
|
|
|
const actionKey = this.actionNameKey;
|
|
|
|
bootbox.confirm(
|
|
|
|
I18n.t(`admin.watched_words.clear_all_confirm_${actionKey}`),
|
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
(result) => {
|
|
|
|
if (result) {
|
|
|
|
ajax(`/admin/logs/watched_words/action/${actionKey}.json`, {
|
2020-03-26 16:00:10 -04:00
|
|
|
type: "DELETE",
|
2019-07-22 07:59:56 -04:00
|
|
|
}).then(() => {
|
|
|
|
const action = this.findAction(actionKey);
|
|
|
|
if (action) {
|
|
|
|
action.setProperties({
|
|
|
|
words: [],
|
|
|
|
count: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2019-08-02 05:53:03 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
test() {
|
|
|
|
WatchedWord.findAll().then((data) => {
|
|
|
|
this.set("adminWatchedWords.model", data);
|
|
|
|
showModal("admin-watched-word-test", {
|
|
|
|
admin: true,
|
|
|
|
model: this.currentAction,
|
|
|
|
});
|
|
|
|
});
|
2017-06-28 16:56:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|