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

Commit ab298b0

Browse files
committed
Merge commit 'd1008fe94' into anoa/dinsic_release_1_21_x
* commit 'd1008fe94': Fix some comments and types in service notices (#7996)
2 parents 479fc9d + d1008fe commit ab298b0

File tree

7 files changed

+56
-59
lines changed

7 files changed

+56
-59
lines changed

changelog.d/7996.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix various comments and minor discrepencies in server notices code.

synapse/server_notices/consent_server_notices.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import logging
16+
from typing import Any
1617

1718
from synapse.api.errors import SynapseError
1819
from synapse.api.urls import ConsentURIBuilder
@@ -55,14 +56,11 @@ def __init__(self, hs):
5556

5657
self._consent_uri_builder = ConsentURIBuilder(hs.config)
5758

58-
async def maybe_send_server_notice_to_user(self, user_id):
59+
async def maybe_send_server_notice_to_user(self, user_id: str) -> None:
5960
"""Check if we need to send a notice to this user, and does so if so
6061
6162
Args:
62-
user_id (str): user to check
63-
64-
Returns:
65-
Deferred
63+
user_id: user to check
6664
"""
6765
if self._server_notice_content is None:
6866
# not enabled
@@ -105,7 +103,7 @@ async def maybe_send_server_notice_to_user(self, user_id):
105103
self._users_in_progress.remove(user_id)
106104

107105

108-
def copy_with_str_subst(x, substitutions):
106+
def copy_with_str_subst(x: Any, substitutions: Any) -> Any:
109107
"""Deep-copy a structure, carrying out string substitions on any strings
110108
111109
Args:
@@ -121,7 +119,7 @@ def copy_with_str_subst(x, substitutions):
121119
if isinstance(x, dict):
122120
return {k: copy_with_str_subst(v, substitutions) for (k, v) in x.items()}
123121
if isinstance(x, (list, tuple)):
124-
return [copy_with_str_subst(y) for y in x]
122+
return [copy_with_str_subst(y, substitutions) for y in x]
125123

126124
# assume it's uninterested and can be shallow-copied.
127125
return x

synapse/server_notices/resource_limits_server_notices.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import logging
16+
from typing import List, Tuple
1617

1718
from synapse.api.constants import (
1819
EventTypes,
@@ -52,18 +53,15 @@ def __init__(self, hs):
5253
and not hs.config.hs_disabled
5354
)
5455

55-
async def maybe_send_server_notice_to_user(self, user_id):
56+
async def maybe_send_server_notice_to_user(self, user_id: str) -> None:
5657
"""Check if we need to send a notice to this user, this will be true in
5758
two cases.
5859
1. The server has reached its limit does not reflect this
5960
2. The room state indicates that the server has reached its limit when
6061
actually the server is fine
6162
6263
Args:
63-
user_id (str): user to check
64-
65-
Returns:
66-
Deferred
64+
user_id: user to check
6765
"""
6866
if not self._enabled:
6967
return
@@ -115,36 +113,38 @@ async def maybe_send_server_notice_to_user(self, user_id):
115113
elif not currently_blocked and limit_msg:
116114
# Room is not notifying of a block, when it ought to be.
117115
await self._apply_limit_block_notification(
118-
user_id, limit_msg, limit_type
116+
user_id, limit_msg, limit_type # type: ignore
119117
)
120118
except SynapseError as e:
121119
logger.error("Error sending resource limits server notice: %s", e)
122120

123-
async def _remove_limit_block_notification(self, user_id, ref_events):
121+
async def _remove_limit_block_notification(
122+
self, user_id: str, ref_events: List[str]
123+
) -> None:
124124
"""Utility method to remove limit block notifications from the server
125125
notices room.
126126
127127
Args:
128-
user_id (str): user to notify
129-
ref_events (list[str]): The event_ids of pinned events that are unrelated to
130-
limit blocking and need to be preserved.
128+
user_id: user to notify
129+
ref_events: The event_ids of pinned events that are unrelated to
130+
limit blocking and need to be preserved.
131131
"""
132132
content = {"pinned": ref_events}
133133
await self._server_notices_manager.send_notice(
134134
user_id, content, EventTypes.Pinned, ""
135135
)
136136

137137
async def _apply_limit_block_notification(
138-
self, user_id, event_body, event_limit_type
139-
):
138+
self, user_id: str, event_body: str, event_limit_type: str
139+
) -> None:
140140
"""Utility method to apply limit block notifications in the server
141141
notices room.
142142
143143
Args:
144-
user_id (str): user to notify
145-
event_body(str): The human readable text that describes the block.
146-
event_limit_type(str): Specifies the type of block e.g. monthly active user
147-
limit has been exceeded.
144+
user_id: user to notify
145+
event_body: The human readable text that describes the block.
146+
event_limit_type: Specifies the type of block e.g. monthly active user
147+
limit has been exceeded.
148148
"""
149149
content = {
150150
"body": event_body,
@@ -162,7 +162,7 @@ async def _apply_limit_block_notification(
162162
user_id, content, EventTypes.Pinned, ""
163163
)
164164

165-
async def _check_and_set_tags(self, user_id, room_id):
165+
async def _check_and_set_tags(self, user_id: str, room_id: str) -> None:
166166
"""
167167
Since server notices rooms were originally not with tags,
168168
important to check that tags have been set correctly
@@ -182,17 +182,16 @@ async def _check_and_set_tags(self, user_id, room_id):
182182
)
183183
self._notifier.on_new_event("account_data_key", max_id, users=[user_id])
184184

