Refactoring

This commit is contained in:
Justin Mazzocchi 2020-09-29 17:42:12 -07:00
parent 992cbd475f
commit e0d63700bc
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
7 changed files with 39 additions and 42 deletions

View file

@ -4,12 +4,16 @@ import Foundation
import GRDB
struct AccountInfo: Codable, Hashable, FetchableRecord {
let account: AccountRecord
let moved: AccountRecord?
let record: AccountRecord
let movedRecord: AccountRecord?
}
extension AccountInfo {
static func addingIncludes<T: DerivableRequest>(_ request: T) -> T where T.RowDecoder == AccountRecord {
request.including(optional: AccountRecord.moved.forKey(CodingKeys.movedRecord))
}
static func request(_ request: QueryInterfaceRequest<AccountRecord>) -> QueryInterfaceRequest<Self> {
request.including(optional: AccountRecord.moved.forKey(CodingKeys.moved)).asRequest(of: self)
addingIncludes(request).asRequest(of: self)
}
}

View file

@ -195,8 +195,8 @@ public extension ContentDatabase {
return [[]]
}
let ancestors = try parent.status.ancestors.fetchAll(db)
let descendants = try parent.status.descendants.fetchAll(db)
let ancestors = try parent.record.ancestors.fetchAll(db)
let descendants = try parent.record.descendants.fetchAll(db)
return [ancestors, [parent], descendants]
}

View file

@ -4,31 +4,21 @@ import Foundation
import GRDB
struct StatusInfo: Codable, Hashable, FetchableRecord {
let account: AccountRecord
let accountMoved: AccountRecord?
let status: StatusRecord
let reblogAccount: AccountRecord?
let reblogAccountMoved: AccountRecord?
let reblog: StatusRecord?
let record: StatusRecord
let accountInfo: AccountInfo
let reblogAccountInfo: AccountInfo?
let reblogRecord: StatusRecord?
}
extension StatusInfo {
static func addingIncludes<T: DerivableRequest>(_ request: T) -> T where T.RowDecoder == StatusRecord {
request.including(required: AccountInfo.addingIncludes(StatusRecord.account).forKey(CodingKeys.accountInfo))
.including(optional: AccountInfo.addingIncludes(StatusRecord.reblogAccount)
.forKey(CodingKeys.reblogAccountInfo))
.including(optional: StatusRecord.reblog.forKey(CodingKeys.reblogRecord))
}
static func request(_ request: QueryInterfaceRequest<StatusRecord>) -> QueryInterfaceRequest<Self> {
request.including(required: StatusRecord.account.forKey(CodingKeys.account))
.including(optional: StatusRecord.accountMoved.forKey(CodingKeys.accountMoved))
.including(optional: StatusRecord.reblogAccount.forKey(CodingKeys.reblogAccount))
.including(optional: StatusRecord.reblogAccountMoved.forKey(CodingKeys.reblogAccountMoved))
.including(optional: StatusRecord.reblog.forKey(CodingKeys.reblog))
.asRequest(of: self)
}
var accountInfo: AccountInfo {
AccountInfo(account: account, moved: accountMoved)
}
var reblogAccountInfo: AccountInfo? {
guard let reblogAccount = reblogAccount else { return nil }
return AccountInfo(account: reblogAccount, moved: reblogAccountMoved)
addingIncludes(request).asRequest(of: self)
}
}

View file

@ -16,11 +16,11 @@ extension Account {
convenience init(info: AccountInfo) {
var moved: Account?
if let movedRecord = info.moved {
if let movedRecord = info.movedRecord {
moved = Self(record: movedRecord, moved: nil)
}
self.init(record: info.account, moved: moved)
self.init(record: info.record, moved: moved)
}
}

View file

@ -6,16 +6,16 @@ import GRDB
extension Identity {
init(info: IdentityInfo) {
self.init(
id: info.identity.id,
url: info.identity.url,
authenticated: info.identity.authenticated,
pending: info.identity.pending,
lastUsedAt: info.identity.lastUsedAt,
preferences: info.identity.preferences,
id: info.record.id,
url: info.record.url,
authenticated: info.record.authenticated,
pending: info.record.pending,
lastUsedAt: info.record.lastUsedAt,
preferences: info.record.preferences,
instance: info.instance,
account: info.account,
lastRegisteredDeviceToken: info.identity.lastRegisteredDeviceToken,
pushSubscriptionAlerts: info.identity.pushSubscriptionAlerts)
lastRegisteredDeviceToken: info.record.lastRegisteredDeviceToken,
pushSubscriptionAlerts: info.record.pushSubscriptionAlerts)
}
}

View file

@ -19,11 +19,11 @@ extension Status {
convenience init(info: StatusInfo) {
var reblog: Status?
if let reblogRecord = info.reblog, let reblogAccountInfo = info.reblogAccountInfo {
if let reblogRecord = info.reblogRecord, let reblogAccountInfo = info.reblogAccountInfo {
reblog = Status(record: reblogRecord, account: Account(info: reblogAccountInfo), reblog: nil)
}
self.init(record: info.status,
self.init(record: info.record,
account: Account(info: info.accountInfo),
reblog: reblog)
}

View file

@ -5,15 +5,18 @@ import GRDB
import Mastodon
struct IdentityInfo: Codable, Hashable, FetchableRecord {
let identity: IdentityRecord
let record: IdentityRecord
let instance: Identity.Instance?
let account: Identity.Account?
}
extension IdentityInfo {
static func request(_ request: QueryInterfaceRequest<IdentityRecord>) -> QueryInterfaceRequest<Self> {
static func addingIncludes<T: DerivableRequest>(_ request: T) -> T where T.RowDecoder == IdentityRecord {
request.including(optional: IdentityRecord.instance.forKey(CodingKeys.instance))
.including(optional: IdentityRecord.account.forKey(CodingKeys.account))
.asRequest(of: self)
}
static func request(_ request: QueryInterfaceRequest<IdentityRecord>) -> QueryInterfaceRequest<Self> {
addingIncludes(request).asRequest(of: self)
}
}