Show a user's reputation/user level next to their name

Hello, I just wanted to share my findings. Its not plugin ready, just quick hack to core files (sorry). But as inspiration for someone it may be handy…

Add User Post Count in post template and user-card

Use as example. It can be adjusted to display various user stats (post count, user_last_seen_at, etc.) in user-card and in post stream.

app/assets/javascripts/discourse/templates/user-card.hbs

{{user-reputation post_count=user.post_count}}

/assets/javascripts/discourse/components/user-reputation.js.es6

    export default Ember.Component.extend({
      tagName: 'div',
      classNames: ['user-reputation-user-card'],

      render: function(buffer) {
        
        const post_count = this.get('post_count');
        const contents = '<b>' + post_count + '</b>'; 
        
        buffer.push(contents);
      }
    });

/assets/javascripts/discourse/initializers/user-reputation-init.js.es6

    import { withPluginApi } from 'discourse/lib/plugin-api';
    import { includeAttributes } from 'discourse/lib/transform-post';
    import { h } from 'virtual-dom';

    export default {
      
      name: 'user-reputation-init',
      
      initialize(){
        
        withPluginApi('0.1', api => {
          api.includePostAttributes('user_last_seen_at');  // (automatically added to transform-post.js.es6)
          
          api.decorateWidget('poster-name:after', function(helper) {
            const attrs = helper.attrs;
            const user_custom_fields = attrs.user_title;
            const post_count = attrs.post_count;
            return [h('span', 'post_count ' + post_count)];
              
          });
        })
      }
      
    }

app/serializers/user_serializer.rb

                  :user_fields,
                  :topic_post_count,
                  :pending_count,
    +              :post_count,
                  :profile_view_count

app/serializers/post_serializer.rb

                  :via_email,
                  :is_auto_generated,
                  :action_code,
                  :action_code_who
    +              :post_count,

    +   def post_count 
    +     object.try(:user).try(:post_count)
    +     #object.try(:user).try(:user_stat).try(:post_count)
    +  end
4 Likes