RECIPE: Set custom theme as the default for all new users [unsupported recipe]

Hi All,
We occasionally get requests from customers to introduce a feature to allow system admins to setup a custom theme as the default for all users on the system. This feature is being looked, but in the near term there is an option to achieve this now.

WARNING: The approach involves using direct database calls, so as a customer facing engineer I’m generally never going to recommend this kind of approach as it is circumventing our API, and could break in the future.

With the warning in place, I’ll still share the recipe for MYSQL in case you’re interested (a similar approach works for Postgres as well). The idea here is we are creating a row-level trigger on the Users table that will automatically create a theme entry in the Preferences table.

I’m basing these notes off what I reviewed here.

The trigger you’d create in MYSQL would be something like the following (adapting the block of theme colour value in single quotes to the theme values you want):

CREATE TRIGGER set_default_theme
AFTER INSERT ON users
FOR EACH ROW
INSERT INTO preferences(userid, category, name, value) VALUES (NEW.id, 'theme', '', '{"sidebarBg":"#2f3136","sidebarText":"#8f9297","sidebarUnreadText":"#dcddde","sidebarTextHoverBg":"#35363c","sidebarTextActiveBorder":"#35363c","sidebarTextActiveColor":"#dcddde","sidebarHeaderBg":"#282b30","sidebarHeaderTextColor":"#818386","onlineIndicator":"#64b285","awayIndicator":"#eea941","dndIndicator":"#b74b47","mentionBj":"#ffffff","mentionColor":"#dcddde","centerChannelBg":"#36393e","centerChannelColor":"#dcddde","newMessageSeparator":"#eea941","linkColor":"#7289da","buttonBg":"#43b581","buttonColor":"#ffffff","errorTextColor":"#fd5960","mentionHighlightBg":"#36393e","mentionHighlightLink":"#7289da","codeTheme":"monokai","mentionBg":"#7289da"}');

Once this is complete, you can list the trigger using:

SHOW TRIGGERS;

Try creating a new user and see if their theme is automatically set. You can also query the Preferences table for that new user to see if you see the inserted theme value:

SELECT * FROM preferences;

If you want some ideas for interesting Mattermost Themes please have a look at this link.

One last thing I’ll add. The above recipe will set the theme for brand new users in the system, but if you want to update the theme for existing users you’ll need to do something different. One option would be ot use our Preferences API Endpoint or you can use a similar DB call as you see in the trigger above. Something like:

UPDATE preferences SET value = '{"sidebarBg":"#2f3136","sidebarText":"#8f9297","sidebarUnreadText":"#dcddde","sidebarTextHoverBg":"#35363c","sidebarTextActiveBorder":"#35363c","sidebarTextActiveColor":"#dcddde","sidebarHeaderBg":"#282b30","sidebarHeaderTextColor":"#818386","onlineIndicator":"#64b285","awayIndicator":"#eea941","dndIndicator":"#b74b47","mentionBj":"#ffffff","mentionColor":"#dcddde","centerChannelBg":"#36393e","centerChannelColor":"#dcddde","newMessageSeparator":"#eea941","linkColor":"#7289da","buttonBg":"#43b581","buttonColor":"#ffffff","errorTextColor":"#fd5960","mentionHighlightBg":"#36393e","mentionHighlightLink":"#7289da","codeTheme":"monokai","mentionBg":"#7289da"}' where category = 'theme'

As always, I’d recommend you test this with specific users, and in staging or development environments, prior to rolling out into production.

Please let me know if you have any questions.

1 Like

IMPORTANT UPDATE

One of our developers has created a plugin that achieves the same thing. Please check this out before looking at the unsupported DB approach I described above below :slight_smile:

1 Like