2020-07-01 04:08:25 -06:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2015-01-22 10:37:12 -07:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
from typing import TYPE_CHECKING, List, Optional, Sequence, Tuple, Union
|
|
|
|
|
|
|
|
import attr
|
|
|
|
|
2015-02-10 09:30:48 -07:00
|
|
|
from synapse.api.errors import (
|
2018-07-09 00:09:20 -06:00
|
|
|
NotFoundError,
|
|
|
|
StoreError,
|
|
|
|
SynapseError,
|
|
|
|
UnrecognizedRequestError,
|
2015-02-10 09:30:48 -07:00
|
|
|
)
|
2021-09-03 07:22:22 -06:00
|
|
|
from synapse.http.server import HttpServer
|
2019-06-03 05:28:59 -06:00
|
|
|
from synapse.http.servlet import (
|
|
|
|
RestServlet,
|
|
|
|
parse_json_value_from_request,
|
|
|
|
parse_string,
|
2019-06-20 03:32:02 -06:00
|
|
|
)
|
2021-09-03 07:22:22 -06:00
|
|
|
from synapse.http.site import SynapseRequest
|
2022-02-02 07:49:31 -07:00
|
|
|
from synapse.push.baserules import BASE_RULE_IDS
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.push.clientformat import format_push_rules_for_user
|
2016-03-03 09:11:59 -07:00
|
|
|
from synapse.push.rulekinds import PRIORITY_CLASS_MAP
|
2021-08-17 05:57:58 -06:00
|
|
|
from synapse.rest.client._base import client_patterns
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.storage.push_rule import InconsistentRuleException, RuleNotFoundException
|
2021-09-03 07:22:22 -06:00
|
|
|
from synapse.types import JsonDict
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
class RuleSpec:
|
|
|
|
scope: str
|
|
|
|
template: str
|
|
|
|
rule_id: str
|
|
|
|
attr: Optional[str]
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2019-06-03 05:28:59 -06:00
|
|
|
class PushRuleRestServlet(RestServlet):
|
|
|
|
PATTERNS = client_patterns("/(?P<path>pushrules/.*)$", v1=True)
|
2015-01-28 07:26:03 -07:00
|
|
|
SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR = (
|
|
|
|
"Unrecognised request: You probably wanted a trailing slash"
|
|
|
|
)
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__()
|
2019-06-03 05:28:59 -06:00
|
|
|
self.auth = hs.get_auth()
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2016-03-03 07:57:45 -07:00
|
|
|
self.notifier = hs.get_notifier()
|
2021-09-13 11:07:12 -06:00
|
|
|
self._is_worker = hs.config.worker.worker_app is not None
|
2016-03-03 07:57:45 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
async def on_PUT(self, request: SynapseRequest, path: str) -> Tuple[int, JsonDict]:
|
2019-04-15 11:40:15 -06:00
|
|
|
if self._is_worker:
|
|
|
|
raise Exception("Cannot handle PUT /push_rules on worker")
|
|
|
|
|
2020-02-21 05:15:07 -07:00
|
|
|
spec = _rule_spec_from_path(path.split("/"))
|
2015-01-22 10:37:12 -07:00
|
|
|
try:
|
2015-01-23 10:07:06 -07:00
|
|
|
priority_class = _priority_class_from_spec(spec)
|
2015-01-22 10:37:12 -07:00
|
|
|
except InvalidRuleException as e:
|
2018-09-12 04:41:31 -06:00
|
|
|
raise SynapseError(400, str(e))
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2019-12-05 08:53:06 -07:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
if "/" in spec.rule_id or "\\" in spec.rule_id:
|
2015-02-05 07:46:06 -07:00
|
|
|
raise SynapseError(400, "rule_id may not contain slashes")
|
2015-02-05 02:35:21 -07:00
|
|
|
|
2016-03-14 08:16:41 -06:00
|
|
|
content = parse_json_value_from_request(request)
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2016-03-03 07:57:45 -07:00
|
|
|
user_id = requester.user.to_string()
|
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
if spec.attr:
|
2019-12-05 08:53:06 -07:00
|
|
|
await self.set_rule_attr(user_id, spec, content)
|
2016-03-03 07:57:45 -07:00
|
|
|
self.notify_user(user_id)
|
2019-08-30 09:28:26 -06:00
|
|
|
return 200, {}
|
2015-02-26 11:07:44 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
if spec.rule_id.startswith("."):
|
2016-02-26 07:28:19 -07:00
|
|
|
# Rule ids starting with '.' are reserved for server default rules.
|
|
|
|
raise SynapseError(400, "cannot add new rule_ids that start with '.'")
|
|
|
|
|
2015-01-22 10:37:12 -07:00
|
|
|
try:
|
2015-02-05 02:35:21 -07:00
|
|
|
(conditions, actions) = _rule_tuple_from_request_object(
|
2021-09-03 07:22:22 -06:00
|
|
|
spec.template, spec.rule_id, content
|
2015-01-22 10:37:12 -07:00
|
|
|
)
|
|
|
|
except InvalidRuleException as e:
|
2018-09-12 04:41:31 -06:00
|
|
|
raise SynapseError(400, str(e))
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2018-07-13 13:40:14 -06:00
|
|
|
before = parse_string(request, "before")
|
2016-01-27 03:24:20 -07:00
|
|
|
if before:
|
2018-07-13 17:51:00 -06:00
|
|
|
before = _namespaced_rule_id(spec, before)
|
2016-01-27 03:24:20 -07:00
|
|
|
|
2018-07-13 13:40:14 -06:00
|
|
|
after = parse_string(request, "after")
|
2016-01-27 03:24:20 -07:00
|
|
|
if after:
|
2018-07-13 17:51:00 -06:00
|
|
|
after = _namespaced_rule_id(spec, after)
|
2015-01-22 10:37:12 -07:00
|
|
|
|
|
|
|
try:
|
2019-12-05 08:53:06 -07:00
|
|
|
await self.store.add_push_rule(
|
2016-03-03 07:57:45 -07:00
|
|
|
user_id=user_id,
|
2015-02-05 02:35:21 -07:00
|
|
|
rule_id=_namespaced_rule_id_from_spec(spec),
|
2015-01-22 10:37:12 -07:00
|
|
|
priority_class=priority_class,
|
|
|
|
conditions=conditions,
|
|
|
|
actions=actions,
|
|
|
|
before=before,
|
|
|
|
after=after,
|
|
|
|
)
|
2016-03-03 07:57:45 -07:00
|
|
|
self.notify_user(user_id)
|
2015-01-22 10:37:12 -07:00
|
|
|
except InconsistentRuleException as e:
|
2018-09-12 04:41:31 -06:00
|
|
|
raise SynapseError(400, str(e))
|
2015-01-23 03:28:25 -07:00
|
|
|
except RuleNotFoundException as e:
|
2018-09-12 04:41:31 -06:00
|
|
|
raise SynapseError(400, str(e))
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2019-08-30 09:28:26 -06:00
|
|
|
return 200, {}
|
2015-01-22 10:37:12 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
async def on_DELETE(
|
|
|
|
self, request: SynapseRequest, path: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-04-15 11:40:15 -06:00
|
|
|
if self._is_worker:
|
|
|
|
raise Exception("Cannot handle DELETE /push_rules on worker")
|
|
|
|
|
2020-02-21 05:15:07 -07:00
|
|
|
spec = _rule_spec_from_path(path.split("/"))
|
2015-01-23 10:49:37 -07:00
|
|
|
|
2019-12-05 08:53:06 -07:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2016-03-03 07:57:45 -07:00
|
|
|
user_id = requester.user.to_string()
|
2015-01-23 10:49:37 -07:00
|
|
|
|
2015-02-05 02:35:21 -07:00
|
|
|
namespaced_rule_id = _namespaced_rule_id_from_spec(spec)
|
2015-01-23 10:49:37 -07:00
|
|
|
|
2015-02-05 02:35:21 -07:00
|
|
|
try:
|
2019-12-05 08:53:06 -07:00
|
|
|
await self.store.delete_push_rule(user_id, namespaced_rule_id)
|
2016-03-03 07:57:45 -07:00
|
|
|
self.notify_user(user_id)
|
2019-08-30 09:28:26 -06:00
|
|
|
return 200, {}
|
2015-02-05 02:35:21 -07:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError()
|
|
|
|
else:
|
|
|
|
raise
|
2015-01-23 10:49:37 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
async def on_GET(self, request: SynapseRequest, path: str) -> Tuple[int, JsonDict]:
|
2019-12-05 08:53:06 -07:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2016-03-03 07:57:45 -07:00
|
|
|
user_id = requester.user.to_string()
|
2015-01-22 12:32:17 -07:00
|
|
|
|
|
|
|
# we build up the full structure and then decide which bits of it
|
|
|
|
# to send which means doing unnecessary work sometimes but is
|
|
|
|
# is probably not going to make a whole lot of difference
|
2019-12-05 08:53:06 -07:00
|
|
|
rules = await self.store.get_push_rules_for_user(user_id)
|
2015-02-05 07:46:06 -07:00
|
|
|
|
2016-06-01 07:27:07 -06:00
|
|
|
rules = format_push_rules_for_user(requester.user, rules)
|
2015-01-22 12:32:17 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
path_parts = path.split("/")[1:]
|
2015-01-23 04:19:02 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
if path_parts == []:
|
2015-01-23 04:19:02 -07:00
|
|
|
# we're a reference impl: pedantry is our job.
|
2015-01-29 09:10:35 -07:00
|
|
|
raise UnrecognizedRequestError(
|
|
|
|
PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
|
|
|
|
)
|
2015-01-22 12:32:17 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
if path_parts[0] == "":
|
2019-08-30 09:28:26 -06:00
|
|
|
return 200, rules
|
2021-09-03 07:22:22 -06:00
|
|
|
elif path_parts[0] == "global":
|
|
|
|
result = _filter_ruleset_with_path(rules["global"], path_parts[1:])
|
2019-08-30 09:28:26 -06:00
|
|
|
return 200, result
|
2015-01-22 12:32:17 -07:00
|
|
|
else:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
def notify_user(self, user_id: str) -> None:
|
2020-08-19 06:41:51 -06:00
|
|
|
stream_id = self.store.get_max_push_rules_stream_id()
|
2016-03-03 07:57:45 -07:00
|
|
|
self.notifier.on_new_event("push_rules_key", stream_id, users=[user_id])
|
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
async def set_rule_attr(
|
|
|
|
self, user_id: str, spec: RuleSpec, val: Union[bool, JsonDict]
|
|
|
|
) -> None:
|
|
|
|
if spec.attr not in ("enabled", "actions"):
|
2020-09-09 04:39:39 -06:00
|
|
|
# for the sake of potential future expansion, shouldn't report
|
|
|
|
# 404 in the case of an unknown request so check it corresponds to
|
|
|
|
# a known attribute first.
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
|
|
|
namespaced_rule_id = _namespaced_rule_id_from_spec(spec)
|
2021-09-03 07:22:22 -06:00
|
|
|
rule_id = spec.rule_id
|
2020-09-09 04:39:39 -06:00
|
|
|
is_default_rule = rule_id.startswith(".")
|
|
|
|
if is_default_rule:
|
|
|
|
if namespaced_rule_id not in BASE_RULE_IDS:
|
|
|
|
raise NotFoundError("Unknown rule %s" % (namespaced_rule_id,))
|
2021-09-03 07:22:22 -06:00
|
|
|
if spec.attr == "enabled":
|
2015-12-07 03:44:33 -07:00
|
|
|
if isinstance(val, dict) and "enabled" in val:
|
|
|
|
val = val["enabled"]
|
2015-02-26 11:07:44 -07:00
|
|
|
if not isinstance(val, bool):
|
2015-12-07 03:44:33 -07:00
|
|
|
# Legacy fallback
|
|
|
|
# This should *actually* take a dict, but many clients pass
|
|
|
|
# bools directly, so let's not break them.
|
2015-02-26 11:07:44 -07:00
|
|
|
raise SynapseError(400, "Value for 'enabled' must be boolean")
|
2021-09-03 07:22:22 -06:00
|
|
|
await self.store.set_push_rule_enabled(
|
2020-09-09 04:39:39 -06:00
|
|
|
user_id, namespaced_rule_id, val, is_default_rule
|
2020-08-14 08:30:16 -06:00
|
|
|
)
|
2021-09-03 07:22:22 -06:00
|
|
|
elif spec.attr == "actions":
|
|
|
|
if not isinstance(val, dict):
|
|
|
|
raise SynapseError(400, "Value must be a dict")
|
2016-02-26 07:28:19 -07:00
|
|
|
actions = val.get("actions")
|
2021-09-03 07:22:22 -06:00
|
|
|
if not isinstance(actions, list):
|
|
|
|
raise SynapseError(400, "Value for 'actions' must be dict")
|
2016-02-26 07:28:19 -07:00
|
|
|
_check_actions(actions)
|
|
|
|
namespaced_rule_id = _namespaced_rule_id_from_spec(spec)
|
2021-09-03 07:22:22 -06:00
|
|
|
rule_id = spec.rule_id
|
2016-02-26 07:28:19 -07:00
|
|
|
is_default_rule = rule_id.startswith(".")
|
|
|
|
if is_default_rule:
|
2022-02-02 07:49:31 -07:00
|
|
|
if namespaced_rule_id not in BASE_RULE_IDS:
|
2016-02-26 07:28:19 -07:00
|
|
|
raise SynapseError(404, "Unknown rule %r" % (namespaced_rule_id,))
|
2021-09-03 07:22:22 -06:00
|
|
|
await self.store.set_push_rule_actions(
|
2016-02-26 07:28:19 -07:00
|
|
|
user_id, namespaced_rule_id, actions, is_default_rule
|
|
|
|
)
|
2015-02-26 11:07:44 -07:00
|
|
|
else:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
2015-03-02 08:58:12 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
def _rule_spec_from_path(path: Sequence[str]) -> RuleSpec:
|
2018-12-04 04:44:02 -07:00
|
|
|
"""Turn a sequence of path components into a rule spec
|
|
|
|
|
|
|
|
Args:
|
2021-09-03 07:22:22 -06:00
|
|
|
path: the URL path components.
|
2018-12-04 04:44:02 -07:00
|
|
|
|
|
|
|
Returns:
|
2021-09-03 07:22:22 -06:00
|
|
|
rule spec, containing scope/template/rule_id entries, and possibly attr.
|
2018-12-04 04:44:02 -07:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
UnrecognizedRequestError if the path components cannot be parsed.
|
|
|
|
"""
|
2015-02-05 02:35:21 -07:00
|
|
|
if len(path) < 2:
|
|
|
|
raise UnrecognizedRequestError()
|
2018-12-04 04:44:02 -07:00
|
|
|
if path[0] != "pushrules":
|
2015-02-05 02:35:21 -07:00
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
2018-12-04 04:44:02 -07:00
|
|
|
scope = path[1]
|
2015-02-05 02:35:21 -07:00
|
|
|
path = path[2:]
|
2016-02-18 09:05:13 -07:00
|
|
|
if scope != "global":
|
2015-02-05 02:35:21 -07:00
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
|
|
|
if len(path) == 0:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
2018-12-04 04:44:02 -07:00
|
|
|
template = path[0]
|
2015-02-05 02:35:21 -07:00
|
|
|
path = path[1:]
|
|
|
|
|
2015-02-25 07:02:38 -07:00
|
|
|
if len(path) == 0 or len(path[0]) == 0:
|
2015-02-05 02:35:21 -07:00
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
2018-12-04 04:44:02 -07:00
|
|
|
rule_id = path[0]
|
2015-02-05 02:35:21 -07:00
|
|
|
|
2015-02-26 11:07:44 -07:00
|
|
|
path = path[1:]
|
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
attr = None
|
2015-02-26 11:07:44 -07:00
|
|
|
if len(path) > 0 and len(path[0]) > 0:
|
2021-09-03 07:22:22 -06:00
|
|
|
attr = path[0]
|
2015-02-26 11:07:44 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
return RuleSpec(scope, template, rule_id, attr)
|
2015-02-05 02:35:21 -07:00
|
|
|
|
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
def _rule_tuple_from_request_object(
|
|
|
|
rule_template: str, rule_id: str, req_obj: JsonDict
|
|
|
|
) -> Tuple[List[JsonDict], List[Union[str, JsonDict]]]:
|
2015-02-05 02:35:21 -07:00
|
|
|
if rule_template in ["override", "underride"]:
|
|
|
|
if "conditions" not in req_obj:
|
|
|
|
raise InvalidRuleException("Missing 'conditions'")
|
|
|
|
conditions = req_obj["conditions"]
|
|
|
|
for c in conditions:
|
|
|
|
if "kind" not in c:
|
|
|
|
raise InvalidRuleException("Condition without 'kind'")
|
|
|
|
elif rule_template == "room":
|
|
|
|
conditions = [{"kind": "event_match", "key": "room_id", "pattern": rule_id}]
|
|
|
|
elif rule_template == "sender":
|
|
|
|
conditions = [{"kind": "event_match", "key": "user_id", "pattern": rule_id}]
|
|
|
|
elif rule_template == "content":
|
|
|
|
if "pattern" not in req_obj:
|
|
|
|
raise InvalidRuleException("Content rule missing 'pattern'")
|
|
|
|
pat = req_obj["pattern"]
|
|
|
|
|
|
|
|
conditions = [{"kind": "event_match", "key": "content.body", "pattern": pat}]
|
|
|
|
else:
|
|
|
|
raise InvalidRuleException("Unknown rule template: %s" % (rule_template,))
|
|
|
|
|
|
|
|
if "actions" not in req_obj:
|
|
|
|
raise InvalidRuleException("No actions found")
|
|
|
|
actions = req_obj["actions"]
|
|
|
|
|
2016-02-26 07:28:19 -07:00
|
|
|
_check_actions(actions)
|
|
|
|
|
|
|
|
return conditions, actions
|
|
|
|
|
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
def _check_actions(actions: List[Union[str, JsonDict]]) -> None:
|
2016-02-26 07:28:19 -07:00
|
|
|
if not isinstance(actions, list):
|
|
|
|
raise InvalidRuleException("No actions found")
|
|
|
|
|
2015-02-05 02:35:21 -07:00
|
|
|
for a in actions:
|
2020-07-01 04:08:25 -06:00
|
|
|
if a in ["notify", "dont_notify", "coalesce"]:
|
2015-02-05 02:35:21 -07:00
|
|
|
pass
|
2015-02-27 11:38:56 -07:00
|
|
|
elif isinstance(a, dict) and "set_tweak" in a:
|
2015-02-05 02:35:21 -07:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise InvalidRuleException("Unrecognised action")
|
|
|
|
|
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
def _filter_ruleset_with_path(ruleset: JsonDict, path: List[str]) -> JsonDict:
|
2015-01-22 12:32:17 -07:00
|
|
|
if path == []:
|
2015-01-29 09:10:35 -07:00
|
|
|
raise UnrecognizedRequestError(
|
|
|
|
PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
|
|
|
|
)
|
2015-01-23 04:19:02 -07:00
|
|
|
|
|
|
|
if path[0] == "":
|
2015-01-22 12:32:17 -07:00
|
|
|
return ruleset
|
|
|
|
template_kind = path[0]
|
|
|
|
if template_kind not in ruleset:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
path = path[1:]
|
|
|
|
if path == []:
|
2015-01-29 09:10:35 -07:00
|
|
|
raise UnrecognizedRequestError(
|
|
|
|
PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
|
|
|
|
)
|
2015-01-23 04:19:02 -07:00
|
|
|
if path[0] == "":
|
2015-01-22 12:32:17 -07:00
|
|
|
return ruleset[template_kind]
|
|
|
|
rule_id = path[0]
|
2015-02-26 11:07:44 -07:00
|
|
|
|
|
|
|
the_rule = None
|
2015-01-22 12:32:17 -07:00
|
|
|
for r in ruleset[template_kind]:
|
|
|
|
if r["rule_id"] == rule_id:
|
2015-02-26 11:07:44 -07:00
|
|
|
the_rule = r
|
|
|
|
if the_rule is None:
|
2021-09-03 07:22:22 -06:00
|
|
|
raise NotFoundError()
|
2015-02-26 11:07:44 -07:00
|
|
|
|
|
|
|
path = path[1:]
|
|
|
|
if len(path) == 0:
|
|
|
|
return the_rule
|
|
|
|
|
|
|
|
attr = path[0]
|
|
|
|
if attr in the_rule:
|
2016-02-25 08:13:07 -07:00
|
|
|
# Make sure we return a JSON object as the attribute may be a
|
|
|
|
# JSON value.
|
|
|
|
return {attr: the_rule[attr]}
|
2015-02-26 11:07:44 -07:00
|
|
|
else:
|
|
|
|
raise UnrecognizedRequestError()
|
2015-01-22 12:32:17 -07:00
|
|
|
|
2015-01-29 09:10:35 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
def _priority_class_from_spec(spec: RuleSpec) -> int:
|
|
|
|
if spec.template not in PRIORITY_CLASS_MAP.keys():
|
|
|
|
raise InvalidRuleException("Unknown template: %s" % (spec.template))
|
|
|
|
pc = PRIORITY_CLASS_MAP[spec.template]
|
2015-01-23 10:07:06 -07:00
|
|
|
|
|
|
|
return pc
|
|
|
|
|
2015-01-29 09:10:35 -07:00
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
def _namespaced_rule_id_from_spec(spec: RuleSpec) -> str:
|
|
|
|
return _namespaced_rule_id(spec, spec.rule_id)
|
2016-01-27 03:24:20 -07:00
|
|
|
|
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
def _namespaced_rule_id(spec: RuleSpec, rule_id: str) -> str:
|
|
|
|
return "global/%s/%s" % (spec.template, rule_id)
|
2015-02-05 02:35:21 -07:00
|
|
|
|
|
|
|
|
2015-01-22 10:37:12 -07:00
|
|
|
class InvalidRuleException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-09-03 07:22:22 -06:00
|
|
|
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
|
2015-01-22 10:37:12 -07:00
|
|
|
PushRuleRestServlet(hs).register(http_server)
|