2020-04-11 19:31:37 +00:00
|
|
|
//
|
|
|
|
// AccountMO.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 4/11/20.
|
|
|
|
// Copyright © 2020 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import CoreData
|
2020-04-11 19:33:39 +00:00
|
|
|
import Pachyderm
|
2020-04-11 19:31:37 +00:00
|
|
|
|
|
|
|
@objc(AccountMO)
|
2020-05-06 22:40:12 +00:00
|
|
|
public final class AccountMO: NSManagedObject, AccountProtocol {
|
2020-04-11 19:31:37 +00:00
|
|
|
|
|
|
|
@nonobjc public class func fetchRequest() -> NSFetchRequest<AccountMO> {
|
|
|
|
return NSFetchRequest<AccountMO>(entityName: "Account")
|
|
|
|
}
|
|
|
|
|
2020-04-12 02:23:31 +00:00
|
|
|
@NSManaged public var acct: String
|
2022-02-17 03:12:47 +00:00
|
|
|
@NSManaged public var avatar: URL?
|
2020-05-06 22:40:12 +00:00
|
|
|
@NSManaged public var botCD: Bool
|
2020-04-12 02:23:31 +00:00
|
|
|
@NSManaged public var createdAt: Date
|
|
|
|
@NSManaged public var displayName: String
|
|
|
|
@NSManaged private var emojisData: Data?
|
|
|
|
@NSManaged private var fieldsData: Data?
|
|
|
|
@NSManaged public var followersCount: Int
|
|
|
|
@NSManaged public var followingCount: Int
|
2020-10-26 03:07:41 +00:00
|
|
|
@NSManaged public var header: URL?
|
2020-04-12 02:23:31 +00:00
|
|
|
@NSManaged public var id: String
|
2020-04-11 19:31:37 +00:00
|
|
|
@NSManaged public var locked: Bool
|
2020-05-06 22:40:12 +00:00
|
|
|
@NSManaged public var movedCD: Bool
|
2020-04-12 02:23:31 +00:00
|
|
|
@NSManaged public var note: String
|
2020-05-12 01:59:46 +00:00
|
|
|
@NSManaged public var referenceCount: Int
|
2020-04-12 02:23:31 +00:00
|
|
|
@NSManaged public var statusesCount: Int
|
|
|
|
@NSManaged public var url: URL
|
|
|
|
@NSManaged public var username: String
|
2020-04-11 19:31:37 +00:00
|
|
|
@NSManaged public var movedTo: AccountMO?
|
|
|
|
|
2020-04-11 19:33:39 +00:00
|
|
|
@LazilyDecoding(arrayFrom: \AccountMO.emojisData)
|
2020-05-06 22:40:12 +00:00
|
|
|
public var emojis: [Emoji]
|
2020-04-11 19:33:39 +00:00
|
|
|
|
|
|
|
@LazilyDecoding(arrayFrom: \AccountMO.fieldsData)
|
2020-05-06 22:40:12 +00:00
|
|
|
public var fields: [Pachyderm.Account.Field]
|
|
|
|
|
|
|
|
public var bot: Bool? { botCD }
|
|
|
|
public var moved: Bool? { movedCD }
|
2020-04-11 19:33:39 +00:00
|
|
|
|
2020-05-12 01:59:46 +00:00
|
|
|
func incrementReferenceCount() {
|
|
|
|
referenceCount += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func decrementReferenceCount() {
|
|
|
|
referenceCount -= 1
|
|
|
|
if referenceCount <= 0 {
|
|
|
|
managedObjectContext!.delete(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override func prepareForDeletion() {
|
|
|
|
super.prepareForDeletion()
|
|
|
|
movedTo?.decrementReferenceCount()
|
|
|
|
}
|
|
|
|
|
2020-04-11 19:31:37 +00:00
|
|
|
}
|
2020-04-12 02:23:31 +00:00
|
|
|
|
|
|
|
extension AccountMO {
|
|
|
|
convenience init(apiAccount account: Pachyderm.Account, container: MastodonCachePersistentStore, context: NSManagedObjectContext) {
|
|
|
|
self.init(context: context)
|
|
|
|
self.updateFrom(apiAccount: account, container: container)
|
2020-05-12 01:59:46 +00:00
|
|
|
|
|
|
|
movedTo?.incrementReferenceCount()
|
2020-04-12 02:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateFrom(apiAccount account: Pachyderm.Account, container: MastodonCachePersistentStore) {
|
|
|
|
guard let context = managedObjectContext else {
|
|
|
|
// we've been deleted, don't bother updating
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.acct = account.acct
|
2020-04-12 16:54:27 +00:00
|
|
|
self.avatar = account.avatarStatic // we don't animate avatars
|
2020-05-06 22:40:12 +00:00
|
|
|
self.botCD = account.bot ?? false
|
2020-04-12 02:23:31 +00:00
|
|
|
self.createdAt = account.createdAt
|
|
|
|
self.displayName = account.displayName
|
|
|
|
self.emojis = account.emojis
|
2020-05-06 22:40:12 +00:00
|
|
|
self.fields = account.fields
|
2020-04-12 02:23:31 +00:00
|
|
|
self.followersCount = account.followersCount
|
|
|
|
self.followingCount = account.followingCount
|
2020-04-12 16:54:27 +00:00
|
|
|
self.header = account.headerStatic // we don't animate headers
|
2020-04-12 02:23:31 +00:00
|
|
|
self.id = account.id
|
|
|
|
self.locked = account.locked
|
2020-05-06 22:40:12 +00:00
|
|
|
self.movedCD = account.moved ?? false
|
2020-04-12 02:23:31 +00:00
|
|
|
self.note = account.note
|
|
|
|
self.statusesCount = account.statusesCount
|
|
|
|
self.url = account.url
|
|
|
|
self.username = account.username
|
|
|
|
if let movedTo = account.movedTo {
|
|
|
|
self.movedTo = container.account(for: movedTo.id, in: context) ?? AccountMO(apiAccount: movedTo, container: container, context: context)
|
|
|
|
} else {
|
|
|
|
self.movedTo = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|