2020-08-13 12:18:21 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
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-08-13 12:18:21 +02:00
|
|
|
|
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
|
|
|
}
|
2020-08-13 12:18:21 +02:00
|
|
|
|
|
|
|
var contentHandler: ((UNNotificationContent) -> Void)?
|
|
|
|
var bestAttemptContent: UNMutableNotificationContent?
|
|
|
|
|
|
|
|
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
|
2020-08-13 12:18:21 +02:00
|
|
|
|
|
|
|
do {
|
2021-02-04 06:45:09 +01:00
|
|
|
(decryptedJSON, identityId) = try parsingService.extractAndDecrypt(userInfo: request.content.userInfo)
|
2020-09-04 03:55:46 +02:00
|
|
|
pushNotification = try MastodonDecoder().decode(PushNotification.self, from: decryptedJSON)
|
2020-08-13 12:18:21 +02:00
|
|
|
} catch {
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-04 09:30:37 +01:00
|
|
|
bestAttemptContent.userInfo[PushNotificationParsingService.pushNotificationUserInfoKey] = decryptedJSON
|
2020-08-13 12:18:21 +02:00
|
|
|
bestAttemptContent.title = pushNotification.title
|
2021-01-11 02:40:39 +01:00
|
|
|
bestAttemptContent.body = XMLUnescaper(string: pushNotification.body).unescape()
|
2020-08-13 12:18:21 +02:00
|
|
|
|
2021-02-23 00:59:33 +01:00
|
|
|
let appPreferences = AppPreferences(environment: Self.environment)
|
2020-08-13 12:18:21 +02:00
|
|
|
|
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
|
|
|
|
2021-02-04 06:24:00 +01:00
|
|
|
if appPreferences.notificationAccountName,
|
2021-02-23 00:59:33 +01:00
|
|
|
let accountName = try? AllIdentitiesService(environment: Self.environment).identity(id: identityId)?.handle {
|
2021-02-04 06:24:00 +01:00
|
|
|
bestAttemptContent.subtitle = accountName
|
|
|
|
}
|
2021-01-30 02:14:22 +01:00
|
|
|
|
2021-02-04 06:24:00 +01:00
|
|
|
if appPreferences.notificationPictures {
|
2021-02-04 21:09:05 +01:00
|
|
|
Self.addImage(url: pushNotification.icon,
|
2021-02-04 06:24:00 +01:00
|
|
|
bestAttemptContent: bestAttemptContent,
|
|
|
|
contentHandler: contentHandler)
|
|
|
|
} else {
|
|
|
|
contentHandler(bestAttemptContent)
|
2020-08-13 12:18:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
reduceMotion: { false })
|
|
|
|
|
2021-02-04 21:09:05 +01:00
|
|
|
static func addImage(url: URL,
|
2021-02-04 06:24:00 +01:00
|
|
|
bestAttemptContent: UNMutableNotificationContent,
|
|
|
|
contentHandler: @escaping (UNNotificationContent) -> Void) {
|
2021-02-04 21:09:05 +01:00
|
|
|
let fileName = url.lastPathComponent
|
2021-02-04 06:24:00 +01:00
|
|
|
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
|
|
|
.appendingPathComponent(fileName)
|
|
|
|
|
2021-02-23 00:59:33 +01:00
|
|
|
SDWebImageManager.shared.loadImage(with: url, options: [], progress: nil) { _, data, _, _, _, _ in
|
|
|
|
if let data = data {
|
2021-02-04 06:24:00 +01:00
|
|
|
do {
|
2021-02-23 00:59:33 +01:00
|
|
|
try data.write(to: fileURL)
|
2021-02-04 06:24:00 +01:00
|
|
|
bestAttemptContent.attachments =
|
|
|
|
[try UNNotificationAttachment(identifier: fileName, url: fileURL)]
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
} catch {
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
}
|
2021-02-23 00:59:33 +01:00
|
|
|
} else {
|
2021-02-04 06:24:00 +01:00
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 12:18:21 +02:00
|
|
|
}
|