2020-02-17 22:17:13 +00:00
|
|
|
package mattermost
|
|
|
|
|
|
|
|
import (
|
2020-02-23 19:24:50 +00:00
|
|
|
"encoding/json"
|
2020-02-17 22:17:13 +00:00
|
|
|
"fmt"
|
2020-02-23 19:24:50 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2020-02-17 22:17:13 +00:00
|
|
|
_ "os"
|
|
|
|
"strings"
|
2020-02-26 14:26:57 +00:00
|
|
|
"sync"
|
2020-02-17 22:17:13 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/42wim/matterbridge/matterclient"
|
2020-02-23 19:24:50 +00:00
|
|
|
"github.com/mattermost/mattermost-server/model"
|
2020-02-18 16:17:04 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-02-17 22:17:13 +00:00
|
|
|
|
|
|
|
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
|
|
|
|
)
|
|
|
|
|
|
|
|
// User id format: nickname@server
|
|
|
|
// Room id format: room_name@team@server
|
|
|
|
|
|
|
|
type Mattermost struct {
|
|
|
|
handler Handler
|
|
|
|
|
2020-02-23 19:24:50 +00:00
|
|
|
server string
|
2020-02-17 22:17:13 +00:00
|
|
|
username string
|
2020-02-23 19:24:50 +00:00
|
|
|
teams map[string]bool
|
2020-02-17 22:17:13 +00:00
|
|
|
|
2020-02-26 14:44:08 +00:00
|
|
|
initial_members int // How many room members (maximum) to load when first joining a channel
|
|
|
|
initial_backlog int // How many previous messages (maximum) to load when first joining a channel
|
|
|
|
|
2020-02-23 19:24:50 +00:00
|
|
|
conn *matterclient.MMClient
|
2020-03-04 17:16:26 +00:00
|
|
|
handlerStopChan chan struct{}
|
2020-02-18 16:17:04 +00:00
|
|
|
|
2020-02-26 14:26:57 +00:00
|
|
|
caches mmCaches
|
|
|
|
}
|
|
|
|
|
|
|
|
type mmCaches struct {
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
mmusers map[string]string // map mm username to mm user id
|
|
|
|
sentjoined map[string]bool // map username/room name to bool
|
|
|
|
displayname map[UserID]string // map username to last displayname
|
2020-03-01 19:37:34 +00:00
|
|
|
initsynced map[string]bool // chans for which init sync has been done
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) SetHandler(h Handler) {
|
|
|
|
mm.handler = h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) Protocol() string {
|
2020-02-28 09:34:22 +00:00
|
|
|
return MATTERMOST_PROTOCOL
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) Configure(c Configuration) error {
|
|
|
|
if mm.conn != nil {
|
|
|
|
mm.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-01 19:37:34 +00:00
|
|
|
// Reinitialize shared data structures
|
2020-03-04 17:16:26 +00:00
|
|
|
mm.handlerStopChan = make(chan struct{})
|
2020-03-01 19:37:34 +00:00
|
|
|
|
|
|
|
mm.caches.mmusers = make(map[string]string)
|
|
|
|
mm.caches.sentjoined = make(map[string]bool)
|
|
|
|
mm.caches.displayname = make(map[UserID]string)
|
|
|
|
mm.caches.initsynced = make(map[string]bool)
|
|
|
|
|
|
|
|
// Read config
|
2020-02-17 22:17:13 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
mm.server, err = c.GetString("server")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mm.username, err = c.GetString("username")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-26 21:49:27 +00:00
|
|
|
mm.initial_members, err = c.GetInt("initial_members", 100)
|
2020-02-26 14:44:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mm.initial_backlog, err = c.GetInt("initial_backlog", 1000)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-21 22:00:52 +00:00
|
|
|
teams, err := c.GetString("teams")
|
2020-02-17 22:17:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-21 22:00:52 +00:00
|
|
|
mm.teams = map[string]bool{}
|
|
|
|
anyteam := ""
|
|
|
|
for _, team := range strings.Split(teams, ",") {
|
|
|
|
anyteam = strings.TrimSpace(team)
|
|
|
|
mm.teams[anyteam] = true
|
|
|
|
}
|
2020-02-17 22:17:13 +00:00
|
|
|
|
2020-02-18 16:17:04 +00:00
|
|
|
notls, err := c.GetBool("no_tls", false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:17:13 +00:00
|
|
|
password, _ := c.GetString("password", "")
|
|
|
|
token, _ := c.GetString("token", "")
|
|
|
|
if token != "" {
|
|
|
|
password = "token=" + token
|
|
|
|
}
|
2020-03-01 19:37:34 +00:00
|
|
|
|
|
|
|
// Try to log in
|
2020-02-21 22:00:52 +00:00
|
|
|
mm.conn = matterclient.New(mm.username, password, anyteam, mm.server)
|
2020-02-18 16:17:04 +00:00
|
|
|
mm.conn.Credentials.NoTLS = notls
|
2020-02-17 22:17:13 +00:00
|
|
|
err = mm.conn.Login()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-01 19:37:34 +00:00
|
|
|
|
|
|
|
// Try to start listening for messages
|
|
|
|
// Everytime the listener reconnects, mm.handleConnected does a sync of room status
|
|
|
|
mm.conn.OnWsConnect = mm.handleConnected
|
2020-02-17 22:17:13 +00:00
|
|
|
go mm.conn.WsReceiver()
|
|
|
|
go mm.conn.StatusLoop()
|
2020-03-01 19:37:34 +00:00
|
|
|
go mm.handleLoop(mm.conn.MessageChan, mm.handlerStopChan)
|
2020-02-17 22:17:13 +00:00
|
|
|
|
2020-03-01 19:37:34 +00:00
|
|
|
return nil
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) User() UserID {
|
|
|
|
return UserID(mm.username + "@" + mm.server)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) getTeamIdByName(name string) string {
|
|
|
|
for _, team := range mm.conn.OtherTeams {
|
|
|
|
if team.Team.Name == name {
|
|
|
|
return team.Id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) checkRoomId(id RoomID) (string, error) {
|
|
|
|
x := strings.Split(string(id), "@")
|
|
|
|
if len(x) == 1 {
|
2020-02-21 22:00:52 +00:00
|
|
|
return "", fmt.Errorf("Please write whole room ID with team and server: %s@<team>@%s", id, mm.server)
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
|
|
|
if len(x) == 2 {
|
|
|
|
return x[0], nil
|
|
|
|
}
|
|
|
|
if len(x) != 3 || x[2] != mm.server {
|
|
|
|
return "", fmt.Errorf("Invalid room ID: %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
team_id := mm.getTeamIdByName(x[1])
|
|
|
|
if team_id == "" {
|
|
|
|
return "", fmt.Errorf("Team not found: %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch_id := mm.conn.GetChannelId(x[0], team_id)
|
|
|
|
if ch_id == "" {
|
|
|
|
return "", fmt.Errorf("Channel not found: %s", id)
|
|
|
|
}
|
|
|
|
return ch_id, nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 22:00:52 +00:00
|
|
|
func (mm *Mattermost) reverseRoomId(id string) (bool, RoomID) {
|
2020-02-17 22:17:13 +00:00
|
|
|
team := mm.conn.GetChannelTeamId(id)
|
|
|
|
if team == "" {
|
2020-02-21 22:00:52 +00:00
|
|
|
return true, RoomID(fmt.Sprintf("%s@%s", id, mm.server))
|
2020-02-17 22:17:13 +00:00
|
|
|
} else {
|
|
|
|
teamName := mm.conn.GetTeamName(team)
|
2020-02-21 22:00:52 +00:00
|
|
|
if u, ok := mm.teams[teamName]; ok && u {
|
|
|
|
name := mm.conn.GetChannelName(id)
|
|
|
|
return true, RoomID(fmt.Sprintf("%s@%s@%s", name, teamName, mm.server))
|
|
|
|
} else {
|
|
|
|
return false, ""
|
|
|
|
}
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) checkUserId(id UserID) (string, error) {
|
|
|
|
x := strings.Split(string(id), "@")
|
|
|
|
if len(x) == 1 {
|
|
|
|
return "", fmt.Errorf("Please write whole user ID with server: %s@%s", id, mm.server)
|
|
|
|
}
|
|
|
|
if len(x) != 2 || x[1] != mm.server {
|
|
|
|
return "", fmt.Errorf("Invalid user ID: %s", id)
|
|
|
|
}
|
2020-02-26 14:26:57 +00:00
|
|
|
|
|
|
|
mm.caches.Lock()
|
|
|
|
defer mm.caches.Unlock()
|
|
|
|
|
|
|
|
if user_id, ok := mm.caches.mmusers[x[0]]; ok {
|
2020-02-18 16:17:04 +00:00
|
|
|
return user_id, nil
|
|
|
|
}
|
2020-02-26 14:26:57 +00:00
|
|
|
|
2020-02-18 16:17:04 +00:00
|
|
|
u, resp := mm.conn.Client.GetUserByUsername(x[0], "")
|
|
|
|
if u == nil || resp.Error != nil {
|
|
|
|
return "", fmt.Errorf("Not found: %s (%s)", x[0], resp.Error)
|
|
|
|
}
|
2020-02-26 14:26:57 +00:00
|
|
|
mm.caches.mmusers[x[0]] = u.Id
|
|
|
|
|
2020-02-18 16:17:04 +00:00
|
|
|
return u.Id, nil
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) SetUserInfo(info *UserInfo) error {
|
|
|
|
return fmt.Errorf("Not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) SetRoomInfo(roomId RoomID, info *RoomInfo) error {
|
|
|
|
ch, err := mm.checkRoomId(roomId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Topic != "" {
|
|
|
|
mm.conn.UpdateChannelHeader(ch, info.Topic)
|
|
|
|
}
|
|
|
|
|
2020-02-29 19:47:44 +00:00
|
|
|
if info.Picture.MediaObject != nil {
|
2020-02-17 22:17:13 +00:00
|
|
|
err = fmt.Errorf("Not supported: channel picture on mattermost")
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Name != "" {
|
|
|
|
err = fmt.Errorf("Not supported: channel name on mattermost")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) Join(roomId RoomID) error {
|
|
|
|
ch, err := mm.checkRoomId(roomId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mm.conn.JoinChannel(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) Invite(userId UserID, roomId RoomID) error {
|
2020-02-18 16:17:04 +00:00
|
|
|
if roomId == "" {
|
|
|
|
_, err := mm.checkUserId(userId)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:17:13 +00:00
|
|
|
return fmt.Errorf("Not supported: invite on mattermost")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) Leave(roomId RoomID) {
|
|
|
|
// Not supported? TODO
|
|
|
|
}
|
|
|
|
|
2020-02-29 09:01:42 +00:00
|
|
|
func (mm *Mattermost) Send(event *Event) (string, error) {
|
2020-02-18 16:17:04 +00:00
|
|
|
post := &model.Post{
|
|
|
|
Message: event.Text,
|
|
|
|
}
|
|
|
|
if event.Type == EVENT_ACTION {
|
|
|
|
post.Type = "me"
|
|
|
|
}
|
2020-02-17 22:17:13 +00:00
|
|
|
|
|
|
|
if event.Room != "" {
|
|
|
|
ch, err := mm.checkRoomId(event.Room)
|
|
|
|
if err != nil {
|
2020-02-29 09:01:42 +00:00
|
|
|
return "", err
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
2020-02-18 16:17:04 +00:00
|
|
|
post.ChannelId = ch
|
2020-02-17 22:17:13 +00:00
|
|
|
} else if event.Recipient != "" {
|
|
|
|
ui, err := mm.checkUserId(event.Recipient)
|
|
|
|
if err != nil {
|
2020-02-29 09:01:42 +00:00
|
|
|
return "", err
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
2020-02-18 16:17:04 +00:00
|
|
|
|
|
|
|
_, resp := mm.conn.Client.CreateDirectChannel(mm.conn.User.Id, ui)
|
|
|
|
if resp.Error != nil {
|
2020-02-29 09:01:42 +00:00
|
|
|
return "", resp.Error
|
2020-02-18 16:17:04 +00:00
|
|
|
}
|
|
|
|
channelName := model.GetDMNameFromIds(ui, mm.conn.User.Id)
|
|
|
|
|
|
|
|
err = mm.conn.UpdateChannels()
|
|
|
|
if err != nil {
|
2020-02-29 09:01:42 +00:00
|
|
|
return "", err
|
2020-02-18 16:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
post.ChannelId = mm.conn.GetChannelId(channelName, "")
|
2020-02-17 22:17:13 +00:00
|
|
|
} else {
|
2020-02-29 09:01:42 +00:00
|
|
|
return "", fmt.Errorf("Invalid target")
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
2020-02-18 16:17:04 +00:00
|
|
|
|
2020-02-21 17:08:40 +00:00
|
|
|
if event.Attachments != nil {
|
|
|
|
post.FileIds = []string{}
|
|
|
|
for _, file := range event.Attachments {
|
|
|
|
rdr, err := file.Read()
|
|
|
|
if err != nil {
|
2020-02-29 09:01:42 +00:00
|
|
|
return "", err
|
2020-02-21 17:08:40 +00:00
|
|
|
}
|
|
|
|
defer rdr.Close()
|
|
|
|
data, err := ioutil.ReadAll(rdr)
|
|
|
|
if err != nil {
|
2020-02-29 09:01:42 +00:00
|
|
|
return "", err
|
2020-02-21 17:08:40 +00:00
|
|
|
}
|
|
|
|
up_file, err := mm.conn.UploadFile(data, post.ChannelId, file.Filename())
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("UploadFile error: %s", err)
|
2020-02-29 09:01:42 +00:00
|
|
|
return "", err
|
2020-02-21 17:08:40 +00:00
|
|
|
}
|
|
|
|
post.FileIds = append(post.FileIds, up_file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 09:01:42 +00:00
|
|
|
created_post, resp := mm.conn.Client.CreatePost(post)
|
2020-02-18 16:25:23 +00:00
|
|
|
if resp.Error != nil {
|
2020-02-21 17:08:40 +00:00
|
|
|
log.Warnf("CreatePost error: %s", resp.Error)
|
2020-02-29 09:01:42 +00:00
|
|
|
return "", resp.Error
|
2020-02-18 16:25:23 +00:00
|
|
|
}
|
2020-02-29 09:01:42 +00:00
|
|
|
return created_post.Id, nil
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) Close() {
|
2020-02-26 21:49:27 +00:00
|
|
|
if mm.conn != nil {
|
|
|
|
mm.conn.WsQuit = true
|
|
|
|
}
|
2020-02-17 22:17:13 +00:00
|
|
|
if mm.handlerStopChan != nil {
|
2020-03-04 17:16:26 +00:00
|
|
|
close(mm.handlerStopChan)
|
2020-02-17 22:17:13 +00:00
|
|
|
mm.handlerStopChan = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) handleConnected() {
|
2020-03-01 19:37:34 +00:00
|
|
|
log.Debugf("(Re-)connected to mattermost: %s@%s ; doing channel sync\n", mm.username, mm.server)
|
2020-02-26 14:26:57 +00:00
|
|
|
|
|
|
|
// Initial channel sync
|
2020-02-17 22:17:13 +00:00
|
|
|
chans := mm.conn.GetChannels()
|
2020-02-26 14:26:57 +00:00
|
|
|
doneCh := make(map[string]bool)
|
2020-02-17 22:17:13 +00:00
|
|
|
for _, ch := range chans {
|
2020-02-26 14:26:57 +00:00
|
|
|
if _, ok := doneCh[ch.Id]; !ok {
|
|
|
|
doneCh[ch.Id] = true
|
2020-03-01 19:37:34 +00:00
|
|
|
go mm.syncChannel(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) syncChannel(ch *model.Channel) {
|
|
|
|
// The first time we see a chan, sync everything (member list, names, profile pictures)
|
|
|
|
must_initsync := func() bool {
|
|
|
|
mm.caches.Lock()
|
|
|
|
defer mm.caches.Unlock()
|
|
|
|
|
|
|
|
if x, ok := mm.caches.initsynced[ch.Id]; ok && x {
|
|
|
|
return false
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
2020-03-01 19:37:34 +00:00
|
|
|
mm.caches.initsynced[ch.Id] = true
|
|
|
|
return true
|
|
|
|
}()
|
|
|
|
|
|
|
|
if must_initsync {
|
|
|
|
mm.initSyncChannel(ch)
|
2020-02-26 14:26:57 +00:00
|
|
|
}
|
2020-03-01 19:37:34 +00:00
|
|
|
|
|
|
|
// The following times, only sync missing messages
|
|
|
|
mm.backlogChannel(ch)
|
2020-02-26 14:26:57 +00:00
|
|
|
}
|
2020-02-21 17:08:40 +00:00
|
|
|
|
2020-02-26 14:26:57 +00:00
|
|
|
func (mm *Mattermost) initSyncChannel(ch *model.Channel) {
|
|
|
|
if len(strings.Split(ch.Name, "__")) == 2 {
|
|
|
|
// DM channel
|
|
|
|
// Update remote user info
|
|
|
|
users := strings.Split(ch.Name, "__")
|
|
|
|
for _, uid := range users {
|
2020-03-01 19:37:34 +00:00
|
|
|
if uid != mm.conn.User.Id {
|
|
|
|
user := mm.conn.GetUser(uid)
|
|
|
|
if user != nil {
|
|
|
|
mm.updateUserInfo(user)
|
|
|
|
}
|
2020-02-26 14:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-21 22:00:52 +00:00
|
|
|
interested, id := mm.reverseRoomId(ch.Id)
|
|
|
|
if !interested {
|
|
|
|
// Skip channels that are not in teams we want to bridge
|
2020-02-26 14:26:57 +00:00
|
|
|
return
|
2020-02-21 22:00:52 +00:00
|
|
|
}
|
2020-02-17 22:17:13 +00:00
|
|
|
mm.handler.Joined(id)
|
2020-02-21 17:08:40 +00:00
|
|
|
|
|
|
|
// Update room info
|
|
|
|
room_info := &RoomInfo{
|
2020-02-23 19:24:50 +00:00
|
|
|
Name: ch.DisplayName,
|
2020-02-17 22:17:13 +00:00
|
|
|
Topic: ch.Header,
|
2020-02-21 17:08:40 +00:00
|
|
|
}
|
|
|
|
for _, t := range mm.conn.OtherTeams {
|
|
|
|
if t.Id == ch.TeamId {
|
|
|
|
if t.Team.DisplayName != "" {
|
|
|
|
room_info.Name = t.Team.DisplayName + " / " + room_info.Name
|
|
|
|
} else {
|
|
|
|
room_info.Name = t.Team.Name + " / " + room_info.Name
|
|
|
|
}
|
|
|
|
if t.Team.LastTeamIconUpdate > 0 {
|
2020-02-29 17:30:43 +00:00
|
|
|
room_info.Picture = SMediaObject{&LazyBlobMediaObject{
|
2020-02-21 17:43:47 +00:00
|
|
|
ObjectFilename: fmt.Sprintf("%s-%d",
|
|
|
|
t.Team.Name,
|
|
|
|
t.Team.LastTeamIconUpdate),
|
|
|
|
GetFn: func(o *LazyBlobMediaObject) error {
|
|
|
|
team_img, resp := mm.conn.Client.GetTeamIcon(t.Id, "")
|
2020-02-21 18:50:55 +00:00
|
|
|
if resp.Error != nil {
|
|
|
|
log.Warnf("Could not get team image: %s", resp.Error.Error())
|
2020-02-21 17:43:47 +00:00
|
|
|
return resp.Error
|
|
|
|
}
|
|
|
|
o.ObjectData = team_img
|
|
|
|
o.ObjectMimetype = http.DetectContentType(team_img)
|
|
|
|
return nil
|
|
|
|
},
|
2020-02-29 17:30:43 +00:00
|
|
|
}}
|
2020-02-21 17:08:40 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mm.handler.RoomInfoUpdated(id, UserID(""), room_info)
|
|
|
|
|
|
|
|
// Update member list
|
2020-02-21 18:36:16 +00:00
|
|
|
// TODO (when this will be slow, i.e. hundreds of members): do only a diff
|
2020-02-26 14:44:08 +00:00
|
|
|
members, resp := mm.conn.Client.GetChannelMembers(ch.Id, 0, mm.initial_members, "")
|
2020-02-21 17:08:40 +00:00
|
|
|
if resp.Error == nil {
|
|
|
|
for _, mem := range *members {
|
|
|
|
if mem.UserId == mm.conn.User.Id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
user := mm.conn.GetUser(mem.UserId)
|
|
|
|
if user != nil {
|
|
|
|
mm.ensureJoined(user, id)
|
|
|
|
mm.updateUserInfo(user)
|
|
|
|
} else {
|
|
|
|
log.Warnf("Could not find joined user: %s", mem.UserId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-21 18:50:55 +00:00
|
|
|
log.Warnf("Could not get channel members: %s", resp.Error.Error())
|
2020-02-21 17:08:40 +00:00
|
|
|
}
|
2020-02-26 14:26:57 +00:00
|
|
|
}
|
2020-03-01 19:37:34 +00:00
|
|
|
}
|
2020-02-21 18:28:00 +00:00
|
|
|
|
2020-03-01 19:37:34 +00:00
|
|
|
func (mm *Mattermost) backlogChannel(ch *model.Channel) {
|
2020-02-26 14:26:57 +00:00
|
|
|
// Read backlog
|
|
|
|
last_seen_post := mm.handler.CacheGet(fmt.Sprintf("last_seen_%s", ch.Id))
|
|
|
|
if last_seen_post != "" {
|
2020-02-26 14:44:08 +00:00
|
|
|
const NUM_PER_PAGE = 100
|
|
|
|
page := 0
|
|
|
|
backlogs := []*model.PostList{}
|
|
|
|
for {
|
|
|
|
backlog, resp := mm.conn.Client.GetPostsAfter(ch.Id, last_seen_post, page, NUM_PER_PAGE, "")
|
|
|
|
if resp.Error == nil {
|
|
|
|
backlogs = append(backlogs, backlog)
|
|
|
|
if len(backlog.Order) == NUM_PER_PAGE {
|
|
|
|
page += 1
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < len(backlogs); i++ {
|
|
|
|
mm.processBacklog(ch, backlogs[i])
|
2020-02-21 18:28:00 +00:00
|
|
|
}
|
2020-02-26 14:26:57 +00:00
|
|
|
} else {
|
2020-02-26 14:44:08 +00:00
|
|
|
backlog, resp := mm.conn.Client.GetPostsForChannel(ch.Id, 0, mm.initial_backlog, "")
|
|
|
|
if resp.Error == nil {
|
|
|
|
mm.processBacklog(ch, backlog)
|
|
|
|
} else {
|
|
|
|
log.Warnf("Could not get channel backlog: %s", resp.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) processBacklog(ch *model.Channel, backlog *model.PostList) {
|
|
|
|
for i := 0; i < len(backlog.Order); i++ {
|
|
|
|
post_id := backlog.Order[len(backlog.Order)-i-1]
|
|
|
|
post := backlog.Posts[post_id]
|
|
|
|
post_time := time.Unix(post.CreateAt/1000, 0)
|
|
|
|
post.Message = fmt.Sprintf("[%s] %s",
|
2020-03-01 14:28:11 +00:00
|
|
|
post_time.Format("2006-01-02 15:04 MST"), post.Message)
|
2020-02-26 14:44:08 +00:00
|
|
|
mm.handlePost(ch.Name, post, true)
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:16:26 +00:00
|
|
|
func (mm *Mattermost) handleLoop(msgCh chan *matterclient.Message, quitCh chan struct{}) {
|
2020-02-17 22:17:13 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-quitCh:
|
|
|
|
break
|
|
|
|
case msg := <-msgCh:
|
2020-02-27 09:35:09 +00:00
|
|
|
log.Tracef("Mattermost: %#v\n", msg)
|
|
|
|
log.Tracef("Mattermost raw: %#v\n", msg.Raw)
|
2020-02-18 16:17:04 +00:00
|
|
|
err := mm.handlePosted(msg.Raw)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Mattermost error: %s", err)
|
2020-02-17 22:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 16:17:04 +00:00
|
|
|
|
2020-02-21 17:08:40 +00:00
|
|
|
func (mm *Mattermost) updateUserInfo(user *model.User) {
|
|
|
|
userId := UserID(fmt.Sprintf("%s@%s", user.Username, mm.server))
|
|
|
|
userDisp := user.GetDisplayName(model.SHOW_NICKNAME_FULLNAME)
|
|
|
|
|
2020-02-26 14:26:57 +00:00
|
|
|
mm.caches.Lock()
|
|
|
|
defer mm.caches.Unlock()
|
|
|
|
|
|
|
|
if lastdn, ok := mm.caches.displayname[userId]; !ok || lastdn != userDisp {
|
2020-02-21 17:08:40 +00:00
|
|
|
ui := &UserInfo{
|
|
|
|
DisplayName: userDisp,
|
|
|
|
}
|
|
|
|
if user.LastPictureUpdate > 0 {
|
2020-02-29 17:30:43 +00:00
|
|
|
ui.Avatar = SMediaObject{&LazyBlobMediaObject{
|
2020-02-21 17:43:47 +00:00
|
|
|
ObjectFilename: fmt.Sprintf("%s-%d",
|
|
|
|
user.Username,
|
|
|
|
user.LastPictureUpdate),
|
|
|
|
GetFn: func(o *LazyBlobMediaObject) error {
|
|
|
|
img, resp := mm.conn.Client.GetProfileImage(user.Id, "")
|
2020-02-21 18:50:55 +00:00
|
|
|
if resp.Error != nil {
|
|
|
|
log.Warnf("Could not get profile picture: %s", resp.Error.Error())
|
2020-02-21 17:43:47 +00:00
|
|
|
return resp.Error
|
|
|
|
}
|
|
|
|
o.ObjectData = img
|
|
|
|
o.ObjectMimetype = http.DetectContentType(img)
|
|
|
|
return nil
|
|
|
|
},
|
2020-02-29 17:30:43 +00:00
|
|
|
}}
|
2020-02-21 17:08:40 +00:00
|
|
|
}
|
2020-02-21 17:43:47 +00:00
|
|
|
mm.handler.UserInfoUpdated(userId, ui)
|
2020-02-26 14:26:57 +00:00
|
|
|
mm.caches.displayname[userId] = userDisp
|
2020-02-21 17:08:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) ensureJoined(user *model.User, roomId RoomID) {
|
|
|
|
userId := UserID(fmt.Sprintf("%s@%s", user.Username, mm.server))
|
|
|
|
cache_key := fmt.Sprintf("%s / %s", userId, roomId)
|
2020-02-26 14:26:57 +00:00
|
|
|
|
|
|
|
mm.caches.Lock()
|
|
|
|
defer mm.caches.Unlock()
|
|
|
|
|
|
|
|
if _, ok := mm.caches.sentjoined[cache_key]; !ok {
|
2020-02-21 17:08:40 +00:00
|
|
|
mm.handler.Event(&Event{
|
|
|
|
Author: userId,
|
2020-02-23 19:24:50 +00:00
|
|
|
Room: roomId,
|
|
|
|
Type: EVENT_JOIN,
|
2020-02-21 17:08:40 +00:00
|
|
|
})
|
2020-02-26 14:26:57 +00:00
|
|
|
mm.caches.sentjoined[cache_key] = true
|
2020-02-21 17:08:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 16:17:04 +00:00
|
|
|
func (mm *Mattermost) handlePosted(msg *model.WebSocketEvent) error {
|
2020-02-21 22:00:52 +00:00
|
|
|
channel_name, ok := msg.Data["channel_name"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-18 16:17:04 +00:00
|
|
|
post_str := msg.Data["post"].(string)
|
|
|
|
var post model.Post
|
|
|
|
err := json.Unmarshal([]byte(post_str), &post)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-21 18:28:00 +00:00
|
|
|
return mm.handlePost(channel_name, &post, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *Mattermost) handlePost(channel_name string, post *model.Post, only_messages bool) error {
|
2020-02-18 16:17:04 +00:00
|
|
|
// Skip self messages
|
|
|
|
if post.UserId == mm.conn.User.Id {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find sending user
|
|
|
|
user := mm.conn.GetUser(post.UserId)
|
|
|
|
if user == nil {
|
|
|
|
return fmt.Errorf("Invalid user")
|
|
|
|
}
|
|
|
|
userId := UserID(fmt.Sprintf("%s@%s", user.Username, mm.server))
|
2020-02-21 17:08:40 +00:00
|
|
|
mm.updateUserInfo(user)
|
2020-02-18 17:07:35 +00:00
|
|
|
|
2020-02-18 16:17:04 +00:00
|
|
|
// Build message event
|
|
|
|
msg_ev := &Event{
|
2020-02-23 19:24:50 +00:00
|
|
|
Id: post.Id,
|
2020-02-18 16:17:04 +00:00
|
|
|
Author: userId,
|
2020-02-23 19:24:50 +00:00
|
|
|
Text: post.Message,
|
|
|
|
Type: EVENT_MESSAGE,
|
2020-02-18 16:17:04 +00:00
|
|
|
}
|
|
|
|
if post.Type == "me" {
|
|
|
|
msg_ev.Type = EVENT_ACTION
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:57:53 +00:00
|
|
|
// Handle files
|
|
|
|
if post.FileIds != nil && len(post.FileIds) > 0 {
|
2020-02-29 17:30:43 +00:00
|
|
|
msg_ev.Attachments = []SMediaObject{}
|
2020-02-21 14:57:53 +00:00
|
|
|
for _, file := range post.Metadata.Files {
|
2020-02-21 22:00:52 +00:00
|
|
|
media_object := &LazyBlobMediaObject{
|
2020-02-21 14:57:53 +00:00
|
|
|
ObjectFilename: file.Name,
|
|
|
|
ObjectMimetype: file.MimeType,
|
2020-02-21 22:00:52 +00:00
|
|
|
GetFn: func(o *LazyBlobMediaObject) error {
|
|
|
|
blob, resp := mm.conn.Client.GetFile(file.Id)
|
|
|
|
if resp.Error != nil {
|
|
|
|
return resp.Error
|
|
|
|
}
|
|
|
|
o.ObjectData = blob
|
|
|
|
return nil
|
|
|
|
},
|
2020-02-21 14:57:53 +00:00
|
|
|
}
|
|
|
|
if file.Width > 0 {
|
|
|
|
media_object.ObjectImageSize = &ImageSize{
|
2020-02-23 19:24:50 +00:00
|
|
|
Width: file.Width,
|
2020-02-21 14:57:53 +00:00
|
|
|
Height: file.Height,
|
|
|
|
}
|
|
|
|
}
|
2020-02-29 17:30:43 +00:00
|
|
|
msg_ev.Attachments = append(msg_ev.Attachments, SMediaObject{media_object})
|
2020-02-21 14:57:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 16:17:04 +00:00
|
|
|
// Dispatch as PM or as room message
|
|
|
|
if len(strings.Split(channel_name, "__")) == 2 {
|
|
|
|
// Private message, no need to find room id
|
|
|
|
if user.Id == mm.conn.User.Id {
|
|
|
|
// Skip self sent messages
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mm.handler.Event(msg_ev)
|
2020-02-26 14:44:08 +00:00
|
|
|
mm.handler.CachePut(fmt.Sprintf("last_seen_%s", post.ChannelId), post.Id)
|
2020-02-18 16:17:04 +00:00
|
|
|
} else {
|
2020-02-21 22:00:52 +00:00
|
|
|
interested, roomId := mm.reverseRoomId(post.ChannelId)
|
|
|
|
if !interested {
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-18 16:17:04 +00:00
|
|
|
if roomId == "" {
|
|
|
|
return fmt.Errorf("Invalid channel id")
|
|
|
|
}
|
|
|
|
|
2020-02-21 17:08:40 +00:00
|
|
|
mm.ensureJoined(user, roomId)
|
2020-02-18 16:17:04 +00:00
|
|
|
|
|
|
|
if post.Type == "system_header_change" {
|
2020-02-21 18:28:00 +00:00
|
|
|
if !only_messages {
|
|
|
|
new_header := post.Props["new_header"].(string)
|
|
|
|
mm.handler.RoomInfoUpdated(roomId, userId, &RoomInfo{
|
|
|
|
Topic: new_header,
|
|
|
|
})
|
|
|
|
}
|
2020-02-18 16:17:04 +00:00
|
|
|
} else if post.Type == "" || post.Type == "me" {
|
|
|
|
msg_ev.Room = roomId
|
|
|
|
mm.handler.Event(msg_ev)
|
2020-02-21 18:36:16 +00:00
|
|
|
mm.handler.CachePut(fmt.Sprintf("last_seen_%s", post.ChannelId), post.Id)
|
2020-02-18 16:17:04 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Unhandled post type: %s", post.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|