Mattermost, Inc.

Get all channels a user is involved (Public, Private and Direct Messages)

Hi Guys,

I’m trying to get all the channels where a user is involved, I have checked the API documentation, Github issues and this forum and I couldn’t find anything related to this.

My purpose is to do this through the REST API (not Websocket, as the integration I’m trying to do is not feasible through them).

So, what I want to accomplish is, given a user ID, get a list of channels which that user belongs to (all type of channels) , is this even possible?

Hi, @jac1013

I went through the API documentation and would like to know if you have tried using Get channels for user:

https://your-mattermost-url.com/api/v4/users/{user_id}/teams/{team_id}/channels

May I know if that works for you? There is also an SQL query for this in case you are interested:

SELECT DISTINCT u.Username, t.DisplayName AS TeamName, c.DisplayName AS ChannelName, cm.SchemeUser AS User, cm.SchemeAdmin AS Admin FROM ChannelMembers AS cm JOIN Channels AS c ON (cm.ChannelId=c.Id) JOIN Users AS u ON (cm.UserId=u.Id) JOIN Teams AS t ON (c.TeamId=t.Id) WHERE u.Username = "<username>";

Hi @ahmaddanial.

Thanks for the answer. So yes, I saw this endpoint in the documentation but since it receives a teamId my impression of it was that I wouldn’t get DM channels. Given you recommended using it I gave it a try and was able to accomplish what I wanted.

For people figuring this out in the future here are the endpoints that I used to accomplish getting all the posts for users in all channels.

I hope this helps anyone later on!

1 Like