~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
92 lines · 3.9 KB Raw History
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)
    }
}