Tusker/Tusker/API/FetchStatusService.swift

48 lines
1.2 KiB
Swift

//
// FetchStatusService.swift
// Tusker
//
// Created by Shadowfacts on 1/17/23.
// Copyright © 2023 Shadowfacts. All rights reserved.
//
import Foundation
import Pachyderm
@MainActor
class FetchStatusService {
let statusID: String
let mastodonController: MastodonController
init(statusID: String, mastodonController: MastodonController) {
self.statusID = statusID
self.mastodonController = mastodonController
}
func run() async -> Result {
let response = await mastodonController.runResponse(Client.getStatus(id: statusID))
switch response {
case .success(let status, _):
return .loaded(status)
case .failure(let error):
switch error.type {
case .unexpectedStatus(404), .mastodonError(404, _):
self.handleStatusNotFound()
return .notFound
default:
return .error(error)
}
}
}
private func handleStatusNotFound() {
// todo: remove from persistent store, send notifications
}
enum Result {
case loaded(Status)
case notFound
case error(Client.Error)
}
}