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:
Namely:
-
Create a dedicated
DirectoryItemUserSerializer
(the current “UserSerializer” inside theDirectoryItemSerializer
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)
-
Adding tests.
However I thought it best to run this by you guys first.
Example
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: