iOS/Sources/Shared/API/Webhook/Networking/WebhookResponseServiceCall.swift
Zac West 7cbde6ddb2
Try and fix Realm crashes via excessive background tasks (#1874)
Starting in iOS 15, there's a number of crashes happening in the background with Realm. They don't appear to be due to the file lock in the shared app container, but this may help resolve them either way -- easy to see if the next beta doesn't crash a bunch.
2021-10-03 21:28:00 +00:00

47 lines
1.5 KiB
Swift

import Foundation
import PromiseKit
import UserNotifications
extension WebhookResponseIdentifier {
static var serviceCall: Self { .init(rawValue: "serviceCall") }
}
struct WebhookResponseServiceCall: WebhookResponseHandler {
let api: HomeAssistantAPI
init(api: HomeAssistantAPI) {
self.api = api
}
static func shouldReplace(request current: WebhookRequest, with proposed: WebhookRequest) -> Bool {
// every service call is distinct
false
}
func handle(
request: Promise<WebhookRequest>,
result: Promise<Any>
) -> Guarantee<WebhookResponseHandlerResult> {
firstly {
when(fulfilled: request, result)
}.then { request, _ -> Promise<Void> in
let requestDictionary = try request.asDictionary()
let domain = requestDictionary["domain"] as? String ?? "(unknown)"
let service = requestDictionary["service"] as? String ?? "(unknown)"
let payload = requestDictionary["service_data"] as? [String: Any] ?? [:]
let event = ClientEvent(
text: "Called service: \(domain).\(service)",
type: .serviceCall,
payload: payload
)
return Current.clientEventStore.addEvent(event)
}.then {
Guarantee.value(WebhookResponseHandlerResult.default)
}.recover { _ in
Guarantee.value(WebhookResponseHandlerResult.default)
}
}
}