From 38172e0aaea2dc6c4f0055cef027a229c34d1466 Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Mon, 9 Sep 2024 18:18:41 +1000 Subject: [PATCH] Add support for DM header updates, as topics --- bridge/mattermost/mattermost.go | 70 ++++++++++++++++++++++++++------- mm-go-irckit/userbridge.go | 19 +++++++-- 2 files changed, 71 insertions(+), 18 deletions(-) diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index a0496644..eb671c82 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -176,6 +176,8 @@ func (m *Mattermost) handleWsMessage(quitChan chan struct{}) { case model.WebsocketEventChannelRestored: // check if we have the users/channels in our cache. If not update m.checkWsActionMessage(message.Raw, updateChannelsThrottle) + case model.WebsocketEventChannelUpdated: + m.handleWsActionPost(message.Raw) case model.WebsocketEventUserUpdated: m.handleWsActionUserUpdated(message.Raw) case model.WebsocketEventStatusChange: @@ -844,6 +846,10 @@ var validIRCNickRegExp = regexp.MustCompile("^[a-zA-Z0-9_]*$") //nolint:funlen,gocognit,gocyclo,cyclop,forcetypeassert func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) { + if rmsg.GetData()["post"] == nil { + return + } + var data model.Post if err := json.NewDecoder(strings.NewReader(rmsg.GetData()["post"].(string))).Decode(&data); err != nil { return @@ -908,19 +914,62 @@ func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) { return } + channelType := "" + if t, ok := props["channel_type"].(string); ok { + channelType = t + } + + dmchannel, _ := rmsg.GetData()["channel_name"].(string) + if data.Type == model.PostTypeHeaderChange { - if topic, ok := extraProps["new_header"].(string); ok { + if _, ok := extraProps["new_header"].(string); !ok { + return + } + topic := extraProps["new_header"].(string) + + // DirectMessage + if channelType == "D" { event := &bridge.Event{ - Type: "channel_topic", - Data: &bridge.ChannelTopicEvent{ - Text: topic, - ChannelID: data.ChannelId, - UserID: data.UserId, - }, + Type: "direct_message", } + + d := &bridge.DirectMessageEvent{ + Text: "\x01ACTION updated topic to: " + topic + " \x01", + ChannelID: data.ChannelId, + MessageID: data.Id, + Event: "dm_topic", + } + + userUpdated := extraProps["username"].(string) + if userUpdated == m.GetMe().Nick { + d.Sender = ghost + d.Receiver = m.getDMUser(dmchannel) + } else { + d.Sender = m.getDMUser(dmchannel) + d.Receiver = ghost + } + + if d.Sender == nil || d.Receiver == nil { + logger.Errorf("dm: couldn't resolve sender or receiver: %#v", rmsg) + return + } + + event.Data = d + m.eventChan <- event + return } + event := &bridge.Event{ + Type: "channel_topic", + Data: &bridge.ChannelTopicEvent{ + Text: topic, + ChannelID: data.ChannelId, + UserID: data.UserId, + }, + } + + m.eventChan <- event return } @@ -933,13 +982,6 @@ func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) { // msgs := strings.Split(data.Message, "\n") msgs := []string{data.Message} - channelType := "" - if t, ok := props["channel_type"].(string); ok { - channelType = t - } - - dmchannel, _ := rmsg.GetData()["channel_name"].(string) - // add an edited/deleted string when messages are edited/deleted if len(msgs) > 0 && (rmsg.EventType() == model.WebsocketEventPostEdited || rmsg.EventType() == model.WebsocketEventPostDeleted) { diff --git a/mm-go-irckit/userbridge.go b/mm-go-irckit/userbridge.go index d7c4c97d..018f659a 100644 --- a/mm-go-irckit/userbridge.go +++ b/mm-go-irckit/userbridge.go @@ -145,11 +145,22 @@ func (u *User) handleDirectMessageEvent(event *bridge.DirectMessageEvent) { } } - prefixUser := event.Sender.User - if event.Sender.Me { - prefixUser = event.Receiver.User + text := "" + prefix := "" + suffix := "" + showContext := false + maxlen := 440 + if event.Event == "dm_topic" { + text = event.Text + showContext = false + maxlen = 0 + } else { + prefixUser := event.Sender.User + if event.Sender.Me { + prefixUser = event.Receiver.User + } + text, prefix, suffix, showContext, maxlen = u.handleMessageThreadContext(prefixUser, event.MessageID, event.ParentID, event.Event, event.Text) } - text, prefix, suffix, showContext, maxlen := u.handleMessageThreadContext(prefixUser, event.MessageID, event.ParentID, event.Event, event.Text) lexer := "" codeBlockBackTick := false