Skip to content

Commit

Permalink
Abstract out trigger reaction configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Jan 7, 2024
1 parent 6912937 commit ba12290
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Sources/D2Handlers/D2Receiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class D2Receiver: Receiver {
@AutoSerializing(filePath: "local/haikuConfig.json") var haikuConfiguration = HaikuConfiguration()
@AutoSerializing(filePath: "local/threadConfig.json") var threadConfiguration = ThreadConfiguration()
@AutoSerializing(filePath: "local/roleReactionsConfig.json") var roleReactionsConfiguration = RoleReactionsConfiguration()
@AutoSerializing(filePath: "local/triggerReactionConfiguration.json") var triggerReactionConfiguration = TriggerReactionConfiguration()
@AutoSerializing(filePath: "local/pronounRoleConfig.json") var pronounRoleConfiguration = PronounRoleConfiguration()
@AutoSerializing(filePath: "local/cityConfig.json") var cityConfiguration = CityConfiguration()

Expand All @@ -81,7 +82,7 @@ public class D2Receiver: Receiver {
MentionD2Handler(conversator: FollowUpConversator(messageDB: messageDB)),
MentionSomeoneHandler(),
MessagePreviewHandler(configuration: _messagePreviewsConfiguration),
TriggerReactionHandler(cityConfiguration: _cityConfiguration),
TriggerReactionHandler(configuration: _triggerReactionConfiguration, cityConfiguration: _cityConfiguration),
CountToNHandler(),
UniversalSummoningHandler(hostInfo: hostInfo),
HaikuHandler(configuration: _haikuConfiguration, inventoryManager: inventoryManager),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// TODO: Move this to D2Commands and add a command for configuring these

public struct TriggerReactionConfiguration: Codable {
public var dateSpecificReactions: Bool
public var weatherReactions: Bool

public init(
dateSpecificReactions: Bool = true,
weatherReactions: Bool = true
) {
self.dateSpecificReactions = dateSpecificReactions
self.weatherReactions = weatherReactions
}
}
13 changes: 5 additions & 8 deletions Sources/D2Handlers/message/handler/TriggerReactionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ public struct TriggerReactionHandler: MessageHandler {
}

public init(
dateSpecificReactions: Bool = true,
weatherReactions: Bool = true,
configuration: AutoSerializing<TriggerReactionConfiguration>,
cityConfiguration: AutoSerializing<CityConfiguration>
) {
self.init(
dateSpecificReactions: dateSpecificReactions,
weatherReactions: weatherReactions,
configuration: { configuration.wrappedValue },
weatherEmojiProvider: {
guard let city = cityConfiguration.wrappedValue.city else { throw ReactionTriggerError.other("No city specified") }
return OpenWeatherMapQuery(city: city).perform()
Expand All @@ -33,8 +31,7 @@ public struct TriggerReactionHandler: MessageHandler {
}

public init(
dateSpecificReactions: Bool = true,
weatherReactions: Bool = true,
configuration: @escaping () -> TriggerReactionConfiguration,
weatherEmojiProvider: @escaping () throws -> Promise<String, any Error>
) {
self.init(triggers: [
Expand All @@ -50,7 +47,7 @@ public struct TriggerReactionHandler: MessageHandler {
Promise.catchingThen {
guard goodMorningOrEveningPattern.matchCount(in: message.content) > 0 else { throw ReactionTriggerError.mismatchingKeywords }

if dateSpecificReactions {
if configuration().dateSpecificReactions {
let calendar = Calendar.current
let todayComponents = calendar.dateComponents([.month, .day], from: Date())

Expand Down Expand Up @@ -80,7 +77,7 @@ public struct TriggerReactionHandler: MessageHandler {
}
}

if weatherReactions {
if configuration().weatherReactions {
return try weatherEmojiProvider()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ final class TriggerReactionHandlers: XCTestCase {

private func messageTriggersWeather(_ content: String) -> Bool {
let emoji = ":test:"
let handler = TriggerReactionHandler(dateSpecificReactions: false) {
let handler = TriggerReactionHandler {
TriggerReactionConfiguration(
dateSpecificReactions: false,
weatherReactions: true
)
} weatherEmojiProvider: {
Promise(emoji)
}
let output = TestOutput()
Expand Down

0 comments on commit ba12290

Please sign in to comment.