This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathNotificationService.swift
109 lines (88 loc) · 4.09 KB
/
NotificationService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import Mastodon
import SDWebImage
import ServiceLayer
import UserNotifications
final class NotificationService: UNNotificationServiceExtension {
override init() {
super.init()
try? ImageCacheConfiguration(environment: Self.environment).configure()
}
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 }
let parsingService = PushNotificationParsingService(environment: Self.environment)
let decryptedJSON: Data
let identityId: Identity.Id
let pushNotification: PushNotification
do {
(decryptedJSON, identityId) = try parsingService.extractAndDecrypt(userInfo: request.content.userInfo)
pushNotification = try MastodonDecoder().decode(PushNotification.self, from: decryptedJSON)
} catch {
contentHandler(bestAttemptContent)
return
}
bestAttemptContent.userInfo[PushNotificationParsingService.pushNotificationUserInfoKey] = decryptedJSON
bestAttemptContent.title = pushNotification.title
bestAttemptContent.body = XMLUnescaper(string: pushNotification.body).unescape()
let appPreferences = AppPreferences(environment: Self.environment)
if appPreferences.notificationSounds.contains(pushNotification.notificationType) {
bestAttemptContent.sound = .default
}
if appPreferences.notificationAccountName,
case let .success(handle) = parsingService.handle(identityId: identityId) {
bestAttemptContent.subtitle = handle
}
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 {
private static let environment = AppEnvironment.live(
userNotificationCenter: .current(),
reduceMotion: { false },
autoplayVideos: { true })
enum ImageError: Error {
case dataMissing
}
static func attachment(imageURL: URL) -> AnyPublisher<UNNotificationAttachment, Error> {
let fileName = imageURL.lastPathComponent
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))
}
}
}
.eraseToAnyPublisher()
}
}