Solicitação de ajuda para encontrar o nome completo de um usuário no contexto de um componente de tema

Estou tentando injetar o nome completo de um usuário no modelo Handlebars do tópico em destaque. Não tenho problemas em inserir um valor de teste no modelo, mas não consigo obter o valor que desejo. No código abaixo, idealmente retornaria realname com o nome completo do usuário no lugar de “Test”:

<script type="text/discourse-plugin" version="0.8">
    var FeaturedTopicView = require('discourse/components/featured-topic').default;
    FeaturedTopicView.reopen({
        fullName: function() {
            // Obter o nome de usuário do último postador
            let username = this.topic.last_poster.username;

            // Tentar obter o nome completo do usuário com o nome de usuário estabelecido acima
            let realname = Discourse.User.findByUsername(username).then(function(result) {
                               return result.name;
                           }).then(function(result) {
                               // Isso nos dá os nomes reais! Mas está inacessível fora daqui
                               console.log(result);
                           });
            
            return "Test";
        }.property()
    });
</script>

O usuário que me interessa é o último postador, então, neste caso, parece que tudo com que tenho para trabalhar é this.topic.last_poster. Isso tem três valores associados: username, id e avatar_template. Infelizmente, o que realmente preciso é name.

Eu esperava haver algo que me permitisse pesquisar o nome completo de um usuário usando id ou username. Não estou muito familiarizado com o conceito de promises e como usá-las, mas provei um conceito usando Discourse.User.findByUsername(username). Consigo fazer console.log com sucesso dos nomes completos de que preciso, mas eles parecem estar presos naquele escopo.

Posso estar fazendo tudo errado, mas foi meu melhor esforço com base no meu conhecimento atual.

Qualquer ajuda ou sugestão seria muito apreciada! Por favor, avise-me se precisar de esclarecimentos sobre algo.

4 curtidas

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.

4 curtidas

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?

1 curtida

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

3 curtidas

boop, reminder on Friday

2 curtidas

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.

2 curtidas

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: