Add 3.8.0 support (mattermost)

This commit is contained in:
Wim 2017-04-09 23:15:11 +02:00
parent 8d52c98373
commit 36024d5439

View File

@ -4,9 +4,11 @@ import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"sync"
"time"
@ -61,6 +63,7 @@ type MMClient struct {
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)
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
}