From 94456ffcecfb327fd95b5e8daf79ca0ce275e536 Mon Sep 17 00:00:00 2001 From: Jonathan Keller <19418817+NobodyNada@users.noreply.github.com> Date: Sun, 2 Aug 2020 17:16:34 -0700 Subject: [PATCH] Bugfixes, modernizations, and tweaks --- Package.swift | 5 ++- Sources/FireAlarmCore/PostFetcher.swift | 1 + .../Filters/FilterNonEnglishPost.swift | 2 +- Sources/Frontend/Reporter.swift | 35 ++++++++++------- Sources/Frontend/Utilities.swift | 38 +++++++++++-------- Sources/Frontend/startup.swift | 3 +- 6 files changed, 51 insertions(+), 33 deletions(-) diff --git a/Package.swift b/Package.swift index a5b2d22..f3476b7 100644 --- a/Package.swift +++ b/Package.swift @@ -1,11 +1,12 @@ -// swift-tools-version:5.2 +// swift-tools-version:5.3 +// You can downgrade to an earlier Swift version if needed (5.0 or newer should be OK) import PackageDescription let package = Package( name: "FireAlarm", platforms: [ - .macOS(.v10_12), + .macOS(.v10_16), // You can downgrade to 10.12 or newer if needed ], dependencies: [ .package(url: "git://github.com/SOBotics/SwiftChatSE", from: "5.1.0"), diff --git a/Sources/FireAlarmCore/PostFetcher.swift b/Sources/FireAlarmCore/PostFetcher.swift index 4ae86c0..be2e71b 100644 --- a/Sources/FireAlarmCore/PostFetcher.swift +++ b/Sources/FireAlarmCore/PostFetcher.swift @@ -146,6 +146,7 @@ open class PostFetcher { } for (site, posts) in posts { + if posts.isEmpty { continue } var fetchedPosts = [Post]() var hasMore = true diff --git a/Sources/Frontend/Filters/FilterNonEnglishPost.swift b/Sources/Frontend/Filters/FilterNonEnglishPost.swift index 1f6c127..2a83985 100644 --- a/Sources/Frontend/Filters/FilterNonEnglishPost.swift +++ b/Sources/Frontend/Filters/FilterNonEnglishPost.swift @@ -17,7 +17,7 @@ class FilterNonEnglishPost: Filter { //Take from https://stackoverflow.com/a/27880748/4688119 func regexMatches(for regex: String, in text: String) -> [String] { do { - let regex = try NSRegularExpression(pattern: regex) + let regex = try NSRegularExpression(pattern: regex, options: .caseInsensitive) let results = regex.matches(in: text, range: NSRange(text.startIndex..., in: text)) return results.map { diff --git a/Sources/Frontend/Reporter.swift b/Sources/Frontend/Reporter.swift index 1c6bda6..f81cad0 100644 --- a/Sources/Frontend/Reporter.swift +++ b/Sources/Frontend/Reporter.swift @@ -215,15 +215,15 @@ class Reporter { postFetcher.callback = { [weak self] in try self?.checkAndReport(post: $0, site: $1) } postScanner.filters = [ - FilterNaiveBayes(reporter: self), - FilterNonEnglishPost(), - FilterMisleadingLinks(), + //FilterNaiveBayes(reporter: self), + //FilterNonEnglishPost(), + //FilterMisleadingLinks(), FilterBlacklistedKeyword(reporter: self), FilterBlacklistedUsername(reporter: self), FilterBlacklistedTag(reporter: self), - FilterImageWithoutCode(), - FilterLowLength(), - FilterCodeWithoutExplanation() + //FilterImageWithoutCode(), + //FilterLowLength(), + //FilterCodeWithoutExplanation() ] trollScanner.filters = [ FilterBlacklistedKeyword(reporter: self, troll: true), @@ -273,9 +273,9 @@ class Reporter { ///Reports a post if it has not been recently reported. Returns either .reported or .alreadyReported. func report(post: Post, site apiSiteParameter: String, reasons: [FilterResult]) throws -> ReportResult { - guard let site = try Site.with(apiSiteParameter: apiSiteParameter, db: staticDB) else { + /*guard let site = try Site.with(apiSiteParameter: apiSiteParameter, db: staticDB) else { return ReportResult(status: .notBad, filterResults: reasons) - } + }*/ var status: ReportResult.Status = .notBad @@ -300,6 +300,11 @@ class Reporter { return } + guard let link = post.share_link else { + print("Not reporting \(id) because it has no link.") + return + } + /*if !isManualReport && post.closed_reason != nil { print ("Not reporting \(post.id ?? 0) as it is closed.") status = .alreadyClosed @@ -325,7 +330,7 @@ class Reporter { let sema = DispatchSemaphore(value: 0) - let rooms: [ChatRoom] = trollSites.contains(site) ? self.trollRooms : self.rooms + let rooms = self.rooms //let rooms: [ChatRoom] = trollSites.contains(site) ? self.trollRooms : self.rooms var bonfireLink: String? //Post weight including custom filter weight subtracted from Naive Bayes difference. @@ -349,14 +354,14 @@ class Reporter { for room in rooms { //Filter out weights which are less than this room's threshold. - let reasons = reasons.filter { + /*let reasons = reasons.filter { if case .bayesianFilter(_) = $0.type { return combinedPostWeight < room.thresholds[site.id] ?? Int.min } else if case .customFilterWithWeight(_, _) = $0.type { return combinedPostWeight < room.thresholds[site.id] ?? Int.min } return true - } + }*/ if reasons.isEmpty { sema.signal() @@ -377,13 +382,17 @@ class Reporter { let header = reasons.map { $0.header }.joined(separator: ", ") let message: String + var tagStr: String + if let tag = tags.first { tagStr = "[tag:\(tag)] " } + else { tagStr = "" } + if let bonfireLink = bonfireLink { message = "[ [\(botName)](\(stackAppsLink)) | [Bonfire](\(bonfireLink)) ] " + - "[tag:\(tags.first ?? "tagless")] \(header) [\(title)](//\(site.domain)/q/\(id)) " + + "\(tagStr)\(header) [\(title)](\(link)) " + room.notificationString(tags: tags, reasons: reasons) } else { message = "[ [\(botName)](\(stackAppsLink)) ] " + - "[tag:\(tags.first ?? "tagless")] \(header) [\(title)](//\(site.domain)/q/\(id)) " + + "\(tagStr)\(header) [\(title)](\(link)) " + room.notificationString(tags: tags, reasons: reasons) } diff --git a/Sources/Frontend/Utilities.swift b/Sources/Frontend/Utilities.swift index 92526e3..8e586dd 100644 --- a/Sources/Frontend/Utilities.swift +++ b/Sources/Frontend/Utilities.swift @@ -190,19 +190,23 @@ func run(command: String, printOutput: Bool = true) -> (exitCode: Int, stdout: S let stderrSource = DispatchSource.makeReadSource(fileDescriptor: stderrPipe.fileHandleForReading.fileDescriptor) stdoutSource.setEventHandler { - let data = stdoutPipe.fileHandleForReading.availableData - stdout += data - combined += data - if printOutput { - FileHandle.standardError.write(data) + queue.sync { + let data = stdoutPipe.fileHandleForReading.availableData + stdout += data + combined += data + if printOutput { + FileHandle.standardError.write(data) + } } } stderrSource.setEventHandler { - let data = stderrPipe.fileHandleForReading.availableData - stderr += data - combined += data - if printOutput { - FileHandle.standardOutput.write(data) + queue.sync { + let data = stderrPipe.fileHandleForReading.availableData + stderr += data + combined += data + if printOutput { + FileHandle.standardOutput.write(data) + } } } @@ -212,12 +216,14 @@ func run(command: String, printOutput: Bool = true) -> (exitCode: Int, stdout: S process.launch() process.waitUntilExit() - queue.sync {} - stdoutSource.cancel() - stderrSource.cancel() - queue.sync {} - stdoutPipe.fileHandleForReading.closeFile() - stderrPipe.fileHandleForReading.closeFile() + queue.sync { + stdoutSource.cancel() + stderrSource.cancel() + } + queue.sync { + stdoutPipe.fileHandleForReading.closeFile() + stderrPipe.fileHandleForReading.closeFile() + } let stdoutString = String(data: stdout, encoding: .utf8) let stderrString = String(data: stderr, encoding: .utf8) diff --git a/Sources/Frontend/startup.swift b/Sources/Frontend/startup.swift index 6d8b8cc..eb323e0 100644 --- a/Sources/Frontend/startup.swift +++ b/Sources/Frontend/startup.swift @@ -60,7 +60,7 @@ func main() throws { saveURL = saveDirURL apiClient.key = "HNA2dbrFtyTZxeHN6rThNg((" - apiClient.defaultFilter = "!21PcIZT2MQcDURcNm2uJH" + apiClient.defaultFilter = "!*2TT1aq3F80e34)G*C84ugY4)D53V1hQSOTMtjcj5" let client = Client() @@ -233,6 +233,7 @@ func main() throws { // ChatRoom(client: client, host: .stackOverflow, roomID: 111347), //SOBotics // ChatRoom(client: client, host: .stackOverflow, roomID: 41570), //SO Close Vote Reviewers // ChatRoom(client: client, host: .stackExchange, roomID: 54445), //SEBotics + ChatRoom(client: client, host: .stackOverflow, roomID: 167908) // SOBotics Workshop ] development = false