Skip to content

Commit 5ebab3a

Browse files
urlwacher now writes error file creation date into the file itself instead of reading the creation date (does not seem to work on Linux)
urlwatcher now sends a message to all admins on errors Error file is now .error instead of error
1 parent d312e35 commit 5ebab3a

File tree

4 files changed

+186
-160
lines changed

4 files changed

+186
-160
lines changed

Shared/Sources/Shared/PermissionHandler.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ public class PermissionHandler {
1818
return configParser.permissionGroup(user: userID) >= permission
1919
}
2020

21+
public var admins: [Int64] {
22+
Array(configParser.permissions.filter { (_, level: BotPermission) in
23+
level == .admin
24+
}.keys)
25+
}
26+
2127
}

urlwatcher/Sources/urlwatcher/Functions.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func sendTelegramMessage(_ message: String, to chatID: Int, image: String? = nil
144144
}
145145

146146
func handleScreenshotError(entry: URLEntry) throws {
147-
let errorFile = "\(directory(for: entry))/error"
147+
let errorFile = "\(directory(for: entry))/.error"
148148
// If the errorReportMinutes are not set, we immediately notify the user
149149
guard kErrorReportDuration > 0 else {
150150
try notifyError(entry: entry)
@@ -158,15 +158,19 @@ func handleScreenshotError(entry: URLEntry) throws {
158158
if !fileManager.fileExists(atPath: errorFile) {
159159
print("Creating error file at \(errorFile)")
160160
// If the error file does not exist yet, create a new one and return
161-
fileManager.createFile(atPath: errorFile, contents: nil)
161+
let content = ISO8601DateFormatter().string(from: Date())
162+
try content.write(toFile: errorFile, atomically: true, encoding: .utf8)
162163
return
163164
}
164165

165-
// If an error file already exists, get the attributes and check the creation date
166+
// If an error file already exists, read the creation date from the file's contents
166167
print("Error file already exists")
167-
if fileManager.fileExists(atPath: errorFile),
168-
let creationDate = try fileManager.attributesOfItem(atPath: errorFile)[.creationDate] as? Date {
169-
print("Creation date: \(creationDate) (\(creationDate.distance(to: Date()) / 3600) hours ago")
168+
if fileManager.fileExists(atPath: errorFile) {
169+
let errorContents = try String(contentsOfFile: errorFile)
170+
print("Creation date: \(errorContents)")
171+
guard let creationDate = ISO8601DateFormatter().date(from: errorContents) else {
172+
throw JFUrlwatcherError.noErrorCreationDate(entry)
173+
}
170174
if creationDate.distance(to: Date()) >= kErrorReportDuration {
171175
// Notify the user that an error persisted for the last `errorReportTime` seconds
172176
try notifyError(entry: entry)
@@ -183,8 +187,8 @@ func notifyError(entry: URLEntry) throws {
183187
// TODO: Replace with DateComponentsFormatter when available on Linux
184188
let f = SharedUtils.DummyFormatter(fullUnits: true)
185189
let durationString = f.string(from: kErrorReportDuration) ?? "some time"
186-
try sendTelegramMessage("The entry '\(entry.name)' failed to capture a screenshot for at least " +
187-
"\(durationString.isEmpty ? "some time" : durationString).",
190+
try sendTelegramMessage("The entry '\(entry.name)' failed to capture a screenshot for " +
191+
"\(durationString.isEmpty ? "some time" : "at least \(durationString)").",
188192
to: Int(entry.chatID))
189193
}
190194

urlwatcher/Sources/urlwatcher/Utils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import Foundation
99
import Shared
1010

11+
enum JFUrlwatcherError: Error {
12+
case noErrorCreationDate(URLEntry)
13+
}
14+
1115
enum BashResult: Equatable {
1216
case success
1317
case failure(Int32)

0 commit comments

Comments
 (0)