Allow users to block keywords

:information_source: Summary This theme component allows users to block keywords. Requires three custom user fields to be created in Admin settings in order to function.
:eyeglasses: Preview I can’t preview this on theme-creator because it requires creating user fields, but I’ve included screenshots below.
:hammer_and_wrench: Repository Github - a-very-old-sock/discourse-keyword-block
:question: Install Guide How to install a theme or theme component
:open_book: New to Discourse Themes? Beginner’s guide to using Discourse Themes

Install this theme component

This theme component allows users to block keywords, specified by the user in Preferences > Profile. Requires three custom user fields to be created in Admin settings in order to function.

Block keywords in the topic title or topic excerpt:

Block topics or reply posts that contain the keyword, as well as suggested topics that contain the keyword:

This theme component allows an individual user to specify keywords they don’t want to see, and hides the topics and reply posts containing that keyword with a link to show anyway. Watched words is an admin setting that applies for all users across the whole site; this allows individual users to specify their own keywords that they don’t want to see.

Example scenario: a pets forum might have a general thread about dealing with pet hair where the main topic isn’t tagged with a specific kind of animal. User A replies about their dog, User B replies about their cat, but User A doesn’t want to see anything about cats at all. What this theme component does is lets User A put “cat” in their blocklist, and then anywhere that “cat” appears in a topic or rely, replaces it with “Blocked for containing cats” with a click to show anyway.

The user must specify a comma-separated list of their blocked keywords in a custom user field in Preferences > Profile, and can specify if the keywords to be blocked are case sensitive or must match on the whole word. (Case sensitivity and whole-word matching apply to the whole block list, not individual keywords).

This theme component requires you to make three custom user fields in Admin > Customize > User Fields in order to function. By default, the theme component settings expect these user fields to be 1, 2, and 3 in the order listed below. If you have other custom user fields defined, you’ll need to enter the correct user field numbers in the theme component settings. (User field numbering starts at 1, so count down the list of how many other user fields you have defined). See the Meta Create and configure custom user fields guide for further details.

1 - User’s blocked terms list

Field Type: Text Field

Field Name: (any name will work here) Blocked Terms

Field Description: (any description will work here) Block words or phrases that appear in posts or topics. Comma separated. To block a username, include @ if whole word matching is selected.

:white_check_mark: Editable after signup

:x: Required at signup

:x: Show on public profile

:x: Show on user card

:x: Searchable

2 - Case sensitivity

Field Type: Confirmation

Field Name: (any name will work here) Blocked terms - case sensitive

Field Description: (any description will work here) Words in the “Blocked terms” list are case sensitive.

:white_check_mark: Editable after signup

:x: Required at signup

:x: Show on public profile

:x: Show on user card

:x: Searchable

3 - Search whole or partial words

Field Type: Confirmation

Field Name: (any name will work here) Blocked terms - whole words

Field Description: (any description will work here) Blocked terms must appear as full words, not as partial words

:white_check_mark: Editable after signup

:x: Required at signup

:x: Show on public profile

:x: Show on user card

:x: Searchable

The css below is included in the theme component and will prevent any custom user fields from being displayed on the signup form. If you have other custom user fields you do want displayed on the signup form, you’ll need to override this with your own custom css.

.login-form .user-fields {
    display: none;
}

I’ve only tested this with the Default theme. Posts and topics are blocked when categories are set to display Categories/Latest and Categories/Top, but not in Categories/Featured or any of the box category views. I’m unlikely to change this because I don’t need it for my own site, but pull requests are welcome.

This currently only blocks a post that contains a keyword in the title or the excerpt visible in the browser. I haven’t been able to figure out how to check the content of the whole post when viewing search results or a topic list, but I’d welcome any ideas how to do so.

I intend for this to also work on the full-page-search view, but as of 2/24/2023 I’m currently struggling to get that working. I’ll edit this if I’m able to get blocking to work on search.

