2022-04-05 13:34:13 -04:00
|
|
|
import Component from "@ember/component";
|
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2022-04-05 14:23:15 -04:00
|
|
|
|
|
|
|
const enabledCategories = settings.enabled_categories
|
|
|
|
.split("|")
|
|
|
|
.map((id) => parseInt(id, 10))
|
|
|
|
.filter((id) => id);
|
2022-04-05 13:34:13 -04:00
|
|
|
|
2022-09-02 09:56:41 -04:00
|
|
|
const enabledTags = settings.enabled_tags.split("|").filter(Boolean);
|
|
|
|
|
2022-04-05 13:34:13 -04:00
|
|
|
export default Component.extend({
|
|
|
|
tagName: "",
|
|
|
|
hidden: true,
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
2022-04-05 14:23:15 -04:00
|
|
|
this.recalculate();
|
|
|
|
},
|
|
|
|
|
|
|
|
didUpdateAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.recalculate();
|
2022-04-05 13:34:13 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
2022-04-05 14:23:15 -04:00
|
|
|
document.body.classList.remove("topic-in-gated-category");
|
2022-04-05 13:34:13 -04:00
|
|
|
},
|
|
|
|
|
2022-04-05 14:23:15 -04:00
|
|
|
recalculate() {
|
|
|
|
// do nothing if:
|
2022-09-02 09:56:41 -04:00
|
|
|
// a) topic does not have a category and does not have a gated tag
|
2022-04-05 14:23:15 -04:00
|
|
|
// b) component setting is empty
|
|
|
|
// c) user is logged in
|
2022-09-02 09:56:41 -04:00
|
|
|
const gatedByTag = this.tags?.some((t) => enabledTags.includes(t));
|
|
|
|
|
2022-04-05 14:23:15 -04:00
|
|
|
if (
|
2022-09-02 09:56:41 -04:00
|
|
|
(!this.categoryId && !gatedByTag) ||
|
|
|
|
(enabledCategories.length === 0 && enabledTags.length === 0) ||
|
2022-04-05 14:23:15 -04:00
|
|
|
this.currentUser
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-02 09:56:41 -04:00
|
|
|
if (enabledCategories.includes(this.categoryId) || gatedByTag) {
|
2022-04-05 14:23:15 -04:00
|
|
|
document.body.classList.add("topic-in-gated-category");
|
|
|
|
this.set("hidden", false);
|
|
|
|
}
|
2022-04-05 13:34:13 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("hidden")
|
|
|
|
shouldShow(hidden) {
|
|
|
|
return !hidden;
|
|
|
|
},
|
|
|
|
});
|