Refactoring

This commit is contained in:
Justin Mazzocchi 2020-09-29 21:00:55 -07:00
parent e0d63700bc
commit a26acd0af1
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
2 changed files with 18 additions and 13 deletions

View file

@ -70,11 +70,20 @@ extension AccountRecord {
StatusRecord.self,
through: pinnedStatusJoins,
using: AccountPinnedStatusJoin.status)
static let statusJoins = hasMany(AccountStatusJoin.self)
var pinnedStatuses: QueryInterfaceRequest<StatusInfo> {
StatusInfo.request(request(for: Self.pinnedStatuses))
}
func statuses(collection: ProfileCollection) -> QueryInterfaceRequest<StatusInfo> {
StatusInfo.request(
request(for: Self.hasMany(
StatusRecord.self,
through: Self.statusJoins.filter(AccountStatusJoin.Columns.collection == collection.rawValue),
using: AccountStatusJoin.status)))
}
init(account: Account) {
id = account.id
username = account.username

View file

@ -192,7 +192,7 @@ public extension ContentDatabase {
ValueObservation.tracking { db -> [[StatusInfo]] in
guard let parent = try StatusInfo.request(StatusRecord.filter(StatusRecord.Columns.id == parentID))
.fetchOne(db) else {
return [[]]
return []
}
let ancestors = try parent.record.ancestors.fetchAll(db)
@ -210,20 +210,16 @@ public extension ContentDatabase {
accountID: String,
collection: ProfileCollection) -> AnyPublisher<[[Status]], Error> {
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])
.contains(StatusRecord.Columns.id))
.order(StatusRecord.Columns.createdAt.desc))
.fetchAll(db)
guard let accountRecord = try AccountRecord
.filter(AccountRecord.Columns.id == accountID)
.fetchOne(db) else {
return []
}
if
case .statuses = collection,
let accountRecord = try AccountRecord.filter(AccountRecord.Columns.id == accountID).fetchOne(db) {
let pinnedStatuses = try accountRecord.pinnedStatuses.fetchAll(db)
let statuses = try accountRecord.statuses(collection: collection).fetchAll(db)
return [pinnedStatuses, statuses]
if case .statuses = collection {
return [try accountRecord.pinnedStatuses.fetchAll(db), statuses]
} else {
return [statuses]
}