2021-04-25 06:48:29 +02:00
|
|
|
//
|
|
|
|
// NotificationService.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
2022-09-30 13:28:09 +02:00
|
|
|
import MastodonCommon
|
2021-04-25 06:48:29 +02:00
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public final class NotificationService {
|
2021-04-25 06:48:29 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
2021-06-11 22:37:54 +02:00
|
|
|
let workingQueue = DispatchQueue(label: "org.joinmastodon.app.NotificationService.working-queue")
|
2021-04-25 06:48:29 +02:00
|
|
|
|
|
|
|
// input
|
2021-04-27 11:27:03 +02:00
|
|
|
weak var apiService: APIService?
|
2021-04-25 06:48:29 +02:00
|
|
|
weak var authenticationService: AuthenticationService?
|
2022-09-30 13:28:09 +02:00
|
|
|
public let isNotificationPermissionGranted = CurrentValueSubject<Bool, Never>(false)
|
|
|
|
public let deviceToken = CurrentValueSubject<Data?, Never>(nil)
|
|
|
|
public let applicationIconBadgeNeedsUpdate = CurrentValueSubject<Void, Never>(Void())
|
2021-04-26 10:57:50 +02:00
|
|
|
|
2021-04-25 06:48:29 +02:00
|
|
|
// output
|
2021-09-29 12:42:52 +02:00
|
|
|
/// [Token: NotificationViewModel]
|
2022-09-30 13:28:09 +02:00
|
|
|
public let notificationSubscriptionDict: [String: NotificationViewModel] = [:]
|
|
|
|
public let unreadNotificationCountDidUpdate = CurrentValueSubject<Void, Never>(Void())
|
|
|
|
public let requestRevealNotificationPublisher = PassthroughSubject<MastodonPushNotification, Never>()
|
2021-04-25 06:48:29 +02:00
|
|
|
|
|
|
|
init(
|
2021-04-27 11:27:03 +02:00
|
|
|
apiService: APIService,
|
2021-04-25 06:48:29 +02:00
|
|
|
authenticationService: AuthenticationService
|
|
|
|
) {
|
2021-04-27 11:27:03 +02:00
|
|
|
self.apiService = apiService
|
2021-04-25 06:48:29 +02:00
|
|
|
self.authenticationService = authenticationService
|
|
|
|
|
2021-04-27 10:26:59 +02:00
|
|
|
authenticationService.mastodonAuthentications
|
|
|
|
.sink(receiveValue: { [weak self] mastodonAuthentications in
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
|
|
|
// request permission when sign-in
|
|
|
|
guard !mastodonAuthentications.isEmpty else { return }
|
|
|
|
self.requestNotificationPermission()
|
|
|
|
})
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-04-25 06:48:29 +02:00
|
|
|
deviceToken
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] deviceToken in
|
2021-04-26 10:57:50 +02:00
|
|
|
guard let _ = self else { return }
|
2021-04-25 06:48:29 +02:00
|
|
|
guard let deviceToken = deviceToken else { return }
|
|
|
|
let token = [UInt8](deviceToken).toHexString()
|
2022-09-30 13:28:09 +02:00
|
|
|
let logger = Logger(subsystem: "DeviceToken", category: "NotificationService")
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): deviceToken: \(token)")
|
2021-04-25 06:48:29 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-09-16 10:30:21 +02:00
|
|
|
|
|
|
|
Publishers.CombineLatest(
|
|
|
|
authenticationService.mastodonAuthentications,
|
|
|
|
applicationIconBadgeNeedsUpdate
|
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] mastodonAuthentications, _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
|
|
|
var count = 0
|
|
|
|
for authentication in mastodonAuthentications {
|
|
|
|
count += UserDefaults.shared.getNotificationCountWithAccessToken(accessToken: authentication.userAccessToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
UserDefaults.shared.notificationBadgeCount = count
|
|
|
|
UIApplication.shared.applicationIconBadgeNumber = count
|
|
|
|
|
|
|
|
self.unreadNotificationCountDidUpdate.send()
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-25 06:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationService {
|
|
|
|
private func requestNotificationPermission() {
|
|
|
|
let center = UNUserNotificationCenter.current()
|
|
|
|
center.requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, error in
|
|
|
|
guard let self = self else { return }
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: request notification permission: %s", ((#file as NSString).lastPathComponent), #line, #function, granted ? "granted" : "fail")
|
|
|
|
|
|
|
|
self.isNotificationPermissionGranted.value = granted
|
|
|
|
|
|
|
|
if let _ = error {
|
|
|
|
// Handle the error here.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable or disable features based on the authorization.
|
|
|
|
}
|
|
|
|
}
|
2021-04-26 10:57:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationService {
|
2021-04-25 06:48:29 +02:00
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
func dequeueNotificationViewModel(
|
2021-07-20 10:40:04 +02:00
|
|
|
mastodonAuthenticationBox: MastodonAuthenticationBox
|
2021-04-26 10:57:50 +02:00
|
|
|
) -> NotificationViewModel? {
|
|
|
|
var _notificationSubscription: NotificationViewModel?
|
2021-04-25 06:48:29 +02:00
|
|
|
workingQueue.sync {
|
|
|
|
let domain = mastodonAuthenticationBox.domain
|
|
|
|
let userID = mastodonAuthenticationBox.userID
|
|
|
|
let key = [domain, userID].joined(separator: "@")
|
|
|
|
|
|
|
|
if let notificationSubscription = notificationSubscriptionDict[key] {
|
|
|
|
_notificationSubscription = notificationSubscription
|
|
|
|
} else {
|
2021-04-26 10:57:50 +02:00
|
|
|
let notificationSubscription = NotificationViewModel(domain: domain, userID: userID)
|
2021-04-25 06:48:29 +02:00
|
|
|
_notificationSubscription = notificationSubscription
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _notificationSubscription
|
|
|
|
}
|
2021-04-27 11:27:03 +02:00
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public func handle(
|
2022-02-11 12:27:14 +01:00
|
|
|
pushNotification: MastodonPushNotification
|
|
|
|
) {
|
2021-09-16 10:30:21 +02:00
|
|
|
defer {
|
|
|
|
unreadNotificationCountDidUpdate.send()
|
|
|
|
}
|
2022-02-11 12:27:14 +01:00
|
|
|
|
|
|
|
Task {
|
|
|
|
// trigger notification timeline update
|
|
|
|
try? await fetchLatestNotifications(pushNotification: pushNotification)
|
|
|
|
|
|
|
|
// cancel sign-out account push notification subscription
|
|
|
|
try? await cancelSubscriptionForDetachedAccount(pushNotification: pushNotification)
|
|
|
|
} // end Task
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationService {
|
2022-10-08 07:43:06 +02:00
|
|
|
public func clearNotificationCountForActiveUser() {
|
2022-02-11 12:27:14 +01:00
|
|
|
guard let authenticationService = self.authenticationService else { return }
|
|
|
|
if let accessToken = authenticationService.activeMastodonAuthentication.value?.userAccessToken {
|
|
|
|
UserDefaults.shared.setNotificationCountWithAccessToken(accessToken: accessToken, value: 0)
|
|
|
|
}
|
2021-04-27 12:05:29 +02:00
|
|
|
|
2022-02-11 12:27:14 +01:00
|
|
|
applicationIconBadgeNeedsUpdate.send()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationService {
|
|
|
|
private func fetchLatestNotifications(
|
|
|
|
pushNotification: MastodonPushNotification
|
|
|
|
) async throws {
|
|
|
|
guard let apiService = apiService else { return }
|
|
|
|
guard let authenticationBox = try await authenticationBox(for: pushNotification) else { return }
|
|
|
|
|
|
|
|
_ = try await apiService.notifications(
|
|
|
|
maxID: nil,
|
|
|
|
scope: .everything,
|
|
|
|
authenticationBox: authenticationBox
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func cancelSubscriptionForDetachedAccount(
|
|
|
|
pushNotification: MastodonPushNotification
|
|
|
|
) async throws {
|
2021-04-27 12:05:29 +02:00
|
|
|
// Subscription maybe failed to cancel when sign-out
|
|
|
|
// Try cancel again if receive that kind push notification
|
|
|
|
guard let managedObjectContext = authenticationService?.managedObjectContext else { return }
|
|
|
|
guard let apiService = apiService else { return }
|
|
|
|
|
2022-02-11 12:27:14 +01:00
|
|
|
let userAccessToken = pushNotification.accessToken
|
|
|
|
|
|
|
|
let needsCancelSubscription: Bool = try await managedObjectContext.perform {
|
|
|
|
// check authentication exists
|
|
|
|
let authenticationRequest = MastodonAuthentication.sortedFetchRequest
|
|
|
|
authenticationRequest.predicate = MastodonAuthentication.predicate(userAccessToken: userAccessToken)
|
|
|
|
return managedObjectContext.safeFetch(authenticationRequest).first == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
guard needsCancelSubscription else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let domain = try await domain(for: pushNotification) else { return }
|
|
|
|
|
|
|
|
do {
|
|
|
|
_ = try await apiService.cancelSubscription(
|
|
|
|
domain: domain,
|
|
|
|
authorization: .init(accessToken: userAccessToken)
|
|
|
|
)
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: [Push Notification] cancel sign-out user subscription", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
} catch {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: [Push Notification] failed to cancel sign-out user subscription: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func domain(for pushNotification: MastodonPushNotification) async throws -> String? {
|
|
|
|
guard let authenticationService = self.authenticationService else { return nil }
|
|
|
|
let managedObjectContext = authenticationService.managedObjectContext
|
|
|
|
return try await managedObjectContext.perform {
|
2021-04-27 12:05:29 +02:00
|
|
|
let subscriptionRequest = NotificationSubscription.sortedFetchRequest
|
2022-02-11 12:27:14 +01:00
|
|
|
subscriptionRequest.predicate = NotificationSubscription.predicate(userToken: pushNotification.accessToken)
|
2021-04-27 12:05:29 +02:00
|
|
|
let subscriptions = managedObjectContext.safeFetch(subscriptionRequest)
|
|
|
|
|
2022-02-11 12:27:14 +01:00
|
|
|
// note: assert setting not remove after sign-out
|
|
|
|
guard let subscription = subscriptions.first else { return nil }
|
|
|
|
guard let setting = subscription.setting else { return nil }
|
2021-04-27 12:05:29 +02:00
|
|
|
let domain = setting.domain
|
|
|
|
|
2022-02-11 12:27:14 +01:00
|
|
|
return domain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func authenticationBox(for pushNotification: MastodonPushNotification) async throws -> MastodonAuthenticationBox? {
|
|
|
|
guard let authenticationService = self.authenticationService else { return nil }
|
|
|
|
let managedObjectContext = authenticationService.managedObjectContext
|
|
|
|
return try await managedObjectContext.perform {
|
|
|
|
let request = MastodonAuthentication.sortedFetchRequest
|
|
|
|
request.predicate = MastodonAuthentication.predicate(userAccessToken: pushNotification.accessToken)
|
|
|
|
request.fetchLimit = 1
|
|
|
|
guard let authentication = managedObjectContext.safeFetch(request).first else { return nil }
|
2021-04-27 12:05:29 +02:00
|
|
|
|
2022-02-11 12:27:14 +01:00
|
|
|
return MastodonAuthenticationBox(
|
2022-01-27 14:23:39 +01:00
|
|
|
authenticationRecord: .init(objectID: authentication.objectID),
|
2022-02-11 12:27:14 +01:00
|
|
|
domain: authentication.domain,
|
|
|
|
userID: authentication.userID,
|
|
|
|
appAuthorization: .init(accessToken: authentication.appAccessToken),
|
|
|
|
userAuthorization: .init(accessToken: authentication.userAccessToken)
|
2021-04-27 12:05:29 +02:00
|
|
|
)
|
|
|
|
}
|
2021-04-27 11:27:03 +02:00
|
|
|
}
|
|
|
|
|
2021-04-25 06:48:29 +02:00
|
|
|
}
|
|
|
|
|
2021-04-27 11:27:03 +02:00
|
|
|
// MARK: - NotificationViewModel
|
|
|
|
|
2021-04-25 06:48:29 +02:00
|
|
|
extension NotificationService {
|
2022-09-30 13:28:09 +02:00
|
|
|
public final class NotificationViewModel {
|
2021-04-25 06:48:29 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let domain: String
|
|
|
|
let userID: Mastodon.Entity.Account.ID
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
// output
|
2021-04-25 06:48:29 +02:00
|
|
|
|
|
|
|
init(domain: String, userID: Mastodon.Entity.Account.ID) {
|
|
|
|
self.domain = domain
|
|
|
|
self.userID = userID
|
|
|
|
}
|
2021-04-26 10:57:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationService.NotificationViewModel {
|
|
|
|
func createSubscribeQuery(
|
|
|
|
deviceToken: Data,
|
|
|
|
queryData: Mastodon.API.Subscriptions.QueryData,
|
2021-07-20 10:40:04 +02:00
|
|
|
mastodonAuthenticationBox: MastodonAuthenticationBox
|
2021-04-26 10:57:50 +02:00
|
|
|
) -> Mastodon.API.Subscriptions.CreateSubscriptionQuery {
|
|
|
|
let deviceToken = [UInt8](deviceToken).toHexString()
|
2021-04-25 06:48:29 +02:00
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
let appSecret = AppSecret.default
|
|
|
|
let endpoint = appSecret.notificationEndpoint + "/" + deviceToken
|
2021-04-27 10:26:59 +02:00
|
|
|
let p256dh = appSecret.notificationPublicKey.x963Representation
|
2021-04-26 10:57:50 +02:00
|
|
|
let auth = appSecret.notificationAuth
|
|
|
|
|
|
|
|
let query = Mastodon.API.Subscriptions.CreateSubscriptionQuery(
|
|
|
|
subscription: Mastodon.API.Subscriptions.QuerySubscription(
|
|
|
|
endpoint: endpoint,
|
|
|
|
keys: Mastodon.API.Subscriptions.QuerySubscription.Keys(
|
|
|
|
p256dh: p256dh,
|
|
|
|
auth: auth
|
|
|
|
)
|
|
|
|
),
|
|
|
|
data: queryData
|
|
|
|
)
|
|
|
|
|
|
|
|
return query
|
2021-04-25 06:48:29 +02:00
|
|
|
}
|
2021-04-27 11:27:03 +02:00
|
|
|
|
2021-04-25 06:48:29 +02:00
|
|
|
}
|