Skip to content
Repos

WO_DevPlaceIOS

Swift 8.8 KB279 lines Opens in site

Directory Raw Download

import Foundation
import DevPlaceSwiftSDK

extension DevPlaceApi where Self == MockDevPlaceApi {
    static var mock: Self {
        .shared
    }
}

final class MockDevPlaceApi: DevPlaceApi {
    static let shared = MockDevPlaceApi()

    private func mockDelay(_ delay: TimeInterval = 1) async {
        try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
    }

    func logIn(email: String, password: String) async throws {
        await mockDelay()
        let token = AuthToken(
            tokenType: "bearer",
            accessToken: UUID().uuidString,
            expireTime: Date().addingTimeInterval(60 * 60 + 10)
        )
        AppState.shared.token = token
        AppState.shared.currentUser = Profile.mock.user
    }

    func signUp(username: String, email: String, password: String, confirmPassword: String, birthDate: Date, acceptTerms: Bool) async throws {
        await mockDelay()
    }

    func deleteAccount(password: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func feed(before: Date?) async throws -> Feed {
        await mockDelay()
        try await refreshTokenIfNeeded()
        return .mock
    }

    func postDetail(slug: String) async throws -> PostDetail {
        await mockDelay()
        try await refreshTokenIfNeeded()
        return .mock
    }

    func createPost(title: String?, topic: PostTopic?, content: String, attachments: [UploadResponse], pollQuestion: String?, pollOptions: [String], projectLink: String?) async throws {
        await mockDelay()
        try await refreshTokenIfNeeded()
    }

    func editPost(slug: String, title: String, topic: PostTopic, content: String, projectLink: String) async throws {
        await mockDelay()
        try await refreshTokenIfNeeded()
    }

    func deletePost(slug: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func deleteComment(uid: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func editComment(uid: String, content: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func createComment(targetType: TargetType, targetId: String, content: String, parentId: String?, attachments: [UploadResponse]) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func profile(username: String?) async throws -> Profile {
        await mockDelay(0.1)
        try await refreshTokenIfNeeded()
        return .mock
    }

    func followUser(username: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func unfollowUser(username: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func blockUser(username: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func unblockUser(username: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func muteUser(username: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func unmuteUser(username: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func messages(withUid: String?) async throws -> MessagesInbox {
        await mockDelay(0.3)
        try await refreshTokenIfNeeded()
        return withUid == nil ? .mockInbox : .mockConversation
    }

    func sendMessage(receiverId: String, content: String, attachments: [UploadResponse]) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func makeMessagesSocket(otherUserId: String) -> any MessagesSocket {
        MockMessagesSocket()
    }

    func usersForMentioning(matching query: String) async throws -> UserSearch {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
        guard !query.isEmpty else {
            return .mock
        }
        let matches = UserSearch.mock.results.filter {
            $0.username.localizedCaseInsensitiveContains(query)
        }
        return UserSearch(results: matches)
    }

    func notifications(before: String?) async throws -> Notifications {
        await mockDelay(0.5)
        try await refreshTokenIfNeeded()
        return .mock
    }

    func openNotification(uid: String) async throws -> NotificationOpen {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
        let targetUrl = Notifications.mock.groups
            .flatMap { $0.entries + $0.eventEntries.map(\.latest) }
            .first { $0.data.id == uid }?
            .data.targetUrl
        return NotificationOpen(
            ok: true,
            redirect: targetUrl ?? "/posts/a-post-with-several-attachments#comment-c6",
        )
    }

    func notificationCounts() async throws -> NotificationCounts {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
        return NotificationCounts(notifications: 3, messages: 1)
    }

    func markNotificationRead(uid: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func markAllNotificationsRead() async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func notificationSettings() async throws -> NotificationSettings {
        await mockDelay(0.5)
        try await refreshTokenIfNeeded()
        return .mock
    }

    func setNotificationGrouping(username: String, enabled: Bool) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func setNotificationThrottling(username: String, enabled: Bool) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func setNotificationPreference(username: String, notificationType: String, channel: NotificationChannel, enabled: Bool) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func resetNotificationPreferences(username: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func registerPushDevice(deviceToken: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func unregisterPushDevice(deviceToken: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func vote(targetType: TargetType, targetId: String, vote: Vote) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func submitPollChoice(pollId: String, optionId: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func react(targetType: TargetType, targetId: String, emoji: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func reportReasons() async throws -> [ReportReason] {
        await mockDelay(0.2)
        return [
            .init(key: "hate", label: "Hate speech or discriminatory content"),
            .init(key: "violence", label: "Realistic violence or threats"),
            .init(key: "harassment", label: "Harassment or bullying"),
            .init(key: "spam", label: "Spam or unwanted promotion"),
            .init(key: "other", label: "Something else"),
        ]
    }

    func report(targetType: TargetType, targetId: String, reason: String, detail: String?) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }

    func uploadFile(data: Data, filename: String, mimeType: String) async throws -> UploadResponse {
        await mockDelay()
        try await refreshTokenIfNeeded()
        return UploadResponse(
            id: UUID().uuidString,
            filename: filename,
            url: "https://example.com/\(filename)",
            size: data.count,
            isImage: mimeType.hasPrefix("image"),
            isVideo: mimeType.hasPrefix("video"),
            isAudio: mimeType.hasPrefix("audio"),
            mimeType: mimeType,
        )
    }

    func uploadFromUrl(url: String, filename: String?) async throws -> UploadResponse {
        await mockDelay()
        try await refreshTokenIfNeeded()
        let resolvedFilename = filename ?? (URL(string: url)?.lastPathComponent ?? "attachment")
        return UploadResponse(
            id: UUID().uuidString,
            filename: resolvedFilename,
            url: url,
            size: 0,
            isImage: false,
            isVideo: false,
            isAudio: false,
            mimeType: "application/octet-stream",
        )
    }

    func deleteAttachment(uid: String) async throws {
        await mockDelay(0.2)
        try await refreshTokenIfNeeded()
    }
}
181.7ms