metatext-app-ios-iphone-ipad/Notification Service Extension/NotificationService.swift

110 lines
4.1 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import Combine
2020-08-31 01:33:11 +02:00
import Mastodon
2021-02-23 00:59:33 +01:00
import SDWebImage
2021-01-30 02:14:22 +01:00
import ServiceLayer
2020-09-05 04:31:43 +02:00
import UserNotifications
2020-11-09 07:22:20 +01:00
final class NotificationService: UNNotificationServiceExtension {
2021-01-30 02:14:22 +01:00
override init() {
super.init()
2021-02-23 00:59:33 +01:00
try? ImageCacheConfiguration(environment: Self.environment).configure()
2021-01-30 02:14:22 +01: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-23 00:59:33 +01:00
let parsingService = PushNotificationParsingService(environment: Self.environment)
2021-02-04 06:24:00 +01:00
let decryptedJSON: Data
let identityId: Identity.Id
2021-02-04 06:45:09 +01:00
let pushNotification: PushNotification
do {
2021-02-04 06:45:09 +01: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 09:30:37 +01:00
bestAttemptContent.userInfo[PushNotificationParsingService.pushNotificationUserInfoKey] = decryptedJSON
bestAttemptContent.title = pushNotification.title
bestAttemptContent.body = XMLUnescaper(string: pushNotification.body).unescape()
2021-02-23 00:59:33 +01:00
let appPreferences = AppPreferences(environment: Self.environment)
2021-02-04 06:24:00 +01:00
if appPreferences.notificationSounds.contains(pushNotification.notificationType) {
bestAttemptContent.sound = .default
}
2021-01-30 02:14:22 +01:00
if appPreferences.notificationAccountName,
case let .success(handle) = parsingService.handle(identityId: identityId) {
bestAttemptContent.subtitle = handle
}
2021-03-29 08:04:14 +02:00
Self.attachment(imageURL: pushNotification.icon.url)
.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-23 00:59:33 +01:00
private static let environment = AppEnvironment.live(
userNotificationCenter: .current(),
2021-03-06 03:25:18 +01:00
reduceMotion: { false },
autoplayVideos: { true })
2021-02-23 00:59:33 +01:00
enum ImageError: Error {
case dataMissing
}
static func attachment(imageURL: URL) -> AnyPublisher<UNNotificationAttachment, Error> {
let fileName = imageURL.lastPathComponent
2021-02-04 06:24:00 +01: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 06:24:00 +01:00
}
}
}
.eraseToAnyPublisher()
2021-02-04 06:24:00 +01:00
}
}