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

geesawra.industries/old-fella

Old people's social media app

0
1
main
111 lines · 5.5 KB Raw History
import Foundation
import UIKit

extension MockBlueskyService {
    func loadNotifications(cursor: String?) async throws -> NotificationPage {
        if ProcessInfo.processInfo.arguments.contains("-ui-testing-pull-refresh-delay") {
            try await Task.sleep(for: .seconds(4))
        }
        if cursor == nil, ProcessInfo.processInfo.arguments.contains("-ui-testing-notification-delay") {
            try await Task.sleep(for: .seconds(15))
        }
        let now = Date()
        let likedPost = firstPage[0]
        if ProcessInfo.processInfo.arguments.contains("-ui-testing-grouped-notifications") {
            return groupedNotifications(now: now, post: likedPost)
        }
        if cursor != nil { return laterNotifications(now: now, likedPost: likedPost) }
        return firstNotifications(now: now, likedPost: likedPost)
    }

    private func groupedNotifications(now: Date, post: Post) -> NotificationPage {
        let reasons: [FeedNotification.Reason] = [.like, .repost]
        let notifications = reasons.flatMap { reason in
            profiles.prefix(2).map { profile in
                var author = profile.author
                author.bio = profile.bio
                return FeedNotification(
                    id: "\(reason.rawValue)-\(author.id)", author: author, reason: reason,
                    indexedAt: now, isRead: false, postID: post.id, relatedPost: post)
            }
        }
        return NotificationPage(notifications: notifications, cursor: nil)
    }

    private func laterNotifications(now: Date, likedPost: Post) -> NotificationPage {
        let quote = withInteractions(
            Post(
                id: "quote-post-1", author: authors[2], text: "A short thought about the quoted post.",
                likeCount: 2, repostCount: 1, isLiked: false, isReposted: false, imageURLs: [],
                quote: Post.Quote(postID: "post-1"), replyIDs: []))
        return NotificationPage(
            notifications: [
                FeedNotification(
                    id: "notification-4", author: authors[0], reason: .follow,
                    indexedAt: now.addingTimeInterval(-172_800), isRead: true, postID: nil),
                FeedNotification(
                    id: "notification-5", author: authors[1], reason: .repost,
                    indexedAt: now.addingTimeInterval(-259_200), isRead: false, postID: likedPost.id,
                    relatedPost: likedPost),
                FeedNotification(
                    id: "notification-6", author: authors[2], reason: .quote,
                    indexedAt: now.addingTimeInterval(-345_600), isRead: false, postID: quote.id, relatedPost: quote
                ),
                FeedNotification(
                    id: "notification-7", author: authors[2], reason: .mention,
                    indexedAt: now.addingTimeInterval(-432_000), isRead: false, postID: "mention-post-1",
                    relatedPost: withInteractions(
                        Post(
                            id: "mention-post-1", author: authors[2],
                            text: "Take a look at this, @demo.bsky.social. The post is the main content.",
                            likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [],
                            replyIDs: [])))
            ], cursor: nil)
    }

    private func firstNotifications(now: Date, likedPost: Post) -> NotificationPage {
        let replyParent =
            ProcessInfo.processInfo.arguments.contains("-ui-testing-own-reply-parent")
            ? Post(
                id: "own-parent",
                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: "A post by the signed-in account.",
                likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: [])
            : likedPost
        let reply = Post(
            id: "reply-post-1",
            author: authors[1],
            text: "Thanks for the thoughtful reply.",
            likeCount: 0,
            repostCount: 0,
            isLiked: false,
            isReposted: false,
            imageURLs: [],
            replyRootURI: replyParent.id,
            replyParentURI: replyParent.id,
            replyIDs: []
        )
        var followAuthor = authors[2]
        followAuthor.bio = profiles[2].bio
        return NotificationPage(
            notifications: [
                FeedNotification(
                    id: "notification-1", author: authors[0], reason: .like, indexedAt: now.addingTimeInterval(-300),
                    isRead: false, postID: likedPost.id, relatedPost: likedPost),
                FeedNotification(
                    id: "notification-2", author: authors[1], reason: .reply, indexedAt: now.addingTimeInterval(-3600),
                    isRead: false, postID: reply.id, relatedPost: withInteractions(reply),
                    parentPost: withInteractions(replyParent)),
                FeedNotification(
                    id: "notification-3", author: followAuthor, reason: .follow,
                    indexedAt: now.addingTimeInterval(-86_400), isRead: true, postID: nil)
            ], cursor: "mock-notifications-page-2")
    }

    func markNotificationsRead(seenAt: Date) async throws {
        loadState.notificationsSeenAt.append(seenAt)
        if failsMarkingNotificationsRead { throw ServiceError.invalidResponse }
    }
}