Skip to content

Commit 6e75c1d

Browse files
Added error status and error notify status to /info command
urlwatcher now creates a .notified file after notifying the user of an error and will not send any further messages Fixed urlwatcher deleting error instead of .error
1 parent 5ebab3a commit 6e75c1d

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

NotifierBot/Sources/Notifier/Commands/InfoCommand.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ struct InfoCommand: Command {
6161
durationString = SharedUtils.muteDurationFormatter.string(from: restDuration) ?? ""
6262
}
6363
lines.append("- Muted: \(e.isMuted ? "Yes (\(durationString) hours remaining)" : "No")".escaped())
64+
let entryPath = SharedUtils.directory(for: e)
65+
66+
let errorFile = "\(entryPath)/.error"
67+
let errored = FileManager.default.fileExists(atPath: errorFile)
68+
// The empty string will propagate the error to the formatter below
69+
let errorFileContents = try? String(contentsOfFile: errorFile) ?? ""
70+
let errorDate = ISO8601DateFormatter().date(from: errorFileContents)
71+
var durationString: String? = nil
72+
if let errorDate = errorDate {
73+
let errorDuration = Date().timeIntervalSince(errorDate)
74+
durationString = SharedUtils.muteDurationFormatter.string(from: errorDuration)
75+
}
76+
lines.append("- Errored: \(errored ? "Yes (for \(durationString ?? "an unknown time"))" : "No")")
77+
let notified = FileManager.default.fileExists(atPath: "\(entryPath)/.notified")
78+
lines.append(" - Notified: \(notified ? "Yes" : "No")")
6479
return lines.joined(separator: "\n")
6580
}
6681

Shared/Sources/Shared/SharedUtils.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ public struct SharedUtils {
7676
return DummyFormatter(fullUnits: false)
7777
}
7878

79+
public static func directory(for entry: URLEntry) -> String {
80+
// Use the name and chat id to create unique directories
81+
return "\(kImagesDirectory)/\(entry.name).\(entry.chatID)"
82+
}
83+
7984
}

urlwatcher/Sources/urlwatcher/Functions.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ 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 entryPath = SharedUtils.directory(for: entry)
148+
let errorFile = "\(entryPath)/.error"
148149
// If the errorReportMinutes are not set, we immediately notify the user
149150
guard kErrorReportDuration > 0 else {
150151
try notifyError(entry: entry)
@@ -163,8 +164,16 @@ func handleScreenshotError(entry: URLEntry) throws {
163164
return
164165
}
165166

166-
// If an error file already exists, read the creation date from the file's contents
167+
// If an error file already exists
167168
print("Error file already exists")
169+
170+
// Check if the user has already been notified
171+
if fileManager.fileExists(atPath: "\(entryPath)/.notified") {
172+
// User has already been notified
173+
return
174+
}
175+
176+
// Read the creation date from the file's contents
168177
if fileManager.fileExists(atPath: errorFile) {
169178
let errorContents = try String(contentsOfFile: errorFile)
170179
print("Creation date: \(errorContents)")
@@ -173,7 +182,12 @@ func handleScreenshotError(entry: URLEntry) throws {
173182
}
174183
if creationDate.distance(to: Date()) >= kErrorReportDuration {
175184
// Notify the user that an error persisted for the last `errorReportTime` seconds
185+
// and create the "notified" file to indicate that the user has already been notified about this error
176186
try notifyError(entry: entry)
187+
let notifiedFile = "\(SharedUtils.directory(for: entry))/.notified"
188+
if fileManager.fileExists(atPath: notifiedFile) {
189+
try fileManager.removeItem(atPath: notifiedFile)
190+
}
177191
}
178192
}
179193
}

urlwatcher/Sources/urlwatcher/Utils.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ enum BashResult: Equatable {
2626
}
2727
}
2828

29-
func directory(for entry: URLEntry) -> String {
30-
// Use the name and chat id to create unique directories
31-
return "\(kImagesDirectory)/\(entry.name).\(entry.chatID)"
32-
}
33-
3429
@discardableResult
3530
func bash(_ command: String, arguments: [String] = [], noEnv: Bool = false, currentDirectory: String? = nil, standardOutput: Any? = nil, standardError: Any? = nil) throws -> BashResult {
3631
let proc = Process()

urlwatcher/Sources/urlwatcher/main.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ do {
6464
try ConfigParser.saveConfig(config)
6565
}
6666

67-
let entryPath = directory(for: entry)
67+
let entryPath = SharedUtils.directory(for: entry)
6868

6969
// Create the image directory, if it does not exist yet
7070
if !fileManager.directoryExists(atPath: entryPath) {
@@ -226,10 +226,14 @@ do {
226226
}
227227

228228
// Delete the error file if it exists, since we just successfully captured a screenshot
229-
let errorFile = "\(entryPath)/error"
229+
let errorFile = "\(entryPath)/.error"
230230
if fileManager.fileExists(atPath: errorFile) {
231231
try fileManager.removeItem(atPath: errorFile)
232232
}
233+
let notifiedFile = "\(entryPath)/.notified"
234+
if fileManager.fileExists(atPath: notifiedFile) {
235+
try fileManager.removeItem(atPath: notifiedFile)
236+
}
233237
}
234238

235239
} catch let error {

0 commit comments

Comments
 (0)