invites: Extract new realm invitation heuristic.

This commit is contained in:
Alex Vandiver 2023-03-07 21:37:17 +00:00 committed by Tim Abbott
parent 46fd215893
commit 75138102f4
1 changed files with 62 additions and 56 deletions

View File

@ -82,24 +82,12 @@ def estimate_recent_invites(realms: Collection[Realm], *, days: int) -> int:
return recent_invites return recent_invites
def check_invite_limit(realm: Realm, num_invitees: int) -> None: def apply_invite_realm_heuristics(realm: Realm) -> None:
"""Discourage using invitation emails as a vector for carrying spam.""" if realm.plan_type != Realm.PLAN_TYPE_LIMITED:
msg = _( return
"To protect users, Zulip limits the number of invitations you can send in one day. Because you have reached the limit, no invitations were sent." if realm._max_invites is not None:
)
if not settings.OPEN_REALM_CREATION:
return return
recent_invites = estimate_recent_invites([realm], days=1)
if num_invitees + recent_invites > realm.max_invites:
raise InvitationError(
msg,
[],
sent_invitations=False,
daily_limit_reached=True,
)
if realm.plan_type == Realm.PLAN_TYPE_LIMITED and realm._max_invites is None:
# If they're a non-paid plan with default invitation limits, # If they're a non-paid plan with default invitation limits,
# we further limit how many invitations can be sent in a day # we further limit how many invitations can be sent in a day
# as a function of how many current users they have. The # as a function of how many current users they have. The
@ -123,9 +111,7 @@ def check_invite_limit(realm: Realm, num_invitees: int) -> None:
if realm.date_created >= timezone_now() - datetime.timedelta(hours=1): if realm.date_created >= timezone_now() - datetime.timedelta(hours=1):
suspicion_score += 1 suspicion_score += 1
current_user_count = len( current_user_count = len(UserProfile.objects.filter(realm=realm, is_bot=False, is_active=True))
UserProfile.objects.filter(realm=realm, is_bot=False, is_active=True)
)
if current_user_count == 1: if current_user_count == 1:
suspicion_score += 1 suspicion_score += 1
@ -156,6 +142,26 @@ def check_invite_limit(realm: Realm, num_invitees: int) -> None:
permitted_ratio, permitted_ratio,
) )
def check_invite_limit(realm: Realm, num_invitees: int) -> None:
"""Discourage using invitation emails as a vector for carrying spam."""
msg = _(
"To protect users, Zulip limits the number of invitations you can send in one day. Because you have reached the limit, no invitations were sent."
)
if not settings.OPEN_REALM_CREATION:
return
recent_invites = estimate_recent_invites([realm], days=1)
if num_invitees + recent_invites > realm.max_invites:
raise InvitationError(
msg,
[],
sent_invitations=False,
daily_limit_reached=True,
)
apply_invite_realm_heuristics(realm)
default_max = settings.INVITES_DEFAULT_REALM_DAILY_MAX default_max = settings.INVITES_DEFAULT_REALM_DAILY_MAX
newrealm_age = datetime.timedelta(days=settings.INVITES_NEW_REALM_DAYS) newrealm_age = datetime.timedelta(days=settings.INVITES_NEW_REALM_DAYS)
if realm.date_created <= timezone_now() - newrealm_age: if realm.date_created <= timezone_now() - newrealm_age: