Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bots: Introduce System bots in UserProfile and port realm_internal_bots. #8961

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion zerver/lib/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def setup_realm_internal_bots(realm: Realm) -> None:
"""
internal_bots = [(bot['name'], bot['email_template'] % (settings.INTERNAL_BOT_DOMAIN,))
for bot in settings.REALM_INTERNAL_BOTS]
create_users(realm, internal_bots, bot_type=UserProfile.DEFAULT_BOT)
create_users(realm, internal_bots, bot_type=UserProfile.SYSTEM_BOT)
bots = UserProfile.objects.filter(
realm=realm,
email__in=[bot_info[1] for bot_info in internal_bots],
Expand Down
30 changes: 30 additions & 0 deletions zerver/migrations/0155_realm_internal_bots_to_system_bots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps

def convert_bots_to_system_bots(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
REALM_INTERNAL_BOTS = [
'reminder-bot@zulip.com',
]
UserProfile = apps.get_model('zerver', 'UserProfile')
internal_bots = UserProfile.objects.filter(
email__in=REALM_INTERNAL_BOTS,
bot_type=1
)
for bot in internal_bots:
bot.bot_type = 5
bot.save(update_fields=['bot_type'])


class Migration(migrations.Migration):

dependencies = [
('zerver', '0154_fix_invalid_bot_owner'),
]

operations = [
migrations.RunPython(convert_bots_to_system_bots),
]
19 changes: 15 additions & 4 deletions zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ def get_admin_users(self) -> Sequence['UserProfile']:

def get_active_users(self) -> Sequence['UserProfile']:
# TODO: Change return type to QuerySet[UserProfile]
return UserProfile.objects.filter(realm=self, is_active=True).select_related()
return UserProfile.objects.filter(
realm=self,
is_active=True).exclude(bot_type=UserProfile.SYSTEM_BOT).select_related()

def get_bot_domain(self) -> str:
# Remove the port. Mainly needed for development environment.
Expand Down Expand Up @@ -567,12 +569,18 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
embedded_bots queue and then handled by a QueueProcessingWorker.
"""
EMBEDDED_BOT = 4
"""
System bots are used by zulip to send automatted messages to users.
(eg. deliver reminders)
"""
SYSTEM_BOT = 5

BOT_TYPES = {
DEFAULT_BOT: 'Generic bot',
INCOMING_WEBHOOK_BOT: 'Incoming webhook',
OUTGOING_WEBHOOK_BOT: 'Outgoing webhook',
EMBEDDED_BOT: 'Embedded bot',
SYSTEM_BOT: 'System bot',
}

SERVICE_BOT_TYPES = [
Expand Down Expand Up @@ -1494,19 +1502,22 @@ def get_system_bot(email: Text) -> UserProfile:
def get_realm_user_dicts(realm_id: int) -> List[Dict[str, Any]]:
return UserProfile.objects.filter(
realm_id=realm_id,
).values(*realm_user_dict_fields)
).exclude(bot_type=UserProfile.SYSTEM_BOT).values(*realm_user_dict_fields)

@cache_with_key(active_user_ids_cache_key, timeout=3600*24*7)
def active_user_ids(realm_id: int) -> List[int]:
query = UserProfile.objects.filter(
realm_id=realm_id,
is_active=True
).values_list('id', flat=True)
).exclude(bot_type=UserProfile.SYSTEM_BOT).values_list('id', flat=True)
return list(query)

@cache_with_key(bot_dicts_in_realm_cache_key, timeout=3600*24*7)
def get_bot_dicts_in_realm(realm: Realm) -> List[Dict[str, Any]]:
return UserProfile.objects.filter(realm=realm, is_bot=True).values(*bot_dict_fields)
return UserProfile.objects.filter(
realm=realm,
is_bot=True
).exclude(bot_type=UserProfile.SYSTEM_BOT).values(*bot_dict_fields)

def is_cross_realm_bot_email(email: Text) -> bool:
return email.lower() in settings.CROSS_REALM_BOT_EMAILS
Expand Down
2 changes: 1 addition & 1 deletion zerver/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def get_member(row: Dict[str, Any]) -> Dict[str, Any]:

return result

members = [get_member(row) for row in query]
members = [get_member(row) for row in query if row['bot_type'] != UserProfile.SYSTEM_BOT]

return json_success({'members': members})

Expand Down