Allow users to hide likes on posts via a user preference or user field

There was a big discussion recently on a forum I administrate about whether or not Likes should be removed. Half want them gone, the other half want them to remain. I’ve found the theme component that hides the ability to like a post for TL0 and anonymous users, and I’ve mostly adapted this for our use, however, I’m not sure how to link it up with a user field boolean I’ve created (“toggle likes”). In the original theme component, it uses something like “settings.hide_for_anon_user” and it creates a theme setting in settings.yml called “hide_for_anon_user” but that can only be toggled by an administrator and not on a user level.

How would I go about referencing the custom field for this? I’ve already added it to public_user_custom_fields, I just don’t know how to go about referencing it in the component JS file. I’ve tried userCustomFields.user_field_10, settings.current_user.custom_fields.user_field_10, and current_user.custom_fields.user_field_10, but none of them work, they just hide all of the posts in a thread.

I’d be much obliged for any help.

2 Likes

I think that you can create a no-likes group and use that to hide likes just like tl0 does. Set the group so that anyone can join.

1 Like

How do I reference the group in this case, though?

I replaced
this.currentUser.trust_level === 0;
with
this.currentUser.no_likes;

but it didn’t work. Again, not sure how to reference custom groups, much like user fields, it doesn’t appear to be clearly stated in any documentation.

This seems to work for the custom user group:

<script type="text/discourse-plugin" version="0.8">
api.reopenWidget("post-menu", {
    menuItems() {
        const user = Discourse.User.current();
        if(user.primary_group_name.toLowerCase() === "no_likes") {
            return this.siteSettings.post_menu
                .split("|")
                .filter((item) => item !== "like");
        }
        return this.siteSettings.post_menu.split("|").filter(Boolean);
    },
});
</script>

But I’d still like to see if there’s a way to do this so it checks the custom user field boolean instead of making people join a group. I’ve been looking through other plugins to get an idea of how to do that, but everything I’ve tried so far hasn’t worked.

ETA: This seems to work, but you need to refresh after checking or unchecking the userfield boolean.

<script type="text/discourse-plugin" version="0.8">
api.reopenWidget("post-menu", {
    menuItems() {
        const user = Discourse.User.current();
        if(user.custom_fields['user_field_10']) {
            return this.siteSettings.post_menu
                .split("|")
                .filter((item) => item !== "like");
        }
        return this.siteSettings.post_menu.split("|").filter(Boolean);
    },
});
</script>
2 Likes

Apparently, this is hiding posts when people are logged out. I’m not sure how to modify the code – does anybody have any suggestions?

<script type="text/discourse-plugin" version="0.8">
api.reopenWidget("post-menu", {
    menuItems() {
        const user = Discourse.User.current();
        if (api.getCurrentUser() == null || user.custom_fields['user_field_10'])  {
            return this.siteSettings.post_menu
                .split("|")
                .filter((item) => item !== "like");
        }
        return this.siteSettings.post_menu.split("|").filter(Boolean);
    },
});
</script>

This seems to work. If there’s a cleaner way of doing this, feel free to let me know.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.