请求帮助在主题组件上下文中查找用户的完整姓名

我试图将用户的完整姓名注入到精选主题的 Handlebars 模板中。将测试值放入模板没有问题,但我似乎无法获取我想要的值。在下面的代码中,我理想情况下希望返回 realname,将用户的完整姓名作为值,以替换“Test”:

<script type="text/discourse-plugin" version="0.8">
    var FeaturedTopicView = require('discourse/components/featured-topic').default;
    FeaturedTopicView.reopen({
        fullName: function() {
            // 获取最后发帖人的用户名
            let username = this.topic.last_poster.username;

            // 尝试根据上面确定的用户名获取用户的完整姓名
            let realname = Discourse.User.findByUsername(username).then(function(result) {
                               return result.name;
                           }).then(function(result) {
                               // 这给了我们真实姓名!但它在此范围之外无法访问
                               console.log(result);
                           });
            
            return "Test";
        }.property()
    });
</script>

我感兴趣的用户是最后发帖人,因此在这种情况下,似乎我只能使用 this.topic.last_poster。它有三个关联值:usernameidavatar_template。不幸的是,我真正需要的是 name

我希望有某种方法可以使用 idusername 查找用户的完整姓名。我对 Promise 的概念及其使用方法不太熟悉,但我通过使用 Discourse.User.findByUsername(username) 大致验证了一个概念。我可以成功地将我需要的完整姓名打印到 console.log,但它们似乎被困在那个作用域内。

我可能完全搞错了方向,但这是基于我目前知识所做的最大努力。

任何帮助或建议都将不胜感激!如果需要我澄清任何内容,请告诉我。

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.

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?

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

boop, reminder on Friday

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.

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: