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

geesawra.industries/old-fella

Old people's social media app

0
1
main
98 lines · 5.0 KB Raw History
import Foundation
import UIKit

extension MockBlueskyService {
    func loadSubscribedFeeds() async throws -> [CustomFeed] {
        if failsLoadingFeeds { throw URLError(.notConnectedToInternet) }
        return subscribedFeeds
    }

    func loadCustomFeed(uri: String, cursor: String?) async throws -> FeedPage {
        if customFeedUnavailable { throw CustomFeedError.unavailable }
        if usesRichScrollFixture {
            return cursor == nil ? richScrollFeedPage(authorID: "mae") : FeedPage(posts: [], cursor: nil)
        }
        let page = try await loadFollowingFeed(cursor: cursor)
        return FeedPage(posts: page.posts.filter { $0.author.id == "mae" }, cursor: page.cursor)
    }

    func loadFollowingFeed(cursor: String?) async throws -> FeedPage {
        if cursor == nil { loadState.followingFirstPageLoadCount += 1 }
        if ProcessInfo.processInfo.arguments.contains("-ui-testing-pull-refresh-delay") {
            try await Task.sleep(for: .seconds(4))
        }
        if ProcessInfo.processInfo.arguments.contains("-ui-testing-three-loading-columns") {
            try await Task.sleep(for: .seconds(12))
        } else if ProcessInfo.processInfo.arguments.contains("-ui-testing-feed-delay") {
            try await Task.sleep(nanoseconds: 5_000_000_000)
        }
        if usesRichScrollFixture {
            return cursor == nil ? richScrollFeedPage() : FeedPage(posts: [], cursor: nil)
        }
        if ProcessInfo.processInfo.arguments.contains("-ui-testing-thread-gap-link") {
            guard cursor == nil else { return FeedPage(posts: [], cursor: nil) }
            let posts = firstPage
            let context = posts.filter { $0.id == "post-1" || $0.id == "post-gap-parent" }
            return FeedPage(
                posts: posts.filter { $0.id == "post-gap" }, cursor: nil,
                replyContexts: ["post-gap": context])
        }
        guard cursor != nil else {
            let repostedBy =
                ProcessInfo.processInfo.arguments.contains("-ui-testing-repost-attribution")
                ? ["post-1": authors[1]] : [:]
            if ProcessInfo.processInfo.arguments.contains("-ui-testing-refresh-new-post"),
                loadState.followingFirstPageLoadCount > 1 {
                let newPosts = (0..<8).map { index in
                    Post(
                        id: index == 0 ? "refresh-new-post" : "refresh-new-post-\(index)",
                        author: authors[index % authors.count],
                        text: "A new post arrived during refresh."
                            + String(repeating: "\nMore from this account.", count: index % 3),
                        likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: [])
                }
                return FeedPage(posts: newPosts + firstPage, cursor: "page-2", repostedBy: repostedBy)
            }
            return FeedPage(posts: firstPage, cursor: "page-2", repostedBy: repostedBy)
        }
        return FeedPage(
            posts: [
                withInteractions(
                    Post(
                        id: "post-4", author: authors[0], text: "Another quiet moment from the neighborhood.",
                        likeCount: 12, repostCount: 2, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []))
            ], cursor: nil)
    }

    func suggestAccounts(query: String) async throws -> [Post.Author] {
        loadState.mentionLookupCount += 1
        if ProcessInfo.processInfo.arguments.contains("-ui-testing-mention-failure"),
            loadState.mentionLookupCount == 1 {
            throw URLError(.notConnectedToInternet)
        }
        return try await searchAccounts(query: query, cursor: nil).accounts
    }

    func searchAccounts(query: String, cursor: String?) async throws -> AccountSearchPage {
        let matches = authors.filter {
            $0.name.localizedCaseInsensitiveContains(query) || $0.handle.localizedCaseInsensitiveContains(query)
        }
        let start = cursor.flatMap { Int($0) }.flatMap { $0 > 0 ? $0 : nil } ?? (cursor == nil ? 0 : matches.count)
        let page = matches.dropFirst(start).prefix(1).map { author in
            var author = author
            author.bio = profiles.first { $0.author.id == author.id }?.bio
            return author
        }
        return AccountSearchPage(
            accounts: page, cursor: start + page.count < matches.count ? String(start + page.count) : nil)
    }

    func searchPosts(query: String, cursor: String?, authorID: String? = nil) async throws -> FeedPage {
        let matches = firstPage.filter {
            $0.text.localizedCaseInsensitiveContains(query) && (authorID == nil || $0.author.id == authorID)
        }.sorted { ($0.createdAt ?? .distantPast) > ($1.createdAt ?? .distantPast) }
        let start = cursor.flatMap { Int($0) }.flatMap { $0 > 0 ? $0 : nil } ?? (cursor == nil ? 0 : matches.count)
        let page = Array(matches.dropFirst(start).prefix(1))
        return FeedPage(posts: page, cursor: start + page.count < matches.count ? String(start + page.count) : nil)
    }
}