Just to confirm, the following PostgreSQL psql
command-line query “one-liner” works to get channel members (if EnableLocalMode
is enabled in config.json
; otherwise, you will need to use mmctl auth login
):
CHANNEL_NAME="PASTE_CHANNEL_NAME_HERE"; \
DB_USER="mmuser_USERNAME"; \
DB_HOST="DATABASE_SERVER_HOSTNAME"; \
DB_NAME="mattermost_DATABASE_NAME"; \
DB_PASSWORD="YOUR_PASSWORD"; \
PGPASSWORD="${DB_PASSWORD}" psql "postgres://${DB_USER}@${DB_HOST}/${DB_NAME}?sslmode=disable&connect_timeout=10" \
-t -A -c \
"SELECT DISTINCT CONCAT('${CHANNEL_NAME}', ' :: ', u.username)
FROM channels AS c
JOIN channelmembers AS cm ON cm.channelid = c.id
JOIN users AS u ON cm.userid = u.id
WHERE c.name = '${CHANNEL_NAME}';"
It would probably be better to turn this into a script, but I felt like this should be documented. It would definitely make sense to have this a command in mmctl
though instead of having to jump through these hoops.
This can be mixed with the following commands in various permutations to remove all users from a channel and then re-add them once a CATEGORY has been assigned under Channel Actions
if you want all users to have a Category for a channel:
Remove all users:
mmctl --local channel users remove TEAM_NAME:CHANNEL_NAME --all-users
Add all users:
mmctl --local channel users add TEAM_NAME:CHANNEL_NAME $(
mmctl --local user list --all --json 2>/dev/null | jq -r '.[] | select(.delete_at == 0) | .username' | xargs
)
To capture the above users, you could do something like this to get them into a var and then re-add them (not tested):
CHANNEL_NAME="PASTE_CHANNEL_NAME_HERE"; DB_USER="mmuser_USERNAME"; \
DB_HOST="DATABASE_SERVER_HOSTNAME"; DB_NAME="mattermost_DATABASE_NAME"; \
DB_PASSWORD="YOUR_PASSWORD"; \
export USERS=$(PGPASSWORD="${DB_PASSWORD}" psql "postgres://${DB_USER}@${DB_HOST}/${DB_NAME}?sslmode=disable&connect_timeout=10" \
-t -A -c \
"SELECT DISTINCT u.username
FROM channels AS c
JOIN channelmembers AS cm ON cm.channelid = c.id
JOIN users AS u ON cm.userid = u.id
WHERE c.name = '${CHANNEL_NAME}';" | xargs)
mmctl --local channel users add TEAM_NAME:CHANNEL_NAME $USERS