2016-07-11 12:23:33 -07:00
|
|
|
package matterclient
|
|
|
|
|
|
|
|
import (
|
2017-07-25 14:57:27 -07:00
|
|
|
"crypto/md5"
|
2016-07-11 12:23:33 -07:00
|
|
|
"crypto/tls"
|
2016-08-15 09:49:17 -07:00
|
|
|
"encoding/json"
|
2016-07-11 12:23:33 -07:00
|
|
|
"errors"
|
2017-04-09 14:15:11 -07:00
|
|
|
"fmt"
|
2016-07-11 12:23:33 -07:00
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2017-07-25 14:57:27 -07:00
|
|
|
"github.com/hashicorp/golang-lru"
|
2016-07-11 12:23:33 -07:00
|
|
|
"github.com/jpillora/backoff"
|
2018-11-15 10:24:22 -08:00
|
|
|
prefixed "github.com/matterbridge/logrus-prefixed-formatter"
|
2016-07-11 12:23:33 -07:00
|
|
|
"github.com/mattermost/platform/model"
|
2018-11-15 10:24:22 -08:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-07-11 12:23:33 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Credentials struct {
|
|
|
|
Login string
|
|
|
|
Team string
|
|
|
|
Pass string
|
2018-10-26 07:47:56 -07:00
|
|
|
Token string
|
|
|
|
CookieToken bool
|
2016-07-11 12:23:33 -07:00
|
|
|
Server string
|
|
|
|
NoTLS bool
|
|
|
|
SkipTLSVerify bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
2016-08-15 09:49:17 -07:00
|
|
|
Raw *model.WebSocketEvent
|
2016-07-11 12:23:33 -07:00
|
|
|
Post *model.Post
|
|
|
|
Team string
|
|
|
|
Channel string
|
|
|
|
Username string
|
|
|
|
Text string
|
2017-04-07 13:27:36 -07:00
|
|
|
Type string
|
2017-06-18 06:44:54 -07:00
|
|
|
UserID string
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type Team struct {
|
|
|
|
Team *model.Team
|
|
|
|
Id string
|
2017-08-16 14:41:35 -07:00
|
|
|
Channels []*model.Channel
|
|
|
|
MoreChannels []*model.Channel
|
2016-07-11 12:23:33 -07:00
|
|
|
Users map[string]*model.User
|
|
|
|
}
|
|
|
|
|
|
|
|
type MMClient struct {
|
|
|
|
sync.RWMutex
|
|
|
|
*Credentials
|
2017-04-09 14:15:11 -07:00
|
|
|
Team *Team
|
|
|
|
OtherTeams []*Team
|
2017-08-16 14:41:35 -07:00
|
|
|
Client *model.Client4
|
2017-04-09 14:15:11 -07:00
|
|
|
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
|
2017-07-01 14:28:16 -07:00
|
|
|
OnWsConnect func()
|
2017-07-25 14:57:27 -07:00
|
|
|
lruCache *lru.Cache
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(login, pass, team, server string) *MMClient {
|
|
|
|
cred := &Credentials{Login: login, Pass: pass, Team: team, Server: server}
|
|
|
|
mmclient := &MMClient{Credentials: cred, MessageChan: make(chan *Message, 100), Users: make(map[string]*model.User)}
|
2018-02-20 16:11:41 -08:00
|
|
|
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true})
|
|
|
|
mmclient.log = log.WithFields(log.Fields{"prefix": "matterclient"})
|
2017-07-25 14:57:27 -07:00
|
|
|
mmclient.lruCache, _ = lru.New(500)
|
2016-07-11 12:23:33 -07:00
|
|
|
return mmclient
|
|
|
|
}
|
|
|
|
|
2018-02-20 16:11:41 -08:00
|
|
|
func (m *MMClient) SetDebugLog() {
|
2018-02-22 13:57:34 -08:00
|
|
|
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false, ForceFormatting: true})
|
2018-02-20 16:11:41 -08:00
|
|
|
}
|
|
|
|
|
2016-07-11 12:23:33 -07:00
|
|
|
func (m *MMClient) SetLogLevel(level string) {
|
|
|
|
l, err := log.ParseLevel(level)
|
|
|
|
if err != nil {
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.SetLevel(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) Login() error {
|
2016-12-07 15:07:24 -08:00
|
|
|
// check if this is a first connect or a reconnection
|
|
|
|
firstConnection := true
|
2017-07-13 15:35:01 -07:00
|
|
|
if m.WsConnected {
|
2016-12-07 15:07:24 -08:00
|
|
|
firstConnection = false
|
|
|
|
}
|
2016-07-11 12:23:33 -07:00
|
|
|
m.WsConnected = false
|
|
|
|
if m.WsQuit {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
b := &backoff.Backoff{
|
|
|
|
Min: time.Second,
|
|
|
|
Max: 5 * time.Minute,
|
|
|
|
Jitter: true,
|
|
|
|
}
|
|
|
|
uriScheme := "https://"
|
|
|
|
if m.NoTLS {
|
|
|
|
uriScheme = "http://"
|
|
|
|
}
|
|
|
|
// login to mattermost
|
2017-08-16 14:41:35 -07:00
|
|
|
m.Client = model.NewAPIv4Client(uriScheme + m.Credentials.Server)
|
2017-05-08 12:20:52 -07:00
|
|
|
m.Client.HttpClient.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: m.SkipTLSVerify}, Proxy: http.ProxyFromEnvironment}
|
2017-02-17 08:51:07 -08:00
|
|
|
m.Client.HttpClient.Timeout = time.Second * 10
|
2017-05-08 11:44:36 -07:00
|
|
|
|
2018-10-26 07:47:56 -07:00
|
|
|
if strings.Contains(m.Credentials.Pass, model.SESSION_COOKIE_TOKEN) {
|
|
|
|
token := strings.Split(m.Credentials.Pass, model.SESSION_COOKIE_TOKEN+"=")
|
|
|
|
if len(token) != 2 {
|
|
|
|
return errors.New("incorrect MMAUTHTOKEN. valid input is MMAUTHTOKEN=yourtoken")
|
|
|
|
}
|
|
|
|
m.Credentials.Token = token[1]
|
|
|
|
m.Credentials.CookieToken = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(m.Credentials.Pass, "token=") {
|
|
|
|
token := strings.Split(m.Credentials.Pass, "token=")
|
|
|
|
if len(token) != 2 {
|
|
|
|
return errors.New("incorrect personal token. valid input is token=yourtoken")
|
|
|
|
}
|
|
|
|
m.Credentials.Token = token[1]
|
|
|
|
}
|
|
|
|
|
2017-05-08 11:44:36 -07:00
|
|
|
for {
|
|
|
|
d := b.Duration()
|
|
|
|
// bogus call to get the serverversion
|
2017-08-16 14:41:35 -07:00
|
|
|
_, resp := m.Client.Logout()
|
|
|
|
if resp.Error != nil {
|
|
|
|
return fmt.Errorf("%#v", resp.Error.Error())
|
2017-07-22 09:13:13 -07:00
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
if firstConnection && !supportedVersion(resp.ServerVersion) {
|
|
|
|
return fmt.Errorf("unsupported mattermost version: %s", resp.ServerVersion)
|
2017-05-08 11:44:36 -07:00
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
m.ServerVersion = resp.ServerVersion
|
2017-05-08 11:44:36 -07:00
|
|
|
if m.ServerVersion == "" {
|
|
|
|
m.log.Debugf("Server not up yet, reconnecting in %s", d)
|
|
|
|
time.Sleep(d)
|
|
|
|
} else {
|
|
|
|
m.log.Infof("Found version %s", m.ServerVersion)
|
|
|
|
break
|
|
|
|
}
|
2017-04-09 14:15:11 -07:00
|
|
|
}
|
2017-05-08 11:44:36 -07:00
|
|
|
b.Reset()
|
2017-04-09 14:15:11 -07:00
|
|
|
|
2017-08-16 14:41:35 -07:00
|
|
|
var resp *model.Response
|
|
|
|
//var myinfo *model.Result
|
2016-07-11 12:23:33 -07:00
|
|
|
var appErr *model.AppError
|
|
|
|
var logmsg = "trying login"
|
|
|
|
for {
|
|
|
|
m.log.Debugf("%s %s %s %s", logmsg, m.Credentials.Team, m.Credentials.Login, m.Credentials.Server)
|
2018-10-26 07:47:56 -07:00
|
|
|
if m.Credentials.Token != "" {
|
2017-08-16 14:41:35 -07:00
|
|
|
m.Client.AuthType = model.HEADER_BEARER
|
2018-10-26 07:47:56 -07:00
|
|
|
m.Client.AuthToken = m.Credentials.Token
|
|
|
|
if m.Credentials.CookieToken {
|
|
|
|
m.log.Debugf(logmsg + " with cookie (MMAUTH) token")
|
|
|
|
m.Client.HttpClient.Jar = m.createCookieJar(m.Credentials.Token)
|
|
|
|
} else {
|
|
|
|
m.log.Debugf(logmsg + " with personal token")
|
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
m.User, resp = m.Client.GetMe("")
|
|
|
|
if resp.Error != nil {
|
2017-08-23 13:49:42 -07:00
|
|
|
return resp.Error
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
if m.User == nil {
|
2016-07-11 12:23:33 -07:00
|
|
|
m.log.Errorf("LOGIN TOKEN: %s is invalid", m.Credentials.Pass)
|
2018-10-26 07:47:56 -07:00
|
|
|
return errors.New("invalid token")
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-16 14:41:35 -07:00
|
|
|
m.User, resp = m.Client.Login(m.Credentials.Login, m.Credentials.Pass)
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
appErr = resp.Error
|
2016-07-11 12:23:33 -07:00
|
|
|
if appErr != nil {
|
|
|
|
d := b.Duration()
|
|
|
|
m.log.Debug(appErr.DetailedError)
|
2016-12-07 15:07:24 -08:00
|
|
|
if firstConnection {
|
2016-07-11 12:23:33 -07:00
|
|
|
if appErr.Message == "" {
|
|
|
|
return errors.New(appErr.DetailedError)
|
|
|
|
}
|
|
|
|
return errors.New(appErr.Message)
|
|
|
|
}
|
|
|
|
m.log.Debugf("LOGIN: %s, reconnecting in %s", appErr, d)
|
|
|
|
time.Sleep(d)
|
|
|
|
logmsg = "retrying login"
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// reset timer
|
|
|
|
b.Reset()
|
|
|
|
|
|
|
|
err := m.initUser()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Team == nil {
|
2018-03-15 12:50:32 -07:00
|
|
|
validTeamNames := make([]string, len(m.OtherTeams))
|
|
|
|
for i, t := range m.OtherTeams {
|
|
|
|
validTeamNames[i] = t.Team.Name
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Team '%s' not found in %v", m.Credentials.Team, validTeamNames)
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
|
2017-07-01 08:49:12 -07:00
|
|
|
m.wsConnect()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) wsConnect() {
|
|
|
|
b := &backoff.Backoff{
|
|
|
|
Min: time.Second,
|
|
|
|
Max: 5 * time.Minute,
|
|
|
|
Jitter: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.WsConnected = false
|
|
|
|
wsScheme := "wss://"
|
|
|
|
if m.NoTLS {
|
|
|
|
wsScheme = "ws://"
|
|
|
|
}
|
|
|
|
|
2016-07-11 12:23:33 -07:00
|
|
|
// setup websocket connection
|
2017-08-16 14:41:35 -07:00
|
|
|
wsurl := wsScheme + m.Credentials.Server + model.API_URL_SUFFIX_V4 + "/websocket"
|
2016-07-11 12:23:33 -07:00
|
|
|
header := http.Header{}
|
|
|
|
header.Set(model.HEADER_AUTH, "BEARER "+m.Client.AuthToken)
|
|
|
|
|
2017-03-25 13:05:02 -07:00
|
|
|
m.log.Debugf("WsClient: making connection: %s", wsurl)
|
2016-07-11 12:23:33 -07:00
|
|
|
for {
|
|
|
|
wsDialer := &websocket.Dialer{Proxy: http.ProxyFromEnvironment, TLSClientConfig: &tls.Config{InsecureSkipVerify: m.SkipTLSVerify}}
|
2017-07-01 08:49:12 -07:00
|
|
|
var err error
|
2016-07-11 12:23:33 -07:00
|
|
|
m.WsClient, _, err = wsDialer.Dial(wsurl, header)
|
|
|
|
if err != nil {
|
|
|
|
d := b.Duration()
|
|
|
|
m.log.Debugf("WSS: %s, reconnecting in %s", err, d)
|
|
|
|
time.Sleep(d)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-04-07 14:25:42 -07:00
|
|
|
m.log.Debug("WsClient: connected")
|
2016-08-15 09:49:17 -07:00
|
|
|
m.WsSequence = 1
|
|
|
|
m.WsPingChan = make(chan *model.WebSocketResponse)
|
2016-07-11 12:23:33 -07:00
|
|
|
// only start to parse WS messages when login is completely done
|
|
|
|
m.WsConnected = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) Logout() error {
|
|
|
|
m.log.Debugf("logout as %s (team: %s) on %s", m.Credentials.Login, m.Credentials.Team, m.Credentials.Server)
|
|
|
|
m.WsQuit = true
|
|
|
|
m.WsClient.Close()
|
|
|
|
m.WsClient.UnderlyingConn().Close()
|
2017-07-01 13:41:28 -07:00
|
|
|
if strings.Contains(m.Credentials.Pass, model.SESSION_COOKIE_TOKEN) {
|
|
|
|
m.log.Debug("Not invalidating session in logout, credential is a token")
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
_, resp := m.Client.Logout()
|
|
|
|
if resp.Error != nil {
|
|
|
|
return resp.Error
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) WsReceiver() {
|
|
|
|
for {
|
2016-08-15 09:49:17 -07:00
|
|
|
var rawMsg json.RawMessage
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if m.WsQuit {
|
|
|
|
m.log.Debug("exiting WsReceiver")
|
|
|
|
return
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
|
2016-09-05 14:08:17 -07:00
|
|
|
if !m.WsConnected {
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-19 13:58:42 -07:00
|
|
|
if _, rawMsg, err = m.WsClient.ReadMessage(); err != nil {
|
|
|
|
m.log.Error("error:", err)
|
|
|
|
// reconnect
|
2017-07-01 08:49:12 -07:00
|
|
|
m.wsConnect()
|
2016-08-19 13:58:42 -07:00
|
|
|
}
|
|
|
|
|
2016-08-15 09:49:17 -07:00
|
|
|
var event model.WebSocketEvent
|
|
|
|
if err := json.Unmarshal(rawMsg, &event); err == nil && event.IsValid() {
|
2017-07-01 13:43:53 -07:00
|
|
|
m.log.Debugf("WsReceiver event: %#v", event)
|
2016-08-15 09:49:17 -07:00
|
|
|
msg := &Message{Raw: &event, Team: m.Credentials.Team}
|
|
|
|
m.parseMessage(msg)
|
2017-07-25 14:57:27 -07:00
|
|
|
// check if we didn't empty the message
|
|
|
|
if msg.Text != "" {
|
|
|
|
m.MessageChan <- msg
|
2017-09-07 15:16:17 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// if we have file attached but the message is empty, also send it
|
|
|
|
if msg.Post != nil {
|
2017-09-18 14:44:16 -07:00
|
|
|
if msg.Text != "" || len(msg.Post.FileIds) > 0 || msg.Post.Type == "slack_attachment" {
|
2017-09-07 15:16:17 -07:00
|
|
|
m.MessageChan <- msg
|
|
|
|
}
|
2017-07-25 14:57:27 -07:00
|
|
|
}
|
2016-08-15 09:49:17 -07:00
|
|
|
continue
|
|
|
|
}
|
2016-07-11 12:23:33 -07:00
|
|
|
|
2016-08-15 09:49:17 -07:00
|
|
|
var response model.WebSocketResponse
|
|
|
|
if err := json.Unmarshal(rawMsg, &response); err == nil && response.IsValid() {
|
2017-07-01 13:43:53 -07:00
|
|
|
m.log.Debugf("WsReceiver response: %#v", response)
|
2016-08-15 09:49:17 -07:00
|
|
|
m.parseResponse(response)
|
|
|
|
continue
|
|
|
|
}
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) parseMessage(rmsg *Message) {
|
2016-08-15 09:49:17 -07:00
|
|
|
switch rmsg.Raw.Event {
|
2017-09-11 13:45:15 -07:00
|
|
|
case model.WEBSOCKET_EVENT_POSTED, model.WEBSOCKET_EVENT_POST_EDITED, model.WEBSOCKET_EVENT_POST_DELETED:
|
2016-07-11 12:23:33 -07:00
|
|
|
m.parseActionPost(rmsg)
|
2018-07-03 13:35:44 -07:00
|
|
|
case "user_updated":
|
|
|
|
user := rmsg.Raw.Data["user"].(map[string]interface{})
|
|
|
|
if _, ok := user["id"].(string); ok {
|
|
|
|
m.UpdateUser(user["id"].(string))
|
|
|
|
}
|
2018-10-27 04:20:40 -07:00
|
|
|
case "group_added":
|
|
|
|
m.UpdateChannels()
|
2016-07-11 12:23:33 -07:00
|
|
|
/*
|
|
|
|
case model.ACTION_USER_REMOVED:
|
|
|
|
m.handleWsActionUserRemoved(&rmsg)
|
|
|
|
case model.ACTION_USER_ADDED:
|
|
|
|
m.handleWsActionUserAdded(&rmsg)
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 09:49:17 -07:00
|
|
|
func (m *MMClient) parseResponse(rmsg model.WebSocketResponse) {
|
|
|
|
if rmsg.Data != nil {
|
|
|
|
// ping reply
|
|
|
|
if rmsg.Data["text"].(string) == "pong" {
|
|
|
|
m.WsPingChan <- &rmsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 12:23:33 -07:00
|
|
|
func (m *MMClient) parseActionPost(rmsg *Message) {
|
2017-07-25 14:57:27 -07:00
|
|
|
// add post to cache, if it already exists don't relay this again.
|
|
|
|
// this should fix reposts
|
|
|
|
if ok, _ := m.lruCache.ContainsOrAdd(digestString(rmsg.Raw.Data["post"].(string)), true); ok {
|
|
|
|
m.log.Debugf("message %#v in cache, not processing again", rmsg.Raw.Data["post"].(string))
|
|
|
|
rmsg.Text = ""
|
|
|
|
return
|
|
|
|
}
|
2016-08-15 09:49:17 -07:00
|
|
|
data := model.PostFromJson(strings.NewReader(rmsg.Raw.Data["post"].(string)))
|
2016-07-11 12:23:33 -07:00
|
|
|
// we don't have the user, refresh the userlist
|
|
|
|
if m.GetUser(data.UserId) == nil {
|
2018-11-07 11:36:50 -08:00
|
|
|
m.log.Infof("User '%v' is not known, ignoring message '%#v'",
|
|
|
|
data.UserId, data)
|
2017-07-01 13:43:53 -07:00
|
|
|
return
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
2017-06-05 12:35:38 -07:00
|
|
|
rmsg.Username = m.GetUserName(data.UserId)
|
2016-07-11 12:23:33 -07:00
|
|
|
rmsg.Channel = m.GetChannelName(data.ChannelId)
|
2017-06-18 06:44:54 -07:00
|
|
|
rmsg.UserID = data.UserId
|
2017-04-07 13:27:36 -07:00
|
|
|
rmsg.Type = data.Type
|
2017-04-15 11:21:57 -07:00
|
|
|
teamid, _ := rmsg.Raw.Data["team_id"].(string)
|
|
|
|
// edit messsages have no team_id for some reason
|
|
|
|
if teamid == "" {
|
|
|
|
// we can find the team_id from the channelid
|
2017-06-12 11:30:30 -07:00
|
|
|
teamid = m.GetChannelTeamId(data.ChannelId)
|
2017-04-15 11:21:57 -07:00
|
|
|
rmsg.Raw.Data["team_id"] = teamid
|
|
|
|
}
|
|
|
|
if teamid != "" {
|
|
|
|
rmsg.Team = m.GetTeamName(teamid)
|
|
|
|
}
|
2016-07-11 12:23:33 -07:00
|
|
|
// direct message
|
2016-08-15 09:49:17 -07:00
|
|
|
if rmsg.Raw.Data["channel_type"] == "D" {
|
2016-07-11 12:23:33 -07:00
|
|
|
rmsg.Channel = m.GetUser(data.UserId).Username
|
|
|
|
}
|
|
|
|
rmsg.Text = data.Message
|
|
|
|
rmsg.Post = data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) UpdateUsers() error {
|
2017-08-16 14:41:35 -07:00
|
|
|
mmusers, resp := m.Client.GetUsers(0, 50000, "")
|
|
|
|
if resp.Error != nil {
|
|
|
|
return errors.New(resp.Error.DetailedError)
|
2016-08-14 14:04:28 -07:00
|
|
|
}
|
2016-07-11 12:23:33 -07:00
|
|
|
m.Lock()
|
2017-08-16 14:41:35 -07:00
|
|
|
for _, user := range mmusers {
|
|
|
|
m.Users[user.Id] = user
|
|
|
|
}
|
2016-07-11 12:23:33 -07:00
|
|
|
m.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) UpdateChannels() error {
|
2017-08-16 14:41:35 -07:00
|
|
|
mmchannels, resp := m.Client.GetChannelsForTeamForUser(m.Team.Id, m.User.Id, "")
|
|
|
|
if resp.Error != nil {
|
|
|
|
return errors.New(resp.Error.DetailedError)
|
2016-08-14 14:04:28 -07:00
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
m.Lock()
|
|
|
|
m.Team.Channels = mmchannels
|
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
mmchannels, resp = m.Client.GetPublicChannelsForTeam(m.Team.Id, 0, 5000, "")
|
|
|
|
if resp.Error != nil {
|
|
|
|
return errors.New(resp.Error.DetailedError)
|
2016-08-14 14:04:28 -07:00
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
|
2016-07-11 12:23:33 -07:00
|
|
|
m.Lock()
|
2017-08-16 14:41:35 -07:00
|
|
|
m.Team.MoreChannels = mmchannels
|
2016-07-11 12:23:33 -07:00
|
|
|
m.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) GetChannelName(channelId string) string {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
for _, t := range m.OtherTeams {
|
2017-07-01 13:43:53 -07:00
|
|
|
if t == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if t.Channels != nil {
|
2017-08-16 14:41:35 -07:00
|
|
|
for _, channel := range t.Channels {
|
2017-07-01 13:43:53 -07:00
|
|
|
if channel.Id == channelId {
|
2018-10-27 04:20:40 -07:00
|
|
|
if channel.Type == model.CHANNEL_GROUP {
|
2018-10-27 13:02:25 -07:00
|
|
|
res := strings.Replace(channel.DisplayName, ", ", "-", -1)
|
|
|
|
res = strings.Replace(res, " ", "_", -1)
|
|
|
|
return res
|
2018-10-27 04:20:40 -07:00
|
|
|
}
|
2017-07-01 13:43:53 -07:00
|
|
|
return channel.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t.MoreChannels != nil {
|
2017-08-16 14:41:35 -07:00
|
|
|
for _, channel := range t.MoreChannels {
|
2017-07-01 13:43:53 -07:00
|
|
|
if channel.Id == channelId {
|
2018-10-27 04:20:40 -07:00
|
|
|
if channel.Type == model.CHANNEL_GROUP {
|
2018-10-27 13:02:25 -07:00
|
|
|
res := strings.Replace(channel.DisplayName, ", ", "-", -1)
|
|
|
|
res = strings.Replace(res, " ", "_", -1)
|
|
|
|
return res
|
2018-10-27 04:20:40 -07:00
|
|
|
}
|
2017-07-01 13:43:53 -07:00
|
|
|
return channel.Name
|
|
|
|
}
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) GetChannelId(name string, teamId string) string {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
if teamId == "" {
|
2018-10-27 13:02:25 -07:00
|
|
|
for _, t := range m.OtherTeams {
|
|
|
|
for _, channel := range append(t.Channels, t.MoreChannels...) {
|
|
|
|
if channel.Type == model.CHANNEL_GROUP {
|
|
|
|
res := strings.Replace(channel.DisplayName, ", ", "-", -1)
|
|
|
|
res = strings.Replace(res, " ", "_", -1)
|
|
|
|
if res == name {
|
|
|
|
return channel.Id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
2018-10-27 13:02:25 -07:00
|
|
|
|
2016-07-11 12:23:33 -07:00
|
|
|
for _, t := range m.OtherTeams {
|
|
|
|
if t.Id == teamId {
|
2017-08-16 14:41:35 -07:00
|
|
|
for _, channel := range append(t.Channels, t.MoreChannels...) {
|
2016-07-11 12:23:33 -07:00
|
|
|
if channel.Name == name {
|
|
|
|
return channel.Id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-06-12 11:30:30 -07:00
|
|
|
func (m *MMClient) GetChannelTeamId(id string) string {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
for _, t := range append(m.OtherTeams, m.Team) {
|
2017-08-16 14:41:35 -07:00
|
|
|
for _, channel := range append(t.Channels, t.MoreChannels...) {
|
2017-06-12 11:30:30 -07:00
|
|
|
if channel.Id == id {
|
|
|
|
return channel.TeamId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-07-11 12:23:33 -07:00
|
|
|
func (m *MMClient) GetChannelHeader(channelId string) string {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
for _, t := range m.OtherTeams {
|
2017-08-16 14:41:35 -07:00
|
|
|
for _, channel := range append(t.Channels, t.MoreChannels...) {
|
2016-07-11 12:23:33 -07:00
|
|
|
if channel.Id == channelId {
|
|
|
|
return channel.Header
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-08-27 15:32:56 -07:00
|
|
|
func (m *MMClient) PostMessage(channelId string, text string) (string, error) {
|
2016-07-11 12:23:33 -07:00
|
|
|
post := &model.Post{ChannelId: channelId, Message: text}
|
2017-08-27 15:32:56 -07:00
|
|
|
res, resp := m.Client.CreatePost(post)
|
|
|
|
if resp.Error != nil {
|
|
|
|
return "", resp.Error
|
|
|
|
}
|
|
|
|
return res.Id, nil
|
|
|
|
}
|
|
|
|
|
2017-09-21 12:27:44 -07:00
|
|
|
func (m *MMClient) PostMessageWithFiles(channelId string, text string, fileIds []string) (string, error) {
|
|
|
|
post := &model.Post{ChannelId: channelId, Message: text, FileIds: fileIds}
|
|
|
|
res, resp := m.Client.CreatePost(post)
|
|
|
|
if resp.Error != nil {
|
|
|
|
return "", resp.Error
|
|
|
|
}
|
|
|
|
return res.Id, nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 15:32:56 -07:00
|
|
|
func (m *MMClient) EditMessage(postId string, text string) (string, error) {
|
|
|
|
post := &model.Post{Message: text}
|
|
|
|
res, resp := m.Client.UpdatePost(postId, post)
|
|
|
|
if resp.Error != nil {
|
|
|
|
return "", resp.Error
|
|
|
|
}
|
|
|
|
return res.Id, nil
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
|
2017-09-11 13:45:15 -07:00
|
|
|
func (m *MMClient) DeleteMessage(postId string) error {
|
|
|
|
_, resp := m.Client.DeletePost(postId)
|
|
|
|
if resp.Error != nil {
|
|
|
|
return resp.Error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-11 12:23:33 -07:00
|
|
|
func (m *MMClient) JoinChannel(channelId string) error {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
2017-08-16 14:41:35 -07:00
|
|
|
for _, c := range m.Team.Channels {
|
2016-07-11 12:23:33 -07:00
|
|
|
if c.Id == channelId {
|
|
|
|
m.log.Debug("Not joining ", channelId, " already joined.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.log.Debug("Joining ", channelId)
|
2017-08-16 14:41:35 -07:00
|
|
|
_, resp := m.Client.AddChannelMember(channelId, m.User.Id)
|
|
|
|
if resp.Error != nil {
|
2017-08-30 05:01:17 -07:00
|
|
|
return resp.Error
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) GetPostsSince(channelId string, time int64) *model.PostList {
|
2017-08-16 14:41:35 -07:00
|
|
|
res, resp := m.Client.GetPostsSince(channelId, time)
|
|
|
|
if resp.Error != nil {
|
2016-07-11 12:23:33 -07:00
|
|
|
return nil
|
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
return res
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) SearchPosts(query string) *model.PostList {
|
2017-08-16 14:41:35 -07:00
|
|
|
res, resp := m.Client.SearchPosts(m.Team.Id, query, false)
|
|
|
|
if resp.Error != nil {
|
2016-07-11 12:23:33 -07:00
|
|
|
return nil
|
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
return res
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) GetPosts(channelId string, limit int) *model.PostList {
|
2017-08-16 14:41:35 -07:00
|
|
|
res, resp := m.Client.GetPostsForChannel(channelId, 0, limit, "")
|
|
|
|
if resp.Error != nil {
|
2016-07-11 12:23:33 -07:00
|
|
|
return nil
|
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
return res
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) GetPublicLink(filename string) string {
|
2017-09-04 03:50:42 -07:00
|
|
|
res, resp := m.Client.GetFileLink(filename)
|
|
|
|
if resp.Error != nil {
|
2016-07-11 12:23:33 -07:00
|
|
|
return ""
|
|
|
|
}
|
2016-11-12 13:00:53 -08:00
|
|
|
return res
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) GetPublicLinks(filenames []string) []string {
|
|
|
|
var output []string
|
|
|
|
for _, f := range filenames {
|
2017-09-04 03:50:42 -07:00
|
|
|
res, resp := m.Client.GetFileLink(f)
|
|
|
|
if resp.Error != nil {
|
2016-07-11 12:23:33 -07:00
|
|
|
continue
|
|
|
|
}
|
2016-11-12 13:00:53 -08:00
|
|
|
output = append(output, res)
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2017-07-12 13:47:30 -07:00
|
|
|
func (m *MMClient) GetFileLinks(filenames []string) []string {
|
|
|
|
uriScheme := "https://"
|
|
|
|
if m.NoTLS {
|
|
|
|
uriScheme = "http://"
|
|
|
|
}
|
|
|
|
|
|
|
|
var output []string
|
|
|
|
for _, f := range filenames {
|
2017-09-04 03:50:42 -07:00
|
|
|
res, resp := m.Client.GetFileLink(f)
|
|
|
|
if resp.Error != nil {
|
2017-07-12 13:47:30 -07:00
|
|
|
// public links is probably disabled, create the link ourselves
|
2018-04-21 11:49:44 -07:00
|
|
|
output = append(output, uriScheme+m.Credentials.Server+model.API_URL_SUFFIX_V4+"/files/"+f)
|
2017-07-12 13:47:30 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
output = append(output, res)
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2016-07-11 12:23:33 -07:00
|
|
|
func (m *MMClient) UpdateChannelHeader(channelId string, header string) {
|
2017-08-16 14:41:35 -07:00
|
|
|
channel := &model.Channel{Id: channelId, Header: header}
|
2016-07-11 12:23:33 -07:00
|
|
|
m.log.Debugf("updating channelheader %#v, %#v", channelId, header)
|
2017-08-16 14:41:35 -07:00
|
|
|
_, resp := m.Client.UpdateChannel(channel)
|
|
|
|
if resp.Error != nil {
|
|
|
|
log.Error(resp.Error)
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-27 13:03:41 -07:00
|
|
|
func (m *MMClient) UpdateLastViewed(channelId string) error {
|
2016-07-11 12:23:33 -07:00
|
|
|
m.log.Debugf("posting lastview %#v", channelId)
|
2017-08-16 14:41:35 -07:00
|
|
|
view := &model.ChannelView{ChannelId: channelId}
|
2018-02-08 15:11:20 -08:00
|
|
|
_, resp := m.Client.ViewChannel(m.User.Id, view)
|
|
|
|
if resp.Error != nil {
|
|
|
|
m.log.Errorf("ChannelView update for %s failed: %s", channelId, resp.Error)
|
2018-10-27 13:03:41 -07:00
|
|
|
return resp.Error
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
2018-10-27 13:03:41 -07:00
|
|
|
return nil
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
|
2017-09-10 07:33:29 -07:00
|
|
|
func (m *MMClient) UpdateUserNick(nick string) error {
|
|
|
|
user := m.User
|
|
|
|
user.Nickname = nick
|
|
|
|
_, resp := m.Client.UpdateUser(user)
|
|
|
|
if resp.Error != nil {
|
|
|
|
return resp.Error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-11 12:23:33 -07:00
|
|
|
func (m *MMClient) UsernamesInChannel(channelId string) []string {
|
2017-08-16 14:41:35 -07:00
|
|
|
res, resp := m.Client.GetChannelMembers(channelId, 0, 50000, "")
|
|
|
|
if resp.Error != nil {
|
|
|
|
m.log.Errorf("UsernamesInChannel(%s) failed: %s", channelId, resp.Error)
|
2016-07-11 12:23:33 -07:00
|
|
|
return []string{}
|
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
allusers := m.GetUsers()
|
2016-07-11 12:23:33 -07:00
|
|
|
result := []string{}
|
2017-08-16 14:41:35 -07:00
|
|
|
for _, member := range *res {
|
|
|
|
result = append(result, allusers[member.UserId].Nickname)
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) createCookieJar(token string) *cookiejar.Jar {
|
|
|
|
var cookies []*http.Cookie
|
|
|
|
jar, _ := cookiejar.New(nil)
|
|
|
|
firstCookie := &http.Cookie{
|
|
|
|
Name: "MMAUTHTOKEN",
|
|
|
|
Value: token,
|
|
|
|
Path: "/",
|
|
|
|
Domain: m.Credentials.Server,
|
|
|
|
}
|
|
|
|
cookies = append(cookies, firstCookie)
|
|
|
|
cookieURL, _ := url.Parse("https://" + m.Credentials.Server)
|
|
|
|
jar.SetCookies(cookieURL, cookies)
|
|
|
|
return jar
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendDirectMessage sends a direct message to specified user
|
|
|
|
func (m *MMClient) SendDirectMessage(toUserId string, msg string) {
|
2018-11-03 13:51:04 -07:00
|
|
|
m.SendDirectMessageProps(toUserId, msg, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) SendDirectMessageProps(toUserId string, msg string, props map[string]interface{}) {
|
2016-07-11 12:23:33 -07:00
|
|
|
m.log.Debugf("SendDirectMessage to %s, msg %s", toUserId, msg)
|
|
|
|
// create DM channel (only happens on first message)
|
2017-09-04 05:24:22 -07:00
|
|
|
_, resp := m.Client.CreateDirectChannel(m.User.Id, toUserId)
|
|
|
|
if resp.Error != nil {
|
|
|
|
m.log.Debugf("SendDirectMessage to %#v failed: %s", toUserId, resp.Error)
|
2017-02-03 07:43:21 -08:00
|
|
|
return
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
channelName := model.GetDMNameFromIds(toUserId, m.User.Id)
|
|
|
|
|
|
|
|
// update our channels
|
2017-08-16 14:41:35 -07:00
|
|
|
m.UpdateChannels()
|
2016-07-11 12:23:33 -07:00
|
|
|
|
|
|
|
// build & send the message
|
|
|
|
msg = strings.Replace(msg, "\r", "", -1)
|
2018-11-03 13:51:04 -07:00
|
|
|
post := &model.Post{ChannelId: m.GetChannelId(channelName, m.Team.Id), Message: msg, Props: props}
|
2016-07-11 12:23:33 -07:00
|
|
|
m.Client.CreatePost(post)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTeamName returns the name of the specified teamId
|
|
|
|
func (m *MMClient) GetTeamName(teamId string) string {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
for _, t := range m.OtherTeams {
|
|
|
|
if t.Id == teamId {
|
|
|
|
return t.Team.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChannels returns all channels we're members off
|
|
|
|
func (m *MMClient) GetChannels() []*model.Channel {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
var channels []*model.Channel
|
|
|
|
// our primary team channels first
|
2017-08-16 14:41:35 -07:00
|
|
|
channels = append(channels, m.Team.Channels...)
|
2016-07-11 12:23:33 -07:00
|
|
|
for _, t := range m.OtherTeams {
|
|
|
|
if t.Id != m.Team.Id {
|
2017-08-16 14:41:35 -07:00
|
|
|
channels = append(channels, t.Channels...)
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return channels
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMoreChannels returns existing channels where we're not a member off.
|
|
|
|
func (m *MMClient) GetMoreChannels() []*model.Channel {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
var channels []*model.Channel
|
|
|
|
for _, t := range m.OtherTeams {
|
2017-08-16 14:41:35 -07:00
|
|
|
channels = append(channels, t.MoreChannels...)
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
return channels
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTeamFromChannel returns teamId belonging to channel (DM channels have no teamId).
|
|
|
|
func (m *MMClient) GetTeamFromChannel(channelId string) string {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
var channels []*model.Channel
|
|
|
|
for _, t := range m.OtherTeams {
|
2017-08-16 14:41:35 -07:00
|
|
|
channels = append(channels, t.Channels...)
|
2017-07-01 13:43:53 -07:00
|
|
|
if t.MoreChannels != nil {
|
2017-08-16 14:41:35 -07:00
|
|
|
channels = append(channels, t.MoreChannels...)
|
2017-07-01 13:43:53 -07:00
|
|
|
}
|
2016-07-11 12:23:33 -07:00
|
|
|
for _, c := range channels {
|
|
|
|
if c.Id == channelId {
|
2018-10-27 13:02:25 -07:00
|
|
|
if c.Type == model.CHANNEL_GROUP {
|
|
|
|
return "G"
|
|
|
|
}
|
2016-07-11 12:23:33 -07:00
|
|
|
return t.Id
|
|
|
|
}
|
|
|
|
}
|
2018-10-27 13:02:25 -07:00
|
|
|
channels = nil
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) GetLastViewedAt(channelId string) int64 {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
2017-08-16 14:41:35 -07:00
|
|
|
res, resp := m.Client.GetChannelMember(channelId, m.User.Id, "")
|
|
|
|
if resp.Error != nil {
|
2016-11-13 07:03:04 -08:00
|
|
|
return model.GetMillis()
|
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
return res.LastViewedAt
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) GetUsers() map[string]*model.User {
|
|
|
|
users := make(map[string]*model.User)
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
for k, v := range m.Users {
|
|
|
|
users[k] = v
|
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) GetUser(userId string) *model.User {
|
2017-07-01 13:43:53 -07:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2017-07-13 15:35:01 -07:00
|
|
|
_, ok := m.Users[userId]
|
2017-07-01 14:02:56 -07:00
|
|
|
if !ok {
|
2017-08-16 14:41:35 -07:00
|
|
|
res, resp := m.Client.GetUser(userId, "")
|
|
|
|
if resp.Error != nil {
|
2017-07-01 14:02:56 -07:00
|
|
|
return nil
|
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
m.Users[userId] = res
|
2017-07-01 14:02:56 -07:00
|
|
|
}
|
2016-07-11 12:23:33 -07:00
|
|
|
return m.Users[userId]
|
|
|
|
}
|
|
|
|
|
2018-07-03 13:35:44 -07:00
|
|
|
func (m *MMClient) UpdateUser(userId string) {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
res, resp := m.Client.GetUser(userId, "")
|
|
|
|
if resp.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.Users[userId] = res
|
|
|
|
}
|
|
|
|
|
2017-06-05 12:35:38 -07:00
|
|
|
func (m *MMClient) GetUserName(userId string) string {
|
|
|
|
user := m.GetUser(userId)
|
|
|
|
if user != nil {
|
|
|
|
return user.Username
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-07-03 13:35:44 -07:00
|
|
|
func (m *MMClient) GetNickName(userId string) string {
|
|
|
|
user := m.GetUser(userId)
|
|
|
|
if user != nil {
|
|
|
|
return user.Nickname
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-08-15 09:49:17 -07:00
|
|
|
func (m *MMClient) GetStatus(userId string) string {
|
2017-08-16 14:41:35 -07:00
|
|
|
res, resp := m.Client.GetUserStatus(userId, "")
|
|
|
|
if resp.Error != nil {
|
2016-08-15 09:49:17 -07:00
|
|
|
return ""
|
2016-08-14 13:53:09 -07:00
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
if res.Status == model.STATUS_AWAY {
|
2016-08-15 09:49:17 -07:00
|
|
|
return "away"
|
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
if res.Status == model.STATUS_ONLINE {
|
2016-08-15 09:49:17 -07:00
|
|
|
return "online"
|
|
|
|
}
|
|
|
|
return "offline"
|
2016-08-14 13:53:09 -07:00
|
|
|
}
|
|
|
|
|
2018-04-01 13:53:12 -07:00
|
|
|
func (m *MMClient) UpdateStatus(userId string, status string) error {
|
|
|
|
_, resp := m.Client.UpdateUserStatus(userId, &model.Status{Status: status})
|
|
|
|
if resp.Error != nil {
|
|
|
|
return resp.Error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-17 13:47:59 -08:00
|
|
|
func (m *MMClient) GetStatuses() map[string]string {
|
2017-08-16 14:41:35 -07:00
|
|
|
var ids []string
|
2017-01-17 13:47:59 -08:00
|
|
|
statuses := make(map[string]string)
|
2017-08-16 14:41:35 -07:00
|
|
|
for id := range m.Users {
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
res, resp := m.Client.GetUsersStatusesByIds(ids)
|
|
|
|
if resp.Error != nil {
|
2017-01-17 13:47:59 -08:00
|
|
|
return statuses
|
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
for _, status := range res {
|
|
|
|
statuses[status.UserId] = "offline"
|
|
|
|
if status.Status == model.STATUS_AWAY {
|
|
|
|
statuses[status.UserId] = "away"
|
|
|
|
}
|
|
|
|
if status.Status == model.STATUS_ONLINE {
|
|
|
|
statuses[status.UserId] = "online"
|
2017-01-17 13:47:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return statuses
|
|
|
|
}
|
|
|
|
|
2016-10-23 12:16:14 -07:00
|
|
|
func (m *MMClient) GetTeamId() string {
|
|
|
|
return m.Team.Id
|
|
|
|
}
|
|
|
|
|
2017-09-21 12:27:44 -07:00
|
|
|
func (m *MMClient) UploadFile(data []byte, channelId string, filename string) (string, error) {
|
|
|
|
f, resp := m.Client.UploadFile(data, channelId, filename)
|
|
|
|
if resp.Error != nil {
|
|
|
|
return "", resp.Error
|
|
|
|
}
|
|
|
|
return f.FileInfos[0].Id, nil
|
|
|
|
}
|
|
|
|
|
2016-08-14 13:53:09 -07:00
|
|
|
func (m *MMClient) StatusLoop() {
|
2017-07-01 14:28:16 -07:00
|
|
|
retries := 0
|
|
|
|
backoff := time.Second * 60
|
|
|
|
if m.OnWsConnect != nil {
|
|
|
|
m.OnWsConnect()
|
|
|
|
}
|
2018-11-07 11:36:50 -08:00
|
|
|
m.log.Debug("StatusLoop:", m.OnWsConnect != nil)
|
2016-08-14 13:53:09 -07:00
|
|
|
for {
|
|
|
|
if m.WsQuit {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if m.WsConnected {
|
2018-10-27 13:03:41 -07:00
|
|
|
m.checkAlive()
|
2016-08-15 09:49:17 -07:00
|
|
|
select {
|
|
|
|
case <-m.WsPingChan:
|
|
|
|
m.log.Debug("WS PONG received")
|
2017-07-01 14:28:16 -07:00
|
|
|
backoff = time.Second * 60
|
2016-08-15 09:49:17 -07:00
|
|
|
case <-time.After(time.Second * 5):
|
2017-07-01 14:28:16 -07:00
|
|
|
if retries > 3 {
|
2017-11-16 11:19:52 -08:00
|
|
|
m.log.Debug("StatusLoop() timeout")
|
2017-07-01 14:28:16 -07:00
|
|
|
m.Logout()
|
|
|
|
m.WsQuit = false
|
2017-11-16 11:19:52 -08:00
|
|
|
err := m.Login()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Login failed: %#v", err)
|
|
|
|
break
|
|
|
|
}
|
2017-07-01 14:28:16 -07:00
|
|
|
if m.OnWsConnect != nil {
|
|
|
|
m.OnWsConnect()
|
|
|
|
}
|
|
|
|
go m.WsReceiver()
|
|
|
|
} else {
|
|
|
|
retries++
|
|
|
|
backoff = time.Second * 5
|
|
|
|
}
|
2016-08-14 13:53:09 -07:00
|
|
|
}
|
|
|
|
}
|
2017-07-01 14:28:16 -07:00
|
|
|
time.Sleep(backoff)
|
2016-08-14 13:53:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 12:23:33 -07:00
|
|
|
// initialize user and teams
|
|
|
|
func (m *MMClient) initUser() error {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
// we only load all team data on initial login.
|
|
|
|
// all other updates are for channels from our (primary) team only.
|
2016-09-19 14:58:57 -07:00
|
|
|
//m.log.Debug("initUser(): loading all team data")
|
2017-08-16 14:41:35 -07:00
|
|
|
teams, resp := m.Client.GetTeamsForUser(m.User.Id, "")
|
|
|
|
if resp.Error != nil {
|
|
|
|
return resp.Error
|
|
|
|
}
|
|
|
|
for _, team := range teams {
|
|
|
|
mmusers, resp := m.Client.GetUsersInTeam(team.Id, 0, 50000, "")
|
|
|
|
if resp.Error != nil {
|
|
|
|
return errors.New(resp.Error.DetailedError)
|
2017-01-06 14:51:44 -08:00
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
usermap := make(map[string]*model.User)
|
|
|
|
for _, user := range mmusers {
|
|
|
|
usermap[user.Id] = user
|
2017-04-09 14:15:11 -07:00
|
|
|
}
|
2017-08-30 05:01:17 -07:00
|
|
|
|
2017-08-16 14:41:35 -07:00
|
|
|
t := &Team{Team: team, Users: usermap, Id: team.Id}
|
|
|
|
|
2017-08-30 05:01:17 -07:00
|
|
|
mmchannels, resp := m.Client.GetChannelsForTeamForUser(team.Id, m.User.Id, "")
|
2017-08-16 14:41:35 -07:00
|
|
|
if resp.Error != nil {
|
2017-08-30 05:01:17 -07:00
|
|
|
return resp.Error
|
2017-01-06 14:51:44 -08:00
|
|
|
}
|
2017-08-16 14:41:35 -07:00
|
|
|
t.Channels = mmchannels
|
2017-08-30 05:01:17 -07:00
|
|
|
mmchannels, resp = m.Client.GetPublicChannelsForTeam(team.Id, 0, 5000, "")
|
|
|
|
if resp.Error != nil {
|
|
|
|
return resp.Error
|
|
|
|
}
|
|
|
|
t.MoreChannels = mmchannels
|
2016-07-11 12:23:33 -07:00
|
|
|
m.OtherTeams = append(m.OtherTeams, t)
|
2017-08-16 14:41:35 -07:00
|
|
|
if team.Name == m.Credentials.Team {
|
2016-07-11 12:23:33 -07:00
|
|
|
m.Team = t
|
2017-08-16 14:41:35 -07:00
|
|
|
m.log.Debugf("initUser(): found our team %s (id: %s)", team.Name, team.Id)
|
2016-07-11 12:23:33 -07:00
|
|
|
}
|
|
|
|
// add all users
|
|
|
|
for k, v := range t.Users {
|
|
|
|
m.Users[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-15 09:49:17 -07:00
|
|
|
|
2018-10-27 13:03:41 -07:00
|
|
|
func (m *MMClient) checkAlive() error {
|
|
|
|
// check if session still is valid
|
|
|
|
_, resp := m.Client.GetMe("")
|
|
|
|
if resp.Error != nil {
|
|
|
|
return resp.Error
|
|
|
|
}
|
|
|
|
m.log.Debug("WS PING")
|
|
|
|
return m.sendWSRequest("ping", nil)
|
|
|
|
}
|
|
|
|
|
2016-08-15 09:49:17 -07:00
|
|
|
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
|
|
|
|
}
|
2017-04-09 14:15:11 -07:00
|
|
|
|
|
|
|
func supportedVersion(version string) bool {
|
2017-08-16 14:41:35 -07:00
|
|
|
if strings.HasPrefix(version, "3.8.0") ||
|
2017-06-14 16:30:05 -07:00
|
|
|
strings.HasPrefix(version, "3.9.0") ||
|
2017-07-09 06:15:22 -07:00
|
|
|
strings.HasPrefix(version, "3.10.0") ||
|
2018-06-17 14:48:06 -07:00
|
|
|
strings.HasPrefix(version, "4.") ||
|
|
|
|
strings.HasPrefix(version, "5.") {
|
2017-04-09 14:15:11 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2017-07-25 14:57:27 -07:00
|
|
|
|
|
|
|
func digestString(s string) string {
|
|
|
|
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
|
|
|
|
}
|