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