12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
import logging
15
- from typing import Any , Dict , List , Optional , Tuple , Union
15
+ from typing import TYPE_CHECKING , Any , Dict , List , Optional , Tuple , Union
16
16
17
17
import attr
18
18
from nacl .signing import SigningKey
19
19
20
- from synapse .api .auth import Auth
21
20
from synapse .api .constants import MAX_DEPTH
22
21
from synapse .api .errors import UnsupportedRoomVersionError
23
22
from synapse .api .room_versions import (
34
33
from synapse .util import Clock
35
34
from synapse .util .stringutils import random_string
36
35
36
+ if TYPE_CHECKING :
37
+ from synapse .api .auth import Auth
38
+ from synapse .server import HomeServer
39
+
37
40
logger = logging .getLogger (__name__ )
38
41
39
42
40
- @attr .s (slots = True , cmp = False , frozen = True )
43
+ @attr .s (slots = True , cmp = False , frozen = True , auto_attribs = True )
41
44
class EventBuilder :
42
45
"""A format independent event builder used to build up the event content
43
46
before signing the event.
@@ -62,31 +65,30 @@ class EventBuilder:
62
65
_signing_key: The signing key to use to sign the event as the server
63
66
"""
64
67
65
- _state = attr . ib ( type = StateHandler )
66
- _auth = attr . ib ( type = Auth )
67
- _store = attr . ib ( type = DataStore )
68
- _clock = attr . ib ( type = Clock )
69
- _hostname = attr . ib ( type = str )
70
- _signing_key = attr . ib ( type = SigningKey )
68
+ _state : StateHandler
69
+ _auth : " Auth"
70
+ _store : DataStore
71
+ _clock : Clock
72
+ _hostname : str
73
+ _signing_key : SigningKey
71
74
72
- room_version = attr . ib ( type = RoomVersion )
75
+ room_version : RoomVersion
73
76
74
- room_id = attr . ib ( type = str )
75
- type = attr . ib ( type = str )
76
- sender = attr . ib ( type = str )
77
+ room_id : str
78
+ type : str
79
+ sender : str
77
80
78
- content = attr .ib ( default = attr . Factory (dict ), type = JsonDict )
79
- unsigned = attr .ib ( default = attr . Factory (dict ), type = JsonDict )
81
+ content : JsonDict = attr .Factory (dict )
82
+ unsigned : JsonDict = attr .Factory (dict )
80
83
81
84
# These only exist on a subset of events, so they raise AttributeError if
82
85
# someone tries to get them when they don't exist.
83
- _state_key = attr . ib ( default = None , type = Optional [str ])
84
- _redacts = attr . ib ( default = None , type = Optional [str ])
85
- _origin_server_ts = attr . ib ( default = None , type = Optional [int ])
86
+ _state_key : Optional [str ] = None
87
+ _redacts : Optional [str ] = None
88
+ _origin_server_ts : Optional [int ] = None
86
89
87
- internal_metadata = attr .ib (
88
- default = attr .Factory (lambda : _EventInternalMetadata ({})),
89
- type = _EventInternalMetadata ,
90
+ internal_metadata : _EventInternalMetadata = attr .Factory (
91
+ lambda : _EventInternalMetadata ({})
90
92
)
91
93
92
94
@property
@@ -184,7 +186,7 @@ async def build(
184
186
185
187
186
188
class EventBuilderFactory :
187
- def __init__ (self , hs ):
189
+ def __init__ (self , hs : "HomeServer" ):
188
190
self .clock = hs .get_clock ()
189
191
self .hostname = hs .hostname
190
192
self .signing_key = hs .signing_key
@@ -193,15 +195,14 @@ def __init__(self, hs):
193
195
self .state = hs .get_state_handler ()
194
196
self .auth = hs .get_auth ()
195
197
196
- def new (self , room_version , key_values ) :
198
+ def new (self , room_version : str , key_values : dict ) -> EventBuilder :
197
199
"""Generate an event builder appropriate for the given room version
198
200
199
201
Deprecated: use for_room_version with a RoomVersion object instead
200
202
201
203
Args:
202
- room_version (str): Version of the room that we're creating an event builder
203
- for
204
- key_values (dict): Fields used as the basis of the new event
204
+ room_version: Version of the room that we're creating an event builder for
205
+ key_values: Fields used as the basis of the new event
205
206
206
207
Returns:
207
208
EventBuilder
@@ -212,13 +213,15 @@ def new(self, room_version, key_values):
212
213
raise UnsupportedRoomVersionError ()
213
214
return self .for_room_version (v , key_values )
214
215
215
- def for_room_version (self , room_version , key_values ):
216
+ def for_room_version (
217
+ self , room_version : RoomVersion , key_values : dict
218
+ ) -> EventBuilder :
216
219
"""Generate an event builder appropriate for the given room version
217
220
218
221
Args:
219
- room_version (synapse.api.room_versions.RoomVersion) :
222
+ room_version:
220
223
Version of the room that we're creating an event builder for
221
- key_values (dict) : Fields used as the basis of the new event
224
+ key_values: Fields used as the basis of the new event
222
225
223
226
Returns:
224
227
EventBuilder
@@ -286,15 +289,15 @@ def create_local_event_from_event_dict(
286
289
_event_id_counter = 0
287
290
288
291
289
- def _create_event_id (clock , hostname ) :
292
+ def _create_event_id (clock : Clock , hostname : str ) -> str :
290
293
"""Create a new event ID
291
294
292
295
Args:
293
- clock (Clock)
294
- hostname (str) : The server name for the event ID
296
+ clock
297
+ hostname: The server name for the event ID
295
298
296
299
Returns:
297
- str
300
+ The new event ID
298
301
"""
299
302
300
303
global _event_id_counter
0 commit comments