Skip to content

Commit

Permalink
Rework the way Matterircd threading works
Browse files Browse the repository at this point in the history
  • Loading branch information
hloeung committed Sep 4, 2023
1 parent 771c5bb commit c80eb79
Showing 1 changed file with 28 additions and 35 deletions.
63 changes: 28 additions & 35 deletions mm-go-irckit/userbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,9 +915,9 @@ func (u *User) increaseMsgCounter(channelID string) int {
defer u.msgCounterMutex.Unlock()
u.msgCounter[channelID]++

// max 4096 entries
if u.msgCounter[channelID] == 4095 {
u.msgCounter[channelID] = 0
// max 4096 entries (0xFFF); set back to 1, 0 is used for absent.
if u.msgCounter[channelID] == 4096 {
u.msgCounter[channelID] = 1
}

return u.msgCounter[channelID]
Expand All @@ -937,30 +937,16 @@ func (u *User) formatContextMessage(ts, threadMsgID, msg string) string {
return formattedMsg
}

func (u *User) prefixContextModified(channelID, messageID string) string {
var (
ok bool
currentcount int
)

// check if we already have a counter for this messageID otherwise
// increase counter and create it
if currentcount, ok = u.msgMap[channelID][messageID]; !ok {
currentcount = u.increaseMsgCounter(channelID)
func (u *User) prefixContext(channelID, messageID, parentID, event string) string {
prefixChar := "->"
if u.v.GetBool(u.br.Protocol() + ".unicode") {
prefixChar = "↪"
}

return fmt.Sprintf("[%03x]", currentcount)
}

func (u *User) prefixContext(channelID, messageID, parentID, event string) string {
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") {
prefixChar = "↪"
}
if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || parentID == messageID {
return fmt.Sprintf("[%s@@%s]", prefixChar, parentID)
}
Expand All @@ -970,21 +956,15 @@ func (u *User) prefixContext(channelID, messageID, parentID, event string) strin
u.msgMapMutex.Lock()
defer u.msgMapMutex.Unlock()

var ok bool
var (
currentcount, parentcount int
ok bool
)

if _, ok = u.msgMap[channelID]; !ok {
u.msgMap[channelID] = make(map[string]int)
}

if event == "post_edited" || event == "post_deleted" {
return u.prefixContextModified(channelID, messageID)
}
if event == "reaction" {
return u.prefixContextModified(channelID, parentID)
}

var currentcount, parentcount int

if parentID != "" {
if _, ok = u.msgMap[channelID][parentID]; !ok {
u.msgMap[channelID][parentID] = u.increaseMsgCounter(channelID)
Expand All @@ -993,13 +973,26 @@ func (u *User) prefixContext(channelID, messageID, parentID, event string) strin
parentcount = u.msgMap[channelID][parentID]
}

currentcount = u.increaseMsgCounter(channelID)
u.msgMap[channelID][messageID] = currentcount
switch {
case event == "post_edited" || event == "post_deleted":
if _, ok = u.msgMap[channelID][messageID]; !ok {
u.msgMap[channelID][messageID] = u.increaseMsgCounter(channelID)
}

currentcount = u.msgMap[channelID][messageID]

if parentID != "" {
return fmt.Sprintf("[%03x->%03x]", currentcount, parentcount)
// No need to have reactions assigned a counter as we can't reply
// to them anyways. We do want their parent though.
case event == "reaction":
return fmt.Sprintf("[%s%03x]", prefixChar, parentcount)
default:
u.msgMap[channelID][messageID] = u.increaseMsgCounter(channelID)
currentcount = u.msgMap[channelID][messageID]
}

if parentID != "" {
return fmt.Sprintf("[%s%03x,%03x]", prefixChar, parentcount, currentcount)
}
return fmt.Sprintf("[%03x]", currentcount)
}

Expand Down

0 comments on commit c80eb79

Please sign in to comment.