Skip to content

Commit

Permalink
crypto: add full support for json.RawMessage in EncryptMegolmEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Oct 18, 2024
1 parent 6c07832 commit 3277c52
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions crypto/encryptmegolm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"fmt"

"github.com/rs/zerolog"
"github.com/tidwall/gjson"
"go.mau.fi/util/exgjson"
"go.mau.fi/util/exzerolog"

"maunium.net/go/mautrix"
Expand All @@ -27,7 +29,24 @@ var (
NoGroupSession = errors.New("no group session created")
)

func getRelatesTo(content interface{}) *event.RelatesTo {
func getRawJSON[T any](content json.RawMessage, path ...string) *T {
value := gjson.GetBytes(content, exgjson.Path(path...))
if !value.IsObject() {
return nil
}
var result T
err := json.Unmarshal([]byte(value.Raw), &result)
if err != nil {
return nil
}
return &result
}

func getRelatesTo(content any) *event.RelatesTo {
contentJSON, ok := content.(json.RawMessage)
if ok {
return getRawJSON[event.RelatesTo](contentJSON, "m.relates_to")
}
contentStruct, ok := content.(*event.Content)
if ok {
content = contentStruct.Parsed
Expand All @@ -39,7 +58,11 @@ func getRelatesTo(content interface{}) *event.RelatesTo {
return nil
}

func getMentions(content interface{}) *event.Mentions {
func getMentions(content any) *event.Mentions {
contentJSON, ok := content.(json.RawMessage)
if ok {
return getRawJSON[event.Mentions](contentJSON, "m.mentions")
}
contentStruct, ok := content.(*event.Content)
if ok {
content = contentStruct.Parsed
Expand Down

0 comments on commit 3277c52

Please sign in to comment.