Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge commit 'dc22090a6' into anoa/dinsic_release_1_21_x
Browse files Browse the repository at this point in the history
* commit 'dc22090a6':
  Add type hints to synapse.handlers.room (#8090)
  Remove some unused database functions. (#8085)
  Convert misc database code to async (#8087)
  Remove a space at the start of a changelog entry.
  • Loading branch information
anoadragon453 committed Oct 19, 2020
2 parents 1b86e62 + dc22090 commit 8766a07
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 348 deletions.
2 changes: 1 addition & 1 deletion changelog.d/8072.misc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Convert various parts of the codebase to async/await.
Convert various parts of the codebase to async/await.
1 change: 1 addition & 0 deletions changelog.d/8085.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove some unused database functions.
1 change: 1 addition & 0 deletions changelog.d/8087.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert various parts of the codebase to async/await.
1 change: 1 addition & 0 deletions changelog.d/8090.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type hints to `synapse.handlers.room`.
104 changes: 66 additions & 38 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import math
import string
from collections import OrderedDict
from typing import Awaitable, Optional, Tuple
from typing import TYPE_CHECKING, Any, Awaitable, Dict, List, Optional, Tuple

from synapse.api.constants import (
EventTypes,
Expand All @@ -32,11 +32,14 @@
RoomEncryptionAlgorithms,
)
from synapse.api.errors import AuthError, Codes, NotFoundError, StoreError, SynapseError
from synapse.api.filtering import Filter
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion
from synapse.events import EventBase
from synapse.events.utils import copy_power_levels_contents
from synapse.http.endpoint import parse_and_validate_server_name
from synapse.storage.state import StateFilter
from synapse.types import (
JsonDict,
Requester,
RoomAlias,
RoomID,
Expand All @@ -53,6 +56,9 @@

from ._base import BaseHandler

if TYPE_CHECKING:
from synapse.server import HomeServer

logger = logging.getLogger(__name__)

id_server_scheme = "https://"
Expand All @@ -61,7 +67,7 @@


class RoomCreationHandler(BaseHandler):
def __init__(self, hs):
def __init__(self, hs: "HomeServer"):
super(RoomCreationHandler, self).__init__(hs)

self.spam_checker = hs.get_spam_checker()
Expand Down Expand Up @@ -92,7 +98,7 @@ def __init__(self, hs):
"guest_can_join": False,
"power_level_content_override": {},
},
}
} # type: Dict[str, Dict[str, Any]]

# Modify presets to selectively enable encryption by default per homeserver config
for preset_name, preset_config in self._presets_dict.items():
Expand Down Expand Up @@ -215,6 +221,9 @@ async def _upgrade_room(

old_room_state = await tombstone_context.get_current_state_ids()

# We know the tombstone event isn't an outlier so it has current state.
assert old_room_state is not None

# update any aliases
await self._move_aliases_to_new_room(
requester, old_room_id, new_room_id, old_room_state
Expand Down Expand Up @@ -540,17 +549,21 @@ async def _move_aliases_to_new_room(
logger.error("Unable to send updated alias events in new room: %s", e)

async def create_room(
self, requester, config, ratelimit=True, creator_join_profile=None
self,
requester: Requester,
config: JsonDict,
ratelimit: bool = True,
creator_join_profile: Optional[JsonDict] = None,
) -> Tuple[dict, int]:
""" Creates a new room.
Args:
requester (synapse.types.Requester):
requester:
The user who requested the room creation.
config (dict) : A dict of configuration options.
ratelimit (bool): set to False to disable the rate limiter
config : A dict of configuration options.
ratelimit: set to False to disable the rate limiter
creator_join_profile (dict|None):
creator_join_profile:
Set to override the displayname and avatar for the creating
user in this room. If unset, displayname and avatar will be
derived from the user's profile. If set, should contain the
Expand Down Expand Up @@ -619,6 +632,7 @@ async def create_room(
Codes.UNSUPPORTED_ROOM_VERSION,
)

room_alias = None
if "room_alias_name" in config:
for wchar in string.whitespace:
if wchar in config["room_alias_name"]:
Expand All @@ -629,8 +643,6 @@ async def create_room(

if mapping:
raise SynapseError(400, "Room alias already taken", Codes.ROOM_IN_USE)
else:
room_alias = None

for i in invite_list:
try:
Expand Down Expand Up @@ -797,31 +809,38 @@ async def create_room(

async def _send_events_for_new_room(
self,
creator, # A Requester object.
room_id,
preset_config,
invite_list,
initial_state,
creation_content,
room_alias=None,
power_level_content_override=None, # Doesn't apply when initial state has power level state event content
creator_join_profile=None,
creator: Requester,
room_id: str,
preset_config: str,
invite_list: List[str],
initial_state: StateMap,
creation_content: JsonDict,
room_alias: Optional[RoomAlias] = None,
power_level_content_override: Optional[JsonDict] = None,
creator_join_profile: Optional[JsonDict] = None,
) -> int:
"""Sends the initial events into a new room.
`power_level_content_override` doesn't apply when initial state has
power level state event content.
Returns:
The stream_id of the last event persisted.
"""

def create(etype, content, **kwargs):
creator_id = creator.user.to_string()

event_keys = {"room_id": room_id, "sender": creator_id, "state_key": ""}

def create(etype: str, content: JsonDict, **kwargs) -> JsonDict:
e = {"type": etype, "content": content}

e.update(event_keys)
e.update(kwargs)

return e

async def send(etype, content, **kwargs) -> int:
async def send(etype: str, content: JsonDict, **kwargs) -> int:
event = create(etype, content, **kwargs)
logger.debug("Sending %s in new room", etype)
(
Expand All @@ -834,10 +853,6 @@ async def send(etype, content, **kwargs) -> int:

config = self._presets_dict[preset_config]

creator_id = creator.user.to_string()

event_keys = {"room_id": room_id, "sender": creator_id, "state_key": ""}

creation_content.update({"creator": creator_id})
await send(etype=EventTypes.Create, content=creation_content)

Expand Down Expand Up @@ -879,7 +894,7 @@ async def send(etype, content, **kwargs) -> int:
"kick": 50,
"redact": 50,
"invite": 50,
}
} # type: JsonDict

if config["original_invitees_have_ops"]:
for invitee in invite_list:
Expand Down Expand Up @@ -933,7 +948,7 @@ async def send(etype, content, **kwargs) -> int:
return last_sent_stream_id

async def _generate_room_id(
self, creator_id: str, is_public: str, room_version: RoomVersion,
self, creator_id: str, is_public: bool, room_version: RoomVersion,
):
# autogen room IDs and try to create it. We may clash, so just
# try a few times till one goes through, giving up eventually.
Expand All @@ -957,23 +972,30 @@ async def _generate_room_id(


class RoomContextHandler(object):
def __init__(self, hs):
def __init__(self, hs: "HomeServer"):
self.hs = hs
self.store = hs.get_datastore()
self.storage = hs.get_storage()
self.state_store = self.storage.state

async def get_event_context(self, user, room_id, event_id, limit, event_filter):
async def get_event_context(
self,
user: UserID,
room_id: str,
event_id: str,
limit: int,
event_filter: Optional[Filter],
) -> Optional[JsonDict]:
"""Retrieves events, pagination tokens and state around a given event
in a room.
Args:
user (UserID)
room_id (str)
event_id (str)
limit (int): The maximum number of events to return in total
user
room_id
event_id
limit: The maximum number of events to return in total
(excluding state).
event_filter (Filter|None): the filter to apply to the events returned
event_filter: the filter to apply to the events returned
(excluding the target event_id)
Returns:
Expand Down Expand Up @@ -1060,12 +1082,18 @@ def filter_evts(events):


class RoomEventSource(object):
def __init__(self, hs):
def __init__(self, hs: "HomeServer"):
self.store = hs.get_datastore()

async def get_new_events(
self, user, from_key, limit, room_ids, is_guest, explicit_room_id=None
):
self,
user: UserID,
from_key: str,
limit: int,
room_ids: List[str],
is_guest: bool,
explicit_room_id: Optional[str] = None,
) -> Tuple[List[EventBase], str]:
# We just ignore the key for now.

to_key = self.get_current_key()
Expand Down Expand Up @@ -1123,7 +1151,7 @@ class RoomShutdownHandler(object):
)
DEFAULT_ROOM_NAME = "Content Violation Notification"

def __init__(self, hs):
def __init__(self, hs: "HomeServer"):
self.hs = hs
self.room_member_handler = hs.get_room_member_handler()
self._room_creation_handler = hs.get_room_creation_handler()
Expand Down
14 changes: 5 additions & 9 deletions synapse/storage/background_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

from canonicaljson import json

from twisted.internet import defer

from synapse.metrics.background_process_metrics import run_as_background_process

from . import engines
Expand Down Expand Up @@ -308,9 +306,8 @@ def register_noop_background_update(self, update_name):
update_name (str): Name of update
"""

@defer.inlineCallbacks
def noop_update(progress, batch_size):
yield self._end_background_update(update_name)
async def noop_update(progress, batch_size):
await self._end_background_update(update_name)
return 1

self.register_background_update_handler(update_name, noop_update)
Expand Down Expand Up @@ -409,12 +406,11 @@ def create_index_sqlite(conn):
else:
runner = create_index_sqlite

@defer.inlineCallbacks
def updater(progress, batch_size):
async def updater(progress, batch_size):
if runner is not None:
logger.info("Adding index %s to %s", index_name, table)
yield self.db_pool.runWithConnection(runner)
yield self._end_background_update(update_name)
await self.db_pool.runWithConnection(runner)
await self._end_background_update(update_name)
return 1

self.register_background_update_handler(update_name, updater)
Expand Down
5 changes: 2 additions & 3 deletions synapse/storage/databases/main/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,9 @@ def get_device_list_last_stream_id_for_remote(self, user_id: str):
@cachedList(
cached_method_name="get_device_list_last_stream_id_for_remote",
list_name="user_ids",
inlineCallbacks=True,
)
def get_device_list_last_stream_id_for_remotes(self, user_ids: str):
rows = yield self.db_pool.simple_select_many_batch(
async def get_device_list_last_stream_id_for_remotes(self, user_ids: str):
rows = await self.db_pool.simple_select_many_batch(
table="device_lists_remote_extremeties",
column="user_id",
iterable=user_ids,
Expand Down
13 changes: 0 additions & 13 deletions synapse/storage/databases/main/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,6 @@ def _get_auth_chain_difference_txn(
# Return all events where not all sets can reach them.
return {eid for eid, n in event_to_missing_sets.items() if n}

def get_oldest_events_in_room(self, room_id):
return self.db_pool.runInteraction(
"get_oldest_events_in_room", self._get_oldest_events_in_room_txn, room_id
)

def get_oldest_events_with_depth_in_room(self, room_id):
return self.db_pool.runInteraction(
"get_oldest_events_with_depth_in_room",
Expand Down Expand Up @@ -303,14 +298,6 @@ async def get_max_depth_of(self, event_ids: List[str]) -> int:
else:
return max(row["depth"] for row in rows)

def _get_oldest_events_in_room_txn(self, txn, room_id):
return self.db_pool.simple_select_onecol_txn(
txn,
table="event_backward_extremities",
keyvalues={"room_id": room_id},
retcol="event_id",
)

def get_prev_events_for_room(self, room_id: str):
"""
Gets a subset of the current forward extremities in the given room.
Expand Down
9 changes: 4 additions & 5 deletions synapse/storage/databases/main/event_push_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from synapse.storage._base import LoggingTransaction, SQLBaseStore, db_to_json
from synapse.storage.database import DatabasePool
from synapse.util import json_encoder
from synapse.util.caches.descriptors import cachedInlineCallbacks
from synapse.util.caches.descriptors import cached

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -86,18 +86,17 @@ def __init__(self, database: DatabasePool, db_conn, hs):
self._rotate_delay = 3
self._rotate_count = 10000

@cachedInlineCallbacks(num_args=3, tree=True, max_entries=5000)
def get_unread_event_push_actions_by_room_for_user(
@cached(num_args=3, tree=True, max_entries=5000)
async def get_unread_event_push_actions_by_room_for_user(
self, room_id, user_id, last_read_event_id
):
ret = yield self.db_pool.runInteraction(
return await self.db_pool.runInteraction(
"get_unread_event_push_actions_by_room",
self._get_unread_counts_by_receipt_txn,
room_id,
user_id,
last_read_event_id,
)
return ret

def _get_unread_counts_by_receipt_txn(
self, txn, room_id, user_id, last_read_event_id
Expand Down
Loading

0 comments on commit 8766a07

Please sign in to comment.