Add OP username into Body Css Classes

AI-generated summary

The discussion revolves around adding the original poster’s (OP) username as a <body> CSS class to apply different styles based on the username. nildarar needs this to make changes to page elements when the OP is anonymous, such as hiding quotes and mentions.

nildarar provided a script that attempts to get the OP user object using the Discourse API. Canapin suggested using api.getCurrentUser(), but nildarar clarified that this returns the logged-in user, not the topic owner.

After further research, nildarar found a solution using api.decorateTopicTitle() to add a class to the topic title if the topic model’s user ID matches a specific ID. This approach allows nildarar to apply styles based on the OP’s username.

Hi,
Can anybody help me to add OP username as <body> CSS class?
I need to write some styles and need different attributes for some usernames.

Some users asked us to delete the topic from their profile but did not insist on deleting it completely. That’s why we created an anonymous user to change the ownership of the requested topics.

Now we need to make changes to the page elements if the OP is anonymous. Including hiding quotes and mentions and etc.

<script type="text/discourse-plugin" version="0.8">
    api.onPageChange((url, title) => {
        if (/^\/\t\/.*$/.test(url)) {
            const opUser = API.????? <--------------------------- How to Get OP user object
            if (opUser && opUser.id === 1234) {
                document.querySelector("body").classList.add("anon");
            }
        } else {
            document.querySelector("body").classList.remove("anon");
        }
    });
</script>

Is there a way we can get topic info via JavaScript API?

api.getCurrentUser()

Calling api.getCurrentUser() method returns the logged-in user. I’m looking for a method to get topic information (including topic owner).

Oh sorry I misread. :confused:

You’re welcome, I think this is more complicated than asking a simple question :+1: :sweat_smile:

To solve my problem, I found the following solution by reading this page:

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/app/lib/plugin-api.js#L1138-L1140

<script type="text/discourse-plugin" version="0.8">
    const specialTopicUserId = 1000;
    api.decorateTopicTitle((topicModel, node, topicTitleType) => {
        if (topicModel && topicModel.user_id === specialTopicUserId) {
            $("#topic").addClass("anon");
        }
    });
</script>