File: push_rules.py

package info (click to toggle)
mautrix-python 0.20.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,812 kB
  • sloc: python: 19,103; makefile: 16
file content (103 lines) | stat: -rw-r--r-- 3,510 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Copyright (c) 2022 Tulir Asokan
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations

from mautrix.api import Method, Path
from mautrix.types import (
    PushAction,
    PushCondition,
    PushRule,
    PushRuleID,
    PushRuleKind,
    PushRuleScope,
)

from ..base import BaseClientAPI


class PushRuleMethods(BaseClientAPI):
    """
    Methods in section 13.13 Push Notifications of the spec. These methods are used for modifying
    what triggers push notifications.

    See also: `API reference <https://matrix.org/docs/spec/client_server/r0.6.1#id89>`__"""

    async def get_push_rule(
        self, scope: PushRuleScope, kind: PushRuleKind, rule_id: PushRuleID
    ) -> PushRule:
        """
        Retrieve a single specified push rule.

        See also: `API reference <https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-pushrules-scope-kind-ruleid>`__

        Args:
            scope: The scope of the push rule.
            kind: The kind of rule.
            rule_id: The identifier of the rule.

        Returns:
            The push rule information.
        """
        resp = await self.api.request(Method.GET, Path.v3.pushrules[scope][kind][rule_id])
        return PushRule.deserialize(resp)

    async def set_push_rule(
        self,
        scope: PushRuleScope,
        kind: PushRuleKind,
        rule_id: PushRuleID,
        actions: list[PushAction],
        pattern: str | None = None,
        before: PushRuleID | None = None,
        after: PushRuleID | None = None,
        conditions: list[PushCondition] = None,
    ) -> None:
        """
        Create or modify a push rule.

        See also: `API reference <https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-pushrules-scope-kind-ruleid>`__

        Args:
            scope: The scope of the push rule.
            kind: The kind of rule.
            rule_id: The identifier for the rule.
            before:
            after:
            actions: The actions to perform when the conditions for the rule are met.
            pattern: The glob-style pattern to match against for ``content`` rules.
            conditions: The conditions for the rule for ``underride`` and ``override`` rules.
        """
        query = {}
        if after:
            query["after"] = after
        if before:
            query["before"] = before
        content = {"actions": [act.serialize() for act in actions]}
        if conditions:
            content["conditions"] = [cond.serialize() for cond in conditions]
        if pattern:
            content["pattern"] = pattern
        await self.api.request(
            Method.PUT,
            Path.v3.pushrules[scope][kind][rule_id],
            query_params=query,
            content=content,
        )

    async def remove_push_rule(
        self, scope: PushRuleScope, kind: PushRuleKind, rule_id: PushRuleID
    ) -> None:
        """
        Remove a push rule.

        See also: `API reference <https://matrix.org/docs/spec/client_server/r0.6.1#delete-matrix-client-r0-pushrules-scope-kind-ruleid>`__

        Args:
            scope: The scope of the push rule.
            kind: The kind of rule.
            rule_id: The identifier of the rule.
        """
        await self.api.request(Method.DELETE, Path.v3.pushrules[scope][kind][rule_id])