aboutsummaryrefslogtreecommitdiff
path: root/matrix/pushrules/pushrules.go
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-03-21 23:29:58 +0200
committerTulir Asokan <tulir@maunium.net>2018-03-21 23:29:58 +0200
commit9fd67102ad2cca16c092e23ffd928b77ab08d7e0 (patch)
tree2c34025e564806cb8f9faad0c15e25a0e6dec3ed /matrix/pushrules/pushrules.go
parentb4902d4edb27baf59b21747117d93db4e0e4e96c (diff)
Refactoring and godocs
Diffstat (limited to 'matrix/pushrules/pushrules.go')
-rw-r--r--matrix/pushrules/pushrules.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/matrix/pushrules/pushrules.go b/matrix/pushrules/pushrules.go
new file mode 100644
index 0000000..bfcb4ef
--- /dev/null
+++ b/matrix/pushrules/pushrules.go
@@ -0,0 +1,42 @@
+package pushrules
+
+import (
+
+"encoding/json"
+"net/url"
+
+"maunium.net/go/gomatrix"
+
+)
+
+// GetPushRules returns the push notification rules for the global scope.
+func GetPushRules(client *gomatrix.Client) (*PushRuleset, error) {
+ return GetScopedPushRules(client, "global")
+}
+
+// GetScopedPushRules returns the push notification rules for the given scope.
+func GetScopedPushRules(client *gomatrix.Client, scope string) (resp *PushRuleset, err error) {
+ 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
+}
+
+// EventToPushRules converts a m.push_rules event to a PushRuleset by passing the data through JSON.
+func EventToPushRules(event *gomatrix.Event) (*PushRuleset, error) {
+ content, _ := event.Content["global"]
+ raw, err := json.Marshal(content)
+ if err != nil {
+ return nil, err
+ }
+
+ ruleset := &PushRuleset{}
+ err = json.Unmarshal(raw, ruleset)
+ if err != nil {
+ return nil, err
+ }
+
+ return ruleset, nil
+}
+