From ee06d680471bb95561b4e5ab8b1488fef83b9cc2 Mon Sep 17 00:00:00 2001
From: Houkime <>
Date: Fri, 6 Sep 2024 08:40:32 +0000
Subject: [PATCH] feature(backups): allow non-autobackup slices for full
 restoration

---
 selfprivacy_api/backup/__init__.py | 18 ++++++++----------
 selfprivacy_api/backup/tasks.py    |  2 +-
 tests/test_autobackup.py           |  4 ++--
 3 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/selfprivacy_api/backup/__init__.py b/selfprivacy_api/backup/__init__.py
index a85d16d..c80464c 100644
--- a/selfprivacy_api/backup/__init__.py
+++ b/selfprivacy_api/backup/__init__.py
@@ -760,9 +760,7 @@ class Backups:
 
     @staticmethod
     def is_same_slice(snap1: Snapshot, snap2: Snapshot) -> bool:
-        # Protologic for slicing. Determines if the snaps were made during the same
-        # autobackup operation
-        # TODO: Replace with slice id tag comparison
+        # Determines if the snaps were made roughly in the same time period
 
         period_minutes = Backups.autobackup_period_minutes()
         # Autobackups are not guaranteed to be enabled during restore.
@@ -779,27 +777,27 @@ class Backups:
         return True
 
     @staticmethod
-    def last_autobackup_slice() -> List[Snapshot]:
+    def last_backup_slice() -> List[Snapshot]:
         """
         Guarantees that the slice is valid, ie, it has an api snapshot too
         Or empty
         """
         slice: List[Snapshot] = []
 
-        # We need snapshots that were made in the same autobackup session.
+        # We need snapshots that were made around the same time.
         # And we need to be sure that api snap is in there
         # That's why we form the slice around api snap
-        api_autosnaps = Backups._auto_snaps(ServiceManager())
-        if api_autosnaps == []:
+        api_snaps = Backups.get_snapshots(ServiceManager())
+        if api_snaps == []:
             return []
 
-        api_autosnaps.sort(key=lambda x: x.created_at, reverse=True)
-        api_snap = api_autosnaps[0]  # pick the latest one
+        api_snaps.sort(key=lambda x: x.created_at, reverse=True)
+        api_snap = api_snaps[0]  # pick the latest one
 
         for service in ServiceManager.get_all_services():
             if isinstance(service, ServiceManager):
                 continue
-            snaps = Backups._auto_snaps(service)
+            snaps = Backups.get_snapshots(service)
             snaps.sort(key=lambda x: x.created_at, reverse=True)
             for snap in snaps:
                 if Backups.is_same_slice(snap, api_snap):
diff --git a/selfprivacy_api/backup/tasks.py b/selfprivacy_api/backup/tasks.py
index 4c4426d..a8c2340 100644
--- a/selfprivacy_api/backup/tasks.py
+++ b/selfprivacy_api/backup/tasks.py
@@ -127,7 +127,7 @@ def eligible_for_full_restoration(snap: Snapshot):
 
 
 def which_snapshots_to_full_restore() -> list[Snapshot]:
-    autoslice = Backups.last_autobackup_slice()
+    autoslice = Backups.last_backup_slice()
     api_snapshot = None
     for snap in autoslice:
         if snap.service_name == "api":
diff --git a/tests/test_autobackup.py b/tests/test_autobackup.py
index 862f4e8..08d61ca 100644
--- a/tests/test_autobackup.py
+++ b/tests/test_autobackup.py
@@ -217,7 +217,7 @@ def test_slices_with_autobackups_disabled(backups, only_dummy_service_and_api):
     snaps = Backups.get_all_snapshots()
     assert len(snaps) == 2
 
-    slice = Backups.last_autobackup_slice()
+    slice = Backups.last_backup_slice()
     assert len(slice) == 2
     assert set([snap.id for snap in slice]) == set([snap.id for snap in snaps])
 
@@ -239,7 +239,7 @@ def test_slices_minimal(backups, only_dummy_service_and_api):
     snaps = Backups.get_all_snapshots()
     assert len(snaps) == 2
 
-    slice = Backups.last_autobackup_slice()
+    slice = Backups.last_backup_slice()
     assert len(slice) == 2
     assert set([snap.id for snap in slice]) == set([snap.id for snap in snaps])