テーマコンポーネントのコンテキストでユーザーのフルネームを検索する支援を求む

ユーザーのフルネームを featured-topic の Handlebars テンプレートに注入しようとしています。テスト値をテンプレートに埋め込むことは問題なくできますが、目的の値を取得できません。以下のコードでは、「Test」の代わりにユーザーのフルネームが入った realname を返したいと考えています。

<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 の 3 つの値が関連付けられています。残念ながら、本当に必要なのは name です。

id または username を使用してユーザーのフルネームを検索できる何かがあることを期待していました。プロミスの概念やその使用方法にはあまり慣れていませんが、Discourse.User.findByUsername(username) を使用して概念的な検証を行いました。必要なフルネームを console.log で表示することは成功しましたが、それらはどうやらそのスコープ内に閉じ込められているようです。

私のアプローチ全体が間違っている可能性もありますが、現在の知識に基づいた最善の努力でした。

ご協力やご提案をいただければ大変幸いです。何か clarification が必要な場合は、お知らせください。

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: