2016-10-25 19:51:31 +02:00
|
|
|
|
|
|
|
from datetime import timedelta
|
2017-05-14 21:14:26 +02:00
|
|
|
|
|
|
|
from django.db import connection, transaction
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2017-05-14 21:14:26 +02:00
|
|
|
from zerver.models import Realm, Message, UserMessage, ArchivedMessage, ArchivedUserMessage, \
|
|
|
|
Attachment, ArchivedAttachment
|
2016-10-25 19:51:31 +02:00
|
|
|
|
2018-09-13 16:29:12 +02:00
|
|
|
from typing import Any, Dict, Optional, Generator, List
|
2016-10-25 19:51:31 +02:00
|
|
|
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_realm_expired_messages(realm: 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
|
|
|
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_expired_messages() -> Generator[Any, None, None]:
|
2016-10-25 19:51:31 +02:00
|
|
|
# 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
|
2017-05-14 21:14:26 +02:00
|
|
|
|
|
|
|
|
2018-09-13 16:29:12 +02:00
|
|
|
def move_attachment_message_to_archive_by_message(message_ids: List[int]) -> None:
|
2017-05-14 21:14:26 +02:00
|
|
|
# Move attachments messages relation table data to archive.
|
2018-09-13 16:29:12 +02:00
|
|
|
id_list = ', '.join(str(message_id) for message_id in message_ids)
|
|
|
|
|
2017-05-14 21:14:26 +02:00
|
|
|
query = """
|
|
|
|
INSERT INTO zerver_archivedattachment_messages (id, archivedattachment_id,
|
|
|
|
archivedmessage_id)
|
|
|
|
SELECT zerver_attachment_messages.id, zerver_attachment_messages.attachment_id,
|
|
|
|
zerver_attachment_messages.message_id
|
|
|
|
FROM zerver_attachment_messages
|
|
|
|
LEFT JOIN zerver_archivedattachment_messages
|
|
|
|
ON zerver_archivedattachment_messages.id = zerver_attachment_messages.id
|
2018-09-13 16:29:12 +02:00
|
|
|
WHERE zerver_attachment_messages.message_id in ({message_ids})
|
2017-05-14 21:14:26 +02:00
|
|
|
AND zerver_archivedattachment_messages.id IS NULL
|
|
|
|
"""
|
|
|
|
with connection.cursor() as cursor:
|
2018-09-13 16:29:12 +02:00
|
|
|
cursor.execute(query.format(message_ids=id_list))
|
2017-05-14 21:14:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic
|
2018-09-13 16:29:12 +02:00
|
|
|
def move_messages_to_archive(message_ids: List[int]) -> None:
|
|
|
|
messages = list(Message.objects.filter(id__in=message_ids).values())
|
|
|
|
if not messages:
|
2017-05-14 21:14:26 +02:00
|
|
|
raise Message.DoesNotExist
|
2018-09-13 16:29:12 +02:00
|
|
|
arc_messages = []
|
|
|
|
for message in messages:
|
|
|
|
arc_message = ArchivedMessage(**message)
|
|
|
|
arc_messages.append(arc_message)
|
|
|
|
ArchivedMessage.objects.bulk_create(arc_messages)
|
2017-05-14 21:14:26 +02:00
|
|
|
# Move user_messages to the archive.
|
|
|
|
user_messages = UserMessage.objects.filter(
|
2018-09-13 16:29:12 +02:00
|
|
|
message_id__in=message_ids).exclude(id__in=ArchivedUserMessage.objects.all())
|
2017-05-14 21:14:26 +02:00
|
|
|
archiving_messages = []
|
|
|
|
for user_message in user_messages.values():
|
|
|
|
archiving_messages.append(ArchivedUserMessage(**user_message))
|
|
|
|
ArchivedUserMessage.objects.bulk_create(archiving_messages)
|
|
|
|
|
|
|
|
# Move attachments to archive
|
2018-09-13 16:29:12 +02:00
|
|
|
attachments = Attachment.objects.filter(messages__id__in=message_ids).exclude(
|
|
|
|
id__in=ArchivedAttachment.objects.all()).distinct()
|
2017-05-14 21:14:26 +02:00
|
|
|
archiving_attachments = []
|
|
|
|
for attachment in attachments.values():
|
|
|
|
archiving_attachments.append(ArchivedAttachment(**attachment))
|
|
|
|
ArchivedAttachment.objects.bulk_create(archiving_attachments)
|
2018-09-13 16:29:12 +02:00
|
|
|
move_attachment_message_to_archive_by_message(message_ids)
|
2017-05-14 21:14:26 +02:00
|
|
|
# Remove data from main tables
|
2018-09-13 16:29:12 +02:00
|
|
|
Message.objects.filter(id__in=message_ids).delete()
|
2017-05-14 21:14:26 +02:00
|
|
|
user_messages.filter(id__in=ArchivedUserMessage.objects.all(),
|
|
|
|
message_id__isnull=True).delete()
|
2018-09-13 16:29:12 +02:00
|
|
|
archived_attachments = ArchivedAttachment.objects.filter(messages__id__in=message_ids).distinct()
|
2017-05-14 21:14:26 +02:00
|
|
|
Attachment.objects.filter(messages__isnull=True, id__in=archived_attachments).delete()
|