This commit is contained in:
Justin Mazzocchi 2020-09-29 16:56:09 -07:00
parent c616b13dcb
commit 992cbd475f
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
12 changed files with 59 additions and 59 deletions

View File

@ -3,12 +3,12 @@
import Foundation import Foundation
import GRDB import GRDB
struct AccountResult: Codable, Hashable, FetchableRecord { struct AccountInfo: Codable, Hashable, FetchableRecord {
let account: AccountRecord let account: AccountRecord
let moved: AccountRecord? let moved: AccountRecord?
} }
extension AccountResult { extension AccountInfo {
static func request(_ request: QueryInterfaceRequest<AccountRecord>) -> QueryInterfaceRequest<Self> { static func request(_ request: QueryInterfaceRequest<AccountRecord>) -> QueryInterfaceRequest<Self> {
request.including(optional: AccountRecord.moved.forKey(CodingKeys.moved)).asRequest(of: self) request.including(optional: AccountRecord.moved.forKey(CodingKeys.moved)).asRequest(of: self)
} }

View File

@ -18,7 +18,7 @@ extension AccountList {
through: joins, through: joins,
using: AccountListJoin.account) using: AccountListJoin.account)
var accounts: QueryInterfaceRequest<AccountResult> { var accounts: QueryInterfaceRequest<AccountInfo> {
AccountResult.request(request(for: Self.accounts)) AccountInfo.request(request(for: Self.accounts))
} }
} }

View File

