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

geesawra.industries/old-fella

Old people's social media app

0
1
main
115 lines · 4.9 KB Raw History
import Foundation
import Observation
import UIKit
import XCTest

@testable import OldFella

@MainActor
final class NotificationLoadingService: BlueskyService {
    let base = MockBlueskyService()
    var pages: [String: Result<NotificationPage, Error>] = [:]
    var requests: [String] = []
    var feedRequests = 0
    var feedCursors: [String] = []
    var feedPages: [String: Result<FeedPage, Error>] = [:]
    private var suspendedFeedCursor: String?
    private var suspendedFeedStarted = false
    private var suspendedFeedWaiter: CheckedContinuation<Void, Never>?
    private var suspendedFeedContinuation: CheckedContinuation<FeedPage, Error>?
    var readError: Error?
    var readDates: [Date] = []
    var mutedWords: [ModerationPolicy.MutedWord] = []
    private var suspendedCursor: String?
    private var suspendedRequestStarted = false
    private var suspendedRequestWaiter: CheckedContinuation<Void, Never>?
    private var suspendedRequestContinuation: CheckedContinuation<NotificationPage, Error>?

    func suspendRequest(for cursor: String) {
        suspendedCursor = cursor
        suspendedRequestStarted = false
    }

    func waitUntilSuspendedRequest() async {
        if suspendedRequestStarted { return }
        await withCheckedContinuation { suspendedRequestWaiter = $0 }
    }

    func resumeSuspendedRequest(with result: Result<NotificationPage, Error>) {
        let continuation = suspendedRequestContinuation
        suspendedRequestContinuation = nil
        continuation?.resume(with: result)
    }

    func loadNotifications(cursor: String?) async throws -> NotificationPage {
        let key = cursor ?? "initial"
        requests.append(key)
        if key == suspendedCursor {
            suspendedCursor = nil
            suspendedRequestStarted = true
            suspendedRequestWaiter?.resume()
            suspendedRequestWaiter = nil
            return try await withCheckedThrowingContinuation { suspendedRequestContinuation = $0 }
        }
        return try pages[key, default: .success(.init(notifications: [], cursor: nil))].get()
    }
    func restoreAccount() async throws -> Account? { try await base.restoreAccount() }
    func signIn(handle: String, appPassword: String, authFactorToken: String?) async throws -> Account {
        try await base.signIn(handle: handle, appPassword: appPassword, authFactorToken: authFactorToken)
    }
    func signOut() async throws { try await base.signOut() }
    func suspendFeedRequest(for cursor: String) {
        suspendedFeedCursor = cursor
        suspendedFeedStarted = false
    }
    func waitUntilSuspendedFeedRequest() async {
        if suspendedFeedStarted { return }
        await withCheckedContinuation { suspendedFeedWaiter = $0 }
    }
    func resumeSuspendedFeedRequest(with result: Result<FeedPage, Error>) {
        let continuation = suspendedFeedContinuation
        suspendedFeedContinuation = nil
        continuation?.resume(with: result)
    }
    func loadFollowingFeed(cursor: String?) async throws -> FeedPage {
        feedRequests += 1
        let key = cursor ?? "initial"
        feedCursors.append(key)
        if key == suspendedFeedCursor {
            suspendedFeedCursor = nil
            suspendedFeedStarted = true
            suspendedFeedWaiter?.resume()
            suspendedFeedWaiter = nil
            return try await withCheckedThrowingContinuation { suspendedFeedContinuation = $0 }
        }
        if let page = feedPages[key] { return try page.get() }
        return try await base.loadFollowingFeed(cursor: cursor)
    }
    func markNotificationsRead(seenAt: Date) async throws {
        readDates.append(seenAt)
        if let readError { throw readError }
        try await base.markNotificationsRead(seenAt: seenAt)
    }
    func loadPost(uri: String) async throws -> [Post] { try await base.loadPost(uri: uri) }
    func loadProfile(actorID: String) async throws -> Profile? { try await base.loadProfile(actorID: actorID) }
    func loadAuthorPosts(actorID: String, cursor: String?) async throws -> FeedPage {
        try await base.loadAuthorPosts(actorID: actorID, cursor: cursor)
    }
    func toggleLike(post: Post, liked: Bool) async throws -> String? {
        try await base.toggleLike(post: post, liked: liked)
    }
    func toggleRepost(post: Post, reposted: Bool) async throws -> String? {
        try await base.toggleRepost(post: post, reposted: reposted)
    }
    func publish(text: String, replyTo: Post?, quotePost: Post?, images: [ImageAttachment]) async throws -> Post {
        try await base.publish(text: text, replyTo: replyTo, quotePost: quotePost, images: images)
    }
    func setFollowing(_ following: Bool, actorID: String) async throws -> String? {
        try await base.setFollowing(following, actorID: actorID)
    }
    func loadModerationPreferences() async throws -> ModerationPolicy.Preferences {
        var preferences = try await base.loadModerationPreferences()
        preferences.mutedWords = mutedWords
        return preferences
    }
}