1212# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313# See the License for the specific language governing permissions and
1414# limitations under the License.
15- from typing import Optional , Union
15+ from typing import TYPE_CHECKING , Optional , Union
1616
1717import attr
1818from frozendict import frozendict
1919
20- from twisted .internet import defer
21-
2220from synapse .appservice import ApplicationService
21+ from synapse .events import EventBase
2322from synapse .logging .context import make_deferred_yieldable , run_in_background
2423from synapse .types import StateMap
2524
25+ if TYPE_CHECKING :
26+ from synapse .storage .data_stores .main import DataStore
27+
2628
2729@attr .s (slots = True )
2830class EventContext :
@@ -129,8 +131,7 @@ def with_state(
129131 delta_ids = delta_ids ,
130132 )
131133
132- @defer .inlineCallbacks
133- def serialize (self , event , store ):
134+ async def serialize (self , event : EventBase , store : "DataStore" ) -> dict :
134135 """Converts self to a type that can be serialized as JSON, and then
135136 deserialized by `deserialize`
136137
@@ -146,7 +147,7 @@ def serialize(self, event, store):
146147 # the prev_state_ids, so if we're a state event we include the event
147148 # id that we replaced in the state.
148149 if event .is_state ():
149- prev_state_ids = yield self .get_prev_state_ids ()
150+ prev_state_ids = await self .get_prev_state_ids ()
150151 prev_state_id = prev_state_ids .get ((event .type , event .state_key ))
151152 else :
152153 prev_state_id = None
@@ -214,8 +215,7 @@ def state_group(self) -> Optional[int]:
214215
215216 return self ._state_group
216217
217- @defer .inlineCallbacks
218- def get_current_state_ids (self ):
218+ async def get_current_state_ids (self ) -> Optional [StateMap [str ]]:
219219 """
220220 Gets the room state map, including this event - ie, the state in ``state_group``
221221
@@ -224,32 +224,31 @@ def get_current_state_ids(self):
224224 ``rejected`` is set.
225225
226226 Returns:
227- Deferred[dict[(str, str), str]|None]: Returns None if state_group
228- is None, which happens when the associated event is an outlier.
227+ Returns None if state_group is None, which happens when the associated
228+ event is an outlier.
229229
230- Maps a (type, state_key) to the event ID of the state event matching
231- this tuple.
230+ Maps a (type, state_key) to the event ID of the state event matching
231+ this tuple.
232232 """
233233 if self .rejected :
234234 raise RuntimeError ("Attempt to access state_ids of rejected event" )
235235
236- yield self ._ensure_fetched ()
236+ await self ._ensure_fetched ()
237237 return self ._current_state_ids
238238
239- @defer .inlineCallbacks
240- def get_prev_state_ids (self ):
239+ async def get_prev_state_ids (self ):
241240 """
242241 Gets the room state map, excluding this event.
243242
244243 For a non-state event, this will be the same as get_current_state_ids().
245244
246245 Returns:
247- Deferred[ dict[(str, str), str]|None] : Returns None if state_group
246+ dict[(str, str), str]|None: Returns None if state_group
248247 is None, which happens when the associated event is an outlier.
249248 Maps a (type, state_key) to the event ID of the state event matching
250249 this tuple.
251250 """
252- yield self ._ensure_fetched ()
251+ await self ._ensure_fetched ()
253252 return self ._prev_state_ids
254253
255254 def get_cached_current_state_ids (self ):
@@ -269,8 +268,8 @@ def get_cached_current_state_ids(self):
269268
270269 return self ._current_state_ids
271270
272- def _ensure_fetched (self ):
273- return defer . succeed ( None )
271+ async def _ensure_fetched (self ):
272+ return None
274273
275274
276275@attr .s (slots = True )
@@ -303,21 +302,20 @@ class _AsyncEventContextImpl(EventContext):
303302 _event_state_key = attr .ib (default = None )
304303 _fetching_state_deferred = attr .ib (default = None )
305304
306- def _ensure_fetched (self ):
305+ async def _ensure_fetched (self ):
307306 if not self ._fetching_state_deferred :
308307 self ._fetching_state_deferred = run_in_background (self ._fill_out_state )
309308
310- return make_deferred_yieldable (self ._fetching_state_deferred )
309+ return await make_deferred_yieldable (self ._fetching_state_deferred )
311310
312- @defer .inlineCallbacks
313- def _fill_out_state (self ):
311+ async def _fill_out_state (self ):
314312 """Called to populate the _current_state_ids and _prev_state_ids
315313 attributes by loading from the database.
316314 """
317315 if self .state_group is None :
318316 return
319317
320- self ._current_state_ids = yield self ._storage .state .get_state_ids_for_group (
318+ self ._current_state_ids = await self ._storage .state .get_state_ids_for_group (
321319 self .state_group
322320 )
323321 if self ._event_state_key is not None :
0 commit comments