invites: Split out some functions from zerver.actions.invites.

This will avoid a circular dependency in future commits.
This commit is contained in:
Alex Vandiver 2024-04-30 20:57:29 +00:00 committed by Tim Abbott
parent afafd04bf9
commit e48d5e4ad2
4 changed files with 55 additions and 46 deletions

View File

@ -10,7 +10,6 @@ from django.utils.translation import override as override_language
from analytics.lib.counts import COUNT_STATS, do_increment_logging_stat from analytics.lib.counts import COUNT_STATS, do_increment_logging_stat
from confirmation import settings as confirmation_settings from confirmation import settings as confirmation_settings
from zerver.actions.invites import notify_invites_changed
from zerver.actions.message_send import ( from zerver.actions.message_send import (
internal_send_huddle_message, internal_send_huddle_message,
internal_send_private_message, internal_send_private_message,
@ -24,6 +23,7 @@ from zerver.lib.create_user import create_user
from zerver.lib.default_streams import get_slim_realm_default_streams from zerver.lib.default_streams import get_slim_realm_default_streams
from zerver.lib.email_notifications import enqueue_welcome_emails, send_account_registered_email from zerver.lib.email_notifications import enqueue_welcome_emails, send_account_registered_email
from zerver.lib.exceptions import JsonableError from zerver.lib.exceptions import JsonableError
from zerver.lib.invites import notify_invites_changed
from zerver.lib.mention import silent_mention_syntax_for_user from zerver.lib.mention import silent_mention_syntax_for_user
from zerver.lib.remote_server import maybe_enqueue_audit_log_upload from zerver.lib.remote_server import maybe_enqueue_audit_log_upload
from zerver.lib.send_email import clear_scheduled_invitation_emails from zerver.lib.send_email import clear_scheduled_invitation_emails

View File

@ -20,24 +20,13 @@ from zerver.lib.email_validation import (
validate_email_is_valid, validate_email_is_valid,
) )
from zerver.lib.exceptions import InvitationError from zerver.lib.exceptions import InvitationError
from zerver.lib.invites import notify_invites_changed
from zerver.lib.queue import queue_json_publish from zerver.lib.queue import queue_json_publish
from zerver.lib.send_email import FromAddress, clear_scheduled_invitation_emails, send_email from zerver.lib.send_email import FromAddress, clear_scheduled_invitation_emails, send_email
from zerver.lib.timestamp import datetime_to_timestamp from zerver.lib.timestamp import datetime_to_timestamp
from zerver.lib.types import UnspecifiedValue from zerver.lib.types import UnspecifiedValue
from zerver.models import Message, MultiuseInvite, PreregistrationUser, Realm, Stream, UserProfile from zerver.models import Message, MultiuseInvite, PreregistrationUser, Realm, Stream, UserProfile
from zerver.models.prereg_users import filter_to_valid_prereg_users from zerver.models.prereg_users import filter_to_valid_prereg_users
from zerver.tornado.django_api import send_event
def notify_invites_changed(
realm: Realm, *, changed_invite_referrer: Optional[UserProfile] = None
) -> None:
event = dict(type="invites_changed")
admin_ids = [user.id for user in realm.get_admin_users_and_bots()]
recipient_ids = admin_ids
if changed_invite_referrer and changed_invite_referrer.id not in recipient_ids:
recipient_ids.append(changed_invite_referrer.id)
send_event(realm, event, recipient_ids)
def do_send_confirmation_email( def do_send_confirmation_email(
@ -404,38 +393,6 @@ def do_get_invites_controlled_by_user(user_profile: UserProfile) -> List[Dict[st
return invites return invites
def get_valid_invite_confirmations_generated_by_user(
user_profile: UserProfile,
) -> List[Confirmation]:
prereg_user_ids = filter_to_valid_prereg_users(
PreregistrationUser.objects.filter(referred_by=user_profile)
).values_list("id", flat=True)
confirmations = list(
Confirmation.objects.filter(type=Confirmation.INVITATION, object_id__in=prereg_user_ids)
)
multiuse_invite_ids = MultiuseInvite.objects.filter(referred_by=user_profile).values_list(
"id", flat=True
)
confirmations += Confirmation.objects.filter(
type=Confirmation.MULTIUSE_INVITE,
object_id__in=multiuse_invite_ids,
).filter(Q(expiry_date__gte=timezone_now()) | Q(expiry_date=None))
return confirmations
def revoke_invites_generated_by_user(user_profile: UserProfile) -> None:
confirmations_to_revoke = get_valid_invite_confirmations_generated_by_user(user_profile)
now = timezone_now()
for confirmation in confirmations_to_revoke:
confirmation.expiry_date = now
Confirmation.objects.bulk_update(confirmations_to_revoke, ["expiry_date"])
if len(confirmations_to_revoke):
notify_invites_changed(realm=user_profile.realm)
def do_create_multiuse_invite_link( def do_create_multiuse_invite_link(
referred_by: UserProfile, referred_by: UserProfile,
invited_as: int, invited_as: int,

View File

@ -8,7 +8,6 @@ from django.db import transaction
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from analytics.lib.counts import COUNT_STATS, do_increment_logging_stat from analytics.lib.counts import COUNT_STATS, do_increment_logging_stat
from zerver.actions.invites import revoke_invites_generated_by_user
from zerver.actions.user_groups import ( from zerver.actions.user_groups import (
do_send_user_group_members_update_event, do_send_user_group_members_update_event,
update_users_in_full_members_system_group, update_users_in_full_members_system_group,
@ -17,6 +16,7 @@ from zerver.lib.avatar import avatar_url_from_dict
from zerver.lib.bot_config import ConfigError, get_bot_config, get_bot_configs, set_bot_config from zerver.lib.bot_config import ConfigError, get_bot_config, get_bot_configs, set_bot_config
from zerver.lib.cache import bot_dict_fields from zerver.lib.cache import bot_dict_fields
from zerver.lib.create_user import create_user from zerver.lib.create_user import create_user
from zerver.lib.invites import revoke_invites_generated_by_user
from zerver.lib.remote_server import maybe_enqueue_audit_log_upload from zerver.lib.remote_server import maybe_enqueue_audit_log_upload
from zerver.lib.send_email import clear_scheduled_emails from zerver.lib.send_email import clear_scheduled_emails
from zerver.lib.sessions import delete_user_sessions from zerver.lib.sessions import delete_user_sessions

52
zerver/lib/invites.py Normal file
View File

@ -0,0 +1,52 @@
from typing import List, Optional
from django.db.models import Q
from django.utils.timezone import now as timezone_now
from confirmation.models import Confirmation
from zerver.models import MultiuseInvite, PreregistrationUser, Realm, UserProfile
from zerver.models.prereg_users import filter_to_valid_prereg_users
from zerver.tornado.django_api import send_event
def notify_invites_changed(
realm: Realm, *, changed_invite_referrer: Optional[UserProfile] = None
) -> None:
event = dict(type="invites_changed")
admin_ids = [user.id for user in realm.get_admin_users_and_bots()]
recipient_ids = admin_ids
if changed_invite_referrer and changed_invite_referrer.id not in recipient_ids:
recipient_ids.append(changed_invite_referrer.id)
send_event(realm, event, recipient_ids)
def get_valid_invite_confirmations_generated_by_user(
user_profile: UserProfile,
) -> List[Confirmation]:
prereg_user_ids = filter_to_valid_prereg_users(
PreregistrationUser.objects.filter(referred_by=user_profile)
).values_list("id", flat=True)
confirmations = list(
Confirmation.objects.filter(type=Confirmation.INVITATION, object_id__in=prereg_user_ids)
)
multiuse_invite_ids = MultiuseInvite.objects.filter(referred_by=user_profile).values_list(
"id", flat=True
)
confirmations += Confirmation.objects.filter(
type=Confirmation.MULTIUSE_INVITE,
object_id__in=multiuse_invite_ids,
).filter(Q(expiry_date__gte=timezone_now()) | Q(expiry_date=None))
return confirmations
def revoke_invites_generated_by_user(user_profile: UserProfile) -> None:
confirmations_to_revoke = get_valid_invite_confirmations_generated_by_user(user_profile)
now = timezone_now()
for confirmation in confirmations_to_revoke:
confirmation.expiry_date = now
Confirmation.objects.bulk_update(confirmations_to_revoke, ["expiry_date"])
if len(confirmations_to_revoke):
notify_invites_changed(realm=user_profile.realm)