Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,6 @@ 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) {
Expand Down
2 changes: 2 additions & 0 deletions matterclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ const (
schemeHTTP = "http://"
)

const mattermostIDLen = 26

// Mattermost has a hardcoded `PerPageMaximum = 200` & `LimitMaximum = 200`
// See https://github.com/mattermost/mattermost/blob/master/server/channels/web/params.go
const mattermostPerPageMax = 200
Expand Down
20 changes: 20 additions & 0 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -240,6 +241,13 @@ func (m *Client) GetUserByUserID(ctx context.Context, userID string) *model.User
func (m *Client) GetUserByUsername(ctx context.Context, username string) *model.User {
m.Users.mu.RLock()

// Fast path: check if caller passed an already-cached User ID
if u, ok := m.Users.users[username]; ok {
m.Users.mu.RUnlock()
return u
}

// Scan cached users by username
for _, u := range m.Users.users {
if u.Username == username {
m.Users.mu.RUnlock()
Expand All @@ -251,6 +259,7 @@ func (m *Client) GetUserByUsername(ctx context.Context, username string) *model.

var mmuser *model.User

// Query API by Username (handles genuine 26-character usernames)
retryCount := 0
for {
m.apiLogger.Warnf("GetUserByUsername: User: %s #%d", username, retryCount)
Expand All @@ -261,12 +270,23 @@ func (m *Client) GetUserByUsername(ctx context.Context, username string) *model.
break
}

// If user not found and the string looks like an ID, cascade to GetUser
if resp != nil && resp.StatusCode == http.StatusNotFound && model.IsValidId(username) {
m.logger.Debugf("GetUserByUsername: %s not found as username, attempting lookup by User ID", username)
return m.GetUser(ctx, username)
}

shouldRetry, hErr := m.HandleRetry(ctx, "GetUserByUsername", err, retryCount, 10, resp)
if hErr == nil && shouldRetry {
retryCount++
continue
}

// Fallback attempt by ID if error was not retryable
if model.IsValidId(username) {
return m.GetUser(ctx, username)
}

m.logger.Debugf("GetUserByUsername failed to fetch missing user %s: %v", username, err)

return nil
Expand Down