Skip to content

Commit

Permalink
Add threading support
Browse files Browse the repository at this point in the history
  • Loading branch information
hloeung committed Feb 25, 2024
1 parent 07e8372 commit 6251d20
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
39 changes: 28 additions & 11 deletions bridge/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,14 @@ func (m *Matrix) handleMessageEvent(source mautrix.EventSource, ev *event.Event)
var parentID string

switch {
case ev.Type.String() == "m.text":
case ev.Type.String() == "m.text" || ev.Type.String() == "m.room.message":
msgEventContent, _ := ev.Content.Parsed.(*event.MessageEventContent)
text = msgEventContent.Body
if msgEventContent.RelatesTo != nil {
parentID = msgEventContent.RelatesTo.EventID.String()
}
default:
logger.Debugf("handleMessageEvent unsupported event type %s", ev.Type.String())
logger.Warnf("handleMessageEvent unsupported event type %s", ev.Type.String())
}

m.RLock()
Expand Down Expand Up @@ -357,7 +357,7 @@ func (m *Matrix) handleReactionEvent(source mautrix.EventSource, ev *event.Event
reaction = reactionEventContent.RelatesTo.Key
parentID = reactionEventContent.RelatesTo.EventID.String()
default:
logger.Debugf("handleEvent unsupported event type %s", ev.Type.String())
logger.Warnf("handleEvent unsupported event type %s", ev.Type.String())
}

m.RLock()
Expand Down Expand Up @@ -422,7 +422,7 @@ func (m *Matrix) MsgUser(userID, text string) (string, error) {
}

