1
0

Add type hints to the spam check module (#6915)

* commit '10027c80b':
  Add type hints to the spam check module (#6915)
This commit is contained in:
Andrew Morgan
2020-03-23 18:00:21 +00:00
4 changed files with 52 additions and 38 deletions

1
changelog.d/6915.misc Normal file
View File

@@ -0,0 +1 @@
Add type hints to the spam checker module.

View File

@@ -15,13 +15,17 @@
# limitations under the License.
import inspect
from typing import Dict
from typing import Dict, Optional, List
from synapse.spam_checker_api import SpamCheckerApi
MYPY = False
if MYPY:
import synapse.server
class SpamChecker(object):
def __init__(self, hs):
def __init__(self, hs: "synapse.server.HomeServer"):
self.spam_checker = None
module = None
@@ -41,7 +45,7 @@ class SpamChecker(object):
else:
self.spam_checker = module(config=config)
def check_event_for_spam(self, event):
def check_event_for_spam(self, event: "synapse.events.EventBase") -> bool:
"""Checks if a given event is considered "spammy" by this server.
If the server considers an event spammy, then it will be rejected if
@@ -49,10 +53,10 @@ class SpamChecker(object):
users receive a blank event.
Args:
event (synapse.events.EventBase): the event to be checked
event: the event to be checked
Returns:
bool: True if the event is spammy.
True if the event is spammy.
"""
if self.spam_checker is None:
return False
@@ -61,34 +65,34 @@ class SpamChecker(object):
def user_may_invite(
self,
inviter_userid,
invitee_userid,
third_party_invite,
room_id,
new_room,
published_room,
):
inviter_userid: str,
invitee_userid: str,
third_party_invite: Optional[Dict],
room_id: str,
new_room: bool,
published_room: bool,
) -> bool:
"""Checks if a given user may send an invite
If this method returns false, the invite will be rejected.
Args:
inviter_userid (str)
invitee_userid (str|None): The user ID of the invitee. Is None
inviter_userid:
invitee_userid: The user ID of the invitee. Is None
if this is a third party invite and the 3PID is not bound to a
user ID.
third_party_invite (dict|None): If a third party invite then is a
third_party_invite: If a third party invite then is a
dict containing the medium and address of the invitee.
room_id (str)
new_room (bool): Whether the user is being invited to the room as
room_id:
new_room: Whether the user is being invited to the room as
part of a room creation, if so the invitee would have been
included in the call to `user_may_create_room`.
published_room (bool): Whether the room the user is being invited
published_room: Whether the room the user is being invited
to has been published in the local homeserver's public room
directory.
Returns:
bool: True if the user may send an invite, otherwise False
True if the user may send an invite, otherwise False
"""
if self.spam_checker is None:
return True
@@ -103,23 +107,27 @@ class SpamChecker(object):
)
def user_may_create_room(
self, userid, invite_list, third_party_invite_list, cloning
):
self,
userid: str,
invite_list: List[str],
third_party_invite_list: List[Dict],
cloning: bool,
) -> bool:
"""Checks if a given user may create a room
If this method returns false, the creation request will be rejected.
Args:
userid (string): The sender's user ID
invite_list (list[str]): List of user IDs that would be invited to
userid: The ID of the user attempting to create a room
invite_list: List of user IDs that would be invited to
the new room.
third_party_invite_list (list[dict]): List of third party invites
third_party_invite_list: List of third party invites
for the new room.
cloning (bool): Whether the user is cloning an existing room, e.g.
cloning: Whether the user is cloning an existing room, e.g.
upgrading a room.
Returns:
bool: True if the user may create a room, otherwise False
True if the user may create a room, otherwise False
"""
if self.spam_checker is None:
return True
@@ -128,34 +136,34 @@ class SpamChecker(object):
userid, invite_list, third_party_invite_list, cloning
)
def user_may_create_room_alias(self, userid, room_alias):
def user_may_create_room_alias(self, userid: str, room_alias: str) -> bool:
"""Checks if a given user may create a room alias
If this method returns false, the association request will be rejected.
Args:
userid (string): The sender's user ID
room_alias (string): The alias to be created
userid: The ID of the user attempting to create a room alias
room_alias: The alias to be created
Returns:
bool: True if the user may create a room alias, otherwise False
True if the user may create a room alias, otherwise False
"""
if self.spam_checker is None:
return True
return self.spam_checker.user_may_create_room_alias(userid, room_alias)
def user_may_publish_room(self, userid, room_id):
def user_may_publish_room(self, userid: str, room_id: str) -> bool:
"""Checks if a given user may publish a room to the directory
If this method returns false, the publish request will be rejected.
Args:
userid (string): The sender's user ID
room_id (string): The ID of the room that would be published
userid: The user ID attempting to publish the room
room_id: The ID of the room that would be published
Returns:
bool: True if the user may publish the room, otherwise False
True if the user may publish the room, otherwise False
"""
if self.spam_checker is None:
return True

View File

@@ -18,6 +18,10 @@ from twisted.internet import defer
from synapse.storage.state import StateFilter
MYPY = False
if MYPY:
import synapse.server
logger = logging.getLogger(__name__)
@@ -26,18 +30,18 @@ class SpamCheckerApi(object):
access to rooms and other relevant information.
"""
def __init__(self, hs):
def __init__(self, hs: "synapse.server.HomeServer"):
self.hs = hs
self._store = hs.get_datastore()
@defer.inlineCallbacks
def get_state_events_in_room(self, room_id, types):
def get_state_events_in_room(self, room_id: str, types: tuple) -> defer.Deferred:
"""Gets state events for the given room.
Args:
room_id (string): The room ID to get state events in.
types (tuple): The event type and state key (using None
room_id: The room ID to get state events in.
types: The event type and state key (using None
to represent 'any') of the room state to acquire.
Returns:

View File

@@ -179,6 +179,7 @@ extras = all
commands = mypy \
synapse/api \
synapse/config/ \
synapse/events/spamcheck.py \
synapse/federation/sender \
synapse/federation/transport \
synapse/handlers/sync.py \