Adding notifications for updater (#70)

This commit is contained in:
Max Goedjen
2020-03-21 18:43:26 -07:00
committed by GitHub
parent 0a9ecb039e
commit df8eedebd0
10 changed files with 334 additions and 21 deletions

View File

@@ -1,7 +1,9 @@
import Cocoa
import OSLog
import Combine
import SecretKit
import SecretAgentKit
import OSLog
import Brief
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@@ -12,6 +14,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
list.add(store: SmartCard.Store())
return list
}()
let updater = Updater()
let notifier = Notifier()
lazy var agent: Agent = {
Agent(storeList: storeList, witness: notifier)
@@ -20,6 +23,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let path = (NSHomeDirectory() as NSString).appendingPathComponent("socket.ssh") as String
return SocketController(path: path)
}()
fileprivate var updateSink: AnyCancellable?
func applicationDidFinishLaunching(_ aNotification: Notification) {
os_log(.debug, "SecretAgent finished launching")
@@ -27,10 +31,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.socketController.handler = self.agent.handle(fileHandle:)
}
notifier.prompt()
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
updateSink = updater.$update.sink { release in
guard let release = release else { return }
self.notifier.notify(update: release)
}
}

View File

@@ -1,11 +1,21 @@
import Foundation
import SecretKit
import SecretAgentKit
import UserNotifications
import AppKit
import SecretKit
import SecretAgentKit
import Brief
class Notifier {
fileprivate let notificationDelegate = NotificationDelegate()
init() {
let action = UNNotificationAction(identifier: Constants.updateIdentitifier, title: "Update", options: [])
let categories = UNNotificationCategory(identifier: Constants.updateIdentitifier, actions: [action], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([categories])
UNUserNotificationCenter.current().delegate = notificationDelegate
}
func prompt() {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: .alert) { _, _ in
@@ -24,6 +34,22 @@ class Notifier {
notificationCenter.add(request, withCompletionHandler: nil)
}
func notify(update: Release) {
notificationDelegate.release = update
let notificationCenter = UNUserNotificationCenter.current()
let notificationContent = UNMutableNotificationContent()
if update.critical {
notificationContent.title = "Critical Security Update - \(update.name)"
} else {
notificationContent.title = "Update Available - \(update.name)"
}
notificationContent.subtitle = "Click to Update"
notificationContent.body = update.body
notificationContent.categoryIdentifier = Constants.updateIdentitifier
let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: nil)
notificationCenter.add(request, withCompletionHandler: nil)
}
}
extension Notifier {
@@ -53,3 +79,32 @@ extension Notifier: SigningWitness {
}
}
extension Notifier {
enum Constants {
static let updateIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.update"
}
}
class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
fileprivate var release: Release?
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
guard response.notification.request.content.categoryIdentifier == Notifier.Constants.updateIdentitifier else { return }
guard let update = release else { return }
NSWorkspace.shared.open(update.html_url)
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
}

View File

@@ -4,6 +4,8 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.smartcard</key>
<true/>
<key>keychain-access-groups</key>