185-
async def _is_room_currently_blocked(self, room_id):
185+
async def _is_room_currently_blocked(self, room_id: str) -> Tuple[bool, List[str]]:
186186
"""
187187
Determines if the room is currently blocked
188188
189189
Args:
190-
room_id(str): The room id of the server notices room
190+
room_id: The room id of the server notices room
191191
192192
Returns:
193-
Deferred[Tuple[bool, List]]:
194193
bool: Is the room currently blocked
195-
list: The list of pinned events that are unrelated to limit blocking
194+
list: The list of pinned event IDs that are unrelated to limit blocking
196195
This list can be used as a convenience in the case where the block
197196
is to be lifted and the remaining pinned event references need to be
198197
preserved
@@ -207,7 +206,7 @@ async def _is_room_currently_blocked(self, room_id):
207206
# The user has yet to join the server notices room
208207
pass
209208

210-
referenced_events = []
209+
referenced_events = [] # type: List[str]
211210
if pinned_state_event is not None:
212211
referenced_events = list(pinned_state_event.content.get("pinned", []))
213212

synapse/server_notices/server_notices_manager.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import logging
16+
from typing import Optional
1617

1718
from synapse.api.constants import EventTypes, Membership, RoomCreationPreset
19+
from synapse.events import EventBase
1820
from synapse.types import UserID, create_requester
1921
from synapse.util.caches.descriptors import cached
2022

@@ -50,20 +52,21 @@ def is_enabled(self):
5052
return self._config.server_notices_mxid is not None
5153

5254
async def send_notice(
53-
self, user_id, event_content, type=EventTypes.Message, state_key=None
54-
):
55+
self,
56+
user_id: str,
57+
event_content: dict,
58+
type: str = EventTypes.Message,
59+
state_key: Optional[bool] = None,
60+
) -> EventBase:
5561
"""Send a notice to the given user
5662
5763
Creates the server notices room, if none exists.
5864
5965
Args:
60-
user_id (str): mxid of user to send event to.
61-
event_content (dict): content of event to send
62-
type(EventTypes): type of event
63-
is_state_event(bool): Is the event a state event
64-
65-
Returns:
66-
Deferred[FrozenEvent]
66+
user_id: mxid of user to send event to.
67+
event_content: content of event to send
68+
type: type of event
69+
is_state_event: Is the event a state event
6770
"""
6871
room_id = await self.get_or_create_notice_room_for_user(user_id)
6972
await self.maybe_invite_user_to_room(user_id, room_id)
@@ -89,17 +92,17 @@ async def send_notice(
8992
return event
9093

9194
@cached()
92-
async def get_or_create_notice_room_for_user(self, user_id):
95+
async def get_or_create_notice_room_for_user(self, user_id: str) -> str:
9396
"""Get the room for notices for a given user
9497
9598
If we have not yet created a notice room for this user, create it, but don't
9699
invite the user to it.
97100
98101
Args:
99-
user_id (str): complete user id for the user we want a room for
102+
user_id: complete user id for the user we want a room for
100103
101104
Returns:
102-
str: room id of notice room.
105+
room id of notice room.
103106
"""
104107
if not self.is_enabled():
105108
raise Exception("Server notices not enabled")
@@ -163,7 +166,7 @@ async def get_or_create_notice_room_for_user(self, user_id):
163166
logger.info("Created server notices room %s for %s", room_id, user_id)
164167
return room_id
165168

166-
async def maybe_invite_user_to_room(self, user_id: str, room_id: str):
169+
async def maybe_invite_user_to_room(self, user_id: str, room_id: str) -> None:
167170
"""Invite the given user to the given server room, unless the user has already
168171
joined or been invited to it.
169172

