Request for help on finding a user's full name in a theme component context


#1

I’m trying to inject a user’s full name into the featured topic handlebars template. I have no problem getting a test value into the template, but I cannot seem to get the value I want. In the code below, I would ideally return realname with the user’s full name as the value in place of “Test”:

<script type="text/discourse-plugin" version="0.8">
    var FeaturedTopicView = require('discourse/components/featured-topic').default;
    FeaturedTopicView.reopen({
        fullName: function() {
            // Get the username of the last poster
            let username = this.topic.last_poster.username;

            // Try to get the full name of the user with the username we established above
            let realname = Discourse.User.findByUsername(username).then(function(result) {
                               return result.name;
                           }).then(function(result) {
                               // This gives us the real names! But it's inaccessible outside of here
                               console.log(result);
                           });
            
            return "Test";
        }.property()
    });
</script>

The user I’m interested in is the last poster, so in this case it seems like all I have to work with is this.topic.last_poster. This has three values tied to it: username, id, and avatar_template. Unfortunately what I really need is name.

I was hoping there was something to allow me to look up a user’s full name using id or username. I’m not extremely familiar with the concept of promises and how to use them, but I sort of proved out a concept using Discourse.User.findByUsername(username). I can successfully console.log the full names I need, but they seem to be trapped in that scope.

I could be going about this all wrong, but this was my best effort based on my current knowledge.

Any help or suggestions would be greatly appreciated! Please let me know if I need to clarify anything either.


(Sam Saffron) #2

This is going to have to go in the serializer or you are going to be doing the worst kind of N+1 which is a Network+1

I am ok with last poster including name if the site has prioritize full names in the UI.

Otherwise you are going to need a plugin.


How to display data beneath the user's avatar?
#3

Thanks a bunch for your response, @sam!

It looks like “name” was included in the relevant serializer at one point, but was removed:

With that in mind, is it cool if I submit a PR with “name” back in place if the “prioritize full names” setting is enabled? Or is there a deeper reason to exclude this from core that I’m not aware of?


(Sam Saffron) #4

Hmmm @tgxworld is ill at the moment, can you hold a few days on this and remind me again Friday.


(Kane York) #5

boop, reminder on Friday


(Alan Tan) #7

It was added in this commit and I removed it because I was adding more overhead to the BasicUserSerializer when I only needed title/name in one spot of the app.


#8

That reasoning makes sense. So with that in mind, what do you suggest as the most ideal solution to my problem? Is it worth doing a PR to include it once again for those concerned with prioritizing the full name of a user in any UI customizations, or is this something that should be restricted to a plugin? If you have any other suggestions on how to tackle this that I haven’t thought of, that would be great as well.

PS: Hope you are feeling all better :slight_smile: