forked from MirrorHub/mautrix-whatsapp
Convert mentions to names in plaintext body. Fixes #3
This commit is contained in:
parent
eca9bc7887
commit
6ad224a58b
3 changed files with 23 additions and 19 deletions
|
@ -64,7 +64,7 @@ var strikethroughRegex = regexp.MustCompile("([\\s>_*]|^)~(.+?)~([^a-zA-Z\\d]|$)
|
|||
var codeBlockRegex = regexp.MustCompile("```(?:.|\n)+?```")
|
||||
var mentionRegex = regexp.MustCompile("@[0-9]+")
|
||||
|
||||
func (user *User) newWhatsAppFormatMaps() (map[*regexp.Regexp]string, map[*regexp.Regexp]func(string) string) {
|
||||
func (user *User) newWhatsAppFormatMaps() (map[*regexp.Regexp]string, map[*regexp.Regexp]func(string) string, map[*regexp.Regexp]func(string) string) {
|
||||
return map[*regexp.Regexp]string{
|
||||
italicRegex: "$1<em>$2</em>$3",
|
||||
boldRegex: "$1<strong>$2</strong>$3",
|
||||
|
@ -86,5 +86,10 @@ func (user *User) newWhatsAppFormatMaps() (map[*regexp.Regexp]string, map[*regex
|
|||
}
|
||||
return fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, mxid, puppet.Displayname)
|
||||
},
|
||||
}, map[*regexp.Regexp]func(string)string {
|
||||
mentionRegex: func(str string) string {
|
||||
puppet := user.GetPuppetByJID(str[1:] + whatsappExt.NewUserSuffix)
|
||||
return puppet.Displayname
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
28
portal.go
28
portal.go
|
@ -421,15 +421,21 @@ func (portal *Portal) SetReply(content *gomatrix.Content, info whatsapp.MessageI
|
|||
return
|
||||
}
|
||||
|
||||
func (portal *Portal) ParseWhatsAppFormat(input string) string {
|
||||
output := html.EscapeString(input)
|
||||
func (portal *Portal) FormatWhatsAppMessage(content *gomatrix.Content) {
|
||||
output := html.EscapeString(content.Body)
|
||||
for regex, replacement := range portal.user.waReplString {
|
||||
output = regex.ReplaceAllString(output, replacement)
|
||||
}
|
||||
for regex, replacer := range portal.user.waReplFunc {
|
||||
output = regex.ReplaceAllStringFunc(output, replacer)
|
||||
}
|
||||
return output
|
||||
if output != content.Body {
|
||||
content.FormattedBody = output
|
||||
content.Format = gomatrix.FormatHTML
|
||||
for regex, replacer := range portal.user.waReplFuncText {
|
||||
content.Body = regex.ReplaceAllStringFunc(content.Body, replacer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
|
||||
|
@ -448,17 +454,13 @@ func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
|
|||
return
|
||||
}
|
||||
|
||||
content := gomatrix.Content{
|
||||
content := &gomatrix.Content{
|
||||
Body: message.Text,
|
||||
MsgType: gomatrix.MsgText,
|
||||
}
|
||||
|
||||
htmlBody := portal.ParseWhatsAppFormat(message.Text)
|
||||
if htmlBody != message.Text {
|
||||
content.FormattedBody = htmlBody
|
||||
content.Format = gomatrix.FormatHTML
|
||||
}
|
||||
portal.SetReply(&content, message.Info)
|
||||
portal.FormatWhatsAppMessage(content)
|
||||
portal.SetReply(content, message.Info)
|
||||
|
||||
intent.UserTyping(portal.MXID, false, 0)
|
||||
resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, int64(message.Info.Timestamp*1000))
|
||||
|
@ -557,11 +559,7 @@ func (portal *Portal) HandleMediaMessage(download func() ([]byte, error), thumbn
|
|||
MsgType: gomatrix.MsgNotice,
|
||||
}
|
||||
|
||||
htmlBody := portal.ParseWhatsAppFormat(captionContent.Body)
|
||||
if htmlBody != captionContent.Body {
|
||||
captionContent.FormattedBody = htmlBody
|
||||
captionContent.Format = gomatrix.FormatHTML
|
||||
}
|
||||
portal.FormatWhatsAppMessage(captionContent)
|
||||
|
||||
_, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, captionContent, ts)
|
||||
if err != nil {
|
||||
|
|
7
user.go
7
user.go
|
@ -50,8 +50,9 @@ type User struct {
|
|||
|
||||
htmlParser *format.HTMLParser
|
||||
|
||||
waReplString map[*regexp.Regexp]string
|
||||
waReplFunc map[*regexp.Regexp]func(string) string
|
||||
waReplString map[*regexp.Regexp]string
|
||||
waReplFunc map[*regexp.Regexp]func(string) string
|
||||
waReplFuncText map[*regexp.Regexp]func(string) string
|
||||
}
|
||||
|
||||
func (bridge *Bridge) GetUser(userID types.MatrixUserID) *User {
|
||||
|
@ -101,7 +102,7 @@ func (bridge *Bridge) NewUser(dbUser *database.User) *User {
|
|||
user.Whitelisted = user.bridge.Config.Bridge.Permissions.IsWhitelisted(user.ID)
|
||||
user.Admin = user.bridge.Config.Bridge.Permissions.IsAdmin(user.ID)
|
||||
user.htmlParser = user.newHTMLParser()
|
||||
user.waReplString, user.waReplFunc = user.newWhatsAppFormatMaps()
|
||||
user.waReplString, user.waReplFunc, user.waReplFuncText = user.newWhatsAppFormatMaps()
|
||||
return user
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue