Antwoorden beperken tot vertrouwensniveaus

AI-generated summary

In a support discussion titled “Restrict answers to trust levels,” j3ang asks if it’s possible to restrict replies (or even hide answers) based on user trust levels or custom fields, since their users migrated from WordPress with tiered memberships. HAWK clarifies whether the goal is to restrict replying or viewing, then confirms that reply restrictions can be set at the category level using trust level groups as shown in an image. When j3ang asks about hiding answers entirely (not just restricting replies), HAWK explains that’s not possible out of the box — but confirms it can be achieved with a custom plugin. j3ang begins experimenting with a plugin, initially overriding postsToRender in TopicRoute, but notes UI glitches and reload issues. After realizing the correct approach is to override the TopicController, they share a revised code snippet that conditionally filters posts based on whether the current user belongs to a privileged group as shown in a video. The final solution uses @discourseComputed to dynamically adjust postsToRender, showing only the first post (the topic) for non-privileged users, while preserving mobile compatibility via capabilities.isAndroid. The thread concludes with j3ang having a working — though still experimental — implementation.

Hi,

I’m wondering if there’s way to restrict answers/replies to certain trust levels or user custom fields? Our users are migrated from WordPress with different membership level. We don’t want to give all user the capability to view answers unless they’ve reached to certain membership level.

To view or to reply? They’re quite different things.

@HAWK , ah sorry, to reply. they can see the topic and replies, but shouldn’t be able to reply unless they are certain level.

Yup, you can set that up at a category level. Add each trust level group and give it the appropriate permissions.

What if we actually do want to hide the answers not just the restrict permission to reply? The ‘see’ column is grayed out.

You can’t do that, I’m afraid.

Would it be possible with a custom plug-in ? I suspect it shouldn’t be hard reading current user and hide the whole answer section via JavaScript?

Sorry, I meant it’s not possible out of the box. Pretty much anything is possible with a custom plugin!

As I’m trying to accomplish this via custom plugin. I think I’m almost there by editing overriding the postsToRender variable.

  TopicRoute.reopen({
    setupController(controller, model) {
      this._super(controller, model);
      topicController = controller;
      console.log(topicController);
      let userPrivileged = userCanViewAnswers(topicController.currentUser);

      const postStream = topicController.get("model.postStream");
      window.postStream = postStream;
      window.topicController = topicController;

      topicController.postsToRender = userPrivileged ? topicController.postsToRender : topicController.postsToRender.posts.filter(p=>p.post_number == 1);

    }
  });

function userCanViewAnswers(user){
    let user_groups = user.groups.map(g => g.name);
    return user_groups.includes('privileged');
}

It seems like it will show error not found briefly and reload correctly when hard reloading the page or come back after home page,

I’m using MacOS Docker dev image, kinda slow.

Sorry, I’m not familiar with the discourse code base quite yet.
Is there a better way to intercept the posts on topic load?

I see I was supposed to reopen the Controller.

  TopicController.reopen({
    @discourseComputed(
    "model.postStream.posts",
    "model.postStream.postsWithPlaceholders"
  )
    postsToRender(posts,postsWithPlaceholders ){
      let userPrivileged = userCanViewAnswers(this.currentUser);
      return userPrivileged 
      ? (this.capabilities.isAndroid ? posts : postsWithPlaceholders) 
      : (this.capabilities.isAndroid ? posts.slice(0, 1) : postsWithPlaceholders.slice(0, 1));
    }
  })