|
1 | | -import Bot |
2 | | -import Sugar |
| 1 | +import Chameleon |
| 2 | +import Foundation |
| 3 | + |
| 4 | +private typealias PartialUpdate = (user: ModelPointer<User>, operation: Operation) |
| 5 | +private typealias Update = (ModelPointer<User>, (current: Int, change: Int)) |
| 6 | + |
| 7 | +private enum Operation: String { |
| 8 | + case plus = "++" |
| 9 | + case minus = "--" |
| 10 | + |
| 11 | + func update(value: Int) -> Int { |
| 12 | + switch self { |
| 13 | + case .plus: return value + 1 |
| 14 | + case .minus: return value - 1 |
| 15 | + } |
| 16 | + } |
| 17 | +} |
3 | 18 |
|
4 | 19 | extension KarmaService { |
5 | | - func adjustKarma( |
6 | | - in message: MessageDecorator, |
7 | | - from sender: User, |
8 | | - in target: SlackTargetType, |
9 | | - storage: Storage, |
10 | | - webApi: WebAPI) throws -> Void { |
11 | | - |
12 | | - //find any karma adjustments |
13 | | - let adjustments: [(user: User, amount: Int)] = message.mentioned_users.flatMap { link in |
14 | | - //user can't adjust their own karma |
15 | | - guard link.value != sender else { return nil } |
16 | | - |
17 | | - //Move past the `>` and get the rest of the message |
18 | | - let start = message.text.index(after: link.range.upperBound) |
19 | | - let text = message.text.substring(from: start) |
20 | | - |
21 | | - //iterate forward until we pass the threshold or hit the end of the string |
22 | | - for index in (0..<min(self.config.textDistanceThreshold, text.characters.count)) { |
23 | | - //check for adjustments |
24 | | - for adjuster in self.config.karmaAdjusters { |
25 | | - if text.substring(from: index).hasPrefix(adjuster.adjuster.karmaValue) { |
26 | | - return (link.value, adjuster.amount) |
27 | | - } |
28 | | - } |
29 | | - |
30 | | - //check the allowed chars |
31 | | - let nextChar = Character(text.substring(to: 1)) |
32 | | - guard self.config.allowedBufferCharacters.contains(nextChar) else { break } |
33 | | - } |
34 | | - |
35 | | - return nil |
| 20 | + func adjust(bot: SlackBot, message: MessageDecorator) throws { |
| 21 | + guard !message.isIM else { return } |
| 22 | + |
| 23 | + func partialUpdate(from link: MessageDecorator.Link<ModelPointer<User>>) throws -> PartialUpdate? { |
| 24 | + guard try link.value.id != message.sender().id else { return nil } |
| 25 | + |
| 26 | + let possibleOperation = message.text |
| 27 | + .substring(from: link.range.upperBound) |
| 28 | + .trimCharacters(in: [" ", ">", ":"]) |
| 29 | + .components(separatedBy: " ") |
| 30 | + .first ?? "" |
| 31 | + |
| 32 | + guard let operation = Operation(rawValue: possibleOperation) |
| 33 | + else { return nil } |
| 34 | + |
| 35 | + return (link.value, operation) |
36 | 36 | } |
37 | | - |
38 | | - guard !adjustments.isEmpty else { return } |
39 | | - |
40 | | - //consolidate adjustments |
41 | | - let consolidated: [(user: User, change: Int, total: Int)] = adjustments |
42 | | - .grouped(by: { $0.user }) |
43 | | - .flatMap { user, changes in |
44 | | - let current: Int = storage.get(.in("Karma"), key: user.id, or: 0) |
45 | | - let change = changes.reduce(0) { $0 + $1.amount } |
46 | | - guard change != 0 else { return nil } |
47 | | - let newTotal = current + change |
48 | | - return (user, change, newTotal) |
| 37 | + |
| 38 | + func consolidatePartialUpdates(for user: ModelPointer<User>, partials: [PartialUpdate]) throws -> Update { |
| 39 | + let count: Int = try storage.get(key: user.id, from: Keys.namespace, or: 0) |
| 40 | + let change = partials.reduce(0) { $1.operation.update(value: $0) } |
| 41 | + return (user, (count, change)) |
49 | 42 | } |
50 | | - |
51 | | - //make adjustments |
52 | | - for adjustment in consolidated where adjustment.change != 0 { |
53 | | - try storage.set(.in("Karma"), key: adjustment.user.id, value: adjustment.total) |
| 43 | + |
| 44 | + let updates = try message |
| 45 | + .mentionedUsers |
| 46 | + .flatMap(partialUpdate) |
| 47 | + .group(by: { $0.user }) |
| 48 | + .map(consolidatePartialUpdates) |
| 49 | + .filter { $0.value.change != 0 } |
| 50 | + |
| 51 | + guard !updates.isEmpty else { return } |
| 52 | + |
| 53 | + let response = try message.respond() |
| 54 | + |
| 55 | + for (user, data) in updates { |
| 56 | + let newTotal = data.current + data.change |
| 57 | + storage.set(value: newTotal, forKey: user.id, in: Keys.namespace) |
| 58 | + |
| 59 | + let comment = (data.change > 0 |
| 60 | + ? config.positiveComments.randomElement |
| 61 | + : config.negativeComments.randomElement |
| 62 | + ) ?? "" |
| 63 | + |
| 64 | + response |
| 65 | + .text([user, comment, newTotal]) |
| 66 | + .newLine() |
54 | 67 | } |
55 | 68 |
|
56 | | - //build a message |
57 | | - let response = consolidated |
58 | | - .flatMap { adjustment in |
59 | | - if adjustment.change > 0 { |
60 | | - return self.config.positiveMessage(adjustment.user, adjustment.total).randomElement |
61 | | - } else if adjustment.change < 0 { |
62 | | - return self.config.negativeMessage(adjustment.user, adjustment.total).randomElement |
63 | | - } |
64 | | - return nil |
65 | | - } |
66 | | - .joined(separator: "\n") |
67 | | - |
68 | | - guard !response.isEmpty else { return } |
69 | | - |
70 | | - //respond |
71 | | - let request = ChatPostMessage(target: target, text: response) |
72 | | - try webApi.execute(request) |
| 69 | + try bot.send(response.makeChatMessage()) |
73 | 70 | } |
74 | 71 | } |
0 commit comments