Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions Apps/FlowKitAdapter/FlowKitAdapterApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,15 @@ struct FlowKitAdapter: App, Log {
entities: $entities,
connectionStatus: $connectionStatus
)
.task {
Self.log.info("runloop task called")
.task(id: serverAddress) {
Self.log.info("runloop task called for address: \(serverAddress)")

// do not start run loop when running in preview canvas
guard ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] != "1" else { return }

try? await Task.sleep(for: .seconds(1))
await initializeActorSystem()
}
.onChange(of: serverAddress) { _, newValue in
Task {
Self.log.info("Server address changed to \(newValue), reinitializing actor system")
await initializeActorSystem()
}
}
}
}

Expand Down
43 changes: 18 additions & 25 deletions Sources/Server/Controllers/OpenAPIController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,21 @@ struct OpenAPIController: APIProtocol {
case .pushNotification, .liveActivityStart:
assert(token.activityType == nil, "For pushNotification and liveActivityStart, activityType should be nil")

// first delete all previous token
try await DeviceToken
// upsert: update existing token or create new one
if let existing = try await DeviceToken
.query(on: request.db)
.filter(\.$deviceName == token.deviceName)
.filter(\.$tokenType == token.tokenType.rawValue)
.delete()

// add the new token
let newPushDevice = DeviceToken(deviceName: token.deviceName,
tokenString: token.tokenString,
tokenType: token.tokenType.rawValue,
activityType: token.activityType) // for pushNotification & liveActivityStart - this should always be nil
try await newPushDevice.save(on: request.db)
.first() {
existing.tokenString = token.tokenString
try await existing.save(on: request.db)
} else {
let newPushDevice = DeviceToken(deviceName: token.deviceName,
tokenString: token.tokenString,
tokenType: token.tokenType.rawValue,
activityType: token.activityType)
try await newPushDevice.save(on: request.db)
}
return .ok

case .liveActivityUpdate:
Expand All @@ -90,29 +92,20 @@ struct OpenAPIController: APIProtocol {
return .internalServerError
}

// delete other tokens with that activity type
try await DeviceToken
// upsert: update existing token or create new one
if let existing = try await DeviceToken
.query(on: request.db)
.filter(\.$deviceName == token.deviceName)
.filter(\.$tokenString != token.tokenString)
.filter(\.$tokenType == token.tokenType.rawValue)
.filter(\.$activityType == activityType)
.delete()

// insert the new token, if needed
let numberOfPushDevices = try await DeviceToken
.query(on: request.db)
.filter(\.$deviceName == token.deviceName)
.filter(\.$tokenString == token.tokenString)
.filter(\.$tokenType == token.tokenType.rawValue)
.filter(\.$activityType == activityType)
.count()
if numberOfPushDevices == 0 {
.first() {
existing.tokenString = token.tokenString
try await existing.save(on: request.db)
} else {
let newPushDevice = DeviceToken(deviceName: token.deviceName,
tokenString: token.tokenString,
tokenType: token.tokenType.rawValue,
activityType: activityType)

try await newPushDevice.save(on: request.db)
}
return .ok
Expand Down
6 changes: 2 additions & 4 deletions Sources/Server/Controllers/PushNotifcationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ actor PushNotifcationService: NotificationSender {
// delete liveActivityUpdate tokens that are older than 4 hours
try await DeviceToken
.query(on: database)
.filter(\.$id != tokenId)
.filter(\.$id == tokenId)
.delete()

} else {
Expand Down Expand Up @@ -117,9 +117,7 @@ actor PushNotifcationService: NotificationSender {
timestamp: Int(Date().timeIntervalSince1970),
attributes: contentState,
attributesType: activityName,
alert: APNSAlertNotificationContent(
title: .raw("empty"),
body: .raw("empty")))
alert: APNSAlertNotificationContent())

// start a new live activity
usedToken = startToken
Expand Down