Skip to content

Commit e1e487a

Browse files
committed
debugging..
1 parent cc2d116 commit e1e487a

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

Sources/Camille/Karma/KarmaService+Adjustments.swift

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
import Chameleon
22
import Foundation
33

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+
421
private typealias PartialUpdate = (user: ModelPointer<User>, operation: Operation)
522
private typealias Update = (ModelPointer<User>, (current: Int, change: Int))
623

@@ -16,20 +33,28 @@ private enum Operation: String {
1633
}
1734
}
1835

19-
private let trimCharacterSet = CharacterSet.whitespaces.union(CharacterSet(charactersIn: ">:"))
20-
2136
extension KarmaService {
2237
func adjust(bot: SlackBot, message: MessageDecorator) throws {
2338
guard !message.isIM else { return }
2439

40+
let d = Debug()
41+
d.add("Karma Tests:")
42+
d.add(message.text)
43+
2544
func partialUpdate(from link: MessageDecorator.Link<ModelPointer<User>>) throws -> PartialUpdate? {
45+
d.add("testing \(link.value.id) against \(try message.sender().id)")
46+
2647
guard try link.value.id != message.sender().id else { return nil }
2748

28-
guard let operation = Operation(rawValue: message.text
49+
let possibleOperation = message.text
2950
.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)
3358
else { return nil }
3459

3560
return (link.value, operation)
@@ -43,11 +68,33 @@ extension KarmaService {
4368

4469
let updates = try message
4570
.mentionedUsers
71+
.then { link in
72+
let u = try? link.value.value()
73+
d.add(u?.name ?? "no name?")
74+
}
4675
.flatMap(partialUpdate)
4776
.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+
}
4889
.map(consolidatePartialUpdates)
90+
.then { update in
91+
d.add("Updates:")
92+
d.add("ID: \(update.key.id): \(update.value.current) > \(update.value.change)")
93+
}
4994
.filter { $0.value.change != 0 }
5095

96+
debug(d.message)
97+
5198
guard !updates.isEmpty else { return }
5299

53100
let response = try message.respond()

Sources/Camille/main.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,13 @@ let bot = SlackBot(
2222
]
2323
)
2424

25+
func debug(_ message: String) {
26+
let chatMessage = ChatMessage(
27+
channel: "U04UAVAEB", // @iankeen
28+
text: message
29+
)
30+
31+
try? bot.send(chatMessage)
32+
}
33+
2534
bot.start()

0 commit comments

Comments
 (0)