2016-10-25 19:51:31 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
from datetime import timedelta
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2016-10-25 19:51:31 +02:00
|
|
|
from zerver.models import Realm, Message
|
|
|
|
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict, Optional, Generator
|
2016-10-25 19:51:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_realm_expired_messages(realm):
|
|
|
|
# type: (Any) -> Optional[Dict[str, Any]]
|
2017-04-15 04:03:56 +02:00
|
|
|
expired_date = timezone_now() - timedelta(days=realm.message_retention_days)
|
2016-10-25 19:51:31 +02:00
|
|
|
expired_messages = Message.objects.order_by('id').filter(sender__realm=realm,
|
|
|
|
pub_date__lt=expired_date)
|
|
|
|
if not expired_messages.exists():
|
|
|
|
return None
|
2016-11-30 21:55:59 +01:00
|
|
|
return {'realm_id': realm.id, 'expired_messages': expired_messages}
|
2016-10-25 19:51:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_expired_messages():
|
|
|
|
# type: () -> Generator[Any, None, None]
|
|
|
|
# Get all expired messages by Realm.
|
2017-03-14 01:20:25 +01:00
|
|
|
realms = Realm.objects.order_by('string_id').filter(
|
|
|
|
deactivated=False, message_retention_days__isnull=False)
|
2016-10-25 19:51:31 +02:00
|
|
|
for realm in realms:
|
|
|
|
realm_expired_messages = get_realm_expired_messages(realm)
|
|
|
|
if realm_expired_messages:
|
|
|
|
yield realm_expired_messages
|