.select(
domains.Domain,
)
- .join(
- sources.Source,
- domains.Domain.source_id == sources.Source.id,
- )
.join(
lists.List,
- sources.Source.list_id == lists.List.id,
+ lists.List.id == domains.Domain.list_id,
)
.where(
+ # List cannot be deleted
+ lists.List.deleted_at == None,
+
+ # Domains cannot be deleted
+ domains.Domain.removed_at == None,
+
+ # Look for an exact match of the name or subdomains
sqlmodel.or_(
domains.Domain.name == name,
sqlmodel.literal(name).like("%." + domains.Domain.name),
),
- domains.Domain.removed_at == None,
- sources.Source.deleted_at == None,
- lists.List.deleted_at == None,
)
)
# Group all matches by list
for domain in self.db.fetch(stmt):
try:
- res[domain.source.list].append(domain)
+ res[domain.list].append(domain.name)
except KeyError:
- res[domain.source.list] = [domain]
+ res[domain.list] = [domain.name]
+
+ # Search the history for the longest match
+ for list, matches in res.items():
+ # Sort the domains by length
+ matches = sorted(matches, key=lambda x: len(x))
+
+ # Fetch the longest match
+ domain = matches.pop()
+
+ # Fetch the history of the longest match
+ for history in list.get_domain_history(domain, limit=1):
+ res[list] = history
+ break
return res
"""
Performs a simple search
"""
+ res = {}
+
# Perform the search
results = backend.search(q)
- # Return the result as a mapping of the list slug and a list of matching domain names
- return {
- list.slug : set(domain.name for domain in results[list]) for list in results
- }
+ # Format the result
+ for list, event in results.items():
+ if event.allows:
+ status = "ALLOWED"
+ elif event.blocks:
+ status = "BLOCKED"
+
+ # Skip if the domain is not listed
+ else:
+ continue
+
+ # Append result
+ res[list.slug] = {
+ "domain" : event.domain.name,
+ "changed_at" : event.timestamp,
+ "status" : status,
+ }
+
+ return res