Auto-delete logs finished + custom App-to-VPN messages

This commit is contained in:
relikd
2020-06-28 23:55:08 +02:00
parent 08483711e2
commit 6dcc2086e6
7 changed files with 142 additions and 18 deletions

View File

@@ -39,9 +39,21 @@ extension SQLiteDatabase {
/// `INSERT INTO cache (dns, opt) VALUES (?, ?);`
func logWrite(_ domain: String, blocked: Bool = false) throws {
try self.run(sql: "INSERT INTO cache (dns, opt) VALUES (?, ?);",
bind: [BindText(domain), BindInt32(blocked ? 1 : 0)])
bind: [BindText(domain), BindInt32(blocked ? 1 : 0)])
{ try ifStep($0, SQLITE_DONE) }
}
/// `DELETE FROM cache WHERE ts < (now - ? days);`
/// - Parameter days: if `0` or negative, this function does nothing.
/// - Returns: `true` if at least one row was deleted.
@discardableResult func dnsLogsDeleteOlderThan(days: Int) throws -> Bool {
guard days > 0 else { return false }
return try self.run(sql: "DELETE FROM cache WHERE ts < strftime('%s', 'now', ?);",
bind: [BindText("-\(days) days")]) {
try ifStep($0, SQLITE_DONE)
return numberOfChanges > 0
}
}
}

View File

@@ -15,7 +15,7 @@ struct TheGreatDestroyer {
}
}
/// Fired when user taps on Settings -> Delete All Logs
/// Fired when user taps on Settings -> "Delete All Logs"
static func deleteAllLogs() {
sync.pause()
DispatchQueue.global().async {
@@ -26,4 +26,18 @@ struct TheGreatDestroyer {
} catch {}
}
}
/// Fired when user changes Settings -> "Auto-delete logs" and every time the App enters foreground
static func deleteLogs(olderThan days: Int) {
guard days > 0 else { return }
sync.pause()
DispatchQueue.global().async {
defer { sync.continue() }
QLog.Info("Auto-delete logs")
guard let success = try? AppDB?.dnsLogsDeleteOlderThan(days: days), success else {
return // nothing changed
}
sync.needsReloadDB()
}
}
}