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.