metatext/Notification Service Extension/NotificationService.swift

109 lines
4.1 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import Combine
2020-08-30 23:33:11 +00:00
import Mastodon
2021-02-22 23:59:33 +00:00
import SDWebImage
2021-01-30 01:14:22 +00:00
import ServiceLayer
2020-09-05 02:31:43 +00:00
import UserNotifications
2020-11-09 06:22:20 +00:00
final class NotificationService: UNNotificationServiceExtension {
2021-01-30 01:14:22 +00:00
override init() {
super.init()
2021-02-22 23:59:33 +00:00
try? ImageCacheConfiguration(environment: Self.environment).configure()
2021-01-30 01:14:22 +00:00
}
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
var cancellables = Set<AnyCancellable>()
override func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
guard let bestAttemptContent = bestAttemptContent else { return }
2021-02-22 23:59:33 +00:00
let parsingService = PushNotificationParsingService(environment: Self.environment)
2021-02-04 05:24:00 +00:00
let decryptedJSON: Data
let identityId: Identity.Id
2021-02-04 05:45:09 +00:00
let pushNotification: PushNotification
do {
2021-02-04 05:45:09 +00:00
(decryptedJSON, identityId) = try parsingService.extractAndDecrypt(userInfo: request.content.userInfo)
pushNotification = try MastodonDecoder().decode(PushNotification.self, from: decryptedJSON)
} catch {
contentHandler(bestAttemptContent)
return
}
2021-02-04 08:30:37 +00:00
bestAttemptContent.userInfo[PushNotificationParsingService.pushNotificationUserInfoKey] = decryptedJSON
bestAttemptContent.title = pushNotification.title
bestAttemptContent.body = XMLUnescaper(string: pushNotification.body).unescape()
2021-02-22 23:59:33 +00:00
let appPreferences = AppPreferences(environment: Self.environment)
2021-02-04 05:24:00 +00:00
if appPreferences.notificationSounds.contains(pushNotification.notificationType) {
bestAttemptContent.sound = .default
}
2021-01-30 01:14:22 +00:00
if appPreferences.notificationAccountName,
case let .success(handle) = parsingService.handle(identityId: identityId) {
bestAttemptContent.subtitle = handle
}
Self.attachment(imageURL: pushNotification.icon)
.map { [$0] }
.replaceError(with: [])
.handleEvents(receiveOutput: { bestAttemptContent.attachments = $0 })
.zip(parsingService.title(pushNotification: pushNotification, identityId: identityId)
.replaceError(with: pushNotification.title)
.handleEvents(receiveOutput: { bestAttemptContent.title = $0 }))
.sink { _ in contentHandler(bestAttemptContent) }
.store(in: &cancellables)
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
private extension NotificationService {
2021-02-22 23:59:33 +00:00
private static let environment = AppEnvironment.live(
userNotificationCenter: .current(),
reduceMotion: { false })
enum ImageError: Error {
case dataMissing
}
static func attachment(imageURL: URL) -> AnyPublisher<UNNotificationAttachment, Error> {
let fileName = imageURL.lastPathComponent
2021-02-04 05:24:00 +00:00
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(fileName)
return Future<UNNotificationAttachment, Error> { promise in
SDWebImageManager.shared.loadImage(with: imageURL, options: [], progress: nil) { _, data, error, _, _, _ in
if let error = error {
promise(.failure(error))
} else if let data = data {
let result = Result<UNNotificationAttachment, Error> {
try data.write(to: fileURL)
return try UNNotificationAttachment(identifier: fileName, url: fileURL)
}
promise(result)
} else {
promise(.failure(ImageError.dataMissing))
2021-02-04 05:24:00 +00:00
}
}
}
.eraseToAnyPublisher()
2021-02-04 05:24:00 +00:00
}
}