2018-08-19 20:14:04 +00:00
|
|
|
//
|
|
|
|
// LocalData.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 8/18/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2020-01-19 03:43:10 +00:00
|
|
|
import Combine
|
2022-09-13 03:05:35 +00:00
|
|
|
import CryptoKit
|
2018-08-19 20:14:04 +00:00
|
|
|
|
2020-01-19 03:43:10 +00:00
|
|
|
class LocalData: ObservableObject {
|
2018-08-19 20:14:04 +00:00
|
|
|
|
|
|
|
static let shared = LocalData()
|
|
|
|
|
2019-12-30 20:59:49 +00:00
|
|
|
let defaults: UserDefaults
|
|
|
|
|
|
|
|
private init() {
|
|
|
|
if ProcessInfo.processInfo.environment.keys.contains("UI_TESTING") {
|
|
|
|
defaults = UserDefaults(suiteName: "\(Bundle.main.bundleIdentifier!).uitesting")!
|
2020-09-13 02:15:45 +00:00
|
|
|
defaults.removePersistentDomain(forName: "\(Bundle.main.bundleIdentifier!).uitesting")
|
2019-12-31 16:40:56 +00:00
|
|
|
if ProcessInfo.processInfo.environment.keys.contains("UI_TESTING_LOGIN") {
|
2020-01-08 02:29:15 +00:00
|
|
|
accounts = [
|
|
|
|
UserAccountInfo(
|
|
|
|
instanceURL: URL(string: "http://localhost:8080")!,
|
|
|
|
clientID: "client_id",
|
|
|
|
clientSecret: "client_secret",
|
|
|
|
username: "admin",
|
|
|
|
accessToken: "access_token")
|
|
|
|
]
|
2019-12-31 16:40:56 +00:00
|
|
|
}
|
2019-12-30 20:59:49 +00:00
|
|
|
} else {
|
2020-07-03 22:45:37 +00:00
|
|
|
defaults = UserDefaults(suiteName: "group.space.vaccor.Tusker")!
|
2019-12-30 20:59:49 +00:00
|
|
|
}
|
2022-09-13 03:05:35 +00:00
|
|
|
|
|
|
|
migrateAccountIDsIfNecessary()
|
2019-12-30 20:59:49 +00:00
|
|
|
}
|
2018-08-19 20:14:04 +00:00
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
private let accountsKey = "accounts"
|
2022-09-13 03:05:35 +00:00
|
|
|
private(set) var accounts: [UserAccountInfo] {
|
2018-08-19 20:14:04 +00:00
|
|
|
get {
|
2020-01-08 02:29:15 +00:00
|
|
|
if let array = defaults.array(forKey: accountsKey) as? [[String: String]] {
|
2022-09-13 03:05:35 +00:00
|
|
|
return array.compactMap(UserAccountInfo.init(userDefaultsDict:))
|
2020-01-08 02:29:15 +00:00
|
|
|
} else {
|
|
|
|
return []
|
|
|
|
}
|
2018-08-19 20:14:04 +00:00
|
|
|
}
|
|
|
|
set {
|
2020-01-19 03:43:10 +00:00
|
|
|
objectWillChange.send()
|
2020-04-12 15:14:10 +00:00
|
|
|
let array = newValue.map { (info) -> [String: String] in
|
|
|
|
var res = [
|
2020-01-19 16:52:06 +00:00
|
|
|
"id": info.id,
|
2020-01-08 02:29:15 +00:00
|
|
|
"instanceURL": info.instanceURL.absoluteString,
|
|
|
|
"clientID": info.clientID,
|
|
|
|
"clientSecret": info.clientSecret,
|
|
|
|
"accessToken": info.accessToken
|
|
|
|
]
|
2020-04-12 15:14:10 +00:00
|
|
|
if let username = info.username {
|
|
|
|
res["username"] = username
|
|
|
|
}
|
|
|
|
return res
|
2020-01-08 02:29:15 +00:00
|
|
|
}
|
|
|
|
defaults.set(array, forKey: accountsKey)
|
2018-08-19 20:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
private let mostRecentAccountKey = "mostRecentAccount"
|
2020-11-10 00:39:42 +00:00
|
|
|
private(set) var mostRecentAccountID: String? {
|
2018-08-19 20:14:04 +00:00
|
|
|
get {
|
2020-01-08 02:29:15 +00:00
|
|
|
return defaults.string(forKey: mostRecentAccountKey)
|
2018-08-19 20:14:04 +00:00
|
|
|
}
|
|
|
|
set {
|
2020-01-19 03:43:10 +00:00
|
|
|
objectWillChange.send()
|
2020-01-08 02:29:15 +00:00
|
|
|
defaults.set(newValue, forKey: mostRecentAccountKey)
|
2018-08-19 20:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 03:05:35 +00:00
|
|
|
private let usesAccountIDHashesKey = "usesAccountIDHashes"
|
|
|
|
private var usesAccountIDHashes: Bool {
|
|
|
|
get {
|
|
|
|
return defaults.bool(forKey: usesAccountIDHashesKey)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
return defaults.set(newValue, forKey: usesAccountIDHashesKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func migrateAccountIDsIfNecessary() {
|
|
|
|
if usesAccountIDHashes {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if let mostRecentAccount = getMostRecentAccount() {
|
|
|
|
let hashedMostRecentID = UserAccountInfo.id(instanceURL: mostRecentAccount.instanceURL, username: mostRecentAccount.username)
|
|
|
|
mostRecentAccountID = hashedMostRecentID
|
|
|
|
}
|
|
|
|
if let array = defaults.array(forKey: accountsKey) as? [[String: String]] {
|
|
|
|
accounts = array.compactMap {
|
|
|
|
guard let urlString = $0["instanceURL"],
|
|
|
|
let url = URL(string: urlString),
|
|
|
|
let username = $0["username"] else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var dict = $0
|
|
|
|
dict["id"] = UserAccountInfo.id(instanceURL: url, username: username)
|
|
|
|
return UserAccountInfo(userDefaultsDict: dict)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usesAccountIDHashes = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Account Management
|
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
var onboardingComplete: Bool {
|
|
|
|
return !accounts.isEmpty
|
2018-08-19 20:14:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 15:14:10 +00:00
|
|
|
func addAccount(instanceURL url: URL, clientID: String, clientSecret secret: String, username: String?, accessToken: String) -> UserAccountInfo {
|
2020-01-08 02:29:15 +00:00
|
|
|
var accounts = self.accounts
|
|
|
|
if let index = accounts.firstIndex(where: { $0.instanceURL == url && $0.username == username }) {
|
|
|
|
accounts.remove(at: index)
|
2018-08-19 20:14:04 +00:00
|
|
|
}
|
2022-09-13 03:05:35 +00:00
|
|
|
let info = UserAccountInfo(instanceURL: url, clientID: clientID, clientSecret: secret, username: username, accessToken: accessToken)
|
2020-01-08 02:29:15 +00:00
|
|
|
accounts.append(info)
|
|
|
|
self.accounts = accounts
|
|
|
|
return info
|
2018-08-19 20:14:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
func removeAccount(_ info: UserAccountInfo) {
|
2020-01-19 16:52:06 +00:00
|
|
|
accounts.removeAll(where: { $0.id == info.id })
|
2020-01-08 02:29:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 20:26:25 +00:00
|
|
|
func getAccount(id: String) -> UserAccountInfo? {
|
|
|
|
return accounts.first(where: { $0.id == id })
|
|
|
|
}
|
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
func getMostRecentAccount() -> UserAccountInfo? {
|
2020-01-19 16:52:06 +00:00
|
|
|
guard onboardingComplete else { return nil }
|
|
|
|
let mostRecent: UserAccountInfo?
|
2020-11-10 00:39:42 +00:00
|
|
|
if let id = mostRecentAccountID {
|
2020-01-19 16:52:06 +00:00
|
|
|
mostRecent = accounts.first { $0.id == id }
|
2020-01-08 02:29:15 +00:00
|
|
|
} else {
|
2020-01-19 16:52:06 +00:00
|
|
|
mostRecent = nil
|
2018-08-19 20:14:04 +00:00
|
|
|
}
|
2020-01-19 16:52:06 +00:00
|
|
|
return mostRecent ?? accounts.first!
|
|
|
|
}
|
|
|
|
|
|
|
|
func setMostRecentAccount(_ account: UserAccountInfo?) {
|
2020-11-10 00:39:42 +00:00
|
|
|
mostRecentAccountID = account?.id
|
2018-08-19 20:14:04 +00:00
|
|
|
}
|
2020-01-08 02:29:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension LocalData {
|
|
|
|
struct UserAccountInfo: Equatable, Hashable {
|
2020-01-19 16:52:06 +00:00
|
|
|
let id: String
|
2020-01-08 02:29:15 +00:00
|
|
|
let instanceURL: URL
|
|
|
|
let clientID: String
|
|
|
|
let clientSecret: String
|
2022-09-13 03:05:35 +00:00
|
|
|
private(set) var username: String!
|
2020-01-08 02:29:15 +00:00
|
|
|
let accessToken: String
|
2020-01-19 16:52:06 +00:00
|
|
|
|
2022-09-13 03:05:35 +00:00
|
|
|
fileprivate static let tempAccountID = "temp"
|
|
|
|
|
|
|
|
fileprivate static func id(instanceURL: URL, username: String?) -> String {
|
|
|
|
// We hash the instance host and username to form the account ID
|
|
|
|
// so that account IDs will match across devices, allowing for data syncing and handoff.
|
|
|
|
var hasher = SHA256()
|
|
|
|
hasher.update(data: instanceURL.host!.data(using: .utf8)!)
|
|
|
|
if let username {
|
|
|
|
hasher.update(data: username.data(using: .utf8)!)
|
|
|
|
}
|
|
|
|
return Data(hasher.finalize()).base64EncodedString()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Only to be used for temporary MastodonController needed to fetch own account info and create final UserAccountInfo with real username
|
|
|
|
init(tempInstanceURL instanceURL: URL, clientID: String, clientSecret: String, accessToken: String) {
|
|
|
|
self.id = UserAccountInfo.tempAccountID
|
|
|
|
self.instanceURL = instanceURL
|
|
|
|
self.clientID = clientID
|
|
|
|
self.clientSecret = clientSecret
|
|
|
|
self.accessToken = accessToken
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate init(instanceURL: URL, clientID: String, clientSecret: String, username: String? = nil, accessToken: String) {
|
|
|
|
self.id = UserAccountInfo.id(instanceURL: instanceURL, username: username)
|
|
|
|
self.instanceURL = instanceURL
|
|
|
|
self.clientID = clientID
|
|
|
|
self.clientSecret = clientSecret
|
|
|
|
self.username = username
|
|
|
|
self.accessToken = accessToken
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate init?(userDefaultsDict dict: [String: String]) {
|
|
|
|
guard let id = dict["id"],
|
|
|
|
let instanceURL = dict["instanceURL"],
|
|
|
|
let url = URL(string: instanceURL),
|
|
|
|
let clientID = dict["clientID"],
|
|
|
|
let secret = dict["clientSecret"],
|
|
|
|
let accessToken = dict["accessToken"] else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.id = id
|
|
|
|
self.instanceURL = url
|
|
|
|
self.clientID = clientID
|
|
|
|
self.clientSecret = secret
|
|
|
|
self.username = dict["username"]
|
|
|
|
self.accessToken = accessToken
|
|
|
|
}
|
|
|
|
|
2022-12-01 02:35:52 +00:00
|
|
|
/// A filename-safe string for this account
|
|
|
|
var persistenceKey: String {
|
|
|
|
// slashes are not allowed in the persistent store coordinator name
|
|
|
|
id.replacingOccurrences(of: "/", with: "_")
|
|
|
|
}
|
|
|
|
|
2020-01-19 16:52:06 +00:00
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func ==(lhs: UserAccountInfo, rhs: UserAccountInfo) -> Bool {
|
|
|
|
return lhs.id == rhs.id
|
|
|
|
}
|
2020-01-08 02:29:15 +00:00
|
|
|
}
|
2018-08-19 20:14:04 +00:00
|
|
|
}
|
2019-09-16 17:12:23 +00:00
|
|
|
|
|
|
|
extension Notification.Name {
|
2020-01-19 16:52:06 +00:00
|
|
|
static let userLoggedOut = Notification.Name("Tusker.userLoggedOut")
|
|
|
|
static let addAccount = Notification.Name("Tusker.addAccount")
|
|
|
|
static let activateAccount = Notification.Name("Tusker.activateAccount")
|
2019-09-16 17:12:23 +00:00
|
|
|
}
|