From 8b7e05a24dd9170c190613f83dddc88ad798f905 Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Mon, 30 May 2022 16:30:47 -0700 Subject: [PATCH] Create task to delete older than 3 days encrypted messages --- api/logics.py | 2 +- api/models.py | 6 +++--- api/tasks.py | 4 +--- chat/tasks.py | 42 +++++++++++++++++++++++++++++++++++++ robosats/celery/__init__.py | 6 +++++- 5 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 chat/tasks.py diff --git a/api/logics.py b/api/logics.py index 978898c4..ed0550e5 100644 --- a/api/logics.py +++ b/api/logics.py @@ -94,7 +94,7 @@ class Logics: ''' Validates PGP valid keys. Formats them in a way understandable by the frontend ''' gpg = gnupg.GPG() - # Uniform format as linux linebreaks. Windows users submitting their own keys have \r\n breaks. + # Standarize format with linux linebreaks '\n'. Windows users submitting their own keys have '\r\n' breaking communication. enc_priv_key = enc_priv_key.replace('\r\n', '\n') pub_key = pub_key.replace('\r\n', '\n') diff --git a/api/models.py b/api/models.py index 44c62800..b55651ff 100644 --- a/api/models.py +++ b/api/models.py @@ -396,11 +396,11 @@ class Order(models.Model): 11: 1 * 24 * 60 * 60, # 'In dispute' 12: 0, # 'Collaboratively cancelled' 13: 10 * 24 * 60 * 60, # 'Sending satoshis to buyer' - 14: 10 * 24 * 60 * 60, # 'Sucessful trade' + 14: 1 * 24 * 60 * 60, # 'Sucessful trade' 15: 10 * 24 * 60 * 60, # 'Failed lightning network routing' 16: 10 * 24 * 60 * 60, # 'Wait for dispute resolution' - 17: 10 * 24 * 60 * 60, # 'Maker lost dispute' - 18: 10 * 24 * 60 * 60, # 'Taker lost dispute' + 17: 1 * 24 * 60 * 60, # 'Maker lost dispute' + 18: 1 * 24 * 60 * 60, # 'Taker lost dispute' } return t_to_expire[status] diff --git a/api/tasks.py b/api/tasks.py index d1576f1b..3acc4a3f 100644 --- a/api/tasks.py +++ b/api/tasks.py @@ -1,6 +1,5 @@ from celery import shared_task - @shared_task(name="users_cleansing") def users_cleansing(): """ @@ -8,7 +7,7 @@ def users_cleansing(): """ from django.contrib.auth.models import User from django.db.models import Q - from .logics import Logics + from api.logics import Logics from datetime import timedelta from django.utils import timezone @@ -163,7 +162,6 @@ def follow_send_payment(hash): context = {"routing_failed": "The payout invoice has expired"} return False, context - @shared_task(name="cache_external_market_prices", ignore_result=True) def cache_market(): diff --git a/chat/tasks.py b/chat/tasks.py new file mode 100644 index 00000000..61c30d77 --- /dev/null +++ b/chat/tasks.py @@ -0,0 +1,42 @@ +from celery import shared_task + +@shared_task(name="chatrooms_cleansing") +def chatrooms_cleansing(): + """ + Deletes chatrooms and encrypted messages of orders + that have completely finished more than 3 days ago. + """ + + from api.models import Order + from chat.models import ChatRoom + from datetime import timedelta + from django.utils import timezone + + finished_states = [Order.Status.SUC, + Order.Status.TLD, + Order.Status.MLD, + Order.Status.CCA, + Order.Status.UCA] + + # Orders that have expired more than 3 days ago + # Usually expiry takes place 1 day after a finished order. So, ~4 days + # until encrypted messages are deleted. + finished_time = timezone.now() - timedelta(days=3) + queryset = Order.objects.filter(status__in=finished_states, expires_at__lt=finished_time) + + # And do not have an active trade, any past contract or any reward. + deleted_chatrooms = [] + for order in queryset: + # Try an except. In case some chatroom is already missing. + try: + chatroom = ChatRoom.objects.get(id = order.id) + deleted_chatrooms.append(str(chatroom)) + chatroom.delete() + except: + pass + + results = { + "num_deleted": len(deleted_chatrooms), + "deleted_chatrooms": deleted_chatrooms, + } + return results \ No newline at end of file diff --git a/robosats/celery/__init__.py b/robosats/celery/__init__.py index 407248ae..7c91943b 100644 --- a/robosats/celery/__init__.py +++ b/robosats/celery/__init__.py @@ -35,13 +35,17 @@ app.conf.beat_schedule = { "task": "users_cleansing", "schedule": crontab(hour=0, minute=0), }, + "chatrooms-cleansing": { # Cleans 3+ days old encrypted messages and chatrooms at midnight + "task": "chatrooms_cleansing", + "schedule": crontab(hour=0, minute=0), + }, "give-rewards": { # Referral rewards go from 'pending' to 'earned' at midnight "task": "give_rewards", "schedule": crontab(hour=0, minute=0), }, "do-accounting": { # Does accounting for the last day "task": "do_accounting", - "schedule": crontab(hour=23, minute=55), + "schedule": crontab(hour=23, minute=59), }, "cache-market-prices": { # Cache market prices every minute "task": "cache_external_market_prices",