Skip to content

Commit

Permalink
just copy old incoming mention parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Aug 5, 2024
1 parent ca1f6a8 commit 198625e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 75 deletions.
2 changes: 1 addition & 1 deletion messagix/socket/threads.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type MentionData struct {
}

func (md *MentionData) Parse() (Mentions, error) {
if len(md.MentionIDs) == 0 {
if md == nil || len(md.MentionIDs) == 0 {
return nil, nil
}
mentionIDs := strings.Split(md.MentionIDs, ",")
Expand Down
117 changes: 43 additions & 74 deletions pkg/connector/msgconv/mentions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ package msgconv

import (
"context"
"regexp"
"slices"

//"log"
"strings"
"unicode/utf16"
Expand All @@ -43,88 +40,60 @@ func (u UTF16String) String() string {
return string(utf16.Decode(u))
}

var (
META_BOLD_REGEX = regexp.MustCompile(`\*([^*]+)\*`)
META_ITALIC_REGEX = regexp.MustCompile(`_([^_]+)_`)
META_STRIKE_REGEX = regexp.MustCompile(`~([^~]+)~`)
META_MONOSPACE_REGEX = regexp.MustCompile("`([^`]+)`")
META_MONOSPACE_BLOCK_REGEX = regexp.MustCompile("```([^`]+)```")
)

func (mc *MessageConverter) MetaToMatrixText(ctx context.Context, text string, rawMentions *socket.MentionData, portal *bridgev2.Portal) (content *event.MessageEventContent) {
content = &event.MessageEventContent{
MsgType: event.MsgText,
Body: text,
Mentions: &event.Mentions{},
}

var mentions *socket.Mentions

if rawMentions != nil {
mentionsP, err := rawMentions.Parse()
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to parse mentions")
}
mentions = &mentionsP
mentions, err := rawMentions.Parse()
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to parse mentions")
}

outputString := text

if mentions != nil {
utf16Text := NewUTF16String(text)
prevEnd := 0
var output strings.Builder
for _, mention := range *mentions {
if mention.Offset < prevEnd {
zerolog.Ctx(ctx).Warn().Msg("Ignoring overlapping mentions in message")
continue
} else if mention.Offset >= len(utf16Text) {
zerolog.Ctx(ctx).Warn().Msg("Ignoring mention outside of message")
continue
}
end := mention.Offset + mention.Length
if end > len(utf16Text) {
end = len(utf16Text)
}
var mentionLink string
switch mention.Type {
case socket.MentionTypePerson:
info, err := mc.getBasicUserInfo(ctx, portal, ids.MakeUserID(mention.ID))
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to get user info for mention")
continue
}
if !slices.Contains(content.Mentions.UserIDs, info.MXID) {
content.Mentions.UserIDs = append(content.Mentions.UserIDs, info.MXID)
}
mentionLink = info.MXID.URI().MatrixToURL()
case socket.MentionTypeThread:
// TODO: how does one send thread mentions?
}
if mentionLink == "" {
if mentions == nil {
return
}
utf16Text := NewUTF16String(text)
prevEnd := 0
var output strings.Builder
for _, mention := range mentions {
if mention.Offset < prevEnd {
zerolog.Ctx(ctx).Warn().Msg("Ignoring overlapping mentions in message")
continue
} else if mention.Offset >= len(utf16Text) {
zerolog.Ctx(ctx).Warn().Msg("Ignoring mention outside of message")
continue
}
end := mention.Offset + mention.Length
if end > len(utf16Text) {
end = len(utf16Text)
}
var mentionLink string
switch mention.Type {
case socket.MentionTypePerson:
info, err := mc.getBasicUserInfo(ctx, portal, ids.MakeUserID(mention.ID))
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to get user info for mention")
continue
}

output.WriteString(utf16Text[prevEnd:mention.Offset].String() + `<a href="` + mentionLink + `">` + utf16Text[mention.Offset:end].String() + `</a>`)
prevEnd = end
content.Mentions.Add(info.MXID)
mentionLink = info.MXID.URI().MatrixToURL()
case socket.MentionTypeThread:
// TODO: how does one send thread mentions?
}
output.WriteString(utf16Text[prevEnd:].String())

outputString = output.String()
if mentionLink == "" {
continue
}
output.WriteString(utf16Text[prevEnd:mention.Offset].String())
output.WriteString(`<a href="`)
output.WriteString(mentionLink)
output.WriteString(`">`)
output.WriteString(utf16Text[mention.Offset:end].String())
output.WriteString(`</a>`)
prevEnd = end
}

// Second parsing pass, replacing other formatting:
outputString = META_BOLD_REGEX.ReplaceAllString(outputString, "<strong>$1</strong>")
outputString = META_ITALIC_REGEX.ReplaceAllString(outputString, "<em>$1</em>")
outputString = META_STRIKE_REGEX.ReplaceAllString(outputString, "<del>$1</del>")
outputString = META_MONOSPACE_REGEX.ReplaceAllString(outputString, "<code>$1</code>")
outputString = META_MONOSPACE_BLOCK_REGEX.ReplaceAllString(outputString, "<pre>$1</pre>")

output.WriteString(utf16Text[prevEnd:].String())
content.Format = event.FormatHTML
content.FormattedBody = outputString

log := zerolog.Ctx(ctx)
log.Debug().Str("text", text).Str("formatted_body", content.FormattedBody).Msg("Converted message to Matrix text")

content.FormattedBody = output.String()
return content
}

0 comments on commit 198625e

Please sign in to comment.