DEV: Plugin API to allow creation of directory columns with item query (PR #13402)

The first thing we needed here was an enum rather than a boolean to determine how a directory_column was created. Now we have automatic, user_field and plugin directory columns.

This plugin API is assuming that the plugin has added a migration to a column to the directory_items table.

This was created to be initially used by discourse-solved. PR with API usage - Use `add_directory_column` API to add solutions count to user directory by markvanlan · Pull Request #137 · discourse/discourse-solved · GitHub

GitHub

Plugins can add columns here, so lets make sure we reset all values, not just the default ones.

The best thing to do here would be to modify the query above to let plugins extend it. That gets mega complicated really quickly. The extra queries shouldn’t be too much of a strain on the server if they’re written well, and this is already running as a background job.

There are too many helpers for directory-items. I decided to shove all the related helpers into 1 file just to be easier when developing.

Is there any basic sanitizing we can do here on the column_names? For example make sure they only include [a-z] and _? Just trying to avoid injection vectors.

Trivial but you could do this in one query:

UPDATE directory_columns 
SET type = CASE WHEN automatic THEN 0 ELSE 1 END;

Should this happen in the plugin api’s add_directory_column and then we can assume from it’s safe after that?

Added the validation in the plugin api

Looks good!