Tusker/Tusker/MastodonCache.swift

137 lines
4.0 KiB
Swift

//
// StatusCache.swift
// Tusker
//
// Created by Shadowfacts on 9/17/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
import Combine
import Pachyderm
class MastodonCache {
private static var statuses = [String: Status]()
private static var accounts = [String: Account]()
private static var relationships = [String: Relationship]()
private static var notifications = [String: Pachyderm.Notification]()
static let statusSubject = PassthroughSubject<Status, Never>()
static let accountSubject = PassthroughSubject<Account, Never>()
// MARK: - Statuses
static func status(for id: String) -> Status? {
return statuses[id]
}
static func set(status: Status, for id: String) {
statuses[id] = status
add(account: status.account)
if let reblog = status.reblog {
add(status: reblog)
add(account: reblog.account)
}
statusSubject.send(status)
}
static func status(for id: String, completion: @escaping (Status?) -> Void) {
let request = MastodonController.client.getStatus(id: id)
MastodonController.client.run(request) { response in
guard case let .success(status, _) = response else {
completion(nil)
return
}
set(status: status, for: id)
completion(status)
}
}
static func add(status: Status) {
set(status: status, for: status.id)
}
static func addAll(statuses: [Status]) {
statuses.forEach(add)
}
// MARK: - Accounts
static func account(for id: String) -> Account? {
return accounts[id]
}
static func set(account: Account, for id: String) {
accounts[id] = account
accountSubject.send(account)
}
static func account(for id: String, completion: @escaping (Account?) -> Void) {
let request = MastodonController.client.getAccount(id: id)
MastodonController.client.run(request) { response in
guard case let .success(account, _) = response else {
completion(nil)
return
}
set(account: account, for: account.id)
completion(account)
}
}
static func add(account: Account) {
set(account: account, for: account.id)
}
static func addAll(accounts: [Account]) {
accounts.forEach(add)
}
// MARK: - Relationships
static func relationship(for id: String) -> Relationship? {
return relationships[id]
}
static func set(relationship: Relationship, id: String) {
relationships[id] = relationship
}
static func relationship(for id: String, completion: @escaping (Relationship?) -> Void) {
let request = MastodonController.client.getRelationships(accounts: [id])
MastodonController.client.run(request) { response in
guard case let .success(relationships, _) = response,
let relationship = relationships.first else {
completion(nil)
return
}
set(relationship: relationship, id: relationship.id)
completion(relationship)
}
}
static func add(relationship: Relationship) {
set(relationship: relationship, id: relationship.id)
}
static func addAll(relationships: [Relationship]) {
relationships.forEach(add)
}
// MARK: - Notifications
static func notification(for id: String) -> Pachyderm.Notification? {
return notifications[id]
}
static func set(notification: Pachyderm.Notification, id: String) {
notifications[id] = notification
}
static func add(notification: Pachyderm.Notification) {
set(notification: notification, id: notification.id)
}
static func addAll(notifications: [Pachyderm.Notification]) {
notifications.forEach(add)
}
}