Skip to content

Commit

Permalink
allow a per-room threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Keller committed Feb 9, 2017
1 parent 528c2bd commit 5278f48
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Sources/CommandBlacklistUsername.swift
Expand Up @@ -15,7 +15,7 @@ class CommandBlacklistUsername: Command {
}

override class func privileges() -> ChatUser.Privileges {
return [.blacklist]
return .filter
}


Expand Down
20 changes: 20 additions & 0 deletions Sources/CommandCheckThreshold.swift
@@ -0,0 +1,20 @@
//
// CommandCheckThreshold.swift
// FireAlarm
//
// Created by NobodyNada on 2/9/17.
//
//

import Foundation
import SwiftChatSE

class CommandCheckThreshold: Command {
override class func usage() -> [String] {
return ["threshold", "check threshold", "get threshold", "current threshold"]
}

override func run() throws {
message.room.postReply("The threshold for this room is \(message.room.threshold) (*higher* thresholds report more posts).", to: message)
}
}
31 changes: 31 additions & 0 deletions Sources/CommandSetThreshold.swift
@@ -0,0 +1,31 @@
//
// CommandSetThreshold.swift
// FireAlarm
//
// Created by NobodyNada on 2/9/17.
//
//

import Foundation
import SwiftChatSE

class CommandSetThreshold: Command {
override public class func usage() -> [String] {
return ["set threshold *", "change threshold *", "change threshold to *"]
}

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

override func run() throws {
guard let newThreshold = (arguments.first.map({Int($0)}) ?? nil) else {
message.reply("Please specify a valid threshold.")
return
}

message.room.threshold = newThreshold

message.reply("The threshold for this room has been set to \(newThreshold) (*higher* thresholds report more posts).")
}
}
2 changes: 1 addition & 1 deletion Sources/CommandUnblacklistUsername.swift
Expand Up @@ -15,7 +15,7 @@ class CommandUnblacklistUsername: Command {
}

override class func privileges() -> ChatUser.Privileges {
return [.blacklist]
return .filter
}

override func run() throws {
Expand Down
21 changes: 12 additions & 9 deletions Sources/Filter.swift
Expand Up @@ -216,15 +216,15 @@ class Filter {
case noSite(json: String)
}

func runBayesianFilter(_ post: Post) -> (Bool, Int) {
func runBayesianFilter(_ post: Post) -> Int {
var trueProbability = Double(0.263)
var falseProbability = Double(1 - trueProbability)
var postWords = [String]()
var checkedWords = [String]()

guard let body = post.body else {
print("No body for \(post.id.map { String($0) } ?? "<no ID>")")
return (false, 0)
return 10000
}

var currentWord: String = ""
Expand Down Expand Up @@ -266,7 +266,7 @@ class Filter {

let difference = -log10(falseProbability - trueProbability)

return (difference < 45, Int(difference.isNormal ? difference : 0))
return Int(difference.isNormal ? difference : 10000)
}

func runUsernameFilter(_ post: Post) -> Bool {
Expand Down Expand Up @@ -347,12 +347,13 @@ class Filter {

func checkPost(_ post: Post) -> ReportReason? {
let bayesianResults = runBayesianFilter(post)

if runLinkFilter(post) {
return .misleadingLink
} else if runUsernameFilter(post) {
return .blacklistedUsername
} else if bayesianResults.0 {
return .bayesianFilter(difference: bayesianResults.1)
} else if rooms.contains(where: {room in return bayesianResults < room.threshold}) {
return .bayesianFilter(difference: bayesianResults)
} else {
return nil
}
Expand Down Expand Up @@ -434,10 +435,12 @@ class Filter {
reportedPosts.append((id: id, when: Date(), difference: difference))

for room in rooms {
let message = "[ [\(botName)](\(githubLink)) ] " +
"[tag:\(tags(for: post).first ?? "tagless")] \(header) [\(post.title ?? "<no title>")](//stackoverflow.com/q/\(id)) " +
room.notificationString(tags: tags(for: post), reason: reason)
room.postMessage(message)
if difference < room.threshold {
let message = "[ [\(botName)](\(githubLink)) ] " +
"[tag:\(tags(for: post).first ?? "tagless")] \(header) [\(post.title ?? "<no title>")](//stackoverflow.com/q/\(id)) " +
room.notificationString(tags: tags(for: post), reason: reason)
room.postMessage(message)
}
}

return .reported(reason: reason)
Expand Down
22 changes: 17 additions & 5 deletions Sources/main.swift
Expand Up @@ -14,6 +14,7 @@ import SwiftStack
let commands: [Command.Type] = [
CommandSay.self,
CommandHelp.self, CommandListRunning.self, CommandStop.self, CommandKill.self, CommandUpdate.self, CommandStatus.self,
CommandCheckThreshold.self, CommandSetThreshold.self,
CommandCheckPrivileges.self, CommandPrivilege.self, CommandUnprivilege.self,
CommandCheckPost.self, CommandQuota.self,
CommandBlacklistUsername.self, CommandGetBlacklistedUsernames.self, CommandUnblacklistUsername.self,
Expand Down Expand Up @@ -93,13 +94,21 @@ extension ChatRoom {

return users.map { "@" + $0.name.replacingOccurrences(of: " ", with: "") }.joined(separator: " ")
}

var threshold: Int {
get {
return (info["threshold"] as? Int) ?? 45
} set {
info["threshold"] = newValue
}
}
}

extension ChatUser.Privileges {
static let blacklist = ChatUser.Privileges(rawValue: 1 << 1)
static let filter = ChatUser.Privileges(rawValue: 1 << 1)
}

ChatUser.Privileges.add(name: "Blacklist", for: .blacklist)
ChatUser.Privileges.add(name: "Filter", for: .filter)


private enum BackgroundTask {
Expand Down Expand Up @@ -208,7 +217,7 @@ func main() throws {
try rooms.forEach { try $0.join() }

//Post the startup message
let startupMessage: String
let startupMessage: String?

currentVersion = getCurrentVersion()
if FileManager.default.fileExists(atPath: "update-failure") {
Expand All @@ -234,10 +243,13 @@ func main() throws {
try! FileManager.default.removeItem(atPath: "version-new.txt")
}
else {
startupMessage = "[\(botName)](\(githubLink)) started."
startupMessage = nil
rooms.first?.postMessage("[\(botName)](\(githubLink)) started.")
}

rooms.forEach { $0.postMessage(startupMessage) }
if let message = startupMessage {
rooms.forEach { $0.postMessage(message) }
}

shortVersion = getShortVersion(currentVersion)
versionLink = getVersionLink(currentVersion)
Expand Down

0 comments on commit 5278f48

Please sign in to comment.