@ -71,8 +71,8 @@ extension AccountRecord {
through: pinnedStatusJoins, through: pinnedStatusJoins,
using: AccountPinnedStatusJoin.status) using: AccountPinnedStatusJoin.status)
var pinnedStatuses: QueryInterfaceRequest<StatusResult> { var pinnedStatuses: QueryInterfaceRequest<StatusInfo> {
StatusResult.request(request(for: Self.pinnedStatuses)) StatusInfo.request(request(for: Self.pinnedStatuses))
} }
init(account: Account) { init(account: Account) {

View File

@ -184,13 +184,13 @@ public extension ContentDatabase {
ValueObservation.tracking(timeline.statuses.fetchAll) ValueObservation.tracking(timeline.statuses.fetchAll)
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseWriter) .publisher(in: databaseWriter)
.map { [$0.map(Status.init(result:))] } .map { [$0.map(Status.init(info:))] }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func contextObservation(parentID: String) -> AnyPublisher<[[Status]], Error> { func contextObservation(parentID: String) -> AnyPublisher<[[Status]], Error> {
ValueObservation.tracking { db -> [[StatusResult]] in ValueObservation.tracking { db -> [[StatusInfo]] in
guard let parent = try StatusResult.request(StatusRecord.filter(StatusRecord.Columns.id == parentID)) guard let parent = try StatusInfo.request(StatusRecord.filter(StatusRecord.Columns.id == parentID))
.fetchOne(db) else { .fetchOne(db) else {
return [[]] return [[]]
} }
@ -202,15 +202,15 @@ public extension ContentDatabase {
} }
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseWriter) .publisher(in: databaseWriter)
.map { $0.map { $0.map(Status.init(result:)) } } .map { $0.map { $0.map(Status.init(info:)) } }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func statusesObservation( func statusesObservation(
accountID: String, accountID: String,
collection: ProfileCollection) -> AnyPublisher<[[Status]], Error> { collection: ProfileCollection) -> AnyPublisher<[[Status]], Error> {
ValueObservation.tracking { db -> [[StatusResult]] in ValueObservation.tracking { db -> [[StatusInfo]] in
let statuses = try StatusResult.request(StatusRecord.filter( let statuses = try StatusInfo.request(StatusRecord.filter(
AccountStatusJoin AccountStatusJoin
.select(AccountStatusJoin.Columns.statusId, as: String.self) .select(AccountStatusJoin.Columns.statusId, as: String.self)
.filter(sql: "accountId = ? AND collection = ?", arguments: [accountID, collection.rawValue]) .filter(sql: "accountId = ? AND collection = ?", arguments: [accountID, collection.rawValue])
@ -230,7 +230,7 @@ public extension ContentDatabase {
} }
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseWriter) .publisher(in: databaseWriter)
.map { $0.map { $0.map(Status.init(result:)) } } .map { $0.map { $0.map(Status.init(info:)) } }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
@ -264,12 +264,12 @@ public extension ContentDatabase {
} }
func accountObservation(id: String) -> AnyPublisher<Account?, Error> { func accountObservation(id: String) -> AnyPublisher<Account?, Error> {
ValueObservation.tracking(AccountResult.request(AccountRecord.filter(AccountRecord.Columns.id == id)).fetchOne) ValueObservation.tracking(AccountInfo.request(AccountRecord.filter(AccountRecord.Columns.id == id)).fetchOne)
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseWriter) .publisher(in: databaseWriter)
.map { .map {
if let result = $0 { if let info = $0 {
return Account(result: result) return Account(info: info)
} else { } else {
return nil return nil
} }
@ -281,7 +281,7 @@ public extension ContentDatabase {
ValueObservation.tracking(list.accounts.fetchAll) ValueObservation.tracking(list.accounts.fetchAll)
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseWriter) .publisher(in: databaseWriter)
.map { $0.map(Account.init(result:)) } .map { $0.map(Account.init(info:)) }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
} }

View File

@ -3,7 +3,7 @@
import Foundation import Foundation
import GRDB import GRDB
struct StatusResult: Codable, Hashable, FetchableRecord { struct StatusInfo: Codable, Hashable, FetchableRecord {
let account: AccountRecord let account: AccountRecord
let accountMoved: AccountRecord? let accountMoved: AccountRecord?
let status: StatusRecord let status: StatusRecord
@ -12,7 +12,7 @@ struct StatusResult: Codable, Hashable, FetchableRecord {
let reblog: StatusRecord? let reblog: StatusRecord?
} }
extension StatusResult { extension StatusInfo {
static func request(_ request: QueryInterfaceRequest<StatusRecord>) -> QueryInterfaceRequest<Self> { static func request(_ request: QueryInterfaceRequest<StatusRecord>) -> QueryInterfaceRequest<Self> {
request.including(required: StatusRecord.account.forKey(CodingKeys.account)) request.including(required: StatusRecord.account.forKey(CodingKeys.account))
.including(optional: StatusRecord.accountMoved.forKey(CodingKeys.accountMoved)) .including(optional: StatusRecord.accountMoved.forKey(CodingKeys.accountMoved))
@ -22,13 +22,13 @@ extension StatusResult {
.asRequest(of: self) .asRequest(of: self)
} }
var accountResult: AccountResult { var accountInfo: AccountInfo {
AccountResult(account: account, moved: accountMoved) AccountInfo(account: account, moved: accountMoved)
} }
var reblogAccountResult: AccountResult? { var reblogAccountInfo: AccountInfo? {
guard let reblogAccount = reblogAccount else { return nil } guard let reblogAccount = reblogAccount else { return nil }
return AccountResult(account: reblogAccount, moved: reblogAccountMoved) return AccountInfo(account: reblogAccount, moved: reblogAccountMoved)
} }
} }

View File

@ -109,12 +109,12 @@ extension StatusRecord {
through: descendantJoins, through: descendantJoins,
using: StatusContextJoin.status) using: StatusContextJoin.status)
var ancestors: QueryInterfaceRequest<StatusResult> { var ancestors: QueryInterfaceRequest<StatusInfo> {
StatusResult.request(request(for: Self.ancestors)) StatusInfo.request(request(for: Self.ancestors))
} }
var descendants: QueryInterfaceRequest<StatusResult> { var descendants: QueryInterfaceRequest<StatusInfo> {
StatusResult.request(request(for: Self.descendants)) StatusInfo.request(request(for: Self.descendants))
} }
init(status: Status) { init(status: Status) {

View File

@ -13,14 +13,14 @@ extension Account {
try AccountRecord(account: self).save(db) try AccountRecord(account: self).save(db)
} }
convenience init(result: AccountResult) { convenience init(info: AccountInfo) {
var moved: Account? var moved: Account?
if let movedRecord = result.moved { if let movedRecord = info.moved {
moved = Self(record: movedRecord, moved: nil) moved = Self(record: movedRecord, moved: nil)
} }
self.init(record: result.account, moved: moved) self.init(record: info.account, moved: moved)
} }
} }

View File

@ -4,18 +4,18 @@ import Foundation
import GRDB import GRDB
extension Identity { extension Identity {
init(result: IdentityResult) { init(info: IdentityInfo) {
self.init( self.init(
id: result.identity.id, id: info.identity.id,
url: result.identity.url, url: info.identity.url,
authenticated: result.identity.authenticated, authenticated: info.identity.authenticated,
pending: result.identity.pending, pending: info.identity.pending,
lastUsedAt: result.identity.lastUsedAt, lastUsedAt: info.identity.lastUsedAt,
preferences: result.identity.preferences, preferences: info.identity.preferences,
instance: result.instance, instance: info.instance,
account: result.account, account: info.account,
lastRegisteredDeviceToken: result.identity.lastRegisteredDeviceToken, lastRegisteredDeviceToken: info.identity.lastRegisteredDeviceToken,
pushSubscriptionAlerts: result.identity.pushSubscriptionAlerts) pushSubscriptionAlerts: info.identity.pushSubscriptionAlerts)
} }
} }

View File

@ -16,15 +16,15 @@ extension Status {
try StatusRecord(status: self).save(db) try StatusRecord(status: self).save(db)
} }
convenience init(result: StatusResult) { convenience init(info: StatusInfo) {
var reblog: Status? var reblog: Status?
if let reblogResult = result.reblog, let reblogAccountResult = result.reblogAccountResult { if let reblogRecord = info.reblog, let reblogAccountInfo = info.reblogAccountInfo {
reblog = Status(record: reblogResult, account: Account(result: reblogAccountResult), reblog: nil) reblog = Status(record: reblogRecord, account: Account(info: reblogAccountInfo), reblog: nil)
} }
self.init(record: result.status, self.init(record: info.status,
account: Account(result: result.accountResult), account: Account(info: info.accountInfo),
reblog: reblog) reblog: reblog)
} }
} }

View File

@ -45,7 +45,7 @@ extension Timeline {
using: TimelineStatusJoin.status) using: TimelineStatusJoin.status)
.order(StatusRecord.Columns.createdAt.desc) .order(StatusRecord.Columns.createdAt.desc)
var statuses: QueryInterfaceRequest<StatusResult> { var statuses: QueryInterfaceRequest<StatusInfo> {
StatusResult.request(request(for: Self.statuses)) StatusInfo.request(request(for: Self.statuses))
} }
} }

View File

@ -156,35 +156,35 @@ public extension IdentityDatabase {
func identityObservation(id: UUID, immediate: Bool) -> AnyPublisher<Identity, Error> { func identityObservation(id: UUID, immediate: Bool) -> AnyPublisher<Identity, Error> {
ValueObservation.tracking( ValueObservation.tracking(
IdentityResult.request(IdentityRecord.filter(IdentityRecord.Columns.id == id)).fetchOne) IdentityInfo.request(IdentityRecord.filter(IdentityRecord.Columns.id == id)).fetchOne)
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseWriter, scheduling: immediate ? .immediate : .async(onQueue: .main)) .publisher(in: databaseWriter, scheduling: immediate ? .immediate : .async(onQueue: .main))
.tryMap { .tryMap {
guard let result = $0 else { throw IdentityDatabaseError.identityNotFound } guard let info = $0 else { throw IdentityDatabaseError.identityNotFound }
return Identity(result: result) return Identity(info: info)
} }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func identitiesObservation() -> AnyPublisher<[Identity], Error> { func identitiesObservation() -> AnyPublisher<[Identity], Error> {
ValueObservation.tracking( ValueObservation.tracking(
IdentityResult.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc)).fetchAll) IdentityInfo.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc)).fetchAll)
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseWriter) .publisher(in: databaseWriter)
.map { $0.map(Identity.init(result:)) } .map { $0.map(Identity.init(info:)) }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func recentIdentitiesObservation(excluding: UUID) -> AnyPublisher<[Identity], Error> { func recentIdentitiesObservation(excluding: UUID) -> AnyPublisher<[Identity], Error> {
ValueObservation.tracking( ValueObservation.tracking(
IdentityResult.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc)) IdentityInfo.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc))
.filter(IdentityRecord.Columns.id != excluding) .filter(IdentityRecord.Columns.id != excluding)
.limit(9) .limit(9)
.fetchAll) .fetchAll)
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseWriter) .publisher(in: databaseWriter)
.map { $0.map(Identity.init(result:)) } .map { $0.map(Identity.init(info:)) }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
@ -199,10 +199,10 @@ public extension IdentityDatabase {
func identitiesWithOutdatedDeviceTokens(deviceToken: Data) -> AnyPublisher<[Identity], Error> { func identitiesWithOutdatedDeviceTokens(deviceToken: Data) -> AnyPublisher<[Identity], Error> {
databaseWriter.readPublisher( databaseWriter.readPublisher(
value: IdentityResult.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc)) value: IdentityInfo.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc))
.filter(IdentityRecord.Columns.lastRegisteredDeviceToken != deviceToken) .filter(IdentityRecord.Columns.lastRegisteredDeviceToken != deviceToken)
.fetchAll) .fetchAll)
.map { $0.map(Identity.init(result:)) } .map { $0.map(Identity.init(info:)) }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
} }

View File

@ -4,13 +4,13 @@ import Foundation
import GRDB import GRDB
import Mastodon import Mastodon
struct IdentityResult: Codable, Hashable, FetchableRecord { struct IdentityInfo: Codable, Hashable, FetchableRecord {
let identity: IdentityRecord let identity: IdentityRecord
let instance: Identity.Instance? let instance: Identity.Instance?
let account: Identity.Account? let account: Identity.Account?
} }
extension IdentityResult { extension IdentityInfo {
static func request(_ request: QueryInterfaceRequest<IdentityRecord>) -> QueryInterfaceRequest<Self> { static func request(_ request: QueryInterfaceRequest<IdentityRecord>) -> QueryInterfaceRequest<Self> {
request.including(optional: IdentityRecord.instance.forKey(CodingKeys.instance)) request.including(optional: IdentityRecord.instance.forKey(CodingKeys.instance))
.including(optional: IdentityRecord.account.forKey(CodingKeys.account)) .including(optional: IdentityRecord.account.forKey(CodingKeys.account))