From a460444a919e2e30b6b6526debc6a3d0bbad6c23 Mon Sep 17 00:00:00 2001
From: kaiyou <dev@kaiyou.fr>
Date: Fri, 23 Jul 2021 17:12:05 +0200
Subject: [PATCH] Add a temporary account purge feature

---
 hiboo/cli.py    |  8 ++++++++
 hiboo/models.py | 11 +++++++++++
 2 files changed, 19 insertions(+)

diff --git a/hiboo/cli.py b/hiboo/cli.py
index 41153f0..e9e70ea 100644
--- a/hiboo/cli.py
+++ b/hiboo/cli.py
@@ -1,4 +1,5 @@
 from flask import cli
+from hiboo import models
 from hiboo.profile import common
 
 import time
@@ -21,3 +22,10 @@ def tasks_loop():
     while True:
         common.apply_all_transitions()
         time.sleep(30)
+
+
+@tasks.command("purge")
+def purge():
+    # Temporary command to list purgeable accounts before we include
+    # this in the main task loop
+    print(models.User.get_unused().all())
diff --git a/hiboo/models.py b/hiboo/models.py
index 518dec9..b54eef9 100644
--- a/hiboo/models.py
+++ b/hiboo/models.py
@@ -114,6 +114,17 @@ class User(db.Model):
         profile.status = Profile.ACTIVE
         return profile
 
+    @classmethod
+    def get_unused(cls, interval=datetime.timedelta(hours=2)):
+        return (cls.query
+            .join(cls.profiles.and_(Profile.status != Profile.PURGED), isouter=True)
+            .filter(Profile.uuid == None)
+            .filter(
+                datetime.datetime.now() - interval >
+                sqlalchemy.sql.func.coalesce(cls.updated_at, cls.created_at)
+            )
+        )
+
 
 class Auth(db.Model):
     """ An authenticator is a method to authenticate a user.
-- 
GitLab