2022-04-14 23:55:07 +02:00
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
|
|
|
from zerver.actions.create_user import created_bot_event
|
2022-05-07 08:56:33 +02:00
|
|
|
from zerver.actions.streams import bulk_remove_subscriptions
|
|
|
|
from zerver.lib.streams import get_subscribed_private_streams_for_user
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models import RealmAuditLog, Stream, UserProfile
|
2024-09-03 15:33:25 +02:00
|
|
|
from zerver.models.realm_audit_logs import AuditLogEventType
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models.users import active_user_ids, bot_owner_user_ids
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
from zerver.tornado.django_api import send_event_on_commit
|
2022-04-14 23:55:07 +02:00
|
|
|
|
|
|
|
|
2022-04-21 10:16:55 +02:00
|
|
|
def send_bot_owner_update_events(
|
2024-07-12 02:30:23 +02:00
|
|
|
user_profile: UserProfile, bot_owner: UserProfile, previous_owner: UserProfile | None
|
2022-04-14 23:55:07 +02:00
|
|
|
) -> None:
|
|
|
|
update_users = bot_owner_user_ids(user_profile)
|
|
|
|
|
|
|
|
# For admins, update event is sent instead of delete/add
|
|
|
|
# event. bot_data of admin contains all the
|
|
|
|
# bots and none of them should be removed/(added again).
|
|
|
|
|
|
|
|
# Delete the bot from previous owner's bot data.
|
|
|
|
if previous_owner and not previous_owner.is_realm_admin:
|
|
|
|
delete_event = dict(
|
|
|
|
type="realm_bot",
|
|
|
|
op="delete",
|
|
|
|
bot=dict(
|
|
|
|
user_id=user_profile.id,
|
|
|
|
),
|
|
|
|
)
|
2022-05-31 01:32:29 +02:00
|
|
|
previous_owner_id = previous_owner.id
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(
|
|
|
|
user_profile.realm,
|
|
|
|
delete_event,
|
|
|
|
{previous_owner_id},
|
2022-04-14 23:55:07 +02:00
|
|
|
)
|
|
|
|
# Do not send update event for previous bot owner.
|
2024-07-14 21:06:04 +02:00
|
|
|
update_users.discard(previous_owner.id)
|
2022-04-14 23:55:07 +02:00
|
|
|
|
|
|
|
# Notify the new owner that the bot has been added.
|
|
|
|
if not bot_owner.is_realm_admin:
|
|
|
|
add_event = created_bot_event(user_profile)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(user_profile.realm, add_event, {bot_owner.id})
|
2022-04-14 23:55:07 +02:00
|
|
|
# Do not send update event for bot_owner.
|
2024-07-14 21:06:04 +02:00
|
|
|
update_users.discard(bot_owner.id)
|
2022-04-14 23:55:07 +02:00
|
|
|
|
|
|
|
bot_event = dict(
|
|
|
|
type="realm_bot",
|
|
|
|
op="update",
|
|
|
|
bot=dict(
|
|
|
|
user_id=user_profile.id,
|
2022-04-21 10:16:55 +02:00
|
|
|
owner_id=bot_owner.id,
|
2022-04-14 23:55:07 +02:00
|
|
|
),
|
|
|
|
)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(
|
|
|
|
user_profile.realm,
|
|
|
|
bot_event,
|
|
|
|
update_users,
|
2022-04-14 23:55:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Since `bot_owner_id` is included in the user profile dict we need
|
|
|
|
# to update the users dict with the new bot owner id
|
|
|
|
event = dict(
|
|
|
|
type="realm_user",
|
|
|
|
op="update",
|
|
|
|
person=dict(
|
|
|
|
user_id=user_profile.id,
|
2022-04-21 10:16:55 +02:00
|
|
|
bot_owner_id=bot_owner.id,
|
2022-04-14 23:55:07 +02:00
|
|
|
),
|
|
|
|
)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(user_profile.realm, event, active_user_ids(user_profile.realm_id))
|
2022-04-14 23:55:07 +02:00
|
|
|
|
|
|
|
|
2022-05-07 08:56:33 +02:00
|
|
|
def remove_bot_from_inaccessible_private_streams(
|
2024-07-12 02:30:23 +02:00
|
|
|
user_profile: UserProfile, *, acting_user: UserProfile | None
|
2022-05-07 08:56:33 +02:00
|
|
|
) -> None:
|
|
|
|
assert user_profile.bot_owner is not None
|
|
|
|
|
|
|
|
new_owner_subscribed_private_streams = get_subscribed_private_streams_for_user(
|
|
|
|
user_profile.bot_owner
|
|
|
|
)
|
|
|
|
new_owner_subscribed_private_stream_ids = [
|
|
|
|
stream.id for stream in new_owner_subscribed_private_streams
|
|
|
|
]
|
|
|
|
|
|
|
|
bot_subscribed_private_streams = get_subscribed_private_streams_for_user(user_profile)
|
|
|
|
bot_subscribed_private_stream_ids = [stream.id for stream in bot_subscribed_private_streams]
|
|
|
|
|
|
|
|
stream_ids_to_unsubscribe = set(bot_subscribed_private_stream_ids) - set(
|
|
|
|
new_owner_subscribed_private_stream_ids
|
|
|
|
)
|
|
|
|
unsubscribed_streams = [
|
|
|
|
stream
|
|
|
|
for stream in bot_subscribed_private_streams
|
|
|
|
if stream.id in stream_ids_to_unsubscribe
|
|
|
|
]
|
|
|
|
bulk_remove_subscriptions(
|
|
|
|
user_profile.realm, [user_profile], unsubscribed_streams, acting_user=acting_user
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-04-21 10:16:55 +02:00
|
|
|
@transaction.atomic(durable=True)
|
|
|
|
def do_change_bot_owner(
|
2024-07-12 02:30:23 +02:00
|
|
|
user_profile: UserProfile, bot_owner: UserProfile, acting_user: UserProfile | None
|
2022-04-21 10:16:55 +02:00
|
|
|
) -> None:
|
|
|
|
previous_owner = user_profile.bot_owner
|
|
|
|
user_profile.bot_owner = bot_owner
|
|
|
|
user_profile.save() # Can't use update_fields because of how the foreign key works.
|
|
|
|
event_time = timezone_now()
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user_profile.realm,
|
|
|
|
acting_user=acting_user,
|
|
|
|
modified_user=user_profile,
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_BOT_OWNER_CHANGED,
|
2022-04-21 10:16:55 +02:00
|
|
|
event_time=event_time,
|
|
|
|
)
|
|
|
|
|
|
|
|
send_bot_owner_update_events(user_profile, bot_owner, previous_owner)
|
|
|
|
|
2022-05-07 08:56:33 +02:00
|
|
|
remove_bot_from_inaccessible_private_streams(user_profile, acting_user=acting_user)
|
|
|
|
|
2022-04-21 10:16:55 +02:00
|
|
|
|
2022-04-14 23:55:07 +02:00
|
|
|
@transaction.atomic(durable=True)
|
|
|
|
def do_change_default_sending_stream(
|
2024-07-12 02:30:23 +02:00
|
|
|
user_profile: UserProfile, stream: Stream | None, *, acting_user: UserProfile | None
|
2022-04-14 23:55:07 +02:00
|
|
|
) -> None:
|
|
|
|
old_value = user_profile.default_sending_stream_id
|
|
|
|
user_profile.default_sending_stream = stream
|
|
|
|
user_profile.save(update_fields=["default_sending_stream"])
|
|
|
|
|
|
|
|
event_time = timezone_now()
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user_profile.realm,
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_DEFAULT_SENDING_STREAM_CHANGED,
|
2022-04-14 23:55:07 +02:00
|
|
|
event_time=event_time,
|
|
|
|
modified_user=user_profile,
|
|
|
|
acting_user=acting_user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: None if stream is None else stream.id,
|
|
|
|
},
|
2022-04-14 23:55:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if user_profile.is_bot:
|
|
|
|
if stream:
|
2024-07-12 02:30:23 +02:00
|
|
|
stream_name: str | None = stream.name
|
2022-04-14 23:55:07 +02:00
|
|
|
else:
|
|
|
|
stream_name = None
|
|
|
|
event = dict(
|
|
|
|
type="realm_bot",
|
|
|
|
op="update",
|
|
|
|
bot=dict(
|
|
|
|
user_id=user_profile.id,
|
|
|
|
default_sending_stream=stream_name,
|
|
|
|
),
|
|
|
|
)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(
|
|
|
|
user_profile.realm,
|
|
|
|
event,
|
|
|
|
bot_owner_user_ids(user_profile),
|
2022-04-14 23:55:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic(durable=True)
|
|
|
|
def do_change_default_events_register_stream(
|
2024-07-12 02:30:23 +02:00
|
|
|
user_profile: UserProfile, stream: Stream | None, *, acting_user: UserProfile | None
|
2022-04-14 23:55:07 +02:00
|
|
|
) -> None:
|
|
|
|
old_value = user_profile.default_events_register_stream_id
|
|
|
|
user_profile.default_events_register_stream = stream
|
|
|
|
user_profile.save(update_fields=["default_events_register_stream"])
|
|
|
|
|
|
|
|
event_time = timezone_now()
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user_profile.realm,
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_DEFAULT_REGISTER_STREAM_CHANGED,
|
2022-04-14 23:55:07 +02:00
|
|
|
event_time=event_time,
|
|
|
|
modified_user=user_profile,
|
|
|
|
acting_user=acting_user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: None if stream is None else stream.id,
|
|
|
|
},
|
2022-04-14 23:55:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if user_profile.is_bot:
|
|
|
|
if stream:
|
2024-07-12 02:30:23 +02:00
|
|
|
stream_name: str | None = stream.name
|
2022-04-14 23:55:07 +02:00
|
|
|
else:
|
|
|
|
stream_name = None
|
|
|
|
|
|
|
|
event = dict(
|
|
|
|
type="realm_bot",
|
|
|
|
op="update",
|
|
|
|
bot=dict(
|
|
|
|
user_id=user_profile.id,
|
|
|
|
default_events_register_stream=stream_name,
|
|
|
|
),
|
|
|
|
)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(
|
|
|
|
user_profile.realm,
|
|
|
|
event,
|
|
|
|
bot_owner_user_ids(user_profile),
|
2022-04-14 23:55:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic(durable=True)
|
|
|
|
def do_change_default_all_public_streams(
|
2024-07-12 02:30:23 +02:00
|
|
|
user_profile: UserProfile, value: bool, *, acting_user: UserProfile | None
|
2022-04-14 23:55:07 +02:00
|
|
|
) -> None:
|
|
|
|
old_value = user_profile.default_all_public_streams
|
|
|
|
user_profile.default_all_public_streams = value
|
|
|
|
user_profile.save(update_fields=["default_all_public_streams"])
|
|
|
|
|
|
|
|
event_time = timezone_now()
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user_profile.realm,
|
2024-09-03 15:33:25 +02:00
|
|
|
event_type=AuditLogEventType.USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED,
|
2022-04-14 23:55:07 +02:00
|
|
|
event_time=event_time,
|
|
|
|
modified_user=user_profile,
|
|
|
|
acting_user=acting_user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: value,
|
|
|
|
},
|
2022-04-14 23:55:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if user_profile.is_bot:
|
|
|
|
event = dict(
|
|
|
|
type="realm_bot",
|
|
|
|
op="update",
|
|
|
|
bot=dict(
|
|
|
|
user_id=user_profile.id,
|
|
|
|
default_all_public_streams=user_profile.default_all_public_streams,
|
|
|
|
),
|
|
|
|
)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(
|
|
|
|
user_profile.realm,
|
|
|
|
event,
|
|
|
|
bot_owner_user_ids(user_profile),
|
2022-04-14 23:55:07 +02:00
|
|
|
)
|