A client approached me with a request to create a card-based user directory.
As he’s hosted on discourse.org, I initially said this would not be possible (aside from any performance issues), as the attributes required to fill a user card are not available in the user object serialized with directory items.
However, after discussing it further, I determined that one approach could be to make a PR into core which adds user attributes to the directory item serializer via a site setting. The idea being that this functionality will be useful for various styles of user directory which require various mixtures of user attributes (example below).
Experimenting with this, I’ve come up with a relatively simple approach, which you can see on this diff:
Create a dedicated DirectoryItemUserSerializer (the current “UserSerializer” inside the DirectoryItemSerializer has always felt a bit strange to me).
If the site admin has selected any user attributes to add:
Create a serialized user to take advantage of the protections of that serializer, e.g. staff / private / untrusted attributes.
Serialize any added attributes which pass the UserSerializer filter.
The goals here being to:
Not affect the existing behviour in any way.
Serialize only those attributes requested.
If you guys are on board with this approach / request, I’ll finish this off by:
Adding a validation on the site setting (i.e. to ensure the string entered is a user attribute)
However I thought it best to run this by you guys first.
As an example of the kind of functionality this would allow, the screenshot below from my local just using this change to core + a theme component that overrides the existing directory and uses a modified user-card component:
How about making the user directory serializer accept a new parameter like users.json?include_profile=true
Then we can add a JS plugin API which allows themes to add this parameter:
“Profile” would include all the things in the regular user serializer (being careful not to introduce n+1 queries). I don’t think specifying individual fields is going to make much difference to performance, so IMO we should keep it simple.
The same approach could work for topic lists as well ?include_excerpts=true
I also like this because it means the “per-theme serializer configuration” won’t get broken by the anon cache!
In that case, I think a hidden site setting is a good starting point. Would be nice to make it per-theme, but we need to build some infrastructure there first. I still think a boolean “user list includes profile” setting will be easier to reason with.