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

geesawra.industries/old-fella

Old people's social media app

0
1
main
99 lines · 4.4 KB Raw History
import Foundation
import SwiftUI

extension AppModel {
    func canDelete(_ post: Post) -> Bool {
        account?.id == post.author.id && account != nil && !isSigningOut
    }

    func deletePost(id: String) async {
        guard let original = post(id: id), canDelete(original), activePostActions.insert(id).inserted else { return }
        let generation = sessionGeneration
        defer { if generation == sessionGeneration { activePostActions.remove(id) } }
        do {
            try await service.deletePost(original)
            guard generation == sessionGeneration else { return }
            contentGeneration = UUID()
            deletedPostIDs.insert(id)
            removePost(id: id)
        } catch {
            guard generation == sessionGeneration else { return }
            if error is ActionNotSentError
                || (error as? ServiceError).map({
                    if case .noSession = $0 { return true }
                    return false
                }) == true {
                if !NetworkFailure.isCancellation(error) { errorMessage = "Could not delete the post. Try again." }
            } else {
                errorMessage =
                    "Could not confirm that the post was deleted. Refresh the feed and try again if it is still there."
            }
        }
    }

    func toggleLike(postID: String) async {
        guard account != nil, !isSigningOut, !uncertainPostIDs.contains(postID), let original = post(id: postID),
            activePostActions.insert(postID).inserted
        else { return }
        let generation = sessionGeneration
        defer { if generation == sessionGeneration { activePostActions.remove(postID) } }
        do {
            let uri = try await service.toggleLike(post: original, liked: !original.isLiked)
            guard generation == sessionGeneration, var updated = post(id: postID) else { return }
            let liked = !original.isLiked
            if updated.isLiked != liked {
                updated.likeCount = max(0, updated.likeCount + (liked ? 1 : -1))
            }
            updated.likeURI = uri
            updated.isLiked = liked
            updatePost(updated)
        } catch {
            guard generation == sessionGeneration else { return }
            if error is ActionNotSentError
                || (error as? ServiceError).map({
                    if case .noSession = $0 { return true }
                    return false
                }) == true {
                if !NetworkFailure.isCancellation(error) { errorMessage = NetworkFailure.message(for: error) }
            } else {
                uncertainPostIDs.insert(postID)
                errorMessage =
                    "\(error.localizedDescription)\n\nBluesky may have received this change. "
                    + "This post's actions are unavailable for this session."
            }
        }
    }

    func toggleRepost(postID: String) async {
        guard account != nil, !isSigningOut, !uncertainPostIDs.contains(postID), let original = post(id: postID),
            activePostActions.insert(postID).inserted
        else { return }
        let generation = sessionGeneration
        defer { if generation == sessionGeneration { activePostActions.remove(postID) } }
        do {
            let uri = try await service.toggleRepost(post: original, reposted: !original.isReposted)
            guard generation == sessionGeneration, var updated = post(id: postID) else { return }
            let reposted = !original.isReposted
            if updated.isReposted != reposted {
                updated.repostCount = max(0, updated.repostCount + (reposted ? 1 : -1))
            }
            updated.repostURI = uri
            updated.isReposted = reposted
            updatePost(updated)
        } catch {
            guard generation == sessionGeneration else { return }
            if error is ActionNotSentError
                || (error as? ServiceError).map({
                    if case .noSession = $0 { return true }
                    return false
                }) == true {
                if !NetworkFailure.isCancellation(error) { errorMessage = NetworkFailure.message(for: error) }
            } else {
                uncertainPostIDs.insert(postID)
                errorMessage =
                    "\(error.localizedDescription)\n\nBluesky may have received this change. "
                    + "This post's actions are unavailable for this session."
            }
        }
    }
}