diff --git a/event/state.go b/event/state.go index 0a82fbe5..006ed2a5 100644 --- a/event/state.go +++ b/event/state.go @@ -7,6 +7,8 @@ package event import ( + "encoding/base64" + "maunium.net/go/mautrix/id" ) @@ -215,6 +217,17 @@ type PolicyHashes struct { SHA256 string `json:"sha256"` } +func (ph *PolicyHashes) DecodeSHA256() *[32]byte { + if ph == nil || ph.SHA256 == "" { + return nil + } + decoded, _ := base64.StdEncoding.DecodeString(ph.SHA256) + if len(decoded) == 32 { + return (*[32]byte)(decoded) + } + return nil +} + // ModPolicyContent represents the content of a m.room.rule.user, m.room.rule.room, and m.room.rule.server state event. // https://spec.matrix.org/v1.2/client-server-api/#moderation-policy-lists type ModPolicyContent struct { @@ -224,6 +237,13 @@ type ModPolicyContent struct { UnstableHashes *PolicyHashes `json:"org.matrix.msc4205.hashes,omitempty"` } +func (mpc *ModPolicyContent) EntityOrHash() string { + if mpc.UnstableHashes != nil && mpc.UnstableHashes.SHA256 != "" { + return mpc.UnstableHashes.SHA256 + } + return mpc.Entity +} + // Deprecated: MSC2716 has been abandoned type InsertionMarkerContent struct { InsertionID id.EventID `json:"org.matrix.msc2716.marker.insertion"`