| 1 |
// Copyright (c) 2020 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package pushrules |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/gob" |
| 11 |
"encoding/json" |
| 12 |
"reflect" |
| 13 |
|
| 14 |
"maunium.net/go/mautrix/event" |
| 15 |
) |
| 16 |
|
| 17 |
// EventContent represents the content of a m.push_rules account data event. |
| 18 |
// https://spec.matrix.org/v1.2/client-server-api/#mpush_rules |
| 19 |
type EventContent struct { |
| 20 |
Ruleset *PushRuleset `json:"global"` |
| 21 |
} |
| 22 |
|
| 23 |
func init() { |
| 24 |
event.TypeMap[event.AccountDataPushRules] = reflect.TypeOf(EventContent{}) |
| 25 |
gob.Register(&EventContent{}) |
| 26 |
} |
| 27 |
|
| 28 |
// EventToPushRules converts a m.push_rules event to a PushRuleset by passing the data through JSON. |
| 29 |
func EventToPushRules(evt *event.Event) (*PushRuleset, error) { |
| 30 |
content := &EventContent{} |
| 31 |
err := json.Unmarshal(evt.Content.VeryRaw, content) |
| 32 |
if err != nil { |
| 33 |
return nil, err |
| 34 |
} |
| 35 |
|
| 36 |
return content.Ruleset, nil |
| 37 |
} |