1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
}
}