diff --git a/matterclient/matterclient.go b/matterclient/matterclient.go index bdd8cbc6..a066d654 100644 --- a/matterclient/matterclient.go +++ b/matterclient/matterclient.go @@ -4,9 +4,11 @@ import ( "crypto/tls" "encoding/json" "errors" + "fmt" "net/http" "net/http/cookiejar" "net/url" + "strconv" "strings" "sync" "time" @@ -48,19 +50,20 @@ type Team struct { type MMClient struct { sync.RWMutex *Credentials - Team *Team - OtherTeams []*Team - Client *model.Client - User *model.User - Users map[string]*model.User - MessageChan chan *Message - log *log.Entry - WsClient *websocket.Conn - WsQuit bool - WsAway bool - WsConnected bool - WsSequence int64 - WsPingChan chan *model.WebSocketResponse + Team *Team + OtherTeams []*Team + Client *model.Client + User *model.User + Users map[string]*model.User + MessageChan chan *Message + log *log.Entry + WsClient *websocket.Conn + WsQuit bool + WsAway bool + WsConnected bool + WsSequence int64 + WsPingChan chan *model.WebSocketResponse + ServerVersion string } func New(login, pass, team, server string) *MMClient { @@ -105,6 +108,14 @@ func (m *MMClient) Login() error { m.Client = model.NewClient(uriScheme + m.Credentials.Server) m.Client.HttpClient.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: m.SkipTLSVerify}} m.Client.HttpClient.Timeout = time.Second * 10 + // bogus call to get the serverversion + m.Client.GetClientProperties() + if firstConnection && !supportedVersion(m.Client.ServerVersion) { + return fmt.Errorf("unsupported mattermost version: %s", m.Client.ServerVersion) + } + m.ServerVersion = m.Client.ServerVersion + m.log.Infof("Found version %s", m.ServerVersion) + var myinfo *model.Result var appErr *model.AppError var logmsg = "trying login" @@ -295,7 +306,12 @@ func (m *MMClient) UpdateChannels() error { if err != nil { return errors.New(err.DetailedError) } - mmchannels2, err := m.Client.GetMoreChannels("") + var mmchannels2 *model.Result + if m.mmVersion() >= 3.8 { + mmchannels2, err = m.Client.GetMoreChannelsPage(0, 5000) + } else { + mmchannels2, err = m.Client.GetMoreChannels("") + } if err != nil { return errors.New(err.DetailedError) } @@ -430,6 +446,14 @@ func (m *MMClient) UpdateChannelHeader(channelId string, header string) { func (m *MMClient) UpdateLastViewed(channelId string) { m.log.Debugf("posting lastview %#v", channelId) + if m.mmVersion() >= 3.8 { + view := model.ChannelView{ChannelId: channelId} + res, _ := m.Client.ViewChannel(view) + if res == false { + m.log.Errorf("ChannelView update for %s failed", channelId) + } + return + } _, err := m.Client.UpdateLastViewedAt(channelId, true) if err != nil { m.log.Error(err) @@ -663,7 +687,11 @@ func (m *MMClient) initUser() error { return errors.New(err.DetailedError) } t.Channels = mmchannels.Data.(*model.ChannelList) - mmchannels, err = m.Client.GetMoreChannels("") + if m.mmVersion() >= 3.8 { + mmchannels, err = m.Client.GetMoreChannelsPage(0, 5000) + } else { + mmchannels, err = m.Client.GetMoreChannels("") + } if err != nil { return errors.New(err.DetailedError) } @@ -691,3 +719,18 @@ func (m *MMClient) sendWSRequest(action string, data map[string]interface{}) err m.WsClient.WriteJSON(req) return nil } + +func (m *MMClient) mmVersion() float64 { + v, _ := strconv.ParseFloat(m.ServerVersion[0:3], 64) + return v +} + +func supportedVersion(version string) bool { + if strings.HasPrefix(version, "3.5.0") || + strings.HasPrefix(version, "3.6.0") || + strings.HasPrefix(version, "3.7.0") || + strings.HasPrefix(version, "3.8.0") { + return true + } + return false +}