|
| 1 | +# Copyright 2023 The Matrix.org Foundation C.I.C |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +from enum import Enum |
| 17 | +from http import HTTPStatus |
| 18 | +from typing import TYPE_CHECKING, Dict, Tuple |
| 19 | + |
| 20 | +from synapse.api.errors import SynapseError |
| 21 | +from synapse.http.servlet import RestServlet, parse_json_object_from_request |
| 22 | +from synapse.http.site import SynapseRequest |
| 23 | +from synapse.rest.admin import admin_patterns, assert_requester_is_admin |
| 24 | +from synapse.types import JsonDict, UserID |
| 25 | + |
| 26 | +if TYPE_CHECKING: |
| 27 | + from synapse.server import HomeServer |
| 28 | + |
| 29 | + |
| 30 | +class ExperimentalFeature(str, Enum): |
| 31 | + """ |
| 32 | + Currently supported per-user features |
| 33 | + """ |
| 34 | + |
| 35 | + MSC3026 = "msc3026" |
| 36 | + MSC2654 = "msc2654" |
| 37 | + MSC3881 = "msc3881" |
| 38 | + MSC3967 = "msc3967" |
| 39 | + |
| 40 | + |
| 41 | +class ExperimentalFeaturesRestServlet(RestServlet): |
| 42 | + """ |
| 43 | + Enable or disable experimental features for a user or determine which features are enabled |
| 44 | + for a given user |
| 45 | + """ |
| 46 | + |
| 47 | + PATTERNS = admin_patterns("/experimental_features/(?P<user_id>[^/]*)") |
| 48 | + |
| 49 | + def __init__(self, hs: "HomeServer"): |
| 50 | + super().__init__() |
| 51 | + self.auth = hs.get_auth() |
| 52 | + self.store = hs.get_datastores().main |
| 53 | + self.is_mine = hs.is_mine |
| 54 | + |
| 55 | + async def on_GET( |
| 56 | + self, |
| 57 | + request: SynapseRequest, |
| 58 | + user_id: str, |
| 59 | + ) -> Tuple[int, JsonDict]: |
| 60 | + """ |
| 61 | + List which features are enabled for a given user |
| 62 | + """ |
| 63 | + await assert_requester_is_admin(self.auth, request) |
| 64 | + |
| 65 | + target_user = UserID.from_string(user_id) |
| 66 | + if not self.is_mine(target_user): |
| 67 | + raise SynapseError( |
| 68 | + HTTPStatus.BAD_REQUEST, |
| 69 | + "User must be local to check what experimental features are enabled.", |
| 70 | + ) |
| 71 | + |
| 72 | + enabled_features = await self.store.list_enabled_features(user_id) |
| 73 | + |
| 74 | + user_features = {} |
| 75 | + for feature in ExperimentalFeature: |
| 76 | + if feature in enabled_features: |
| 77 | + user_features[feature] = True |
| 78 | + else: |
| 79 | + user_features[feature] = False |
| 80 | + return HTTPStatus.OK, {"features": user_features} |
| 81 | + |
| 82 | + async def on_PUT( |
| 83 | + self, request: SynapseRequest, user_id: str |
| 84 | + ) -> Tuple[HTTPStatus, Dict]: |
| 85 | + """ |
| 86 | + Enable or disable the provided features for the requester |
| 87 | + """ |
| 88 | + await assert_requester_is_admin(self.auth, request) |
| 89 | + |
| 90 | + body = parse_json_object_from_request(request) |
| 91 | + |
| 92 | + target_user = UserID.from_string(user_id) |
| 93 | + if not self.is_mine(target_user): |
| 94 | + raise SynapseError( |
| 95 | + HTTPStatus.BAD_REQUEST, |
| 96 | + "User must be local to enable experimental features.", |
| 97 | + ) |
| 98 | + |
| 99 | + features = body.get("features") |
| 100 | + if not features: |
| 101 | + raise SynapseError( |
| 102 | + HTTPStatus.BAD_REQUEST, "You must provide features to set." |
| 103 | + ) |
| 104 | + |
| 105 | + # validate the provided features |
| 106 | + validated_features = {} |
| 107 | + for feature, enabled in features.items(): |
| 108 | + try: |
| 109 | + validated_feature = ExperimentalFeature(feature) |
| 110 | + validated_features[validated_feature] = enabled |
| 111 | + except ValueError: |
| 112 | + raise SynapseError( |
| 113 | + HTTPStatus.BAD_REQUEST, |
| 114 | + f"{feature!r} is not recognised as a valid experimental feature.", |
| 115 | + ) |
| 116 | + |
| 117 | + await self.store.set_features_for_user(user_id, validated_features) |
| 118 | + |
| 119 | + return HTTPStatus.OK, {} |
0 commit comments