Make Categories the same for all Members

Mattermost Version: 7.5.2
Database Schema Version: 95
Database: postgres

How does the Administrator make categories the same for everyone?

For example, if I put in Channel 1, Channel 2 & Channel 3 in Category My Category, then I want new member to have this category, including the three channels, visible in the sidebar.

Hey Akito,

You can do that with an onboardingscript which creates the categories and adds the channels for the user in the sidebar via the API, but that requires you to know when people join or to prepare their accounts before first use, not sure if this is an option.

There’s a feature which never really worked for me and is called “Default channels” which can be used to add all new users to a list of channels you specify in the server configuration. Using this feature combined with the feature “Channel actions”, you can automatically put the channels into categories for the users, at least during provisioning, but the users can change that, so you cannot enforce this configuration on them if they want to change it (or leave channels, f.ex.) and they can also rename their categories.

Would something like that help or do you require more control on what your users see and can do to their view?

Was searching for that, but I did not find anything.

What the Welcome Bot can do, is very useful. If it only would create categories, as well.

The onboardingscript is nothing you can find online, it’s more a hint that you would need something like that.
I wrote a script for someone on the community server some time ago, which should give you an idea on how to proceed. If not, let me know how we can fine-tune it to your needs:

#!/bin/bash

# space separated list of channels to add for each user
CHANNELS="writing_work lab_work cyber_342w_homework_help general_ist_homework_help off_topic"
# team to add the users to
TEAM="cyber-342w"
# the maildomain added to the username
MAILDOMAIN="school.edu"
# mmctl command
MMCTL="mmctl --local"
# password to be used for all accounts. Leave empty and set PWLEN > 0 if you want passwords to be auto-generated
PASSWORD=""
PWLEN=16
# set to empty string if you want to actually run the commands, set to "echo" if you just want to see what woule be done
DEBUG="echo"

# first parameter of the script will be the file to read the input data from
# if no parameter is given or if the file cannot be found, fail
if [ -z "$1" ] || ! [ -f "$1" ]; then
  echo "input file $1 not found."
  exit 1
fi

while read line; do
  # split the input line by space into parts
  IFS=" " read -r -a PARTS <<< "$line"
  # generate random password if requested
  [ "$PWLEN" -gt "0" ] && PASSWORD=$(makepasswd --chars=$PWLEN)
  # create the user account
  $DEBUG $MMCTL user create --email "${PARTS[0]}@$MAILDOMAIN" --email-verified --firstname ${PARTS[1]} --lastname ${PARTS[2]} --locale en --password $PASSWORD --username ${PARTS[0]}
  # add it to all mentioned channels
  for channel in $CHANNELS; do
    $DEBUG $MMCTL channel users add $TEAM:$channel ${PARTS[0]}
  done
  # output user + password for copy/pasting it to the users
  echo "${PARTS[0]} / $PASSWORD"
done < "$1"

This script reads in a CSV of users to create and sets them up automatically and also adds them to a list of predefined channels.
The categories can be done using channel actions, as I said - so in your existing channels, you can click on the channel and choose “Channel actions” which will give you the following modal:

Here you can specify the category to add this channel to and when the category does not exist, it will be created on the fly for the user that joined the channel.

The script seems very helpful. Thank you.

Meanwhile, I checked out the API Documentation and simply could not find a way to add a channel to a specific category or assign a category to a specific channel.

Am I missing something?

That’s because a channel does not belong to a sidebar category. The categories are part of the user’s view, so every user could have his own categories, that’s why you cannot configure that by channel.

The API calls you’re looking for are CreateSidebarCategoryForTeamForUser et al.

Yes, I understand, but there must be a way to move a channel into a specific category, for that particular user. The database has some kind of connection there, as well, to know, which channel ought to be shown under which category.

Seen that one, but it does not let me assign channels to it, by user or something equivalent to that functionality. :confused:

Okay, so the only place I found anything related to what I am seeking is in GetSidebarCategoriesForTeamForUser.

Example Output

[
  {
    "order": [
      "string"
    ],
    "categories": [
      {
        "id": "string",
        "user_id": "string",
        "team_id": "string",
        "display_name": "string",
        "type": "channels",
        "channel_ids": [
          "string"
        ]
      }
    ]
  }
]

Which displays the assigned channel IDs.

Now, I need the same thing, except the other way around. I want to send a payload, patching an existing sidebar category to include all channel IDs from the provided array.

Never trust the official API documentation!

As it turns out, providing the channel IDs via UpdateSidebarCategoriesForTeamForUser was already possible – it’s just, that the API documentation is incomplete.

So, if anyone wants to move a channel from one category to another, one must fetch both categories, remove the channel IDs in question from the current category & add them to the channel ID list of the new category. Then, PUT the category array to https://your-mattermost-url.com/api/v4/users/{user_id}/teams/{team_id}/channels/categories & the channel is moved, after the user reloaded the Mattermost page in the browser.

Nu Definition

#!/usr/bin/nu

def content_type [] { "application/json" }
def url_base [] { "https://your-mattermost-url.com/api/v4/" }
def url_user_sidebar_categories [ team_id: string user_id: string ] { $"(url_base)users/($user_id)/teams/($team_id)/channels/categories" }

# Update User's Sidebar Categories
# https://api.mattermost.com/#tag/channels/operation/UpdateSidebarCategoriesForTeamForUser
export def updateUsersSidebarCategories [
  token: string # https://docs.mattermost.com/developer/personal-access-tokens.html#creating-a-personal-access-token
  team_id: string
  user_id: string
  body: string
] {
  # PUT to API
  (
    http put
      -H [ Authorization $"Bearer ($token)" ]
      -H [ Accept content_type ]
      $"(url_user_sidebar_categories $team_id $user_id)"
      $body
  )
}
Currently only works with Nu 0.77.0, which is not yet released, at the time of writing.

Thanks for the hint about the documentation, I cannot see the channel_id in your example. Are you saying that the payload in the documentation

[
  {
    "id": "string",
    "user_id": "string",
    "team_id": "string",
    "display_name": "string",
    "type": "channels"
  }
]

does also support a channel_id like this`?

[
  {
    "id": "string",
    "user_id": "string",
    "team_id": "string",
    "display_name": "string",
    "channel_id": "string",
    "type": "channels"
  }
]

Also, can you send me a link to the nu tool you’re referencing here? I’m unable to find it on the net.

@agriesser

I’m back.

Had decided to dive a bit deeper into this issue.

This is my almost final result.

Sorry to butt in, but is it possible to have such a function as a feature request?
We would need that too.

Hi @iqoqi and welcome to the Mattermost forums!

The more people request such a feature on Mattermost’s product board, the more likely we will see it, so please raise your voice on https://mattermost.com/suggestions for such a feature.

@agriesser @Akito
Based on a recent support ticket, I was told that this API exists, but looks like it hasn’t been documented yet. The “Channel Actions” mentioned here can be done graphically, but to do it via API you need to use this:

https:/<url>/plugins/playbooks/api/v0/actions/channels/<channel id>
with a JSON payload of:

{
    "enabled":true,
    "payload":
        {
            "category_name":"Testing01"
        },
    "channel_id":"sd9sxu89c7d5igqchqxdrryoyw",
    "action_type":"categorize_channel",
    "trigger_type":"new_member_joins"
}

I tested this and it works for me (cloud).