Changes to the user card data source

At the moment, when you click a user card, we load /u/david.json, which is exactly the same endpoint as my full profile. This is a little wasteful, since the user card doesn’t even use most of the data!

Therefore, we will be switching the user card to use a new route /u/david/card.json. This contains just enough information to render the card. In my testing, this makes user cards 2-3 times faster than before!

At the moment this change is behind a hidden site setting. To try it out, head to your rails console and run

SiteSetting.enable_new_user_card_route = true

In the next few weeks, this will be enabled by default, and the site setting will be removed. This is enabled right now on Meta.

Great! What do I need to do?

If you’re a user or a forum admin, nothing! You should see improved performance on user cards very soon!

If you’re a plugin developer, you may need to make a few tweaks to support the new route.

I use custom fields in my plugin, and render them on the user card

No problem, custom fields will continue working in exactly the same way

I add data to the user serializer, and use it in the full user profile

Fine, the user profile will continue to use the old endpoint, and any existing add_to_serializer calls will continue to work

I add data to the user serializer, and use it on the user card

Ok, you will need to do a little work. Previously we just had a structure like

class UserSerializer < BasicUserSerializer

Now we have a structure like

class UserCardSerializer < BasicUserSerializer
# and
class UserSerializer < UserCardSerializer

If you want something to be included in the card endpoint, you need to add it to the UserCardSerializer. For example, if you have code like this:

add_to_serializer(:user, :favourite_forum_software) do
  "discourse"
end

You need to change it to

add_to_serializer(:user_card, :favourite_forum_software) do
  "discourse"
end

The UserSerializer will automatically include anything in the UserCardSerializer so you can make this change straight away and it will work on tests-passed (even with the hidden site setting disabled).

To maintain compatibility with stable/beta, add a check for UserCardSerializer like this:

# TODO: Remove switch once Discourse 2.4 stable is released
serializer = (defined? UserCardSerializer) ? :user_card : :user 
add_to_serializer(serializer, :favourite_forum_software) do
  "discourse"
end
22 Likes

Great stuff @david. Really excited to see this one. I agree a 100% to the fact that a new serializer was needed to user cards.

I think this change also lays the ground for displaying user cards on the /u route.

7 Likes

It does indeed! Watch this space :eyes:

7 Likes

This is now the new default, and the setting has been removed

https://github.com/discourse/discourse/commit/576872a2d93ce8546c5fded6d9edf5344c9bb4fe

5 Likes