Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/swift' into swift
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Keller committed Mar 2, 2017
2 parents 62e2af0 + 5d4f318 commit b3aa40f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
64 changes: 64 additions & 0 deletions 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 ?? "<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)
}
}
}
15 changes: 11 additions & 4 deletions Sources/Filter.swift
Expand Up @@ -363,6 +363,7 @@ class Filter {
case bayesianFilter(difference: Int)
case blacklistedUsername
case misleadingLink
case manuallyReported
}

enum ReportResult {
Expand Down Expand Up @@ -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 ?? "<no title>")"

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

let message = "[ [\(botName)](\(stackAppsLink)) ] " +
"[tag:\(tags(for: post).first ?? "tagless")] \(header) [\(post.title ?? "<no 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)

}
}

Expand Down
16 changes: 10 additions & 6 deletions Sources/main.swift
Expand Up @@ -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,
]


Expand Down Expand Up @@ -100,8 +100,13 @@ extension ChatRoom {

shouldNotify = true
}
}


case .manuallyReported:
if shouldNotify == true {
shouldNotify = true
}

}
}

if shouldNotify {
Expand Down Expand Up @@ -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()
}
}
Expand Down

0 comments on commit b3aa40f

Please sign in to comment.