Skip to content

Commit

Permalink
Bugfixes, modernizations, and tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
NobodyNada committed Aug 3, 2020
1 parent 22ea51e commit 94456ff
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
5 changes: 3 additions & 2 deletions 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"),
Expand Down
1 change: 1 addition & 0 deletions Sources/FireAlarmCore/PostFetcher.swift
Expand Up @@ -146,6 +146,7 @@ open class PostFetcher {
}

for (site, posts) in posts {
if posts.isEmpty { continue }
var fetchedPosts = [Post]()

var hasMore = true
Expand Down
2 changes: 1 addition & 1 deletion Sources/Frontend/Filters/FilterNonEnglishPost.swift
Expand Up @@ -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 {
Expand Down
35 changes: 22 additions & 13 deletions Sources/Frontend/Reporter.swift
Expand Up @@ -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),
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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()
Expand All @@ -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)
}

Expand Down
38 changes: 22 additions & 16 deletions Sources/Frontend/Utilities.swift
Expand Up @@ -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)
}
}
}

Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion Sources/Frontend/startup.swift
Expand Up @@ -60,7 +60,7 @@ func main() throws {
saveURL = saveDirURL

apiClient.key = "HNA2dbrFtyTZxeHN6rThNg(("
apiClient.defaultFilter = "!21PcIZT2MQcDURcNm2uJH"
apiClient.defaultFilter = "!*2TT1aq3F80e34)G*C84ugY4)D53V1hQSOTMtjcj5"

let client = Client()

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 94456ff

Please sign in to comment.