Push notifications: display account name if multi account fix #344

This commit is contained in:
Thomas Ricouard 2023-01-24 13:38:26 +01:00
parent 4927b12e90
commit d05afd93c4
8 changed files with 39 additions and 11 deletions

View file

@ -46,6 +46,7 @@
9F7335EF29674F7100AFF0BA /* QuickLook.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9F7335EE29674F7100AFF0BA /* QuickLook.framework */; }; 9F7335EF29674F7100AFF0BA /* QuickLook.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9F7335EE29674F7100AFF0BA /* QuickLook.framework */; };
9F7335F22967608F00AFF0BA /* AddRemoteTimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F7335F12967608F00AFF0BA /* AddRemoteTimelineView.swift */; }; 9F7335F22967608F00AFF0BA /* AddRemoteTimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F7335F12967608F00AFF0BA /* AddRemoteTimelineView.swift */; };
9F7335F92968576500AFF0BA /* DisplaySettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F7335F82968576500AFF0BA /* DisplaySettingsView.swift */; }; 9F7335F92968576500AFF0BA /* DisplaySettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F7335F82968576500AFF0BA /* DisplaySettingsView.swift */; };
9F7D93942980063100EE6B7A /* AppAccount in Frameworks */ = {isa = PBXBuildFile; productRef = 9F7D93932980063100EE6B7A /* AppAccount */; };
9F8CA5972979B61100481E8E /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = E9B576C529743F4C00BCE646 /* Localizable.strings */; }; 9F8CA5972979B61100481E8E /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = E9B576C529743F4C00BCE646 /* Localizable.strings */; };
9F8CA5982979B63D00481E8E /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = E9B576C529743F4C00BCE646 /* Localizable.strings */; }; 9F8CA5982979B63D00481E8E /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = E9B576C529743F4C00BCE646 /* Localizable.strings */; };
9FAD85832971BF7200496AB1 /* Secret.plist in Resources */ = {isa = PBXBuildFile; fileRef = 9FAD85822971BF7200496AB1 /* Secret.plist */; }; 9FAD85832971BF7200496AB1 /* Secret.plist in Resources */ = {isa = PBXBuildFile; fileRef = 9FAD85822971BF7200496AB1 /* Secret.plist */; };
@ -178,6 +179,7 @@
isa = PBXFrameworksBuildPhase; isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
9F7D93942980063100EE6B7A /* AppAccount in Frameworks */,
9F2A5428296AB683009B2D7C /* Models in Frameworks */, 9F2A5428296AB683009B2D7C /* Models in Frameworks */,
9F2A5426296AB67E009B2D7C /* KeychainSwift in Frameworks */, 9F2A5426296AB67E009B2D7C /* KeychainSwift in Frameworks */,
9F2A5424296AB67A009B2D7C /* Env in Frameworks */, 9F2A5424296AB67A009B2D7C /* Env in Frameworks */,
@ -401,6 +403,7 @@
9F2A5423296AB67A009B2D7C /* Env */, 9F2A5423296AB67A009B2D7C /* Env */,
9F2A5425296AB67E009B2D7C /* KeychainSwift */, 9F2A5425296AB67E009B2D7C /* KeychainSwift */,
9F2A5427296AB683009B2D7C /* Models */, 9F2A5427296AB683009B2D7C /* Models */,
9F7D93932980063100EE6B7A /* AppAccount */,
); );
productName = IceCubesNotifications; productName = IceCubesNotifications;
productReference = 9F2A5416296AB631009B2D7C /* IceCubesNotifications.appex */; productReference = 9F2A5416296AB631009B2D7C /* IceCubesNotifications.appex */;
@ -1103,6 +1106,10 @@
isa = XCSwiftPackageProductDependency; isa = XCSwiftPackageProductDependency;
productName = Conversations; productName = Conversations;
}; };
9F7D93932980063100EE6B7A /* AppAccount */ = {
isa = XCSwiftPackageProductDependency;
productName = AppAccount;
};
9FAD85972974405D00496AB1 /* Status */ = { 9FAD85972974405D00496AB1 /* Status */ = {
isa = XCSwiftPackageProductDependency; isa = XCSwiftPackageProductDependency;
productName = Status; productName = Status;

View file

@ -225,7 +225,11 @@ struct AddAccountView: View {
do { do {
oauthURL = nil oauthURL = nil
let oauthToken = try await client.continueOauthFlow(url: url) let oauthToken = try await client.continueOauthFlow(url: url)
appAccountsManager.add(account: AppAccount(server: client.server, oauthToken: oauthToken)) let client = Client(server: client.server, oauthToken: oauthToken)
let account: Account = try await client.get(endpoint: Accounts.verifyCredentials)
appAccountsManager.add(account: AppAccount(server: client.server,
accountName: "\(account.acct)@\(client.server)",
oauthToken: oauthToken))
Task { Task {
await pushNotifications.updateSubscriptions(accounts: appAccountsManager.pushAccounts) await pushNotifications.updateSubscriptions(accounts: appAccountsManager.pushAccounts)
} }

View file

@ -64,7 +64,9 @@ struct SettingsTabs: View {
let account = appAccountsManager.availableAccounts[index] let account = appAccountsManager.availableAccounts[index]
if let token = account.oauthToken { if let token = account.oauthToken {
Task { Task {
await pushNotifications.deleteSubscriptions(accounts: [.init(server: account.server, token: token)]) await pushNotifications.deleteSubscriptions(accounts: [.init(server: account.server,
token: token,
accountName: account.accountName)])
} }
} }
appAccountsManager.delete(account: account) appAccountsManager.delete(account: account)

View file

@ -4,6 +4,7 @@ import KeychainSwift
import Models import Models
import UIKit import UIKit
import UserNotifications import UserNotifications
import AppAccount
@MainActor @MainActor
class NotificationService: UNNotificationServiceExtension { class NotificationService: UNNotificationServiceExtension {
@ -52,7 +53,9 @@ class NotificationService: UNNotificationServiceExtension {
} }
bestAttemptContent.title = notification.title bestAttemptContent.title = notification.title
bestAttemptContent.subtitle = "" if AppAccountsManager.shared.availableAccounts.count > 1 {
bestAttemptContent.subtitle = bestAttemptContent.userInfo["i"] as? String ?? ""
}
bestAttemptContent.body = notification.body.escape() bestAttemptContent.body = notification.body.escape()
bestAttemptContent.userInfo["plaintext"] = plaintextData bestAttemptContent.userInfo["plaintext"] = plaintextData
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "glass.wav")) bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "glass.wav"))

View file

@ -6,6 +6,7 @@ import SwiftUI
public struct AppAccount: Codable, Identifiable { public struct AppAccount: Codable, Identifiable {
public let server: String public let server: String
public var accountName: String?
public let oauthToken: OauthToken? public let oauthToken: OauthToken?
public var id: String { public var id: String {
@ -28,8 +29,11 @@ public struct AppAccount: Codable, Identifiable {
} }
} }
public init(server: String, oauthToken: OauthToken? = nil) { public init(server: String,
accountName: String?,
oauthToken: OauthToken? = nil) {
self.server = server self.server = server
self.accountName = accountName
self.oauthToken = oauthToken self.oauthToken = oauthToken
} }

View file

@ -4,7 +4,7 @@ import SwiftUI
@MainActor @MainActor
public class AppAccountViewModel: ObservableObject { public class AppAccountViewModel: ObservableObject {
let appAccount: AppAccount var appAccount: AppAccount
let client: Client let client: Client
let isCompact: Bool let isCompact: Bool
@ -23,6 +23,10 @@ public class AppAccountViewModel: ObservableObject {
func fetchAccount() async { func fetchAccount() async {
do { do {
account = try await client.get(endpoint: Accounts.verifyCredentials) account = try await client.get(endpoint: Accounts.verifyCredentials)
if appAccount.accountName == nil, let account {
appAccount.accountName = "\(account.acct)@\(appAccount.server)"
try appAccount.save()
}
} catch { } catch {
print(error) print(error)
} }

View file

@ -21,13 +21,13 @@ public class AppAccountsManager: ObservableObject {
public var pushAccounts: [PushNotificationsService.PushAccounts] { public var pushAccounts: [PushNotificationsService.PushAccounts] {
availableAccounts.filter { $0.oauthToken != nil } availableAccounts.filter { $0.oauthToken != nil }
.map { .init(server: $0.server, token: $0.oauthToken!) } .map { .init(server: $0.server, token: $0.oauthToken!, accountName: $0.accountName) }
} }
public static var shared = AppAccountsManager() public static var shared = AppAccountsManager()
internal init() { internal init() {
var defaultAccount = AppAccount(server: AppInfo.defaultServer, oauthToken: nil) var defaultAccount = AppAccount(server: AppInfo.defaultServer, accountName: nil, oauthToken: nil)
let keychainAccounts = AppAccount.retrieveAll() let keychainAccounts = AppAccount.retrieveAll()
availableAccounts = keychainAccounts availableAccounts = keychainAccounts
if let currentAccount = keychainAccounts.first(where: { $0.id == Self.latestCurrentAccountKey }) { if let currentAccount = keychainAccounts.first(where: { $0.id == Self.latestCurrentAccountKey }) {
@ -51,7 +51,9 @@ public class AppAccountsManager: ObservableObject {
availableAccounts.removeAll(where: { $0.id == account.id }) availableAccounts.removeAll(where: { $0.id == account.id })
account.delete() account.delete()
if currentAccount.id == account.id { if currentAccount.id == account.id {
currentAccount = availableAccounts.first ?? AppAccount(server: AppInfo.defaultServer, oauthToken: nil) currentAccount = availableAccounts.first ?? AppAccount(server: AppInfo.defaultServer,
accountName: nil,
oauthToken: nil)
} }
} }
} }

View file

@ -17,10 +17,12 @@ public class PushNotificationsService: ObservableObject {
public struct PushAccounts { public struct PushAccounts {
public let server: String public let server: String
public let token: OauthToken public let token: OauthToken
public let accountName: String?
public init(server: String, token: OauthToken) { public init(server: String, token: OauthToken, accountName: String?) {
self.server = server self.server = server
self.token = token self.token = token
self.accountName = accountName
} }
} }
@ -85,7 +87,7 @@ public class PushNotificationsService: ObservableObject {
var listenerURL = Constants.endpoint var listenerURL = Constants.endpoint
listenerURL += "/push/" listenerURL += "/push/"
listenerURL += pushToken.hexString listenerURL += pushToken.hexString
listenerURL += "/\(account.server)" listenerURL += "/\(account.accountName ?? account.server)"
#if DEBUG #if DEBUG
listenerURL += "?sandbox=true" listenerURL += "?sandbox=true"
#endif #endif