]> git.ipfire.org Git - dbl.git/commitdiff
lists: Split whitelisted domains on history
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 31 Dec 2025 15:42:19 +0000 (15:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 31 Dec 2025 15:42:19 +0000 (15:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/dnsbl/api/lists.py
src/dnsbl/lists.py

index a2231a76c1b73ea5cb9dcbedaac2bcf78868e802..766431402c7b5777ce93416fe0a2f9ae9850264a 100644 (file)
@@ -65,7 +65,8 @@ def get_list_history(
        return [
                {
                        "ts"              : e.ts,
-                       "domains_added"   : e.domains_added,
+                       "domains_blocked" : e.domains_blocked,
+                       "domains_allowed" : e.domains_allowed,
                        "domains_removed" : e.domains_removed,
                } for e in list.get_history(before=before, limit=limit)
        ]
index 328562a7aa69e4b99a378da19ca815fa030254f8..99c29ec812d0561029cb8c5935e411c648b6c6e1 100644 (file)
@@ -483,7 +483,7 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
                        Fetches the history of the list
                """
                # Collect all domains that have been added
-               domains_added = (
+               domains_blocked = (
                        sqlmodel
                        .select(
                                domains.Domain.added_at.label("ts"),
@@ -498,6 +498,34 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
                        )
                        .where(
                                domains.Domain.list == self,
+                               domains.Domain.block == True,
+                       )
+                       .order_by(
+                               domains.Domain.added_at.desc(),
+                       )
+                       .group_by(
+                               domains.Domain.added_at,
+                       )
+                       .limit(limit)
+               )
+
+               # Collect all domains that have been allowed
+               domains_allowed = (
+                       sqlmodel
+                       .select(
+                               domains.Domain.added_at.label("ts"),
+
+                               # Aggregate all domains
+                               sqlmodel.func.array_agg(
+                                       sqlalchemy.dialects.postgresql.aggregate_order_by(
+                                               domains.Domain.name,
+                                               domains.Domain.name.asc(),
+                                       ),
+                               ).label("names"),
+                       )
+                       .where(
+                               domains.Domain.list == self,
+                               domains.Domain.block == False,
                        )
                        .order_by(
                                domains.Domain.added_at.desc(),
@@ -537,7 +565,11 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
 
                # Filter if we want data before a certain point in time
                if before:
-                       domains_added = domains_added.where(
+                       domains_blocked = domains_blocked.where(
+                               domains.Domain.added_at < before,
+                       )
+
+                       domains_allowed = domains_allowed.where(
                                domains.Domain.added_at < before,
                        )
 
@@ -546,7 +578,8 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
                        )
 
                # Create CTEs
-               domains_added   = domains_added.cte("domains_added")
+               domains_blocked = domains_blocked.cte("domains_blocked")
+               domains_allowed = domains_allowed.cte("domains_allowed")
                domains_removed = domains_removed.cte("domains_removed")
 
                # Union timestamps
@@ -554,7 +587,10 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
                        sqlmodel
                        .union(
                                sqlmodel.select(
-                                       domains_added.c.ts,
+                                       domains_blocked.c.ts,
+                               ),
+                               sqlmodel.select(
+                                       domains_allowed.c.ts,
                                ),
                                sqlmodel.select(
                                        domains_removed.c.ts,
@@ -568,11 +604,17 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
                        .select(
                                timestamps.c.ts,
 
-                               # Fetch all added domains
+                               # Fetch all blocked domains
                                sqlmodel.func.coalesce(
-                                       domains_added.c.names,
+                                       domains_blocked.c.names,
                                        sqlmodel.text("'{}'"),
-                               ).label("domains_added"),
+                               ).label("domains_blocked"),
+
+                               # Fetch all allowed domains
+                               sqlmodel.func.coalesce(
+                                       domains_allowed.c.names,
+                                       sqlmodel.text("'{}'"),
+                               ).label("domains_allowed"),
 
                                # Fetch all removed domains
                                sqlmodel.func.coalesce(
@@ -581,8 +623,12 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
                                ).label("domains_removed"),
                        )
                        .outerjoin(
-                               domains_added,
-                               domains_added.c.ts == timestamps.c.ts,
+                               domains_blocked,
+                               domains_blocked.c.ts == timestamps.c.ts,
+                       )
+                       .outerjoin(
+                               domains_allowed,
+                               domains_allowed.c.ts == timestamps.c.ts,
                        )
                        .outerjoin(
                                domains_removed,