12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
import collections .abc
15
- from typing import Iterable , Type , Union , cast
15
+ from typing import Iterable , List , Type , Union , cast
16
16
17
17
import jsonschema
18
+ from pydantic import Field , StrictBool , StrictStr
18
19
19
20
from synapse .api .constants import (
20
21
MAX_ALIAS_LENGTH ,
33
34
validate_canonicaljson ,
34
35
)
35
36
from synapse .federation .federation_server import server_matches_acl_event
37
+ from synapse .http .servlet import validate_json_object
38
+ from synapse .rest .models import RequestBodyModel
36
39
from synapse .types import EventID , JsonDict , RoomID , UserID
37
40
38
41
@@ -135,13 +138,9 @@ def validate_new(self, event: EventBase, config: HomeServerConfig) -> None:
135
138
EventContentFields .MSC3952_MENTIONS in event .content
136
139
and config .experimental .msc3952_intentional_mentions
137
140
):
138
- mentions = event .content [EventContentFields .MSC3952_MENTIONS ]
139
- try :
140
- jsonschema .validate (
141
- instance = mentions , schema = MENTIONS_SCHEMA , cls = MENTIONS_VALIDATOR
142
- )
143
- except jsonschema .ValidationError as e :
144
- raise SynapseError (400 , e .message )
141
+ validate_json_object (
142
+ event .content [EventContentFields .MSC3952_MENTIONS ], Mentions
143
+ )
145
144
146
145
def _validate_retention (self , event : EventBase ) -> None :
147
146
"""Checks that an event that defines the retention policy for a room respects the
@@ -270,20 +269,10 @@ def _ensure_state_event(self, event: Union[EventBase, EventBuilder]) -> None:
270
269
},
271
270
}
272
271
273
- MENTIONS_SCHEMA = {
274
- "type" : "object" ,
275
- "properties" : {
276
- "user_ids" : {
277
- "type" : "array" ,
278
- "items" : {
279
- "type" : "string" ,
280
- },
281
- },
282
- "room" : {
283
- "type" : "boolean" ,
284
- },
285
- },
286
- }
272
+
273
+ class Mentions (RequestBodyModel ):
274
+ user_ids : List [StrictStr ] = Field (default_factory = list )
275
+ room : StrictBool = False
287
276
288
277
289
278
# This could return something newer than Draft 7, but that's the current "latest"
@@ -302,4 +291,3 @@ def _create_validator(schema: JsonDict) -> Type[jsonschema.Draft7Validator]:
302
291
303
292
304
293
POWER_LEVELS_VALIDATOR = _create_validator (POWER_LEVELS_SCHEMA )
305
- MENTIONS_VALIDATOR = _create_validator (MENTIONS_SCHEMA )
0 commit comments