Skip to content

Commit

Permalink
Add support for DM header updates, as topics
Browse files Browse the repository at this point in the history
  • Loading branch information
hloeung committed Sep 9, 2024
1 parent 6487375 commit 38172e0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
70 changes: 56 additions & 14 deletions bridge/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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) {
Expand Down
19 changes: 15 additions & 4 deletions mm-go-irckit/userbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,22 @@ func (u *User) handleDirectMessageEvent(event *bridge.DirectMessageEvent) {
}
}

prefixUser := event.Sender.User
if event.Sender.Me {
prefixUser = event.Receiver.User
text := ""

Check failure on line 148 in mm-go-irckit/userbridge.go

View workflow job for this annotation

GitHub Actions / golangci-lint

assigned to text, but reassigned without using the value (wastedassign)
prefix := ""
suffix := ""
showContext := false

Check failure on line 151 in mm-go-irckit/userbridge.go

View workflow job for this annotation

GitHub Actions / golangci-lint

assigned to showContext, but reassigned without using the value (wastedassign)
maxlen := 440

Check failure on line 152 in mm-go-irckit/userbridge.go

View workflow job for this annotation

GitHub Actions / golangci-lint

ineffectual assignment to maxlen (ineffassign)
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
Expand Down

0 comments on commit 38172e0

Please sign in to comment.