diff --git a/Sources/CommandReport.swift b/Sources/CommandReport.swift new file mode 100644 index 0000000..68fdd8a --- /dev/null +++ b/Sources/CommandReport.swift @@ -0,0 +1,64 @@ +// +// CommandReport.swift +// FireAlarm +// +// Created by Ashish Ahuja on 2/26/17. +// Copyright © 2017 Ashish Ahuja (Fortunate-MAN). All rights reserved. +// + +import Foundation +import SwiftChatSE +import SwiftStack +import Dispatch + +class CommandReport: Command { + override class func usage() -> [String] { + return ["report print ...", "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]) { + questionID = id + } + else if let url = URL(string: arguments[0]), let id = postIDFromURL(url) { + questionID = id + } + else { + reply("Please enter a valid post ID or URL.") + return + } + + guard let question = try apiClient.fetchQuestion(questionID).items?.first else { + reply("Could not fetch the question!") + return + } + + if usageIndex == 0 + { + var newTitle = "\(question.title ?? "")" + + newTitle = newTitle.replacingOccurrences(of: "[", with: "\\[") + newTitle = newTitle.replacingOccurrences(of: "]", with: "\\]") + + let messagePost = "[ [\(botName)](\(stackAppsLink)) ] " + + "[tag:\(tags(for: question).first ?? "")] Manually reported post [\(newTitle)](//stackoverflow.com/q/\(questionID!))" + + message.room.postMessage(messagePost) + } + } +} diff --git a/Sources/Filter.swift b/Sources/Filter.swift index c404003..edd628b 100644 --- a/Sources/Filter.swift +++ b/Sources/Filter.swift @@ -363,6 +363,7 @@ class Filter { case bayesianFilter(difference: Int) case blacklistedUsername case misleadingLink + case manuallyReported } enum ReportResult { @@ -430,19 +431,25 @@ class Filter { header = "Blacklisted username:" case .misleadingLink: header = "Misleading link:" + case .manuallyReported: + header = "Manually reported question:" } reportedPosts.append((id: id, when: Date(), difference: difference)) for room in rooms { if difference < room.threshold { - /*let title = post.title - title = title.replacingOccurrences(of: "[", with: "\\[") - title = title.replacingOccurrences(of: "]", with: "\\]")*/ + var newTitle = "\(post.title ?? "")" + + newTitle = newTitle.replacingOccurrences(of: "[", with: "\\[") + newTitle = newTitle.replacingOccurrences(of: "]", with: "\\]") + let message = "[ [\(botName)](\(stackAppsLink)) ] " + - "[tag:\(tags(for: post).first ?? "tagless")] \(header) [\(post.title ?? "")](//stackoverflow.com/q/\(id)) (filter score: \(difference))" + + "[tag:\(tags(for: post).first ?? "tagless")] \(header) [\(newTitle)](//stackoverflow.com/q/\(id)) (filter score: \(difference))" + room.notificationString(tags: tags(for: post), reason: reason) + room.postMessage(message) + } } diff --git a/Sources/main.swift b/Sources/main.swift index bbad66d..04874f1 100644 --- a/Sources/main.swift +++ b/Sources/main.swift @@ -19,7 +19,7 @@ let commands: [Command.Type] = [ CommandCheckPost.self, CommandQuota.self, CommandBlacklistUsername.self, CommandGetBlacklistedUsernames.self, CommandUnblacklistUsername.self, CommandOptIn.self, CommandOptOut.self, CommandCheckNotification.self, CommandLeaveRoom.self, - CommandLocation.self, + CommandLocation.self, CommandReport.self, ] @@ -100,8 +100,13 @@ extension ChatRoom { shouldNotify = true } - } - + + case .manuallyReported: + if shouldNotify == true { + shouldNotify = true + } + + } } if shouldNotify { @@ -321,11 +326,10 @@ func main() throws { var updated = false while !updated { //wait one minute - sleep(60) + /*sleep(60) if !updated && !development { updated = update(listener, rooms, auto: true) - } - + }*/ save() } }