Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,37 @@ func (m *Client) GetDMChannelName(userID1 string, userID2 string) string {
return model.GetDMNameFromIds(userID1, userID2)
}

const mattermostIDLen = 26

// GetDMUserIDs extracts the two user IDs from a direct message channel name (userID1__userID2).
// It returns ok as false if the channel name does not conform to the DM naming format.
func (m *Client) GetDMUserIDs(channelName string) (string, string, bool) {
userID1, userID2, ok := strings.Cut(channelName, "__")
if !ok || len(userID1) != mattermostIDLen || len(userID2) != mattermostIDLen {
return "", "", false
}

return userID1, userID2, true
}

// GetDMOtherUserID extracts the opposite participant's user ID from a DM channel name.
func (m *Client) GetDMOtherUserID(channelName, myUserID string) (string, bool) {
userID1, userID2, ok := m.GetDMUserIDs(channelName)
if !ok {
return "", false
}

if userID1 == myUserID {
return userID2, true
}

if userID2 == myUserID {
return userID1, true
}

return "", false
}

//nolint:funlen,gocognit,gocyclo
func (m *Client) GetChannelUsers(ctx context.Context, channelID string) ([]*model.User, error) {
m.Users.mu.RLock()
Expand Down Expand Up @@ -552,6 +583,14 @@ func (m *Client) IsChannelMember(channelID string) bool {
return exists
}

// IsDMChannelName reports whether the given channel name matches the Mattermost
// direct message naming convention.
func (m *Client) IsDMChannelName(channelName string) bool {
_, _, ok := m.GetDMUserIDs(channelName)

return ok
}

func (m *Client) JoinChannel(ctx context.Context, channelID string) error {
m.Users.mu.RLock()
_, joined := m.Users.joinedChannels[channelID]
Expand Down