Richiesta di aiuto per trovare il nome completo di un utente nel contesto di un componente del tema

Sto cercando di iniettare il nome completo di un utente nel template Handlebars dell’argomento in evidenza. Non ho problemi a inserire un valore di test nel template, ma non riesco a ottenere il valore che desidero. Nel codice sottostante, idealmente vorrei restituire realname con il nome completo dell’utente al posto di “Test”:

<script type="text/discourse-plugin" version="0.8">
    var FeaturedTopicView = require('discourse/components/featured-topic').default;
    FeaturedTopicView.reopen({
        fullName: function() {
            // Ottieni il nome utente dell'ultimo autore
            let username = this.topic.last_poster.username;

            // Prova a ottenere il nome completo dell'utente con il nome utente stabilito sopra
            let realname = Discourse.User.findByUsername(username).then(function(result) {
                               return result.name;
                           }).then(function(result) {
                               // Questo ci restituisce i nomi reali! Ma sono inaccessibili fuori da qui
                               console.log(result);
                           });
            
            return "Test";
        }.property()
    });
</script>

L’utente che mi interessa è l’ultimo autore, quindi in questo caso sembra che tutto ciò con cui posso lavorare sia this.topic.last_poster. Questo ha tre valori associati: username, id e avatar_template. Purtroppo ciò che mi serve davvero è name.

Speravo ci fosse qualcosa che mi permettesse di cercare il nome completo di un utente usando id o username. Non sono molto familiare con il concetto di promesse e su come utilizzarle, ma ho in qualche modo dimostrato un concetto usando Discourse.User.findByUsername(username). Posso eseguire correttamente console.log sui nomi completi di cui ho bisogno, ma sembrano intrappolati in quel scope.

Potrei stare procedendo in modo sbagliato, ma questo è stato il mio miglior sforzo basato sulle mie conoscenze attuali.

Qualsiasi aiuto o suggerimento sarebbe molto apprezzato! Fate sapere se devo chiarire qualcosa.

4 Mi Piace

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 Mi Piace

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 Mi Piace

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

3 Mi Piace

boop, reminder on Friday

2 Mi Piace

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 Mi Piace

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: