Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for DM header updates, as topics #581

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 16 additions & 4 deletions mm-go-irckit/userbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,23 @@ func (u *User) handleDirectMessageEvent(event *bridge.DirectMessageEvent) {
}
}

prefixUser := event.Sender.User
if event.Sender.Me {
prefixUser = event.Receiver.User
var text string
var showContext bool
var maxlen int

prefix := ""
suffix := ""
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
Loading