user_groups: Add cron job for adding users to full members system group.

This commit adds a cron job which runs every hour to add the users to
full members system group if user is promoted to a full member.

This should ensure that full member status is available no more than
an hour after configuration suggests it should be.
This commit is contained in:
Sahil Batra 2022-01-31 22:54:00 +05:30 committed by Tim Abbott
parent 565d0928dd
commit f0606b34ad
5 changed files with 76 additions and 1 deletions

View File

@ -0,0 +1,6 @@
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
USER=zulip
# Cron job to promote users to full members system group.
35 * * * * zulip /home/zulip/deployments/current/manage.py promote_new_full_members

View File

@ -51,6 +51,14 @@ class zulip::app_frontend_once {
source => 'puppet:///modules/zulip/cron.d/soft-deactivate-users',
}
file { '/etc/cron.d/promote-new-full-members':
ensure => file,
owner => 'root',
group => 'root',
mode => '0644',
source => 'puppet:///modules/zulip/cron.d/promote-new-full-members',
}
file { '/etc/cron.d/archive-messages':
ensure => file,
owner => 'root',

View File

@ -923,6 +923,11 @@ def update_users_in_full_members_system_group(
bulk_add_members_to_user_group(full_members_system_group, new_full_member_ids)
def promote_new_full_members() -> None:
for realm in Realm.objects.filter(deactivated=False).exclude(waiting_period_threshold=0):
update_users_in_full_members_system_group(realm)
@transaction.atomic(savepoint=False)
def do_set_realm_property(
realm: Realm, name: str, value: Any, *, acting_user: Optional[UserProfile]

View File

@ -0,0 +1,11 @@
from typing import Any
from zerver.lib.actions import promote_new_full_members
from zerver.lib.management import ZulipBaseCommand
class Command(ZulipBaseCommand):
help = """Add users to full members system group."""
def handle(self, *args: Any, **options: Any) -> None:
promote_new_full_members()

View File

@ -1,10 +1,11 @@
from datetime import timedelta
from typing import Optional
from unittest import mock
import orjson
from django.utils.timezone import now as timezone_now
from zerver.lib.actions import do_set_realm_property, ensure_stream
from zerver.lib.actions import do_set_realm_property, ensure_stream, promote_new_full_members
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import most_recent_usermessage
from zerver.lib.user_groups import (
@ -726,3 +727,47 @@ class UserGroupAPITestCase(UserGroupTestCase):
check_support_group_permission(desdemona)
check_support_group_permission(iago)
check_support_group_permission(othello)
def test_promote_new_full_members(self) -> None:
realm = get_realm("zulip")
cordelia = self.example_user("cordelia")
hamlet = self.example_user("hamlet")
cordelia.date_joined = timezone_now() - timedelta(days=11)
cordelia.save()
hamlet.date_joined = timezone_now() - timedelta(days=8)
hamlet.save()
do_set_realm_property(realm, "waiting_period_threshold", 10, acting_user=None)
full_members_group = UserGroup.objects.get(
realm=realm, name="@role:fullmembers", is_system_group=True
)
self.assertTrue(
UserGroupMembership.objects.filter(
user_profile=cordelia, user_group=full_members_group
).exists()
)
self.assertFalse(
UserGroupMembership.objects.filter(
user_profile=hamlet, user_group=full_members_group
).exists()
)
current_time = timezone_now()
with mock.patch(
"zerver.lib.actions.timezone_now", return_value=current_time + timedelta(days=3)
):
promote_new_full_members()
self.assertTrue(
UserGroupMembership.objects.filter(
user_profile=cordelia, user_group=full_members_group
).exists()
)
self.assertTrue(
UserGroupMembership.objects.filter(
user_profile=hamlet, user_group=full_members_group
).exists()
)