func (m *Matrix) MsgUserThread(userID, parentID, text string) (string, error) {
logger.Debug("sending message ", userID, parentID, text)
logger.Debugf("sending message '%s' '%s' '%s'", userID, parentID, text)
invites := []id.UserID{id.UserID(userID)}

var roomID id.RoomID
Expand Down Expand Up @@ -468,13 +468,30 @@ func (m *Matrix) MsgChannel(channelID, text string) (string, error) {
}

func (m *Matrix) MsgChannelThread(channelID, parentID, text string) (string, error) {
logger.Debug("msgchannelthread: sending message thread ", channelID, parentID, text)
resp, err := m.mc.SendMessageEvent(id.RoomID(channelID), event.EventMessage, event.MessageEventContent{
MsgType: "m.text",
Body: text,
FormattedBody: helper.ParseMarkdown(text),
Format: "org.matrix.custom.html",
})
logger.Debugf("msgchannelthread: sending message thread '%s' '%s' '%s'", channelID, parentID, text)

var context event.MessageEventContent
if parentID != "" {
rel := event.RelatesTo{
Type: "m.thread",
EventID: id.EventID(parentID),
}
context = event.MessageEventContent{
MsgType: "m.text",
Body: text,
FormattedBody: helper.ParseMarkdown(text),
Format: "org.matrix.custom.html",
RelatesTo: &rel,
}
} else {
context = event.MessageEventContent{
MsgType: "m.text",
Body: text,
FormattedBody: helper.ParseMarkdown(text),
Format: "org.matrix.custom.html",
}
}
resp, err := m.mc.SendMessageEvent(id.RoomID(channelID), event.EventMessage, context)
if err != nil {
return "", err
}
Expand Down
13 changes: 10 additions & 3 deletions mm-go-irckit/server_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func CmdPrivMsg(s Server, u *User, msg *irc.Message) error {
return s.EncodeMessage(u, irc.ERR_NOSUCHNICK, msg.Params, "No such nick/channel")
}

var parseReactionToMsgRegExp = regexp.MustCompile(`^\@\@([0-9a-f]{3}|[0-9a-z]{26})\s+([\-\+]):(\S+):\s*$`)
var parseReactionToMsgRegExp = regexp.MustCompile(`^\@\@([0-9a-f]{3}|[0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43})\s+([\-\+]):(\S+):\s*$`)

func parseReactionToMsg(u *User, msg *irc.Message, channelID string) bool {
matches := parseReactionToMsgRegExp.FindStringSubmatch(msg.Trailing)
Expand Down Expand Up @@ -505,7 +505,7 @@ func parseReactionToMsg(u *User, msg *irc.Message, channelID string) bool {
return true
}

var parseModifyMsgRegExp = regexp.MustCompile(`^s(\/(?:[0-9a-f]{3}|[0-9a-z]{26}|!!)?\/)(.*)`)
var parseModifyMsgRegExp = regexp.MustCompile(`^s(\/(?:!!|[0-9a-f]{3}|[0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43})?\/)(.*)`)

func parseModifyMsg(u *User, msg *irc.Message, channelID string) bool {
matches := parseModifyMsgRegExp.FindStringSubmatch(msg.Trailing)
Expand Down Expand Up @@ -579,7 +579,7 @@ func parseModifyMsg(u *User, msg *irc.Message, channelID string) bool {
return true
}

var parseThreadIDRegExp = regexp.MustCompile(`(?s)^\@\@(?:(!!|[0-9a-f]{3}|[0-9a-z]{26})\s)(.*)`)
var parseThreadIDRegExp = regexp.MustCompile(`(?s)^\@\@(?:(!!|[0-9a-f]{3}|[0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43})\s)(.*)`)

func parseThreadID(u *User, msg *irc.Message, channelID string) (string, string) {
matches := parseThreadIDRegExp.FindStringSubmatch(msg.Trailing)
Expand All @@ -605,6 +605,7 @@ func parseThreadID(u *User, msg *irc.Message, channelID string) (string, string)
parentID = msgLast[1]
}
return parentID, matches[2]
// matterircd's hexadecimal format.
case len(matches[1]) == 3:
id, err := strconv.ParseInt(matches[1], 16, 0)
if err != nil {
Expand All @@ -618,8 +619,14 @@ func parseThreadID(u *User, msg *irc.Message, channelID string) (string, string)
if _, ok := u.msgMapIndex[channelID][int(id)]; ok {
return u.msgMapIndex[channelID][int(id)], matches[2]
}
// Mattermost's ID format.
case len(matches[1]) == 26:
return matches[1], matches[2]
// Matrix's Event ID format:
// https://spec.matrix.org/latest/appendices/#common-identifier-format
// https://spec.matrix.org/latest/appendices/#common-namespaced-identifier-grammar
case len(matches[1]) == 44:
return matches[1], matches[2]
default:
logger.Errorf("parseThreadID: could not parse reply ID %q", matches[1])
return "", ""
Expand Down
4 changes: 2 additions & 2 deletions mm-go-irckit/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ func (u *User) Encode(msgs ...*irc.Message) (err error) {
}

var (
replyRegExp = regexp.MustCompile(`\@\@(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)\s`)
modifyRegExp = regexp.MustCompile(`^s/(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)?/`)
replyRegExp = regexp.MustCompile(`\@\@(?:!!|[0-9a-z]{26}|[0-9a-f]{3}|\$[0-9A-Za-z\-_\.]{43})\s`)
modifyRegExp = regexp.MustCompile(`^s/(?:!!|[0-9a-z]{26}|[0-9a-f]{3}|\$[0-9A-Za-z\-_\.]{43})?/`)
)

// Decode will receive and return a decoded message, or an error.
Expand Down
2 changes: 1 addition & 1 deletion mm-go-irckit/userbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ func (u *User) prefixContext(channelID, messageID, parentID, event string) strin
prefixChar = "↪"
}

if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post" { //nolint:goconst
if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post" || u.v.GetString(u.br.Protocol()+".threadcontext") == "matrix" { //nolint:goconst
if parentID == "" {
return fmt.Sprintf("[@@%s]", messageID)
}
Expand Down

0 comments on commit 6251d20

Please sign in to comment.