Refactor search

This commit is contained in:
Justin Mazzocchi 2021-02-07 22:11:32 -08:00
parent e420c6b934
commit 70de6dd475
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -572,19 +572,21 @@ public extension ContentDatabase {
.eraseToAnyPublisher()
}
// swiftlint:disable:next function_body_length
func publisher(results: Results, limit: Int?) -> AnyPublisher<[CollectionSection], Error> {
let accountIds = results.accounts.map(\.id)
let statusIds = results.statuses.map(\.id)
let accountsPublisher = ValueObservation.tracking(
AccountAndRelationshipInfo.request(
return ValueObservation.tracking { db -> ([AccountAndRelationshipInfo], [StatusInfo]) in
(try AccountAndRelationshipInfo.request(
AccountRecord.filter(accountIds.contains(AccountRecord.Columns.id)))
.fetchAll)
.removeDuplicates()
.fetchAll(db),
try StatusInfo.request(
StatusRecord.filter(statusIds.contains(StatusRecord.Columns.id)))
.fetchAll(db))
}
.publisher(in: databaseWriter)
.map { infos -> [CollectionItem] in
var accounts = infos.sorted {
.map { accountAndRelationshipInfos, statusInfos in
var accounts = accountAndRelationshipInfos.sorted {
accountIds.firstIndex(of: $0.accountInfo.record.id) ?? 0
< accountIds.firstIndex(of: $1.accountInfo.record.id) ?? 0
}
@ -594,17 +596,7 @@ public extension ContentDatabase {
accounts.append(.moreResults(.init(scope: .accounts)))
}
return accounts
}
let statusesPublisher = ValueObservation.tracking(
StatusInfo.request(
StatusRecord.filter(statusIds.contains(StatusRecord.Columns.id)))
.fetchAll)
.removeDuplicates()
.publisher(in: databaseWriter)
.map { infos -> [CollectionItem] in
var statuses = infos.sorted {
var statuses = statusInfos.sorted {
statusIds.firstIndex(of: $0.record.id) ?? 0
< statusIds.firstIndex(of: $1.record.id) ?? 0
}
@ -620,18 +612,13 @@ public extension ContentDatabase {
statuses.append(.moreResults(.init(scope: .statuses)))
}
return statuses
}
var hashtags = results.hashtags.map(CollectionItem.tag)
if let limit = limit, hashtags.count >= limit {
hashtags.append(.moreResults(.init(scope: .tags)))
}
return accountsPublisher.combineLatest(statusesPublisher)
.map { accounts, statuses in
[.init(items: accounts, searchScope: .accounts),
return [.init(items: accounts, searchScope: .accounts),
.init(items: statuses, searchScope: .statuses),
.init(items: hashtags, searchScope: .tags)]
.filter { !$0.items.isEmpty }