Update code to mattermost 3.3.0 API changes

This commit is contained in:
Wim 2016-08-15 18:49:17 +02:00
parent 24defcb970
commit 6c6000dbbd
2 changed files with 76 additions and 38 deletions

View File

@ -154,8 +154,9 @@ func (b *Bmattermost) handleMatter() {
func (b *Bmattermost) handleMatterClient(mchan chan *MMMessage) { func (b *Bmattermost) handleMatterClient(mchan chan *MMMessage) {
for message := range b.mc.MessageChan { for message := range b.mc.MessageChan {
// do not post our own messages back to irc // do not post our own messages back to irc
if message.Raw.Action == "posted" && b.mc.User.Username != message.Username { if message.Raw.Event == "posted" && b.mc.User.Username != message.Username {
flog.mm.Debugf("receiving from matterclient %#v", message) flog.mm.Debugf("receiving from matterclient %#v", message)
flog.mm.Debugf("receiving from matterclient %#v", message.Raw)
m := &MMMessage{} m := &MMMessage{}
m.Username = message.Username m.Username = message.Username
m.Channel = message.Channel m.Channel = message.Channel

View File

@ -2,6 +2,7 @@ package matterclient
import ( import (
"crypto/tls" "crypto/tls"
"encoding/json"
"errors" "errors"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
@ -27,7 +28,7 @@ type Credentials struct {
} }
type Message struct { type Message struct {
Raw *model.Message Raw *model.WebSocketEvent
Post *model.Post Post *model.Post
Team string Team string
Channel string Channel string
@ -49,14 +50,16 @@ type MMClient struct {
Team *Team Team *Team
OtherTeams []*Team OtherTeams []*Team
Client *model.Client Client *model.Client
WsClient *websocket.Conn
WsQuit bool
WsAway bool
WsConnected bool
User *model.User User *model.User
Users map[string]*model.User Users map[string]*model.User
MessageChan chan *Message MessageChan chan *Message
log *log.Entry log *log.Entry
WsClient *websocket.Conn
WsQuit bool
WsAway bool
WsConnected bool
WsSequence int64
WsPingChan chan *model.WebSocketResponse
} }
func New(login, pass, team, server string) *MMClient { func New(login, pass, team, server string) *MMClient {
@ -151,7 +154,7 @@ func (m *MMClient) Login() error {
m.Client.SetTeamId(m.Team.Id) m.Client.SetTeamId(m.Team.Id)
// setup websocket connection // setup websocket connection
wsurl := wsScheme + m.Credentials.Server + "/api/v3/users/websocket" wsurl := wsScheme + m.Credentials.Server + model.API_URL_SUFFIX + "/users/websocket"
header := http.Header{} header := http.Header{}
header.Set(model.HEADER_AUTH, "BEARER "+m.Client.AuthToken) header.Set(model.HEADER_AUTH, "BEARER "+m.Client.AuthToken)
@ -169,6 +172,8 @@ func (m *MMClient) Login() error {
} }
b.Reset() b.Reset()
m.WsSequence = 1
m.WsPingChan = make(chan *model.WebSocketResponse)
// only start to parse WS messages when login is completely done // only start to parse WS messages when login is completely done
m.WsConnected = true m.WsConnected = true
@ -190,42 +195,43 @@ func (m *MMClient) Logout() error {
func (m *MMClient) WsReceiver() { func (m *MMClient) WsReceiver() {
for { for {
var rmsg model.Message var rawMsg json.RawMessage
if m.WsQuit { var err error
m.log.Debug("exiting WsReceiver") if _, rawMsg, err = m.WsClient.ReadMessage(); err != nil {
return
}
if err := m.WsClient.ReadJSON(&rmsg); err != nil {
m.log.Error("error:", err) m.log.Error("error:", err)
// reconnect // reconnect
m.Login() m.Login()
} }
// we're not fully logged in yet.
if !m.WsConnected { if !m.WsConnected {
continue continue
} }
if rmsg.Action == "ping" { if m.WsQuit {
m.handleWsPing() m.log.Debug("exiting WsReceiver")
continue return
} }
msg := &Message{Raw: &rmsg, Team: m.Credentials.Team}
var event model.WebSocketEvent
if err := json.Unmarshal(rawMsg, &event); err == nil && event.IsValid() {
m.log.Debugf("WsReceiver: %#v", event)
msg := &Message{Raw: &event, Team: m.Credentials.Team}
m.parseMessage(msg) m.parseMessage(msg)
m.MessageChan <- msg m.MessageChan <- msg
continue
} }
} var response model.WebSocketResponse
if err := json.Unmarshal(rawMsg, &response); err == nil && response.IsValid() {
func (m *MMClient) handleWsPing() { m.log.Debugf("WsReceiver: %#v", response)
m.log.Debug("Ws PING") m.parseResponse(response)
if !m.WsQuit && !m.WsAway { continue
m.log.Debug("Ws PONG") }
m.WsClient.WriteMessage(websocket.PongMessage, []byte{})
} }
} }
func (m *MMClient) parseMessage(rmsg *Message) { func (m *MMClient) parseMessage(rmsg *Message) {
switch rmsg.Raw.Action { switch rmsg.Raw.Event {
case model.ACTION_POSTED: case model.WEBSOCKET_EVENT_POSTED:
m.parseActionPost(rmsg) m.parseActionPost(rmsg)
/* /*
case model.ACTION_USER_REMOVED: case model.ACTION_USER_REMOVED:
@ -236,8 +242,17 @@ func (m *MMClient) parseMessage(rmsg *Message) {
} }
} }
func (m *MMClient) parseResponse(rmsg model.WebSocketResponse) {
if rmsg.Data != nil {
// ping reply
if rmsg.Data["text"].(string) == "pong" {
m.WsPingChan <- &rmsg
}
}
}
func (m *MMClient) parseActionPost(rmsg *Message) { func (m *MMClient) parseActionPost(rmsg *Message) {
data := model.PostFromJson(strings.NewReader(rmsg.Raw.Props["post"])) data := model.PostFromJson(strings.NewReader(rmsg.Raw.Data["post"].(string)))
// we don't have the user, refresh the userlist // we don't have the user, refresh the userlist
if m.GetUser(data.UserId) == nil { if m.GetUser(data.UserId) == nil {
m.UpdateUsers() m.UpdateUsers()
@ -246,7 +261,7 @@ func (m *MMClient) parseActionPost(rmsg *Message) {
rmsg.Channel = m.GetChannelName(data.ChannelId) rmsg.Channel = m.GetChannelName(data.ChannelId)
rmsg.Team = m.GetTeamName(rmsg.Raw.TeamId) rmsg.Team = m.GetTeamName(rmsg.Raw.TeamId)
// direct message // direct message
if data.Type == "D" { if rmsg.Raw.Data["channel_type"] == "D" {
rmsg.Channel = m.GetUser(data.UserId).Username rmsg.Channel = m.GetUser(data.UserId).Username
} }
rmsg.Text = data.Message rmsg.Text = data.Message
@ -543,12 +558,19 @@ func (m *MMClient) GetUser(userId string) *model.User {
return m.Users[userId] return m.Users[userId]
} }
func (m *MMClient) GetStatuses() error { func (m *MMClient) GetStatus(userId string) string {
_, err := m.Client.GetStatuses([]string{m.User.Id}) res, err := m.Client.GetStatuses()
if err != nil { if err != nil {
return errors.New(err.DetailedError) return ""
} }
return nil status := res.Data.(map[string]string)
if status[userId] == model.STATUS_AWAY {
return "away"
}
if status[userId] == model.STATUS_ONLINE {
return "online"
}
return "offline"
} }
func (m *MMClient) StatusLoop() { func (m *MMClient) StatusLoop() {
@ -557,13 +579,17 @@ func (m *MMClient) StatusLoop() {
return return
} }
if m.WsConnected { if m.WsConnected {
err := m.GetStatuses() m.log.Debug("WS PING")
if err != nil { m.sendWSRequest("ping", nil)
select {
case <-m.WsPingChan:
m.log.Debug("WS PONG received")
case <-time.After(time.Second * 5):
m.Logout() m.Logout()
m.Login() m.Login()
} }
} }
time.Sleep(time.Second * 30) time.Sleep(time.Second * 60)
} }
} }
@ -601,3 +627,14 @@ func (m *MMClient) initUser() error {
} }
return nil return nil
} }
func (m *MMClient) sendWSRequest(action string, data map[string]interface{}) error {
req := &model.WebSocketRequest{}
req.Seq = m.WsSequence
req.Action = action
req.Data = data
m.WsSequence++
m.log.Debugf("sendWsRequest %#v", req)
m.WsClient.WriteJSON(req)
return nil
}