From 3e39c88796f5a4e1ae144674291a7b418a6428b7 Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Sun, 2 Dec 2018 02:28:01 +0800 Subject: [PATCH] Added support for mattermost threading. --- bridge/mattermost/handlers.go | 2 +- bridge/mattermost/mattermost.go | 4 ++-- bridge/slack/helpers.go | 2 ++ matterclient/messages.go | 16 ++++++++-------- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/bridge/mattermost/handlers.go b/bridge/mattermost/handlers.go index d75e8d40..5d3826ce 100644 --- a/bridge/mattermost/handlers.go +++ b/bridge/mattermost/handlers.go @@ -163,7 +163,7 @@ func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) { if b.GetBool("PrefixMessagesWithNick") { msg.Text = msg.Username + msg.Text } - res, err = b.mc.PostMessageWithFiles(channelID, msg.Text, []string{id}) + res, err = b.mc.PostMessageWithFiles(channelID, msg.Text, msg.ParentID, []string{id}) } return res, err } diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index b0dca0ba..ce4f4c33 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -124,7 +124,7 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { // Upload a file if it exists if msg.Extra != nil { for _, rmsg := range helper.HandleExtra(&msg, b.General) { - if _, err := b.mc.PostMessage(b.mc.GetChannelId(rmsg.Channel, b.TeamID), rmsg.Username+rmsg.Text); err != nil { + if _, err := b.mc.PostMessage(b.mc.GetChannelId(rmsg.Channel, b.TeamID), rmsg.Username+rmsg.Text, msg.ParentID); err != nil { b.Log.Errorf("PostMessage failed: %s", err) } } @@ -144,5 +144,5 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { } // Post normal message - return b.mc.PostMessage(b.mc.GetChannelId(msg.Channel, b.TeamID), msg.Text) + return b.mc.PostMessage(b.mc.GetChannelId(msg.Channel, b.TeamID), msg.Text, msg.ParentID) } diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go index b0af50f5..6553a4e6 100644 --- a/bridge/slack/helpers.go +++ b/bridge/slack/helpers.go @@ -64,6 +64,7 @@ func (b *Bslack) getChannelBy(lookupKey string, lookupMap map[string]*slack.Chan const minimumRefreshInterval = 10 * time.Second func (b *Bslack) populateUsers() { + time.Sleep(time.Second) b.refreshMutex.Lock() if time.Now().Before(b.earliestUserRefresh) || b.refreshInProgress { b.Log.Debugf("Not refreshing user list as it was done less than %v ago.", @@ -108,6 +109,7 @@ func (b *Bslack) populateUsers() { } func (b *Bslack) populateChannels() { + time.Sleep(time.Second) b.refreshMutex.Lock() if time.Now().Before(b.earliestChannelRefresh) || b.refreshInProgress { b.Log.Debugf("Not refreshing channel list as it was done less than %v seconds ago.", diff --git a/matterclient/messages.go b/matterclient/messages.go index 28e3ec28..68e1bc14 100644 --- a/matterclient/messages.go +++ b/matterclient/messages.go @@ -144,8 +144,8 @@ func (m *MMClient) GetPublicLinks(filenames []string) []string { return output } -func (m *MMClient) PostMessage(channelId string, text string) (string, error) { //nolint:golint - post := &model.Post{ChannelId: channelId, Message: text} +func (m *MMClient) PostMessage(channelId string, text string, rootId string) (string, error) { //nolint:golint + post := &model.Post{ChannelId: channelId, Message: text, RootId: rootId} res, resp := m.Client.CreatePost(post) if resp.Error != nil { return "", resp.Error @@ -153,8 +153,8 @@ func (m *MMClient) PostMessage(channelId string, text string) (string, error) { return res.Id, nil } -func (m *MMClient) PostMessageWithFiles(channelId string, text string, fileIds []string) (string, error) { //nolint:golint - post := &model.Post{ChannelId: channelId, Message: text, FileIds: fileIds} +func (m *MMClient) PostMessageWithFiles(channelId string, text string, rootId string, fileIds []string) (string, error) { //nolint:golint + post := &model.Post{ChannelId: channelId, Message: text, RootId: rootId, FileIds: fileIds} res, resp := m.Client.CreatePost(post) if resp.Error != nil { return "", resp.Error @@ -171,11 +171,11 @@ func (m *MMClient) SearchPosts(query string) *model.PostList { } // SendDirectMessage sends a direct message to specified user -func (m *MMClient) SendDirectMessage(toUserId string, msg string) { //nolint:golint - m.SendDirectMessageProps(toUserId, msg, nil) +func (m *MMClient) SendDirectMessage(toUserId string, msg string, rootId string) { //nolint:golint + m.SendDirectMessageProps(toUserId, msg, rootId, nil) } -func (m *MMClient) SendDirectMessageProps(toUserId string, msg string, props map[string]interface{}) { //nolint:golint +func (m *MMClient) SendDirectMessageProps(toUserId string, msg string, rootId string, props map[string]interface{}) { //nolint:golint m.log.Debugf("SendDirectMessage to %s, msg %s", toUserId, msg) // create DM channel (only happens on first message) _, resp := m.Client.CreateDirectChannel(m.User.Id, toUserId) @@ -190,7 +190,7 @@ func (m *MMClient) SendDirectMessageProps(toUserId string, msg string, props map // build & send the message msg = strings.Replace(msg, "\r", "", -1) - post := &model.Post{ChannelId: m.GetChannelId(channelName, m.Team.Id), Message: msg, Props: props} + post := &model.Post{ChannelId: m.GetChannelId(channelName, m.Team.Id), Message: msg, RootId: rootId, Props: props} m.Client.CreatePost(post) }