Skip to content

Commit 00e1f13

Browse files
committed
Add threading support
1 parent 07e8372 commit 00e1f13

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

bridge/matrix/matrix.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,14 @@ func (m *Matrix) handleMessageEvent(source mautrix.EventSource, ev *event.Event)
288288
var parentID string
289289

290290
switch {
291-
case ev.Type.String() == "m.text":
291+
case ev.Type.String() == "m.text" || ev.Type.String() == "m.room.message":
292292
msgEventContent, _ := ev.Content.Parsed.(*event.MessageEventContent)
293293
text = msgEventContent.Body
294294
if msgEventContent.RelatesTo != nil {
295295
parentID = msgEventContent.RelatesTo.EventID.String()
296296
}
297297
default:
298-
logger.Debugf("handleMessageEvent unsupported event type %s", ev.Type.String())
298+
logger.Warnf("handleMessageEvent unsupported event type %s", ev.Type.String())
299299
}
300300

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

363363
m.RLock()
@@ -422,7 +422,7 @@ func (m *Matrix) MsgUser(userID, text string) (string, error) {
422422
}
423423

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

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

470470
func (m *Matrix) MsgChannelThread(channelID, parentID, text string) (string, error) {
471-
logger.Debug("msgchannelthread: sending message thread ", channelID, parentID, text)
472-
resp, err := m.mc.SendMessageEvent(id.RoomID(channelID), event.EventMessage, event.MessageEventContent{
473-
MsgType: "m.text",
474-
Body: text,
475-
FormattedBody: helper.ParseMarkdown(text),
476-
Format: "org.matrix.custom.html",
477-
})
471+
logger.Debugf("msgchannelthread: sending message thread '%s' '%s' '%s'", channelID, parentID, text)
472+
473+
var context event.MessageEventContent
474+
if parentID != "" {
475+
rel := event.RelatesTo{
476+
Type: "m.thread",
477+
EventID: id.EventID(parentID),
478+
}
479+
context = event.MessageEventContent{
480+
MsgType: "m.text",
481+
Body: text,
482+
FormattedBody: helper.ParseMarkdown(text),
483+
Format: "org.matrix.custom.html",
484+
RelatesTo: &rel,
485+
}
486+
} else {
487+
context = event.MessageEventContent{
488+
MsgType: "m.text",
489+
Body: text,
490+
FormattedBody: helper.ParseMarkdown(text),
491+
Format: "org.matrix.custom.html",
492+
}
493+
}
494+
resp, err := m.mc.SendMessageEvent(id.RoomID(channelID), event.EventMessage, context)
478495
if err != nil {
479496
return "", err
480497
}

mm-go-irckit/server_commands.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func CmdPrivMsg(s Server, u *User, msg *irc.Message) error {
461461
return s.EncodeMessage(u, irc.ERR_NOSUCHNICK, msg.Params, "No such nick/channel")
462462
}
463463

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

466466
func parseReactionToMsg(u *User, msg *irc.Message, channelID string) bool {
467467
matches := parseReactionToMsgRegExp.FindStringSubmatch(msg.Trailing)
@@ -505,7 +505,7 @@ func parseReactionToMsg(u *User, msg *irc.Message, channelID string) bool {
505505
return true
506506
}
507507

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

510510
func parseModifyMsg(u *User, msg *irc.Message, channelID string) bool {
511511
matches := parseModifyMsgRegExp.FindStringSubmatch(msg.Trailing)
@@ -579,7 +579,7 @@ func parseModifyMsg(u *User, msg *irc.Message, channelID string) bool {
579579
return true
580580
}
581581

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

584584
func parseThreadID(u *User, msg *irc.Message, channelID string) (string, string) {
585585
matches := parseThreadIDRegExp.FindStringSubmatch(msg.Trailing)
@@ -605,6 +605,7 @@ func parseThreadID(u *User, msg *irc.Message, channelID string) (string, string)
605605
parentID = msgLast[1]
606606
}
607607
return parentID, matches[2]
608+
// matterircd's hexidecimal format.
608609
case len(matches[1]) == 3:
609610
id, err := strconv.ParseInt(matches[1], 16, 0)
610611
if err != nil {
@@ -618,8 +619,14 @@ func parseThreadID(u *User, msg *irc.Message, channelID string) (string, string)
618619
if _, ok := u.msgMapIndex[channelID][int(id)]; ok {
619620
return u.msgMapIndex[channelID][int(id)], matches[2]
620621
}
622+
// Mattermost's ID format.
621623
case len(matches[1]) == 26:
622624
return matches[1], matches[2]
625+
// Matrix's Event ID format:
626+
// https://spec.matrix.org/latest/appendices/#common-identifier-format
627+
// https://spec.matrix.org/latest/appendices/#common-namespaced-identifier-grammar
628+
case len(matches[1]) == 44:
629+
return matches[1], matches[2]
623630
default:
624631
logger.Errorf("parseThreadID: could not parse reply ID %q", matches[1])
625632
return "", ""

mm-go-irckit/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ func (u *User) Encode(msgs ...*irc.Message) (err error) {
154154
}
155155

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

161161
// Decode will receive and return a decoded message, or an error.

mm-go-irckit/userbridge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ func (u *User) prefixContext(channelID, messageID, parentID, event string) strin
10271027
prefixChar = "↪"
10281028
}
10291029

1030-
if u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post" { //nolint:goconst
1030+
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
10311031
if parentID == "" {
10321032
return fmt.Sprintf("[@@%s]", messageID)
10331033
}

0 commit comments

Comments
 (0)