gomuks/matrix/pushrules/pushrules.go

38 lines
1.1 KiB
Go
Raw Normal View History

2018-03-21 22:29:58 +01:00
package pushrules
import (
2018-03-22 22:46:43 +01:00
"encoding/json"
"net/url"
2018-03-21 22:29:58 +01:00
2018-11-13 23:00:35 +01:00
"maunium.net/go/mautrix"
2018-03-21 22:29:58 +01:00
)
// GetPushRules returns the push notification rules for the global scope.
2018-11-13 23:00:35 +01:00
func GetPushRules(client *mautrix.Client) (*PushRuleset, error) {
2018-03-21 22:29:58 +01:00
return GetScopedPushRules(client, "global")
}
// GetScopedPushRules returns the push notification rules for the given scope.
2018-11-13 23:00:35 +01:00
func GetScopedPushRules(client *mautrix.Client, scope string) (resp *PushRuleset, err error) {
2018-03-21 22:29:58 +01:00
u, _ := url.Parse(client.BuildURL("pushrules", scope))
// client.BuildURL returns the URL without a trailing slash, but the pushrules endpoint requires the slash.
u.Path += "/"
_, err = client.MakeRequest("GET", u.String(), nil, &resp)
return
}
type contentWithRuleset struct {
Ruleset *PushRuleset `json:"global"`
}
2018-03-21 22:29:58 +01:00
// EventToPushRules converts a m.push_rules event to a PushRuleset by passing the data through JSON.
2018-11-13 23:00:35 +01:00
func EventToPushRules(event *mautrix.Event) (*PushRuleset, error) {
content := &contentWithRuleset{}
err := json.Unmarshal(event.Content.VeryRaw, content)
2018-03-21 22:29:58 +01:00
if err != nil {
return nil, err
}
return content.Ruleset, nil
2018-03-21 22:29:58 +01:00
}