Skip to content

Commit

Permalink
Merge pull request #13 from SOBotics/report_command_fix
Browse files Browse the repository at this point in the history
Report command fix
  • Loading branch information
double-fault committed May 4, 2017
2 parents b15eeb2 + 01401a1 commit eb71816
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 42 deletions.
36 changes: 13 additions & 23 deletions Sources/CommandReport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,13 @@ import Dispatch

class CommandReport: Command {
override class func usage() -> [String] {
return ["report print ...", "report ..."]
return ["report ..."]
}

override class func privileges() -> ChatUser.Privileges {
return .owner
}

func tags(for post: Post) -> [String] {
if let q = post as? Question {
return q.tags ?? []
} else if let a = post as? Answer {
return a.tags ?? []
} else {
return []
}
}

override func run() throws {
var questionID: Int!
if let id = Int(arguments[0]) {
Expand All @@ -43,22 +33,22 @@ class CommandReport: Command {
return
}

if reporter == nil {
reply("Waiting for the filter to load...")
repeat {
sleep(1)
} while reporter == nil
}

guard let question = try apiClient.fetchQuestion(questionID).items?.first else {
reply("Could not fetch the question!")
return
}

if usageIndex == 0
{
var newTitle = "\(question.title ?? "<no title>")"

newTitle = newTitle.replacingOccurrences(of: "[", with: "\\[")
newTitle = newTitle.replacingOccurrences(of: "]", with: "\\]")

let messagePost = "[ [\(botName)](\(stackAppsLink)) ] " +
"[tag:\(tags(for: question).first ?? "<unknown tag>")] Manually reported post [\(newTitle)](//stackoverflow.com/q/\(questionID!))"

message.room.postMessage(messagePost)
}
var filterResult = reporter.checkPost(question)

filterResult [0] = FilterResult (type: .manuallyReported, header: "Manually reported question", details: "Question manually reported.")

reporter.report(post: question, reasons: filterResult)
}
}
2 changes: 1 addition & 1 deletion Sources/Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ extension Post {
}
}


struct FilterResult {
enum ResultType {
case bayesianFilter(difference: Int)
case customFilter(filter: Filter)
case manuallyReported
}

let type: ResultType
Expand Down
47 changes: 29 additions & 18 deletions Sources/Reporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,36 @@ class Reporter {
print("No post ID!")
return .notBad
}

var manuallyReported = false

let _ = reasons.filter {
if case .manuallyReported = $0.type {
manuallyReported = true
return true
}
return false
}

if (post.closed_reason != nil) {
print ("Not reporting \(post.id ?? 0) as it is closed.")
return .alreadyClosed
}

if let minDate: Date = Calendar(identifier: .gregorian).date(byAdding: DateComponents(hour: -6), to: Date()) {
let recentlyReportedPosts = reportedPosts.filter {
$0.when > minDate
}
if recentlyReportedPosts.contains(where: { $0.id == id }) {
print("Not reporting \(id) because it was recently reported.")
return .alreadyReported
}
}
else {
rooms.forEach {$0.postMessage("Failed to calculate minimum report date!")}
}

if manuallyReported == false {
if let minDate: Date = Calendar(identifier: .gregorian).date(byAdding: DateComponents(hour: -6), to: Date()) {
let recentlyReportedPosts = reportedPosts.filter {
$0.when > minDate
}
if recentlyReportedPosts.contains(where: { $0.id == id }) {
print("Not reporting \(id) because it was recently reported.")
return .alreadyReported
}
}
else {
rooms.forEach {$0.postMessage("Failed to calculate minimum report date!")}
}

if (post.closed_reason != nil) {
print ("Not reporting \(post.id ?? 0) as it is closed.")
return .alreadyClosed
}
}

var reported = false

Expand Down

0 comments on commit eb71816

Please sign in to comment.