synapse/server_notices/server_notices_sender.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
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 Iterable, Union
16+
1517
from synapse.server_notices.consent_server_notices import ConsentServerNotices
1618
from synapse.server_notices.resource_limits_server_notices import (
1719
ResourceLimitsServerNotices,
@@ -32,22 +34,22 @@ def __init__(self, hs):
3234
self._server_notices = (
3335
ConsentServerNotices(hs),
3436
ResourceLimitsServerNotices(hs),
35-
)
37+
) # type: Iterable[Union[ConsentServerNotices, ResourceLimitsServerNotices]]
3638

37-
async def on_user_syncing(self, user_id):
39+
async def on_user_syncing(self, user_id: str) -> None:
3840
"""Called when the user performs a sync operation.
3941
4042
Args:
41-
user_id (str): mxid of user who synced
43+
user_id: mxid of user who synced
4244
"""
4345
for sn in self._server_notices:
4446
await sn.maybe_send_server_notice_to_user(user_id)
4547

46-
async def on_user_ip(self, user_id):
48+
async def on_user_ip(self, user_id: str) -> None:
4749
"""Called on the master when a worker process saw a client request.
4850
4951
Args:
50-
user_id (str): mxid
52+
user_id: mxid
5153
"""
5254
# The synchrotrons use a stubbed version of ServerNoticesSender, so
5355
# we check for notices to send to the user in on_user_ip as well as

synapse/server_notices/worker_server_notices_sender.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
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 twisted.internet import defer
1615

1716

1817
class WorkerServerNoticesSender(object):
@@ -24,24 +23,18 @@ def __init__(self, hs):
2423
hs (synapse.server.HomeServer):
2524
"""
2625

27-
def on_user_syncing(self, user_id):
26+
async def on_user_syncing(self, user_id: str) -> None:
2827
"""Called when the user performs a sync operation.
2928
3029
Args:
31-
user_id (str): mxid of user who synced
32-
33-
Returns:
34-
Deferred
30+
user_id: mxid of user who synced
3531
"""
36-
return defer.succeed(None)
32+
return None
3733

38-
def on_user_ip(self, user_id):
34+
async def on_user_ip(self, user_id: str) -> None:
3935
"""Called on the master when a worker process saw a client request.
4036
4137
Args:
42-
user_id (str): mxid
43-
44-
Returns:
45-
Deferred
38+
user_id: mxid
4639
"""
4740
raise AssertionError("on_user_ip unexpectedly called on worker")

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ commands = mypy \
203203
synapse/push/push_rule_evaluator.py \
204204
synapse/replication \
205205
synapse/rest \
206+
synapse/server_notices \
206207
synapse/spam_checker_api \
207208
synapse/storage/data_stores/main/ui_auth.py \
208209
synapse/storage/database.py \

0 commit comments

Comments
 (0)