2020-08-13 10:18:21 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
2021-01-30 01:14:22 +00:00
|
|
|
import Kingfisher
|
2020-08-30 23:33:11 +00:00
|
|
|
import Mastodon
|
2021-01-30 01:14:22 +00:00
|
|
|
import ServiceLayer
|
2020-09-05 02:31:43 +00:00
|
|
|
import UserNotifications
|
2020-08-13 10:18:21 +00:00
|
|
|
|
2020-11-09 06:22:20 +00:00
|
|
|
final class NotificationService: UNNotificationServiceExtension {
|
2021-01-30 01:14:22 +00:00
|
|
|
private let environment = AppEnvironment.live(
|
|
|
|
userNotificationCenter: .current(),
|
|
|
|
reduceMotion: { false })
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
super.init()
|
|
|
|
}
|
2020-08-13 10:18:21 +00: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-04 05:45:09 +00:00
|
|
|
let parsingService = PushNotificationParsingService(environment: 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
|
2020-08-13 10:18:21 +00:00
|
|
|
|
|
|
|
do {
|
2021-02-04 05:45:09 +00:00
|
|
|
(decryptedJSON, identityId) = try parsingService.extractAndDecrypt(userInfo: request.content.userInfo)
|
2020-09-04 01:55:46 +00:00
|
|
|
pushNotification = try MastodonDecoder().decode(PushNotification.self, from: decryptedJSON)
|
2020-08-13 10:18:21 +00:00
|
|
|
} catch {
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-04 08:30:37 +00:00
|
|
|
bestAttemptContent.userInfo[PushNotificationParsingService.pushNotificationUserInfoKey] = decryptedJSON
|
2020-08-13 10:18:21 +00:00
|
|
|
bestAttemptContent.title = pushNotification.title
|
2021-01-11 01:40:39 +00:00
|
|
|
bestAttemptContent.body = XMLUnescaper(string: pushNotification.body).unescape()
|
2020-08-13 10:18:21 +00:00
|
|
|
|
2021-02-04 05:24:00 +00:00
|
|
|
let appPreferences = AppPreferences(environment: environment)
|
2020-08-13 10:18:21 +00:00
|
|
|
|
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
|
|
|
|
2021-02-04 05:24:00 +00:00
|
|
|
if appPreferences.notificationAccountName,
|
|
|
|
let accountName = try? AllIdentitiesService(environment: environment).identity(id: identityId)?.handle {
|
|
|
|
bestAttemptContent.subtitle = accountName
|
|
|
|
}
|
2021-01-30 01:14:22 +00:00
|
|
|
|
2021-02-04 05:24:00 +00:00
|
|
|
if appPreferences.notificationPictures {
|
2021-02-04 20:09:05 +00:00
|
|
|
Self.addImage(url: pushNotification.icon,
|
2021-02-04 05:24:00 +00:00
|
|
|
bestAttemptContent: bestAttemptContent,
|
|
|
|
contentHandler: contentHandler)
|
|
|
|
} else {
|
|
|
|
contentHandler(bestAttemptContent)
|
2020-08-13 10:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func serviceExtensionTimeWillExpire() {
|
|
|
|
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension NotificationService {
|
2021-02-04 20:09:05 +00:00
|
|
|
static func addImage(url: URL,
|
2021-02-04 05:24:00 +00:00
|
|
|
bestAttemptContent: UNMutableNotificationContent,
|
|
|
|
contentHandler: @escaping (UNNotificationContent) -> Void) {
|
2021-02-04 20:09:05 +00:00
|
|
|
let fileName = url.lastPathComponent
|
2021-02-04 05:24:00 +00:00
|
|
|
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
|
|
|
.appendingPathComponent(fileName)
|
|
|
|
|
2021-02-04 20:09:05 +00:00
|
|
|
KingfisherManager.shared.retrieveImage(with: url) {
|
2021-02-04 05:24:00 +00:00
|
|
|
switch $0 {
|
|
|
|
case let .success(result):
|
|
|
|
let format: ImageFormat
|
|
|
|
|
|
|
|
switch fileURL.pathExtension.lowercased() {
|
|
|
|
case "jpg", "jpeg":
|
|
|
|
format = .JPEG
|
|
|
|
case "gif":
|
|
|
|
format = .GIF
|
|
|
|
case "png":
|
|
|
|
format = .PNG
|
|
|
|
default:
|
|
|
|
format = .unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
try result.image.kf.data(format: format)?.write(to: fileURL)
|
|
|
|
bestAttemptContent.attachments =
|
|
|
|
[try UNNotificationAttachment(identifier: fileName, url: fileURL)]
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
} catch {
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
}
|
|
|
|
case .failure:
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 10:18:21 +00:00
|
|
|
}
|