I’m working on a theme component to block posts that contain a keyword contained in a custom user field. I’ve got this working to hide replies as well as topics when listed in latest or top on desktop view, but when viewed in the app or on mobile it will append the notice that the topic was blocked, but not apply the blocked class to the content. Blocking replies within a topic works just fine on mobile or the app.
From a look through components it didn’t look like a different .hbs was used for mobile, am I missing something else?
const discourseComputed = require("discourse-common/utils/decorators").default;
function addIgnoredTopicClass() {
if (currentUser) {
api.container.lookup('store:main').find('user', currentUser.username).then((user) => {
const blocklist = user.user_fields[1];
const case_sensitive = user.user_fields[2];
const whole_words = user.user_fields[3];
const blocked = blocklist.split(",").map(function(item) {
if (case_sensitive) {
return item.trim();
} else {
return item.trim().toLowerCase();
}
});
let classList = this._super(...arguments);
let elem = document.querySelectorAll("[data-topic-id='" + this.topic.id.toString() + "']")[0];
console.log(elem);
const title = (case_sensitive) ? this.topic.fancy_title : this.topic.fancy_title.toLowerCase();
const excerpt = (case_sensitive) ? this.topic.excerpt : this.topic.excerpt.toLowerCase();
let result = "";
for (let index = 0; index < blocked.length; ++index) {
let pattern = (whole_words) ? new RegExp('(?:\\b|\\s|^)' + blocked[index] + '(?:\\b|\\s|$)') : new RegExp(blocked[index]);
// console.log(pattern)
if (pattern.test(title)) {
classList += "blocker-blocked"
const found = blocked.filter(text => title.includes(text));
if (found.length >= 2) {
const last = found.pop();
result = found.join(', ') + ' and ' + last;
} else {
result = found.join(', ');
}
const newNode = document.createElement("a");
const textNode = document.createTextNode("Blocked for containing " + result + ".");
newNode.classList.add("block-notice")
newNode.appendChild(textNode);
newNode.onclick = function() { showComment(this); };
elem.children[0].insertBefore(newNode, elem.children[0].children[0]);
for (let index = 0; index < elem.children.length; ++index) {
if (elem.children[index].classList.contains('main-link')) {
elem.children[index].classList.add("blocker-blocked");
}
}
newNode.classList.remove("blocker-blocked");
}
}
// console.log(classList);
return classList;
});
}
}
api.modifyClass("component:topic-list-item", {
@discourseComputed()
unboundClassNames() {
return addIgnoredTopicClass.call(this);
}
});
api.modifyClass("component:latest-topic-list-item", {
@discourseComputed()
unboundClassNames() {
return addIgnoredTopicClass.call(this);
}
});