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

geesawra.industries/old-fella

Old people's social media app

0
1
main
83 lines · 3.8 KB Raw History
import Foundation
import UIKit

extension MockBlueskyService {
    func loadSavedPosts(cursor: String?) async throws -> FeedPage {
        let start = cursor.flatMap(Int.init) ?? 0
        let page = loadState.savedPosts.dropFirst(start).prefix(2).map(withInteractions)
        let end = start + page.count
        return FeedPage(posts: page, cursor: end < loadState.savedPosts.count ? String(end) : nil)
    }

    func setPostSaved(_ saved: Bool, post: Post) async throws {
        if failsSavingPosts { throw URLError(.notConnectedToInternet) }
        loadState.savedPosts.removeAll { $0.id == post.id }
        if saved { loadState.savedPosts.insert(post, at: 0) }
    }

    func toggleLike(post: Post, liked: Bool) async throws -> String? {
        loadState.likes[post.id] = liked
        return liked ? "mock-like-\(post.id)" : nil
    }

    func toggleRepost(post: Post, reposted: Bool) async throws -> String? {
        loadState.reposts[post.id] = reposted
        return reposted ? "mock-repost-\(post.id)" : nil
    }

    func deletePost(_ post: Post) async throws {
        guard post.author.id == account.id else { throw ServiceError.invalidResponse }
        if failsDeleting { throw ServiceError.requestFailed("Could not delete the post.") }
    }

    func publish(text: String, replyTo: Post?, quotePost: Post?, images: [ImageAttachment]) async throws -> Post {
        if failsPublishing { throw ServiceError.requestFailed("The post could not be published. Try again.") }
        return Post(
            id: UUID().uuidString,
            author: Post.Author(
                id: account.id, name: account.displayName, handle: account.handle,
                moderationLabels: ProcessInfo.processInfo.arguments.contains("-ui-testing-info-label")
                    ? [.init(value: "scam", sourceDID: account.id)] : []), text: text, likeCount: 0, repostCount: 0,
            isLiked: false, isReposted: false, imageURLs: [],
            replyRootURI: replyTo?.replyRootURI ?? replyTo?.id, replyRootCID: replyTo?.replyRootCID ?? replyTo?.cid,
            replyParentURI: replyTo?.id, replyParentCID: replyTo?.cid,
            quote: quotePost.map { Post.Quote(postID: $0.id) },
            replyIDs: [], createdAt: Date())
    }

    func publish(text: String, replyTo: Post?, quotePost: Post?, images: [ImageAttachment], video: VideoAttachment?)
        async throws -> Post {
        guard video == nil || images.isEmpty else { throw ServiceError.mixedMedia }
        return try await publish(text: text, replyTo: replyTo, quotePost: quotePost, images: images)
    }

    func setFollowing(_ following: Bool, actorID: String) async throws -> String? {
        following ? "mock-follow-\(actorID)" : nil
    }

    func moderationState(actorID: String) async throws -> ModerationState {
        if ProcessInfo.processInfo.arguments.contains("-ui-testing-moderation-delay") {
            try await Task.sleep(nanoseconds: 3_000_000_000)
        }
        return ModerationState()
    }

    func setMuted(_ muted: Bool, actorID: String) async throws {
        if ProcessInfo.processInfo.arguments.contains("-ui-testing-moderation-failure") {
            throw ServiceError.requestFailed("The account action failed.")
        }
    }

    func setBlocked(_ blocked: Bool, actorID: String, blockURI: String?) async throws -> String? {
        if ProcessInfo.processInfo.arguments.contains("-ui-testing-moderation-failure") {
            throw ServiceError.requestFailed("The account action failed.")
        }
        return blocked ? "mock-block-\(actorID)" : nil
    }

    func report(_ subject: ReportSubject, reason: String) async throws {
        if ProcessInfo.processInfo.arguments.contains("-ui-testing-moderation-failure") {
            throw ServiceError.requestFailed("The report failed.")
        }
    }
}