1
1
import Chameleon
2
2
import Foundation
3
3
4
+ extension Sequence {
5
+ func then( _ closure: ( Iterator . Element ) -> Void ) -> Self {
6
+ for item in self {
7
+ closure ( item)
8
+ }
9
+ return self
10
+ }
11
+ }
12
+
13
+ class Debug {
14
+ private( set) var message : String = " "
15
+
16
+ func add( _ m: String ) {
17
+ message += " \( m) \n "
18
+ }
19
+ }
20
+
4
21
private typealias PartialUpdate = ( user: ModelPointer < User > , operation: Operation )
5
22
private typealias Update = ( ModelPointer < User > , ( current: Int , change: Int ) )
6
23
@@ -16,20 +33,28 @@ private enum Operation: String {
16
33
}
17
34
}
18
35
19
- private let trimCharacterSet = CharacterSet . whitespaces. union ( CharacterSet ( charactersIn: " >: " ) )
20
-
21
36
extension KarmaService {
22
37
func adjust( bot: SlackBot , message: MessageDecorator ) throws {
23
38
guard !message. isIM else { return }
24
39
40
+ let d = Debug ( )
41
+ d. add ( " Karma Tests: " )
42
+ d. add ( message. text)
43
+
25
44
func partialUpdate( from link: MessageDecorator . Link < ModelPointer < User > > ) throws -> PartialUpdate ? {
45
+ d. add ( " testing \( link. value. id) against \( try message. sender ( ) . id) " )
46
+
26
47
guard try link. value. id != message. sender ( ) . id else { return nil }
27
48
28
- guard let operation = Operation ( rawValue : message. text
49
+ let possibleOperation = message. text
29
50
. substring ( from: link. range. upperBound)
30
- . trimmingCharacters ( in: trimCharacterSet)
31
- . components ( separatedBy: . whitespaces)
32
- . first ?? " " )
51
+ . trimCharacters ( in: [ " " , " > " , " : " ] )
52
+ . components ( separatedBy: " " )
53
+ . first ?? " "
54
+
55
+ d. add ( " possible operation: ' \( possibleOperation) ' - from \( message. text. substring ( from: link. range. upperBound) ) - link \( link. range. lowerBound) ..< \( link. range. upperBound) " )
56
+
57
+ guard let operation = Operation ( rawValue: possibleOperation)
33
58
else { return nil }
34
59
35
60
return ( link. value, operation)
@@ -43,11 +68,33 @@ extension KarmaService {
43
68
44
69
let updates = try message
45
70
. mentionedUsers
71
+ . then { link in
72
+ let u = try ? link. value. value ( )
73
+ d. add ( u? . name ?? " no name? " )
74
+ }
46
75
. flatMap ( partialUpdate)
47
76
. group ( by: { $0. user } )
77
+ . then { key, value in
78
+ d. add ( " Partial Updates: " )
79
+
80
+ func v( v: PartialUpdate ) -> String {
81
+ switch v. operation {
82
+ case . plus: return " plus "
83
+ case . minus: return " minus "
84
+ }
85
+ }
86
+
87
+ d. add ( " ID: \( key. id) : \( value. map ( v) . joined ( separator: " , " ) ) " )
88
+ }
48
89
. map ( consolidatePartialUpdates)
90
+ . then { update in
91
+ d. add ( " Updates: " )
92
+ d. add ( " ID: \( update. key. id) : \( update. value. current) > \( update. value. change) " )
93
+ }
49
94
. filter { $0. value. change != 0 }
50
95
96
+ debug ( d. message)
97
+
51
98
guard !updates. isEmpty else { return }
52
99
53
100
let response = try message. respond ( )
0 commit comments