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 (84 lines) | stat: -rw-r--r-- 2,099 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
# 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 typing import List, Optional, Union

from attr import dataclass
import attr

from .primitive import JSON, RoomID, UserID
from .util import ExtensibleEnum, SerializableAttrs, deserializer

PushRuleID = Union[RoomID, UserID, str]


class PushActionType(ExtensibleEnum):
    NOTIFY = "notify"
    DONT_NOTIFY = "dont_notify"
    COALESCE = "coalesce"


@dataclass
class PushActionDict(SerializableAttrs):
    set_tweak: Optional[str] = None
    value: Optional[str] = None


PushAction = Union[PushActionDict, PushActionType]


@deserializer(PushAction)
def deserialize_push_action(data: JSON) -> PushAction:
    if isinstance(data, str):
        return PushActionType(data)
    else:
        return PushActionDict.deserialize(data)


class PushOperator(ExtensibleEnum):
    EQUALS = "=="
    LESS_THAN = "<"
    GREATER_THAN = ">"
    LESS_THAN_OR_EQUAL = "<="
    GREATER_THAN_OR_EQUAL = ">="


class PushRuleScope(ExtensibleEnum):
    GLOBAL = "global"


class PushConditionKind(ExtensibleEnum):
    EVENT_MATCH = "event_match"
    CONTAINS_DISPLAY_NAME = "contains_display_name"
    ROOM_MEMBER_COUNT = "room_member_count"
    SENDER_NOTIFICATION_PERMISSION = "sender_notification_permission"


class PushRuleKind(ExtensibleEnum):
    OVERRIDE = "override"
    SENDER = "sender"
    ROOM = "room"
    CONTENT = "content"
    UNDERRIDE = "underride"


@dataclass
class PushCondition(SerializableAttrs):
    kind: PushConditionKind
    key: Optional[str] = None
    pattern: Optional[str] = None
    operator: PushOperator = attr.ib(
        default=PushOperator.EQUALS, metadata={"json": "is", "omitdefault": True}
    )


@dataclass
class PushRule(SerializableAttrs):
    rule_id: PushRuleID
    default: bool
    enabled: bool
    actions: List[PushAction]
    conditions: List[PushCondition] = attr.ib(factory=lambda: [])
    pattern: Optional[str] = None