This is compatible with the Topic Excerpts theme component; it is not compatible with Topic List Previews. I have not tested this extensively with other theme components.

3 Likes

After creating custom user fields and enabling the component I get this:

Any ideas what it tries warn me? Because of this perhaps:

User Keyword Blocker theme/component is throwing errors: TypeError: null is not an object (evaluating 'e.match')

backtrace
Message

User Keyword Blocker theme/component is throwing errors:
TypeError: null is not an object (evaluating 'e.match')

Backtrace

viewingCategoryId@https://foorumi.katiska.eu/theme-javascripts/2d7e05cf5f7eb3e5f8699871f25f32a21d9221c2.js?__ws=foorumi.katiska.eu:209:77
@https://cdnfoorumi.katiska.eu/assets/vendor-f2e959203bc406b4698a4388ad51464d9ee2c5ff7b4011f332b7d88c6ce5dab8.br.js:1347:71
@https://cdnfoorumi.katiska.eu/assets/vendor-f2e959203bc406b4698a4388ad51464d9ee2c5ff7b4011f332b7d88c6ce5dab8.br.js:3677:13
get@https://cdnfoorumi.katiska.eu/assets/vendor-f2e959203bc406b4698a4388ad51464d9ee2c5ff7b4011f332b7d88c6ce5dab8.br.js:1347:56
Ee@https://cdnfoorumi.katiska.eu/assets/vendor-f2e959203bc406b4698a4388ad51464d9ee2c5ff7b4011f332b7d88c6ce5dab8.br.js:1324:40
map@[native code]
@https://cdnfoorumi.katiska.eu/assets/vendor-f2e959203bc406b4698a4388ad51464d9ee2c5ff7b4011f332b7d88c6ce5dab8.br.js:5042:163
@https://cdnfoorumi.katiska.eu/assets/vendor-f2e959203bc406b4698a4388ad51464d9ee2c5ff7b4011f332b7d88c6ce5dab8.br.js:1347:71
@https://cdnfoorumi.katiska.eu/assets/vendor-f2e959203bc406b4698a4388ad51464d9ee2c5ff7b4011f332b7d88c6ce5dab8.br.js:3677:13
get@https://cdnfoorumi.katiska.eu/assets/vendor-f2e959203bc406b4698a4388ad51464d9ee2c5ff7b4011f332b7d88c6ce5dab8.br.js:1347:56

Env

HTTP HOSTS: foorumi.katiska.eu

So… what the heck did I wrong?

1 Like

Hmm, I’m not sure, I haven’t been able to reproduce this with either a newly created user field or a non-existent user field.

It looks like you have the default theme? Do you have any other theme components enabled? It looks like it has something to do with the regex matching but it shouldn’t throw errors if it doesn’t find anything.

Of course I have another components too :wink: But sure — it can be a conflict between this and some other component or plugin.

I’ll try to dig a bit deeper at some point.

Aaaand that some point happened now :wink:

The conflict happends between this and topic list preview. At my setup anyway.

So I let you and @merefield argue which of you (whom? I really hate english) fixes and what :wink:

1 Like

Waaaaay too busy to support conflicts with third party Theme Components I’m afraid, but open to PR. For now you’ll just have to decide between them. :slight_smile:

2 Likes

I would wonder if this coupd be used to extend user mute function?

Ie put muted user in Blocked wird list to hide replies to muted user?

I think it’s possible in theory, but I’m not finding an easy way to do it right now. Right now what I’m doing is using decorateCooked, which doesn’t seem to allow easy access to the parent element where the reply information is. I might fiddle with this more and see if I can get it working, but no promises.

This will block mentions of a username in the post if the username is included in the block list; if whole-word matching is on, the username in the blocklist needs to include the @mention, but if whole word matching is off then only the username without @ can be included.

1 Like

I don’t think I’m going to be able to make them compatible either, but I’ve noted in the OP that they’re not compatible. PR welcome though.