Create task to delete older than 3 days encrypted messages

This commit is contained in:
Reckless_Satoshi 2022-05-30 16:30:47 -07:00
parent bedac53e29
commit 8b7e05a24d
No known key found for this signature in database
GPG Key ID: 9C4585B561315571
5 changed files with 52 additions and 8 deletions

View File

@ -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')

View File

@ -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]

View File

@ -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():

42
chat/tasks.py Normal file
View File

@ -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

View File

@ -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",