From 198625eb9b3bb0e967cdda652029dba3185e9059 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 5 Aug 2024 14:14:53 +0300 Subject: [PATCH] just copy old incoming mention parsing --- messagix/socket/threads.go | 2 +- pkg/connector/msgconv/mentions.go | 117 +++++++++++------------------- 2 files changed, 44 insertions(+), 75 deletions(-) diff --git a/messagix/socket/threads.go b/messagix/socket/threads.go index abd444c..da986db 100644 --- a/messagix/socket/threads.go +++ b/messagix/socket/threads.go @@ -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, ",") diff --git a/pkg/connector/msgconv/mentions.go b/pkg/connector/msgconv/mentions.go index 8ae15f3..5d74c66 100644 --- a/pkg/connector/msgconv/mentions.go +++ b/pkg/connector/msgconv/mentions.go @@ -18,9 +18,6 @@ package msgconv import ( "context" - "regexp" - "slices" - //"log" "strings" "unicode/utf16" @@ -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() + `` + utf16Text[mention.Offset:end].String() + ``) - 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(``) + output.WriteString(utf16Text[mention.Offset:end].String()) + output.WriteString(``) + prevEnd = end } - - // Second parsing pass, replacing other formatting: - outputString = META_BOLD_REGEX.ReplaceAllString(outputString, "$1") - outputString = META_ITALIC_REGEX.ReplaceAllString(outputString, "$1") - outputString = META_STRIKE_REGEX.ReplaceAllString(outputString, "$1") - outputString = META_MONOSPACE_REGEX.ReplaceAllString(outputString, "$1") - outputString = META_MONOSPACE_BLOCK_REGEX.ReplaceAllString(outputString, "
$1
") - + 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 }