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 GRDB
struct AccountResult: Codable, Hashable, FetchableRecord {
struct AccountInfo: Codable, Hashable, FetchableRecord {
let account: AccountRecord
let moved: AccountRecord?
}
extension AccountResult {
extension AccountInfo {
static func request(_ request: QueryInterfaceRequest<AccountRecord>) -> QueryInterfaceRequest<Self> {
request.including(optional: AccountRecord.moved.forKey(CodingKeys.moved)).asRequest(of: self)
}

View file

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

View file

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

View file

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

View file

@ -3,7 +3,7 @@
import Foundation
import GRDB
struct StatusResult: Codable, Hashable, FetchableRecord {
struct StatusInfo: Codable, Hashable, FetchableRecord {
let account: AccountRecord
let accountMoved: AccountRecord?
let status: StatusRecord
@ -12,7 +12,7 @@ struct StatusResult: Codable, Hashable, FetchableRecord {
let reblog: StatusRecord?
}
extension StatusResult {
extension StatusInfo {
static func request(_ request: QueryInterfaceRequest<StatusRecord>) -> QueryInterfaceRequest<Self> {
request.including(required: StatusRecord.account.forKey(CodingKeys.account))
.including(optional: StatusRecord.accountMoved.forKey(CodingKeys.accountMoved))
@ -22,13 +22,13 @@ extension StatusResult {
.asRequest(of: self)
}
var accountResult: AccountResult {
AccountResult(account: account, moved: accountMoved)
var accountInfo: AccountInfo {
AccountInfo(account: account, moved: accountMoved)
}
var reblogAccountResult: AccountResult? {
var reblogAccountInfo: AccountInfo? {
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,
using: StatusContextJoin.status)
var ancestors: QueryInterfaceRequest<StatusResult> {
StatusResult.request(request(for: Self.ancestors))
var ancestors: QueryInterfaceRequest<StatusInfo> {
StatusInfo.request(request(for: Self.ancestors))
}
var descendants: QueryInterfaceRequest<StatusResult> {
StatusResult.request(request(for: Self.descendants))
var descendants: QueryInterfaceRequest<StatusInfo> {
StatusInfo.request(request(for: Self.descendants))
}
init(status: Status) {

View file

@ -13,14 +13,14 @@ extension Account {
try AccountRecord(account: self).save(db)
}
convenience init(result: AccountResult) {
convenience init(info: AccountInfo) {
var moved: Account?
if let movedRecord = result.moved {
if let movedRecord = info.moved {
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
extension Identity {
init(result: IdentityResult) {
init(info: IdentityInfo) {
self.init(
id: result.identity.id,
url: result.identity.url,
authenticated: result.identity.authenticated,
pending: result.identity.pending,
lastUsedAt: result.identity.lastUsedAt,
preferences: result.identity.preferences,
instance: result.instance,
account: result.account,
lastRegisteredDeviceToken: result.identity.lastRegisteredDeviceToken,
pushSubscriptionAlerts: result.identity.pushSubscriptionAlerts)
id: info.identity.id,
url: info.identity.url,
authenticated: info.identity.authenticated,
pending: info.identity.pending,
lastUsedAt: info.identity.lastUsedAt,
preferences: info.identity.preferences,
instance: info.instance,
account: info.account,
lastRegisteredDeviceToken: info.identity.lastRegisteredDeviceToken,
pushSubscriptionAlerts: info.identity.pushSubscriptionAlerts)
}
}

View file

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

View file

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

View file

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

View file

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