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
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.")
}
}
}