1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Foundation
import SwiftUI
extension AppModel {
func resetSearch(query: String, kind: SearchKind, ownPostsOnly: Bool = false) {
searchOwnPostsOnly = ownPostsOnly
searchGeneration = UUID()
searchQuery = query.trimmingCharacters(in: .whitespacesAndNewlines)
searchKind = kind
isSearching = false
hasSearched = false
searchError = nil
searchCursor = nil
searchUsedCursors = []
withContentUpdates {
searchAccounts = []
searchPosts = []
searchReplyContexts = [:]
searchRepostedBy = [:]
}
}
func loadSearchPage() async {
guard !searchQuery.isEmpty, !isSearching, account != nil, contentPreferences != nil,
!hasSearched || searchCursor != nil
else { return }
let generation = searchGeneration
let session = sessionGeneration
let cursor = searchCursor
isSearching = true
searchError = nil
defer { if generation == searchGeneration { isSearching = false } }
do {
let next: String?
switch searchKind {
case .accounts:
let page = try await service.searchAccounts(query: searchQuery, cursor: cursor)
guard generation == searchGeneration, session == sessionGeneration, !Task.isCancelled else { return }
var seen = Set(searchAccounts.map(\.id))
searchAccounts.append(contentsOf: page.accounts.filter { seen.insert($0.id).inserted })
next = page.cursor
case .posts:
let page = try await service.searchPosts(
query: searchQuery, cursor: cursor, authorID: searchOwnPostsOnly ? account?.id : nil)
guard generation == searchGeneration, session == sessionGeneration, !Task.isCancelled else { return }
withContentUpdates {
let page = excludingDeletedPosts(page)
applyMutedPostIDs(page.mutedPostIDs)
updateFeedPostCaches(page)
var seen = Set(searchPosts.map(\.id))
searchPosts.append(contentsOf: page.posts.filter { seen.insert($0.id).inserted })
searchReplyContexts.merge(page.replyContexts) { _, new in new }
searchRepostedBy.merge(page.repostedBy) { _, new in new }
}
next = page.cursor
}
if let cursor { searchUsedCursors.insert(cursor) }
searchCursor = next.flatMap { $0.isEmpty || searchUsedCursors.contains($0) ? nil : $0 }
hasSearched = true
} catch {
guard generation == searchGeneration, session == sessionGeneration, !Task.isCancelled else { return }
searchError = "Search failed. Try again."
}
}
var visibleSearchAccounts: [Post.Author] {
searchAccounts.filter(isAccountSummaryVisible)
}
func mentionSuggestions(query: String) async throws -> [Post.Author] {
try Task.checkCancellation()
let query = query.trimmingCharacters(in: .whitespacesAndNewlines)
guard !query.isEmpty, let account, contentPreferences != nil, !isSigningOut else { return [] }
let session = sessionGeneration
let accounts = try await service.suggestAccounts(query: query)
try Task.checkCancellation()
guard session == sessionGeneration, self.account?.id == account.id,
contentPreferences != nil, !isSigningOut
else { throw CancellationError() }
var seen: Set<String> = []
return Array(
accounts.filter {
self.isAccountSummaryVisible($0) && seen.insert($0.id).inserted
}.prefix(5))
}
var visibleSearchPosts: [Post] {
filteredFeedPosts(
searchPosts.filter { !deletedPostIDs.contains($0.id) }, contexts: searchReplyContexts,
reposts: searchRepostedBy)
}
}