Theme component - Hide categories according to user group membership

Continuing the discussion from How to split the forum into two main parts (general/groups) and how to display only subcategories I can create a new post in?:

Hey everyone,

Here is a theme component that does a thing I’ve been wanting to do for a long time, but I’d like your comments on the code to improve it, or your suggestions.

I don’t believe the code is very good so I don’t want to recommend it before it has been checked by someone else.

What it does

Hide with CSS all the topics for which the user has a category permission restriction in chosen pages.

Repository

Repository link
framagit.org/oca/discourse-theme-shroud-subcategories

Motivation

I’ve got a community with a lot of groups, and I want people to peacefully ignore what is from other groups than theirs. I don’t want to restrict access to the content, I just don’t want it to be on main page. They should be able to access it by going for it, by choosing to go on this category specifically (for example, in order to know what is discussed here). Obviously, the relevant tool would be the watch/ignore function, but since the UI requires more than 2 clicks to set it up, no-one in the community uses it. Some improvement on the Category page might do the trick but they are out of my skill.

So, I use what UI is simple enough for them: the group page. On the same page are displayed all the groups they can access, freely or not, and their description. That’s perfect, they just need to click and chose them. The group page is too hidden for them (inside the hamburger) so I could put a big button link on the main page.

Now, I need to link the group membership and the display of the category posts.

How it works

The trick I used was to setup the category permission as:

  • everyone: read, write
  • that group: start a message, read, write

and then, for each topic in the list, check if there is a category restriction, and if yes, add a class not-in-group to the topic element.

Then, the CSS add display:none accordingly.

UI

The admin can chose the pages where this behaviour is effective. It should not be done on the category page because, by definition, that is where we want to access the content. On my website, all those groups are subcategories that are hidden from the latest page, we access it through the /c/groups/ page, so that is that page which is used.

Improvements

  • Being able to select parents category of the filtered categories (so any category that is not a child of this one is safe from being skipped)
  • Add tags, so that if a topic has this tag, then it will not be removed.
4 Likes

This does not make sense to me. Why not use category permissions to restrict what the groups see?

3 Likes

Because I want them to be able to see those categories, but only if they make the effort to reach it. For example, in order to see what is discussed on that specific project before they register.

But on the main page, I don’t want them to be spammed by topics they don’t care. The best way would be for them to ignore it with the watch/ignore, but that’s too complicated for them. The other option would be to register in the group, go to the category page to read what is discussed, and then unregister from the group. That’s too many clicks for most of them, plus they would not dare do it.

Last option is the category setting «remove from latest» but that setting affects all the users, even the one that are following that category. With this method, I do have the equivalent of the «remove from latest» but only for some users and for all the pages I want.

Hope it’s more clear.

1 Like