Compare commits

...

13 Commits

21 changed files with 426 additions and 182 deletions

View File

@ -1,5 +1,9 @@
# Changelog
## 2024.1 (119)
Features/Improvements:
- Add Account Settings button to Preferences
## 2024.1 (118)
Bugfixes:
- Fix music not pausing/resuming when video playback starts

View File

@ -13,7 +13,7 @@ class DisabledPushManager: _PushManager {
false
}
var pushProxyRegistration: PushProxyRegistration? {
var proxyRegistration: PushProxyRegistration? {
nil
}
@ -36,7 +36,7 @@ class DisabledPushManager: _PushManager {
throw Disabled()
}
func updateIfNecessary() async {
func updateIfNecessary(updateSubscription: @escaping (PushSubscription) async -> Bool) async {
}
func didRegisterForRemoteNotifications(deviceToken: Data) {

View File

@ -7,9 +7,6 @@
import Foundation
import OSLog
#if canImport(Sentry)
import Sentry
#endif
import Pachyderm
import UserAccounts
@ -18,6 +15,9 @@ public struct PushManager {
public static let shared = createPushManager()
public static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "PushManager")
@MainActor
public static var captureError: ((any Error) -> Void)?
private init() {}
@ -43,7 +43,7 @@ public struct PushManager {
@MainActor
public protocol _PushManager {
var enabled: Bool { get }
var pushProxyRegistration: PushProxyRegistration? { get }
var proxyRegistration: PushProxyRegistration? { get }
func createSubscription(account: UserAccountInfo) throws -> PushSubscription
func removeSubscription(account: UserAccountInfo)
@ -51,7 +51,7 @@ public protocol _PushManager {
func register(transactionID: UInt64) async throws -> PushProxyRegistration
func unregister() async throws
func updateIfNecessary() async
func updateIfNecessary(updateSubscription: @escaping (PushSubscription) async -> Bool) async
func didRegisterForRemoteNotifications(deviceToken: Data)
func didFailToRegisterForRemoteNotifications(error: any Error)

View File

@ -7,9 +7,6 @@
import UIKit
import UserAccounts
#if canImport(Sentry)
import Sentry
#endif
import CryptoKit
class PushManagerImpl: _PushManager {
@ -30,7 +27,7 @@ class PushManagerImpl: _PushManager {
private var remoteNotificationsRegistrationContinuation: CheckedContinuation<Data, any Error>?
private let defaults = UserDefaults(suiteName: "group.space.vaccor.Tusker")!
private(set) var pushProxyRegistration: PushProxyRegistration? {
private(set) var proxyRegistration: PushProxyRegistration? {
get {
if let dict = defaults.dictionary(forKey: "PushProxyRegistration") as? [String: String],
let registration = PushProxyRegistration(defaultsDict: dict) {
@ -43,7 +40,7 @@ class PushManagerImpl: _PushManager {
defaults.setValue(newValue?.defaultsDict, forKey: "PushProxyRegistration")
}
}
private(set) var pushSubscriptions: [PushSubscription] {
private(set) var subscriptions: [PushSubscription] {
get {
if let array = defaults.array(forKey: "PushSubscriptions") as? [[String: Any]] {
return array.compactMap(PushSubscription.init(defaultsDict:))
@ -61,7 +58,7 @@ class PushManagerImpl: _PushManager {
}
func createSubscription(account: UserAccountInfo) throws -> PushSubscription {
guard let pushProxyRegistration else {
guard let proxyRegistration else {
throw CreateSubscriptionError.notRegisteredWithProxy
}
if let existing = pushSubscription(account: account) {
@ -77,22 +74,29 @@ class PushManagerImpl: _PushManager {
}
let subscription = PushSubscription(
accountID: account.id,
endpoint: pushProxyRegistration.endpoint,
endpoint: endpointURL(registration: proxyRegistration, accountID: account.id),
secretKey: key,
authSecret: authSecret,
alerts: [],
policy: .all
)
pushSubscriptions.append(subscription)
subscriptions.append(subscription)
return subscription
}
private func endpointURL(registration: PushProxyRegistration, accountID: String) -> URL {
var endpoint = URLComponents(url: registration.endpoint, resolvingAgainstBaseURL: false)!
endpoint.queryItems = endpoint.queryItems ?? []
endpoint.queryItems!.append(URLQueryItem(name: "ctx", value: accountID))
return endpoint.url!
}
func removeSubscription(account: UserAccountInfo) {
pushSubscriptions.removeAll { $0.accountID == account.id }
subscriptions.removeAll { $0.accountID == account.id }
}
func pushSubscription(account: UserAccountInfo) -> PushSubscription? {
pushSubscriptions.first { $0.accountID == account.id }
subscriptions.first { $0.accountID == account.id }
}
func register(transactionID: UInt64) async throws -> PushProxyRegistration {
@ -109,22 +113,22 @@ class PushManagerImpl: _PushManager {
PushManager.logger.error("Proxy registration failed: \(String(describing: error))")
throw PushRegistrationError.registeringWithProxy(error)
}
pushProxyRegistration = registration
proxyRegistration = registration
return registration
}
func unregister() async throws {
guard let pushProxyRegistration else {
guard let proxyRegistration else {
return
}
var url = URLComponents(url: endpoint, resolvingAgainstBaseURL: false)!
url.path = "/app/v1/registrations/\(pushProxyRegistration.id)"
url.path = "/app/v1/registrations/\(proxyRegistration.id)"
var request = URLRequest(url: url.url!)
request.httpMethod = "DELETE"
let (data, resp) = try await URLSession.shared.data(for: request)
let status = (resp as! HTTPURLResponse).statusCode
if (200...299).contains(status) {
self.pushProxyRegistration = nil
self.proxyRegistration = nil
PushManager.logger.debug("Unregistered from proxy")
} else {
PushManager.logger.error("Unregistering: unexpected status \(status)")
@ -133,26 +137,35 @@ class PushManagerImpl: _PushManager {
}
}
func updateIfNecessary() async {
guard let pushProxyRegistration else {
func updateIfNecessary(updateSubscription: @escaping (PushSubscription) async -> Bool) async {
guard let proxyRegistration else {
return
}
PushManager.logger.debug("Push proxy registration: \(pushProxyRegistration.id, privacy: .public)")
PushManager.logger.debug("Push proxy registration: \(proxyRegistration.id, privacy: .public)")
do {
let token = try await getDeviceToken().hexEncodedString()
guard token != pushProxyRegistration.deviceToken else {
guard token != proxyRegistration.deviceToken else {
// already up-to-date, nothing to do
return
}
let newRegistration = try await update(registration: pushProxyRegistration, deviceToken: token)
if pushProxyRegistration.endpoint != newRegistration.endpoint {
// TODO: update subscriptions if the endpoint's changed
let newRegistration = try await update(registration: proxyRegistration, deviceToken: token)
self.proxyRegistration = newRegistration
if proxyRegistration.endpoint != newRegistration.endpoint {
self.subscriptions = await AsyncSequenceAdaptor(wrapping: self.subscriptions).map {
var copy = $0
copy.endpoint = await self.endpointURL(registration: newRegistration, accountID: $0.accountID)
if await updateSubscription(copy) {
return copy
} else {
return $0
}
}.reduce(into: [], { partialResult, el in
partialResult.append(el)
})
}
} catch {
PushManager.logger.error("Failed to update push registration: \(String(describing: error), privacy: .public)")
#if canImport(Sentry)
SentrySDK.capture(error: error)
#endif
PushManager.captureError?(error)
}
}
@ -301,3 +314,25 @@ private extension Data {
}
}
}
private struct AsyncSequenceAdaptor<S: Sequence>: AsyncSequence {
typealias Element = S.Element
let base: S
init(wrapping base: S) {
self.base = base
}
func makeAsyncIterator() -> AsyncIterator {
AsyncIterator(base: base.makeIterator())
}
struct AsyncIterator: AsyncIteratorProtocol {
var base: S.Iterator
mutating func next() async -> Element? {
base.next()
}
}
}

View File

@ -9,8 +9,8 @@ import Foundation
import CryptoKit
public struct PushSubscription {
let accountID: String
let endpoint: URL
public let accountID: String
public internal(set) var endpoint: URL
public let secretKey: P256.KeyAgreement.PrivateKey
public let authSecret: Data
public var alerts: Alerts

View File

@ -1,6 +1,6 @@
//
// TriStateToggle.swift
// Tusker
// AsyncToggle.swift
// TuskerComponents
//
// Created by Shadowfacts on 4/7/24.
// Copyright © 2024 Shadowfacts. All rights reserved.
@ -8,26 +8,21 @@
import SwiftUI
struct TriStateToggle: View {
public struct AsyncToggle: View {
let titleKey: LocalizedStringKey
@available(iOS, obsoleted: 16.0, message: "Switch to LabeledContent")
let labelHidden: Bool
@Binding var mode: Mode
let onChange: (Bool) async -> Void
let onChange: (Bool) async -> Bool
init(_ titleKey: LocalizedStringKey, labelHidden: Bool = false, mode: Binding<Mode>, onChange: @escaping (Bool) async -> Void) {
public init(_ titleKey: LocalizedStringKey, labelHidden: Bool = false, mode: Binding<Mode>, onChange: @escaping (Bool) async -> Bool) {
self.titleKey = titleKey
self.labelHidden = labelHidden
self._mode = mode
self.onChange = onChange
}
var body: some View {
content
}
@ViewBuilder
private var content: some View {
public var body: some View {
if #available(iOS 16.0, *) {
LabeledContent(titleKey) {
toggleOrSpinner
@ -51,8 +46,12 @@ struct TriStateToggle: View {
} set: { newValue in
mode = .loading
Task {
await onChange(newValue)
mode = newValue ? .on : .off
let operationCompleted = await onChange(newValue)
if operationCompleted {
mode = newValue ? .on : .off
} else {
mode = newValue ? .off : .on
}
}
})
.labelsHidden()
@ -64,7 +63,7 @@ struct TriStateToggle: View {
}
}
enum Mode {
public enum Mode {
case off
case loading
case on
@ -72,8 +71,9 @@ struct TriStateToggle: View {
}
#Preview {
@State var mode = TriStateToggle.Mode.on
return TriStateToggle("", mode: $mode) { _ in
@State var mode = AsyncToggle.Mode.on
return AsyncToggle("", mode: $mode) { _ in
try! await Task.sleep(nanoseconds: NSEC_PER_SEC)
return true
}
}

View File

@ -13,8 +13,9 @@ public struct UserAccountInfo: Equatable, Hashable, Identifiable, Sendable {
public let instanceURL: URL
public let clientID: String
public let clientSecret: String
public private(set) var username: String!
public let accessToken: String
public let username: String!
public internal(set) var accessToken: String
public internal(set) var scopes: [String]?
// Sort of hack to be able to access these from the share extension.
public internal(set) var serverDefaultLanguage: String?
@ -40,16 +41,19 @@ public struct UserAccountInfo: Equatable, Hashable, Identifiable, Sendable {
self.instanceURL = instanceURL
self.clientID = clientID
self.clientSecret = clientSecret
self.username = nil
self.accessToken = accessToken
self.scopes = nil
}
init(instanceURL: URL, clientID: String, clientSecret: String, username: String? = nil, accessToken: String) {
init(instanceURL: URL, clientID: String, clientSecret: String, username: String? = nil, accessToken: String, scopes: [String]) {
self.id = UserAccountInfo.id(instanceURL: instanceURL, username: username)
self.instanceURL = instanceURL
self.clientID = clientID
self.clientSecret = clientSecret
self.username = username
self.accessToken = accessToken
self.scopes = scopes
}
init?(userDefaultsDict dict: [String: Any]) {
@ -67,6 +71,7 @@ public struct UserAccountInfo: Equatable, Hashable, Identifiable, Sendable {
self.clientSecret = secret
self.username = dict["username"] as? String
self.accessToken = accessToken
self.scopes = dict["scopes"] as? [String]
self.serverDefaultLanguage = dict["serverDefaultLanguage"] as? String
self.serverDefaultVisibility = dict["serverDefaultVisibility"] as? String
self.serverDefaultFederation = dict["serverDefaultFederation"] as? Bool
@ -83,6 +88,9 @@ public struct UserAccountInfo: Equatable, Hashable, Identifiable, Sendable {
if let username {
dict["username"] = username
}
if let scopes {
dict["scopes"] = scopes
}
if let serverDefaultLanguage {
dict["serverDefaultLanguage"] = serverDefaultLanguage
}
@ -100,12 +108,4 @@ public struct UserAccountInfo: Equatable, Hashable, Identifiable, Sendable {
// slashes are not allowed in the persistent store coordinator name
id.replacingOccurrences(of: "/", with: "_")
}
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
public static func ==(lhs: UserAccountInfo, rhs: UserAccountInfo) -> Bool {
return lhs.id == rhs.id
}
}

View File

@ -25,7 +25,9 @@ public class UserAccountsManager: ObservableObject {
clientID: "client_id",
clientSecret: "client_secret",
username: "admin",
accessToken: "access_token")
accessToken: "access_token",
scopes: []
)
]
}
} else {
@ -38,7 +40,7 @@ public class UserAccountsManager: ObservableObject {
private let accountsKey = "accounts"
public private(set) var accounts: [UserAccountInfo] {
get {
if let array = defaults.array(forKey: accountsKey) as? [[String: String]] {
if let array = defaults.array(forKey: accountsKey) as? [[String: Any]] {
return array.compactMap(UserAccountInfo.init(userDefaultsDict:))
} else {
return []
@ -101,12 +103,12 @@ public class UserAccountsManager: ObservableObject {
return !accounts.isEmpty
}
public func addAccount(instanceURL url: URL, clientID: String, clientSecret secret: String, username: String?, accessToken: String) -> UserAccountInfo {
public func addAccount(instanceURL url: URL, clientID: String, clientSecret secret: String, username: String?, accessToken: String, scopes: [String]) -> UserAccountInfo {
var accounts = self.accounts
if let index = accounts.firstIndex(where: { $0.instanceURL == url && $0.username == username }) {
accounts.remove(at: index)
}
let info = UserAccountInfo(instanceURL: url, clientID: clientID, clientSecret: secret, username: username, accessToken: accessToken)
let info = UserAccountInfo(instanceURL: url, clientID: clientID, clientSecret: secret, username: username, accessToken: accessToken, scopes: scopes)
accounts.append(info)
self.accounts = accounts
return info
@ -145,6 +147,16 @@ public class UserAccountsManager: ObservableObject {
account.serverDefaultFederation = defaultFederation
accounts[index] = account
}
public func updateAccessToken(_ account: UserAccountInfo, token: String, scopes: [String]) {
guard let index = accounts.firstIndex(where: { $0.id == account.id }) else {
return
}
var account = account
account.accessToken = token
account.scopes = scopes
accounts[index] = account
}
}

View File

@ -93,6 +93,8 @@
D62E9987279D094F00C26176 /* StatusMetaIndicatorsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D62E9986279D094F00C26176 /* StatusMetaIndicatorsView.swift */; };
D62FF04823D7CDD700909D6E /* AttributedStringHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D62FF04723D7CDD700909D6E /* AttributedStringHelperTests.swift */; };
D630C3C82BC43AFD00208903 /* PushNotifications in Frameworks */ = {isa = PBXBuildFile; productRef = D630C3C72BC43AFD00208903 /* PushNotifications */; };
D630C3CA2BC59FF500208903 /* MastodonController+Push.swift in Sources */ = {isa = PBXBuildFile; fileRef = D630C3C92BC59FF500208903 /* MastodonController+Push.swift */; };
D630C3CC2BC5FD4600208903 /* GetAuthorizationTokenService.swift in Sources */ = {isa = PBXBuildFile; fileRef = D630C3CB2BC5FD4600208903 /* GetAuthorizationTokenService.swift */; };
D6311C5025B3765B00B27539 /* ImageDataCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6311C4F25B3765B00B27539 /* ImageDataCache.swift */; };
D6333B372137838300CE884A /* AttributedString+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6333B362137838300CE884A /* AttributedString+Helpers.swift */; };
D6333B792139AEFD00CE884A /* Date+TimeAgo.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6333B782139AEFD00CE884A /* Date+TimeAgo.swift */; };
@ -133,7 +135,6 @@
D6552367289870790048A653 /* ScreenCorners in Frameworks */ = {isa = PBXBuildFile; platformFilters = (ios, maccatalyst, ); productRef = D6552366289870790048A653 /* ScreenCorners */; };
D659F35E2953A212002D944A /* TTTKit in Frameworks */ = {isa = PBXBuildFile; productRef = D659F35D2953A212002D944A /* TTTKit */; };
D659F36229541065002D944A /* TTTView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D659F36129541065002D944A /* TTTView.swift */; };
D65A261B2BC3928A005EB5D8 /* TriStateToggle.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65A261A2BC3928A005EB5D8 /* TriStateToggle.swift */; };
D65A261D2BC39399005EB5D8 /* PushInstanceSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65A261C2BC39399005EB5D8 /* PushInstanceSettingsView.swift */; };
D65B4B542971F71D00DABDFB /* EditedReport.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B532971F71D00DABDFB /* EditedReport.swift */; };
D65B4B562971F98300DABDFB /* ReportView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B552971F98300DABDFB /* ReportView.swift */; };
@ -498,6 +499,8 @@
D62E9984279CA23900C26176 /* URLSession+Development.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URLSession+Development.swift"; sourceTree = "<group>"; };
D62E9986279D094F00C26176 /* StatusMetaIndicatorsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusMetaIndicatorsView.swift; sourceTree = "<group>"; };
D62FF04723D7CDD700909D6E /* AttributedStringHelperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributedStringHelperTests.swift; sourceTree = "<group>"; };
D630C3C92BC59FF500208903 /* MastodonController+Push.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MastodonController+Push.swift"; sourceTree = "<group>"; };
D630C3CB2BC5FD4600208903 /* GetAuthorizationTokenService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetAuthorizationTokenService.swift; sourceTree = "<group>"; };
D6311C4F25B3765B00B27539 /* ImageDataCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageDataCache.swift; sourceTree = "<group>"; };
D6333B362137838300CE884A /* AttributedString+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AttributedString+Helpers.swift"; sourceTree = "<group>"; };
D6333B782139AEFD00CE884A /* Date+TimeAgo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+TimeAgo.swift"; sourceTree = "<group>"; };
@ -536,7 +539,6 @@
D6531DED246B81C9000F9538 /* GifvPlayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GifvPlayerView.swift; sourceTree = "<group>"; };
D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewSwipeActionProvider.swift; sourceTree = "<group>"; };
D659F36129541065002D944A /* TTTView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TTTView.swift; sourceTree = "<group>"; };
D65A261A2BC3928A005EB5D8 /* TriStateToggle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TriStateToggle.swift; sourceTree = "<group>"; };
D65A261C2BC39399005EB5D8 /* PushInstanceSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PushInstanceSettingsView.swift; sourceTree = "<group>"; };
D65A26242BC39A02005EB5D8 /* PushNotifications */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = PushNotifications; sourceTree = "<group>"; };
D65B4B532971F71D00DABDFB /* EditedReport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditedReport.swift; sourceTree = "<group>"; };
@ -1203,7 +1205,6 @@
D64B967B2BC19C28002C8990 /* NotificationsPrefsView.swift */,
D65A261C2BC39399005EB5D8 /* PushInstanceSettingsView.swift */,
D64B96832BC3893C002C8990 /* PushSubscriptionView.swift */,
D65A261A2BC3928A005EB5D8 /* TriStateToggle.swift */,
);
path = Notifications;
sourceTree = "<group>";
@ -1654,6 +1655,7 @@
isa = PBXGroup;
children = (
D6F953EF21251A2900CF0F2B /* MastodonController.swift */,
D630C3C92BC59FF500208903 /* MastodonController+Push.swift */,
D61ABEFD28F1C92600B29151 /* FavoriteService.swift */,
D621733228F1D5ED004C7DB1 /* ReblogService.swift */,
D6F6A54F291F058600F496A8 /* CreateListService.swift */,
@ -1668,6 +1670,7 @@
D65B4B6529773AE600DABDFB /* DeleteStatusService.swift */,
D6DD8FFC298495A8002AD3FD /* LogoutService.swift */,
D6D79F252A0C8D2700AB2315 /* FetchStatusSourceService.swift */,
D630C3CB2BC5FD4600208903 /* GetAuthorizationTokenService.swift */,
);
path = API;
sourceTree = "<group>";
@ -2064,6 +2067,7 @@
D667E5F82135C3040057A976 /* Mastodon+Equatable.swift in Sources */,
D6B4A4FF2506B81A000C81C1 /* AccountDisplayNameView.swift in Sources */,
D63D8DF42850FE7A008D95E1 /* ViewTags.swift in Sources */,
D630C3CC2BC5FD4600208903 /* GetAuthorizationTokenService.swift in Sources */,
D61DC84B28F4FD2000B82C6E /* ProfileHeaderCollectionViewCell.swift in Sources */,
D61A45E828DF477D002BE511 /* LoadingCollectionViewCell.swift in Sources */,
D6934F3A2BA8F3D7002B1C8D /* FallbackGalleryContentViewController.swift in Sources */,
@ -2153,7 +2157,6 @@
D6333B792139AEFD00CE884A /* Date+TimeAgo.swift in Sources */,
D6D706A029466649000827ED /* ScrollingSegmentedControl.swift in Sources */,
D686BBE324FBF8110068E6AA /* WrappedProgressView.swift in Sources */,
D65A261B2BC3928A005EB5D8 /* TriStateToggle.swift in Sources */,
D620483423D3801D008A63EF /* LinkTextView.swift in Sources */,
D61F75882932DB6000C0B37F /* StatusSwipeActions.swift in Sources */,
D68A76EA295285D0001DA1B3 /* AddHashtagPinnedTimelineView.swift in Sources */,
@ -2232,6 +2235,7 @@
D6BEA247291A0F2D002F4D01 /* Duckable+Root.swift in Sources */,
D6C1B2082545D1EC00DAAA66 /* StatusCardView.swift in Sources */,
D64D8CA92463B494006B0BAA /* MultiThreadDictionary.swift in Sources */,
D630C3CA2BC59FF500208903 /* MastodonController+Push.swift in Sources */,
D6F6A554291F0D9600F496A8 /* DeleteListService.swift in Sources */,
D68E6F5F253C9B2D001A1B4C /* BaseEmojiLabel.swift in Sources */,
D6F0B12B24A3071C001E48C3 /* MainSplitViewController.swift in Sources */,

View File

@ -0,0 +1,63 @@
//
// GetAuthorizationTokenService.swift
// Tusker
//
// Created by Shadowfacts on 4/9/24.
// Copyright © 2024 Shadowfacts. All rights reserved.
//
import Foundation
import AuthenticationServices
class GetAuthorizationTokenService {
let instanceURL: URL
let clientID: String
let presentationContextProvider: ASWebAuthenticationPresentationContextProviding
init(instanceURL: URL, clientID: String, presentationContextProvider: ASWebAuthenticationPresentationContextProviding) {
self.instanceURL = instanceURL
self.clientID = clientID
self.presentationContextProvider = presentationContextProvider
}
@MainActor
func run() async throws -> String {
var components = URLComponents(url: instanceURL, resolvingAgainstBaseURL: false)!
components.path = "/oauth/authorize"
components.queryItems = [
URLQueryItem(name: "client_id", value: clientID),
URLQueryItem(name: "response_type", value: "code"),
URLQueryItem(name: "scope", value: MastodonController.oauthScopes.map(\.rawValue).joined(separator: " ")),
URLQueryItem(name: "redirect_uri", value: "tusker://oauth")
]
let authorizeURL = components.url!
return try await withCheckedThrowingContinuation({ continuation in
let authenticationSession = ASWebAuthenticationSession(url: authorizeURL, callbackURLScheme: "tusker", completionHandler: { url, error in
if let error = error {
if (error as? ASWebAuthenticationSessionError)?.code == .canceledLogin {
continuation.resume(throwing: Error.cancelled)
} else {
continuation.resume(throwing: error)
}
} else if let url = url,
let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
let item = components.queryItems?.first(where: { $0.name == "code" }),
let code = item.value {
continuation.resume(returning: code)
} else {
continuation.resume(throwing: Error.noAuthorizationCode)
}
})
// Prefer ephemeral sessions to make it easier to sign into multiple accounts on the same instance.
authenticationSession.prefersEphemeralWebBrowserSession = true
authenticationSession.presentationContextProvider = presentationContextProvider
authenticationSession.start()
})
}
enum Error: Swift.Error {
case cancelled
case noAuthorizationCode
}
}

View File

@ -8,6 +8,8 @@
import Foundation
import UserAccounts
import PushNotifications
import Pachyderm
@MainActor
class LogoutService {
@ -20,7 +22,12 @@ class LogoutService {
}
func run() {
let accountInfo = self.accountInfo
Task.detached {
if await PushManager.shared.pushSubscription(account: accountInfo) != nil {
_ = try? await self.mastodonController.run(Pachyderm.PushSubscription.delete())
await PushManager.shared.removeSubscription(account: accountInfo)
}
try? await self.mastodonController.client.revokeAccessToken()
}
MastodonController.removeForAccount(accountInfo)

View File

@ -0,0 +1,71 @@
//
// MastodonController+Push.swift
// Tusker
//
// Created by Shadowfacts on 4/9/24.
// Copyright © 2024 Shadowfacts. All rights reserved.
//
import Foundation
import Pachyderm
import PushNotifications
import CryptoKit
extension MastodonController {
func createPushSubscription(subscription: PushNotifications.PushSubscription) async throws -> Pachyderm.PushSubscription {
let req = Pachyderm.PushSubscription.create(
endpoint: subscription.endpoint,
// mastodon docs just say "Base64 encoded string of a public key from a ECDH keypair using the prime256v1 curve."
// other apps use SecKeyCopyExternalRepresentation which is documented to use X9.63 for elliptic curve keys
// and that seems to work
publicKey: subscription.secretKey.publicKey.x963Representation,
authSecret: subscription.authSecret,
alerts: .init(subscription.alerts),
policy: .init(subscription.policy)
)
return try await run(req).0
}
func updatePushSubscription(subscription: PushNotifications.PushSubscription) async throws -> Pachyderm.PushSubscription {
// when updating anything other than the alerts/policy, we need to go through the create route
return try await createPushSubscription(subscription: subscription)
}
func updatePushSubscription(alerts: PushNotifications.PushSubscription.Alerts, policy: PushNotifications.PushSubscription.Policy) async throws -> Pachyderm.PushSubscription {
let req = Pachyderm.PushSubscription.update(alerts: .init(alerts), policy: .init(policy))
return try await run(req).0
}
func deletePushSubscription() async throws {
let req = Pachyderm.PushSubscription.delete()
_ = try await run(req)
}
}
private extension Pachyderm.PushSubscription.Alerts {
init(_ alerts: PushNotifications.PushSubscription.Alerts) {
self.init(
mention: alerts.contains(.mention),
status: alerts.contains(.status),
reblog: alerts.contains(.reblog),
follow: alerts.contains(.follow),
followRequest: alerts.contains(.followRequest),
favourite: alerts.contains(.favorite),
poll: alerts.contains(.poll),
update: alerts.contains(.update)
)
}
}
private extension Pachyderm.PushSubscription.Policy {
init(_ policy: PushNotifications.PushSubscription.Policy) {
switch policy {
case .all:
self = .all
case .followers:
self = .followers
case .followed:
self = .followed
}
}
}

View File

@ -24,22 +24,22 @@ final class MastodonController: ObservableObject, Sendable {
static let oauthScopes = [Scope.read, .write, .follow, .push]
@MainActor
static private(set) var all = [UserAccountInfo: MastodonController]()
static private(set) var all = [String: MastodonController]()
@MainActor
static func getForAccount(_ account: UserAccountInfo) -> MastodonController {
if let controller = all[account] {
if let controller = all[account.id] {
return controller
} else {
let controller = MastodonController(instanceURL: account.instanceURL, accountInfo: account)
all[account] = controller
all[account.id] = controller
return controller
}
}
@MainActor
static func removeForAccount(_ account: UserAccountInfo) {
all.removeValue(forKey: account)
all.removeValue(forKey: account.id)
}
@MainActor

View File

@ -84,9 +84,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
BackgroundManager.shared.registerHandlers()
Task {
await PushManager.shared.updateIfNecessary()
}
initializePushManager()
return true
}
@ -182,6 +180,26 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
PushManager.shared.didFailToRegisterForRemoteNotifications(error: error)
}
private func initializePushManager() {
Task {
PushManager.captureError = { SentrySDK.capture(error: $0) }
await PushManager.shared.updateIfNecessary(updateSubscription: {
guard let account = UserAccountsManager.shared.getAccount(id: $0.accountID) else {
return false
}
let mastodonController = MastodonController.getForAccount(account)
do {
let result = try await mastodonController.updatePushSubscription(subscription: $0)
PushManager.logger.debug("Updated push subscription \(result.id) on \(mastodonController.instanceURL)")
return true
} catch {
PushManager.logger.error("Error updating push subscription: \(String(describing: error))")
return false
}
})
}
}
#if !os(visionOS)
private func swizzleStatusBar() {
let selector = Selector(("handleTapAction:"))

View File

@ -158,7 +158,14 @@ class OnboardingViewController: UINavigationController {
throw Error.gettingOwnAccount(error)
}
let accountInfo = UserAccountsManager.shared.addAccount(instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: ownAccount.username, accessToken: accessToken)
let accountInfo = UserAccountsManager.shared.addAccount(
instanceURL: instanceURL,
clientID: clientID,
clientSecret: clientSecret,
username: ownAccount.username,
accessToken: accessToken,
scopes: MastodonController.oauthScopes.map(\.rawValue)
)
self.onboardingDelegate?.didFinishOnboarding(account: accountInfo)
}
@ -177,38 +184,19 @@ class OnboardingViewController: UINavigationController {
@MainActor
private func getAuthorizationCode(instanceURL: URL, clientID: String) async throws -> String {
var components = URLComponents(url: instanceURL, resolvingAgainstBaseURL: false)!
components.path = "/oauth/authorize"
components.queryItems = [
URLQueryItem(name: "client_id", value: clientID),
URLQueryItem(name: "response_type", value: "code"),
URLQueryItem(name: "scope", value: MastodonController.oauthScopes.map(\.rawValue).joined(separator: " ")),
URLQueryItem(name: "redirect_uri", value: "tusker://oauth")
]
let authorizeURL = components.url!
return try await withCheckedThrowingContinuation({ continuation in
self.authenticationSession = ASWebAuthenticationSession(url: authorizeURL, callbackURLScheme: "tusker", completionHandler: { url, error in
if let error = error {
if (error as? ASWebAuthenticationSessionError)?.code == .canceledLogin {
continuation.resume(throwing: Error.cancelled)
} else {
continuation.resume(throwing: Error.authenticationSessionError(error))
}
} else if let url = url,
let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
let item = components.queryItems?.first(where: { $0.name == "code" }),
let code = item.value {
continuation.resume(returning: code)
} else {
continuation.resume(throwing: Error.noAuthorizationCode)
}
})
// Prefer ephemeral sessions to make it easier to sign into multiple accounts on the same instance.
self.authenticationSession!.prefersEphemeralWebBrowserSession = true
self.authenticationSession!.presentationContextProvider = self
self.authenticationSession!.start()
})
do {
let service = GetAuthorizationTokenService(instanceURL: instanceURL, clientID: clientID, presentationContextProvider: self)
return try await service.run()
} catch let error as GetAuthorizationTokenService.Error {
switch error {
case .cancelled:
throw Error.cancelled
case .noAuthorizationCode:
throw Error.noAuthorizationCode
}
} catch {
throw Error.authenticationSessionError(error)
}
}
}

View File

@ -10,10 +10,11 @@ import SwiftUI
import UserNotifications
import UserAccounts
import PushNotifications
import TuskerComponents
struct NotificationsPrefsView: View {
@State private var error: NotificationsSetupError?
@State private var isSetup = TriStateToggle.Mode.off
@State private var isSetup = AsyncToggle.Mode.off
@State private var pushProxyRegistration: PushProxyRegistration?
@ObservedObject private var userAccounts = UserAccountsManager.shared
@ -32,7 +33,7 @@ struct NotificationsPrefsView: View {
private var enableSection: some View {
Section {
TriStateToggle("Push Notifications", mode: $isSetup, onChange: isSetupChanged(newValue:))
AsyncToggle("Push Notifications", mode: $isSetup, onChange: isSetupChanged(newValue:))
}
.appGroupedListRowBackground()
.alertWithData("An Error Occurred", data: $error) { error in
@ -41,7 +42,7 @@ struct NotificationsPrefsView: View {
Text(error.localizedDescription)
}
.task { @MainActor in
pushProxyRegistration = PushManager.shared.pushProxyRegistration
pushProxyRegistration = PushManager.shared.proxyRegistration
isSetup = pushProxyRegistration != nil ? .on : .off
if !UIApplication.shared.isRegisteredForRemoteNotifications {
_ = await registerForRemoteNotifications()
@ -58,17 +59,11 @@ struct NotificationsPrefsView: View {
.appGroupedListRowBackground()
}
private func isSetupChanged(newValue: Bool) async {
private func isSetupChanged(newValue: Bool) async -> Bool {
if newValue {
let success = await startRegistration()
if !success {
isSetup = .off
}
return await startRegistration()
} else {
let success = await unregister()
if !success {
isSetup = .on
}
return await unregister()
}
}

View File

@ -10,13 +10,15 @@ import SwiftUI
import UserAccounts
import Pachyderm
import PushNotifications
import TuskerComponents
struct PushInstanceSettingsView: View {
let account: UserAccountInfo
let pushProxyRegistration: PushProxyRegistration
@State private var mode: TriStateToggle.Mode
@State private var mode: AsyncToggle.Mode
@State private var error: Error?
@State private var subscription: PushNotifications.PushSubscription?
@State private var showReLoginRequiredAlert = false
@MainActor
init(account: UserAccountInfo, pushProxyRegistration: PushProxyRegistration) {
@ -32,7 +34,7 @@ struct PushInstanceSettingsView: View {
HStack {
PrefsAccountView(account: account)
Spacer()
TriStateToggle("\(account.instanceURL.host!) notifications enabled", labelHidden: true, mode: $mode, onChange: updateNotificationsEnabled(enabled:))
AsyncToggle("\(account.instanceURL.host!) notifications enabled", labelHidden: true, mode: $mode, onChange: updateNotificationsEnabled(enabled:))
.labelsHidden()
}
PushSubscriptionView(account: account, subscription: subscription, updateSubscription: updateSubscription)
@ -42,15 +44,24 @@ struct PushInstanceSettingsView: View {
} message: { data in
Text(data.localizedDescription)
}
.alert("Re-Login Required", isPresented: $showReLoginRequiredAlert) {
Button("Cancel", role: .cancel) {}
Button("Login") {
NotificationCenter.default.post(name: .reLogInRequired, object: account)
}
} message: {
Text("You must grant permission on \(account.instanceURL.host!) to turn on push notifications.")
}
}
private func updateNotificationsEnabled(enabled: Bool) async {
private func updateNotificationsEnabled(enabled: Bool) async -> Bool {
if enabled {
do {
try await enableNotifications()
return try await enableNotifications()
} catch {
PushManager.logger.error("Error creating instance subscription: \(String(describing: error))")
self.error = .enabling(error)
return false
}
} else {
do {
@ -58,24 +69,25 @@ struct PushInstanceSettingsView: View {
} catch {
PushManager.logger.error("Error removing instance subscription: \(String(describing: error))")
self.error = .disabling(error)
return false
}
}
return true
}
private func enableNotifications() async throws {
private func enableNotifications() async throws -> Bool {
guard account.scopes?.contains(Scope.push.rawValue) == true else {
showReLoginRequiredAlert = true
return false
}
let subscription = try await PushManager.shared.createSubscription(account: account)
let req = Pachyderm.PushSubscription.create(
endpoint: pushProxyRegistration.endpoint,
publicKey: subscription.secretKey.publicKey.rawRepresentation,
authSecret: subscription.authSecret,
alerts: .init(subscription.alerts),
policy: .init(subscription.policy)
)
let mastodonController = await MastodonController.getForAccount(account)
do {
let (result, _) = try await mastodonController.run(req)
let result = try await mastodonController.createPushSubscription(subscription: subscription)
PushManager.logger.debug("Push subscription \(result.id) created on \(account.instanceURL)")
self.subscription = subscription
return true
} catch {
// if creation failed, remove the subscription locally as well
await PushManager.shared.removeSubscription(account: account)
@ -84,26 +96,25 @@ struct PushInstanceSettingsView: View {
}
private func disableNotifications() async throws {
let req = Pachyderm.PushSubscription.delete()
let mastodonController = await MastodonController.getForAccount(account)
_ = try await mastodonController.run(req)
try await mastodonController.deletePushSubscription()
await PushManager.shared.removeSubscription(account: account)
subscription = nil
PushManager.logger.debug("Push subscription removed on \(account.instanceURL)")
}
private func updateSubscription(alerts: PushNotifications.PushSubscription.Alerts, policy: PushNotifications.PushSubscription.Policy) async {
try! await Task.sleep(nanoseconds: NSEC_PER_SEC)
let req = Pachyderm.PushSubscription.update(alerts: .init(alerts), policy: .init(policy))
private func updateSubscription(alerts: PushNotifications.PushSubscription.Alerts, policy: PushNotifications.PushSubscription.Policy) async -> Bool {
let mastodonController = await MastodonController.getForAccount(account)
do {
let (result, _) = try await mastodonController.run(req)
let result = try await mastodonController.updatePushSubscription(alerts: alerts, policy: policy)
PushManager.logger.debug("Push subscription \(result.id) updated on \(account.instanceURL)")
subscription?.alerts = alerts
subscription?.policy = policy
return true
} catch {
PushManager.logger.error("Error updating subscription: \(String(describing: error))")
self.error = .updating(error)
return false
}
}
}
@ -125,34 +136,6 @@ private enum Error: LocalizedError {
}
}
private extension Pachyderm.PushSubscription.Alerts {
init(_ alerts: PushNotifications.PushSubscription.Alerts) {
self.init(
mention: alerts.contains(.mention),
status: alerts.contains(.status),
reblog: alerts.contains(.reblog),
follow: alerts.contains(.follow),
followRequest: alerts.contains(.followRequest),
favourite: alerts.contains(.favorite),
poll: alerts.contains(.poll),
update: alerts.contains(.update)
)
}
}
private extension Pachyderm.PushSubscription.Policy {
init(_ policy: PushNotifications.PushSubscription.Policy) {
switch policy {
case .all:
self = .all
case .followers:
self = .followers
case .followed:
self = .followed
}
}
}
//#Preview {
// PushInstanceSettingsView()
//}

View File

@ -9,11 +9,12 @@
import SwiftUI
import UserAccounts
import PushNotifications
import TuskerComponents
struct PushSubscriptionView: View {
let account: UserAccountInfo
let subscription: PushSubscription?
let updateSubscription: (PushSubscription.Alerts, PushSubscription.Policy) async -> Void
let updateSubscription: (PushSubscription.Alerts, PushSubscription.Policy) async -> Bool
var body: some View {
if let subscription {
@ -28,10 +29,10 @@ struct PushSubscriptionView: View {
private struct PushSubscriptionSettingsView: View {
let account: UserAccountInfo
let subscription: PushSubscription
let updateSubscription: (PushSubscription.Alerts, PushSubscription.Policy) async -> Void
let updateSubscription: (PushSubscription.Alerts, PushSubscription.Policy) async -> Bool
@State private var isLoading: [PushSubscription.Alerts: Bool] = [:]
init(account: UserAccountInfo, subscription: PushSubscription, updateSubscription: @escaping (PushSubscription.Alerts, PushSubscription.Policy) async -> Void) {
init(account: UserAccountInfo, subscription: PushSubscription, updateSubscription: @escaping (PushSubscription.Alerts, PushSubscription.Policy) async -> Bool) {
self.account = account
self.subscription = subscription
self.updateSubscription = updateSubscription
@ -39,9 +40,14 @@ private struct PushSubscriptionSettingsView: View {
var body: some View {
VStack(alignment: .prefsAvatar) {
TriStateToggle("Mentions", mode: alertsBinding(for: .mention)) {
await onChange(alert: .mention, value: $0)
}
toggle("All", alert: [.mention, .favorite, .reblog, .follow, .followRequest, .poll, .update])
toggle("Mentions", alert: .mention)
toggle("Favorites", alert: .favorite)
toggle("Reblogs", alert: .reblog)
toggle("Follows", alert: [.follow, .followRequest])
toggle("Polls", alert: .poll)
toggle("Edits", alert: .update)
// status notifications not supported until we can enable/disable them in the app
}
// this is the default value of the alignment guide, but this modifier is loading bearing
.alignmentGuide(.prefsAvatar, computeValue: { dimension in
@ -51,22 +57,25 @@ private struct PushSubscriptionSettingsView: View {
.padding(.leading, 38)
}
private func alertsBinding(for alert: PushSubscription.Alerts) -> Binding<TriStateToggle.Mode> {
return Binding {
private func toggle(_ titleKey: LocalizedStringKey, alert: PushSubscription.Alerts) -> some View {
let binding: Binding<AsyncToggle.Mode> = Binding {
isLoading[alert] == true ? .loading : subscription.alerts.contains(alert) ? .on : .off
} set: { newValue in
isLoading[alert] = newValue == .loading
}
return AsyncToggle(titleKey, mode: binding) {
return await updateSubscription(alert: alert, isOn: $0)
}
}
private func onChange(alert: PushSubscription.Alerts, value: Bool) async {
private func updateSubscription(alert: PushSubscription.Alerts, isOn: Bool) async -> Bool {
var newAlerts = subscription.alerts
if value {
if isOn {
newAlerts.insert(alert)
} else {
newAlerts.remove(alert)
}
await updateSubscription(newAlerts, subscription.policy)
return await updateSubscription(newAlerts, subscription.policy)
}
}

View File

@ -9,12 +9,19 @@
import UIKit
import SwiftUI
import UserAccounts
import SafariServices
import AuthenticationServices
import Pachyderm
class PreferencesNavigationController: UINavigationController {
private let mastodonController: MastodonController
private var isSwitchingAccounts = false
init(mastodonController: MastodonController) {
self.mastodonController = mastodonController
let view = PreferencesView(mastodonController: mastodonController)
let hostingController = UIHostingController(rootView: view)
super.init(rootViewController: hostingController)
@ -31,6 +38,8 @@ class PreferencesNavigationController: UINavigationController {
NotificationCenter.default.addObserver(self, selector: #selector(showAddAccount), name: .addAccount, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(activateAccount(_:)), name: .activateAccount, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(userLoggedOut), name: .userLoggedOut, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(showMastodonSettings), name: .showMastodonSettings, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(reLogInToAccount), name: .reLogInRequired, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
@ -57,7 +66,7 @@ class PreferencesNavigationController: UINavigationController {
dismiss(animated: true) // dismisses instance selector
}
@objc func activateAccount(_ notification: Notification) {
@objc func activateAccount(_ notification: Foundation.Notification) {
// TODO: this is a temporary measure
// when switching accounts shortly after adding a new one, there is an old instance of PreferncesNavigationController still around
// which tries to handle the notification but is unable to because it no longer is in a window (and therefore doesn't have a scene delegate)
@ -92,6 +101,34 @@ class PreferencesNavigationController: UINavigationController {
UIApplication.shared.requestSceneSessionDestruction(windowScene.session, options: nil)
}
}
@objc private func showMastodonSettings() {
var components = URLComponents(url: mastodonController.accountInfo!.instanceURL, resolvingAgainstBaseURL: false)!
components.path = "/auth/edit"
let vc = SFSafariViewController(url: components.url!)
present(vc, animated: true)
}
@objc private func reLogInToAccount(_ notification: Foundation.Notification) {
guard let account = notification.object as? UserAccountInfo else {
return
}
Task {
do {
let service = GetAuthorizationTokenService(instanceURL: account.instanceURL, clientID: account.clientID, presentationContextProvider: self)
let code = try await service.run()
let mastodonController = MastodonController.getForAccount(account)
let token = try await mastodonController.authorize(authorizationCode: code)
UserAccountsManager.shared.updateAccessToken(account, token: token, scopes: MastodonController.oauthScopes.map(\.rawValue))
// try to revoke the old token
try? await Client(baseURL: account.instanceURL, accessToken: account.accessToken, clientID: account.clientID, clientSecret: account.clientSecret).revokeAccessToken()
} catch {
let alert = UIAlertController(title: "Error Updating Permissions", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alert, animated: true)
}
}
}
}
@ -111,3 +148,14 @@ extension PreferencesNavigationController: OnboardingViewControllerDelegate {
}
}
}
extension PreferencesNavigationController: ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
view.window!
}
}
extension Foundation.Notification.Name {
static let showMastodonSettings = Notification.Name("Tusker.showMastodonSettings")
static let reLogInRequired = Notification.Name("Tusker.reLogInRequired")
}

View File

@ -68,15 +68,22 @@ struct PreferencesView: View {
Button(action: {
NotificationCenter.default.post(name: .addAccount, object: nil)
}) {
Text("Add Account...")
Text("Add Account")
}
Button(action: {
self.showingLogoutConfirmation = true
}) {
Text("Logout from current")
Text("Logout from Current…")
}.alert(isPresented: $showingLogoutConfirmation) {
Alert(title: Text("Are you sure you want to logout?"), message: nil, primaryButton: .destructive(Text("Logout"), action: self.logoutPressed), secondaryButton: .cancel())
}
Button {
NotificationCenter.default.post(name: .showMastodonSettings, object: nil)
} label: {
Text("Account Settings")
}
} header: {
Text("Accounts")
}

View File

@ -10,7 +10,7 @@
// https://help.apple.com/xcode/#/dev745c5c974
MARKETING_VERSION = 2024.1
CURRENT_PROJECT_VERSION = 118
CURRENT_PROJECT_VERSION = 119
CURRENT_PROJECT_VERSION = $(inherited)$(CURRENT_PROJECT_VERSION_BUILD_SUFFIX_$(CONFIGURATION))
CURRENT_PROJECT_VERSION_BUILD_SUFFIX_Debug=-dev