From f0c1571978f551c334224665f31896e799af099a Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Thu, 15 Sep 2022 12:49:44 +1000 Subject: [PATCH 1/3] Add new thread context to show both Mattermost thread/parent/root and post IDs --- matterircd.toml.example | 2 ++ mm-go-irckit/service.go | 16 ++++++++-------- mm-go-irckit/userbridge.go | 20 +++++++++++++++----- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/matterircd.toml.example b/matterircd.toml.example index 0336a7b0..ac574701 100644 --- a/matterircd.toml.example +++ b/matterircd.toml.example @@ -138,6 +138,8 @@ SuffixContext = false # If either PrefixContext or SuffixContext specify which thread ID to use. Default is the # matterircd generated @@([0-9][a-f]){3}. Uncomment to use Mattermost's message/parent thread IDs instead. #ThreadContext = "mattermost" +# Similar to the above, but also show the message post IDs in addition to the parent thread ID. +#ThreadContext = "mattermost+post" #This will show (mention yournick) after a message if it contains one of the words configured #in your mattermost "word that trigger mentions" notifications. diff --git a/mm-go-irckit/service.go b/mm-go-irckit/service.go index d0021eff..5cf017d5 100644 --- a/mm-go-irckit/service.go +++ b/mm-go-irckit/service.go @@ -394,14 +394,14 @@ func scrollback(u *User, toUser *User, args []string, service string) { } switch { // nolint:dupl - case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" && strings.HasPrefix(args[0], "#") && nick != "system": + case (u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post") && strings.HasPrefix(args[0], "#") && nick != "system": threadMsgID := u.prefixContext("", p.Id, p.ParentId, "") scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, post) spoof(nick, scrollbackMsg) case strings.HasPrefix(args[0], "#"): scrollbackMsg := "[" + ts.Format("2006-01-02 15:04") + "] " + post spoof(nick, scrollbackMsg) - case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost": + case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post": threadMsgID := u.prefixContext("", p.Id, p.ParentId, "") scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, post) u.MsgSpoofUser(scrollbackUser, nick, scrollbackMsg) @@ -418,11 +418,11 @@ func scrollback(u *User, toUser *User, args []string, service string) { for _, fname := range u.br.GetFileLinks(p.FileIds) { fileMsg := "download file - " + fname switch { // nolint:dupl - case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" && strings.HasPrefix(args[0], "#"): + case (u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post") && strings.HasPrefix(args[0], "#"): threadMsgID := u.prefixContext("", p.Id, p.ParentId, "") scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, fileMsg) spoof(nick, scrollbackMsg) - case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost": + case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post": threadMsgID := u.prefixContext("", p.Id, p.ParentId, "") scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, fileMsg) u.MsgSpoofUser(scrollbackUser, nick, scrollbackMsg) @@ -514,14 +514,14 @@ func scrollback6(u *User, toUser *User, args []string, service string) { } switch { // nolint:dupl - case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" && strings.HasPrefix(args[0], "#") && nick != "system": + case (u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post") && strings.HasPrefix(args[0], "#") && nick != "system": threadMsgID := u.prefixContext("", p.Id, p.RootId, "") scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, post) spoof(nick, scrollbackMsg) case strings.HasPrefix(args[0], "#"): scrollbackMsg := "[" + ts.Format("2006-01-02 15:04") + "] " + post spoof(nick, scrollbackMsg) - case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost": + case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post": threadMsgID := u.prefixContext("", p.Id, p.RootId, "") scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, post) u.MsgSpoofUser(scrollbackUser, nick, scrollbackMsg) @@ -538,11 +538,11 @@ func scrollback6(u *User, toUser *User, args []string, service string) { for _, fname := range u.br.GetFileLinks(p.FileIds) { fileMsg := "download file - " + fname switch { // nolint:dupl - case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" && strings.HasPrefix(args[0], "#"): + case (u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post") && strings.HasPrefix(args[0], "#"): threadMsgID := u.prefixContext("", p.Id, p.RootId, "") scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, fileMsg) spoof(nick, scrollbackMsg) - case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost": + case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post": threadMsgID := u.prefixContext("", p.Id, p.RootId, "") scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, fileMsg) u.MsgSpoofUser(scrollbackUser, nick, scrollbackMsg) diff --git a/mm-go-irckit/userbridge.go b/mm-go-irckit/userbridge.go index e8376879..0199af44 100644 --- a/mm-go-irckit/userbridge.go +++ b/mm-go-irckit/userbridge.go @@ -306,7 +306,7 @@ func (u *User) handleChannelMessageEvent(event *bridge.ChannelMessageEvent) { func (u *User) handleFileEvent(event *bridge.FileEvent) { for _, fname := range event.Files { fileMsg := "download file - " + fname.Name - if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" { + if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post" { threadMsgID := u.prefixContext(event.ChannelID, event.MessageID, event.ParentID, "") fileMsg = u.formatContextMessage("", threadMsgID, fileMsg) } @@ -710,7 +710,7 @@ func (u *User) addUserToChannelWorker(channels <-chan *bridge.ChannelInfo, throt } replayMsg := fmt.Sprintf("[%s] %s", ts.Format("15:04"), post) - if (u.v.GetBool(u.br.Protocol()+".prefixcontext") || u.v.GetBool(u.br.Protocol()+".suffixcontext")) && u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" && nick != systemUser { + if (u.v.GetBool(u.br.Protocol()+".prefixcontext") || u.v.GetBool(u.br.Protocol()+".suffixcontext")) && (u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post") && nick != systemUser { threadMsgID := u.prefixContext("", p.Id, p.ParentId, "") replayMsg = u.formatContextMessage(ts.Format("15:04"), threadMsgID, post) } @@ -723,7 +723,7 @@ func (u *User) addUserToChannelWorker(channels <-chan *bridge.ChannelInfo, throt for _, fname := range u.br.GetFileLinks(p.FileIds) { fileMsg := "download file - " + fname - if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" { + if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post" { threadMsgID := u.prefixContext("", p.Id, p.ParentId, "") fileMsg = u.formatContextMessage(ts.Format("15:04"), threadMsgID, fileMsg) } @@ -837,7 +837,7 @@ func (u *User) addUserToChannelWorker6(channels <-chan *bridge.ChannelInfo, thro } replayMsg := fmt.Sprintf("[%s] %s", ts.Format("15:04"), post) - if (u.v.GetBool(u.br.Protocol()+".prefixcontext") || u.v.GetBool(u.br.Protocol()+".suffixcontext")) && u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" && nick != systemUser { + if (u.v.GetBool(u.br.Protocol()+".prefixcontext") || u.v.GetBool(u.br.Protocol()+".suffixcontext")) && (u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post") && nick != systemUser { threadMsgID := u.prefixContext("", p.Id, p.RootId, "") replayMsg = u.formatContextMessage(ts.Format("15:04"), threadMsgID, post) } @@ -850,7 +850,7 @@ func (u *User) addUserToChannelWorker6(channels <-chan *bridge.ChannelInfo, thro for _, fname := range u.br.GetFileLinks(p.FileIds) { fileMsg := "download file - " + fname - if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" { + if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post" { threadMsgID := u.prefixContext("", p.Id, p.RootId, "") fileMsg = u.formatContextMessage(ts.Format("15:04"), threadMsgID, fileMsg) } @@ -1080,6 +1080,16 @@ func (u *User) prefixContext(channelID, messageID, parentID, event string) strin return fmt.Sprintf("[->@@%s]", parentID) } + if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post" { + if parentID == "" { + return fmt.Sprintf("[@@%s]", messageID) + } + if u.v.GetBool(u.br.Protocol() + ".unicode") { + return fmt.Sprintf("[↪@@%s,@@%s]", parentID, messageID) + } + return fmt.Sprintf("[->@@%s,@@%s]", parentID, messageID) + } + u.msgMapMutex.Lock() defer u.msgMapMutex.Unlock() From 4a5f640ebe0c1c15d96bc287f1d1b06d27e16bef Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Thu, 15 Sep 2022 15:53:14 +1000 Subject: [PATCH 2/3] Disable deprecated linters golint & scopelint --- .golangci.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.golangci.yaml b/.golangci.yaml index d2f7724f..a76bc61d 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -192,6 +192,8 @@ linters: - wrapcheck - ireturn - varnamelen + - golint + - scopelint # rules to deal with reported isues issues: From 917b7aa1b1f06ddaa4bf39e080c9c45dfae8068e Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Thu, 15 Sep 2022 17:08:56 +1000 Subject: [PATCH 3/3] Handle when message and parent IDs are the same, also refactor --- mm-go-irckit/userbridge.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/mm-go-irckit/userbridge.go b/mm-go-irckit/userbridge.go index 0199af44..67ea2ed7 100644 --- a/mm-go-irckit/userbridge.go +++ b/mm-go-irckit/userbridge.go @@ -1070,24 +1070,18 @@ func (u *User) prefixContextModified(channelID, messageID string) string { } func (u *User) prefixContext(channelID, messageID, parentID, event string) string { - if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" { + if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post" { if parentID == "" { return fmt.Sprintf("[@@%s]", messageID) } + prefixChar := "->" if u.v.GetBool(u.br.Protocol() + ".unicode") { - return fmt.Sprintf("[↪@@%s]", parentID) + prefixChar = "↪" } - return fmt.Sprintf("[->@@%s]", parentID) - } - - if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post" { - if parentID == "" { - return fmt.Sprintf("[@@%s]", messageID) - } - if u.v.GetBool(u.br.Protocol() + ".unicode") { - return fmt.Sprintf("[↪@@%s,@@%s]", parentID, messageID) + if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || parentID == messageID { + return fmt.Sprintf("[%s@@%s]", prefixChar, parentID) } - return fmt.Sprintf("[->@@%s,@@%s]", parentID, messageID) + return fmt.Sprintf("[%s@@%s,@@%s]", prefixChar, parentID, messageID) } u.msgMapMutex.Lock()