metatext-app-ios-iphone-ipad/DB/Sources/DB/Entities/Identity.swift

79 lines
2.7 KiB
Swift
Raw Normal View History

2020-09-03 05:28:34 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Mastodon
public struct Identity: Codable, Hashable, Identifiable {
2020-10-06 00:50:05 +02:00
public let id: Id
2020-09-03 05:28:34 +02:00
public let url: URL
2020-09-09 07:40:49 +02:00
public let authenticated: Bool
2020-09-13 10:03:08 +02:00
public let pending: Bool
2020-09-03 05:28:34 +02:00
public let lastUsedAt: Date
public let preferences: Identity.Preferences
public let instance: Identity.Instance?
public let account: Identity.Account?
2020-09-06 23:37:54 +02:00
public let lastRegisteredDeviceToken: Data?
2020-09-03 05:28:34 +02:00
public let pushSubscriptionAlerts: PushSubscription.Alerts
}
public extension Identity {
2020-10-06 00:50:05 +02:00
typealias Id = UUID
2020-09-03 05:28:34 +02:00
struct Instance: Codable, Hashable {
public let uri: String
2021-03-29 08:04:14 +02:00
public let streamingAPI: UnicodeURL
2020-09-03 05:28:34 +02:00
public let title: String
2021-03-29 08:04:14 +02:00
public let thumbnail: UnicodeURL?
public let version: String
public let maxTootChars: Int?
2020-09-03 05:28:34 +02:00
}
struct Account: Codable, Hashable {
2020-10-06 00:50:05 +02:00
public let id: Mastodon.Account.Id
public let identityId: Identity.Id
2020-09-03 05:28:34 +02:00
public let username: String
public let displayName: String
2021-01-31 22:59:26 +01:00
public let url: String
2021-03-29 08:04:14 +02:00
public let avatar: UnicodeURL
public let avatarStatic: UnicodeURL
public let header: UnicodeURL
public let headerStatic: UnicodeURL
2020-09-03 05:28:34 +02:00
public let emojis: [Emoji]
2021-01-26 07:57:44 +01:00
public let followRequestCount: Int
2020-09-03 05:28:34 +02:00
}
struct Preferences: Codable, Hashable {
@DecodableDefault.True public var useServerPostingReadingPreferences
@DecodableDefault.StatusVisibilityPublic public var postingDefaultVisibility: Status.Visibility
@DecodableDefault.False public var postingDefaultSensitive
public var postingDefaultLanguage: String?
@DecodableDefault.ExpandMediaDefault public var readingExpandMedia: Mastodon.Preferences.ExpandMedia
@DecodableDefault.False public var readingExpandSpoilers
}
var handle: String {
2021-01-31 22:59:26 +01:00
if let urlString = account?.url, let url = URL(string: urlString), let host = url.host {
return url.lastPathComponent.appending("@").appending(host)
2020-09-03 05:28:34 +02:00
}
return instance?.title ?? url.host ?? url.absoluteString
}
2021-03-29 08:04:14 +02:00
var image: URL? { (account?.avatar ?? instance?.thumbnail)?.url }
2020-09-03 05:28:34 +02:00
}
public extension Identity.Preferences {
func updated(from serverPreferences: Preferences) -> Self {
var mutable = self
if useServerPostingReadingPreferences {
mutable.postingDefaultVisibility = serverPreferences.postingDefaultVisibility
mutable.postingDefaultSensitive = serverPreferences.postingDefaultSensitive
mutable.readingExpandMedia = serverPreferences.readingExpandMedia
mutable.readingExpandSpoilers = serverPreferences.readingExpandSpoilers
}
return mutable
}
}