forgot to batch

This commit is contained in:
Alek Michelson 2026-04-10 00:42:08 -04:00
parent 94512211d8
commit 049f194304
2 changed files with 23 additions and 17 deletions

View file

@ -115,24 +115,32 @@ class Store: ObservableObject {
// MARK: Notifications // MARK: Notifications
func save(notificationFromMessage message: Message, withSubscription subscription: Subscription) { func save(notificationFromMessage message: Message, withSubscription subscription: Subscription) {
save(notificationsFromMessages: [message], withSubscription: subscription)
}
func save(notificationsFromMessages messages: [Message], withSubscription subscription: Subscription) {
guard !messages.isEmpty else { return }
context.performAndWait { context.performAndWait {
do { do {
let notification = Notification(context: context) for message in messages {
notification.id = message.id let notification = Notification(context: context)
notification.time = message.time notification.id = message.id
notification.message = message.message ?? "" notification.time = message.time
notification.title = message.title ?? "" notification.message = message.message ?? ""
notification.priority = (message.priority != nil && message.priority != 0) ? message.priority! : 3 notification.title = message.title ?? ""
notification.tags = message.tags?.joined(separator: ",") ?? "" notification.priority = (message.priority != nil && message.priority != 0) ? message.priority! : 3
notification.actions = Actions.shared.encode(message.actions) notification.tags = message.tags?.joined(separator: ",") ?? ""
notification.click = message.click ?? "" notification.actions = Actions.shared.encode(message.actions)
notification.subscription = subscription notification.click = message.click ?? ""
subscription.addToNotifications(notification) notification.subscription = subscription
subscription.lastNotificationId = message.id subscription.addToNotifications(notification)
Log.d(Store.tag, "Storing notification with ID \(notification.id ?? "<unknown>")") subscription.lastNotificationId = message.id
Log.d(Store.tag, "Storing notification with ID \(notification.id ?? "<unknown>")")
}
try context.save() try context.save()
} catch let error { } catch let error {
Log.w(Store.tag, "Cannot store notification (fromMessage)", error) Log.w(Store.tag, "Cannot store notifications (fromMessages)", error)
rollbackAndRefresh() rollbackAndRefresh()
} }
} }

View file

@ -61,9 +61,7 @@ struct SubscriptionManager {
} }
Log.d(tag, "Polling success, \(messages.count) new message(s)", messages) Log.d(tag, "Polling success, \(messages.count) new message(s)", messages)
if !messages.isEmpty { if !messages.isEmpty {
for message in messages { store.save(notificationsFromMessages: messages, withSubscription: subscription)
store.save(notificationFromMessage: message, withSubscription: subscription)
}
} }
completionHandler(messages) completionHandler(messages)
} }