Implement MSC3952 for incoming messages

This commit is contained in:
Tulir Asokan 2023-03-02 13:54:27 +02:00
parent 59b6e9f272
commit 65e0c0019e
5 changed files with 14 additions and 4 deletions

View File

@ -118,6 +118,7 @@ func DoUpgrade(helper *up.Helper) {
helper.Copy(up.Bool, "bridge", "encryption", "default")
helper.Copy(up.Bool, "bridge", "encryption", "require")
helper.Copy(up.Bool, "bridge", "encryption", "appservice")
helper.Copy(up.Bool, "bridge", "encryption", "plaintext_mentions")
helper.Copy(up.Str, "bridge", "encryption", "verification_levels", "receive")
helper.Copy(up.Str, "bridge", "encryption", "verification_levels", "send")
helper.Copy(up.Str, "bridge", "encryption", "verification_levels", "share")

View File

@ -354,6 +354,8 @@ bridge:
# Enable key sharing? If enabled, key requests for rooms where users are in will be fulfilled.
# You must use a client that supports requesting keys from other users to use this feature.
allow_key_sharing: false
# Should users mentions be in the event wire content to enable the server to send push notifications?
plaintext_mentions: false
# What level of device verification should be required from users?
#
# Valid levels:

View File

@ -1,5 +1,5 @@
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
// Copyright (C) 2021 Tulir Asokan
// Copyright (C) 2023 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
@ -125,6 +125,8 @@ func (formatter *Formatter) ParseWhatsApp(roomID id.RoomID, content *event.Messa
return fmt.Sprintf(`<a href="%s">%s</a>`, groups[2], groups[1])
})
}
alreadyMentioned := make(map[id.UserID]struct{})
content.Mentions = &event.Mentions{}
for _, rawJID := range mentionedJIDs {
jid, err := types.ParseJID(rawJID)
if err != nil {
@ -136,7 +138,12 @@ func (formatter *Formatter) ParseWhatsApp(roomID id.RoomID, content *event.Messa
number := "@" + jid.User
output = strings.ReplaceAll(output, number, fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, mxid, displayname))
content.Body = strings.ReplaceAll(content.Body, number, displayname)
if _, ok := alreadyMentioned[mxid]; !ok {
alreadyMentioned[mxid] = struct{}{}
content.Mentions.UserIDs = append(content.Mentions.UserIDs, mxid)
}
}
content.UnstableMentions = content.Mentions
if output != content.Body || forceHTML {
output = strings.ReplaceAll(output, "\n", "<br/>")
content.FormattedBody = output

2
go.mod
View File

@ -16,7 +16,7 @@ require (
golang.org/x/net v0.6.0
google.golang.org/protobuf v1.28.1
maunium.net/go/maulogger/v2 v2.4.1
maunium.net/go/mautrix v0.15.0-beta.1.0.20230226232632-00f40652f33d
maunium.net/go/mautrix v0.15.0-beta.2
)
require (

4
go.sum
View File

@ -127,5 +127,5 @@ maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
maunium.net/go/maulogger/v2 v2.4.1 h1:N7zSdd0mZkB2m2JtFUsiGTQQAdP0YeFWT7YMc80yAL8=
maunium.net/go/maulogger/v2 v2.4.1/go.mod h1:omPuYwYBILeVQobz8uO3XC8DIRuEb5rXYlQSuqrbCho=
maunium.net/go/mautrix v0.15.0-beta.1.0.20230226232632-00f40652f33d h1:16Q4co5TusYEovGLiHSkT6FY6fFn5tNNLCR3FvGCLFk=
maunium.net/go/mautrix v0.15.0-beta.1.0.20230226232632-00f40652f33d/go.mod h1:AE3TCX9q4W7fYfrL/1RsuOell9rTUBO27XUULuwArH4=
maunium.net/go/mautrix v0.15.0-beta.2 h1:J7sEehF7taahVtoCsM6E41VauaBNQMPCCcxYCe/0Zbk=
maunium.net/go/mautrix v0.15.0-beta.2/go.mod h1:AE3TCX9q4W7fYfrL/1RsuOell9rTUBO27XUULuwArH4=