2021-01-27 07:50:13 +01:00
|
|
|
//
|
2021-01-28 09:10:30 +01:00
|
|
|
// Toot.swift
|
2021-01-27 07:50:13 +01:00
|
|
|
// CoreDataStack
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021/1/27.
|
|
|
|
//
|
|
|
|
|
|
|
|
import CoreData
|
2021-02-01 11:05:34 +01:00
|
|
|
import Foundation
|
2021-01-27 07:50:13 +01:00
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
public final class Toot: NSManagedObject {
|
2021-01-27 07:50:13 +01:00
|
|
|
public typealias ID = String
|
|
|
|
@NSManaged public private(set) var identifier: ID
|
|
|
|
@NSManaged public private(set) var domain: String
|
|
|
|
|
|
|
|
@NSManaged public private(set) var id: String
|
2021-02-01 11:05:34 +01:00
|
|
|
@NSManaged public private(set) var uri: String
|
|
|
|
@NSManaged public private(set) var createdAt: Date
|
2021-01-27 07:50:13 +01:00
|
|
|
@NSManaged public private(set) var content: String
|
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
@NSManaged public private(set) var visibility: String?
|
|
|
|
@NSManaged public private(set) var sensitive: Bool
|
|
|
|
@NSManaged public private(set) var spoilerText: String?
|
|
|
|
|
|
|
|
// Informational
|
2021-02-02 07:10:25 +01:00
|
|
|
@NSManaged public private(set) var reblogsCount: NSNumber
|
|
|
|
@NSManaged public private(set) var favouritesCount: NSNumber
|
|
|
|
@NSManaged public private(set) var repliesCount: NSNumber?
|
2021-02-01 11:05:34 +01:00
|
|
|
|
|
|
|
@NSManaged public private(set) var url: String?
|
|
|
|
@NSManaged public private(set) var inReplyToID: Toot.ID?
|
|
|
|
@NSManaged public private(set) var inReplyToAccountID: MastodonUser.ID?
|
2021-02-02 07:10:25 +01:00
|
|
|
|
|
|
|
@NSManaged public private(set) var language: String? // (ISO 639 Part 1 two-letter language code)
|
2021-02-01 11:05:34 +01:00
|
|
|
@NSManaged public private(set) var text: String?
|
|
|
|
|
2021-02-02 07:10:25 +01:00
|
|
|
// many-to-one relastionship
|
|
|
|
@NSManaged public private(set) var favouritedBy: MastodonUser?
|
|
|
|
@NSManaged public private(set) var rebloggedBy: MastodonUser?
|
|
|
|
@NSManaged public private(set) var mutedBy: MastodonUser?
|
|
|
|
@NSManaged public private(set) var bookmarkedBy: MastodonUser?
|
|
|
|
|
|
|
|
// one-to-one relastionship
|
|
|
|
@NSManaged public private(set) var pinnedBy: MastodonUser?
|
|
|
|
|
2021-01-27 07:50:13 +01:00
|
|
|
@NSManaged public private(set) var updatedAt: Date
|
2021-01-28 09:10:30 +01:00
|
|
|
@NSManaged public private(set) var deletedAt: Date?
|
2021-01-27 07:50:13 +01:00
|
|
|
|
2021-02-02 07:49:55 +01:00
|
|
|
// one-to-many relationship
|
|
|
|
@NSManaged public private(set) var reblogFrom: Set<Toot>?
|
|
|
|
|
2021-02-02 07:10:25 +01:00
|
|
|
// one-to-many relationship
|
|
|
|
@NSManaged public private(set) var mentions: Set<Mention>?
|
|
|
|
// one-to-many relationship
|
|
|
|
@NSManaged public private(set) var emojis: Set<Emoji>?
|
2021-02-02 07:49:55 +01:00
|
|
|
|
2021-02-02 07:10:25 +01:00
|
|
|
// one-to-many relationship
|
|
|
|
@NSManaged public private(set) var tags: Set<Tag>?
|
|
|
|
|
|
|
|
// many-to-one relastionship
|
|
|
|
@NSManaged public private(set) var reblog: Toot?
|
|
|
|
|
2021-01-27 07:50:13 +01:00
|
|
|
// many-to-one relationship
|
|
|
|
@NSManaged public private(set) var author: MastodonUser
|
|
|
|
|
|
|
|
// one-to-many relationship
|
|
|
|
@NSManaged public private(set) var homeTimelineIndexes: Set<HomeTimelineIndex>?
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
public extension Toot {
|
2021-01-27 07:50:13 +01:00
|
|
|
@discardableResult
|
2021-02-01 11:05:34 +01:00
|
|
|
static func insert(
|
2021-01-27 07:50:13 +01:00
|
|
|
into context: NSManagedObjectContext,
|
|
|
|
property: Property,
|
|
|
|
author: MastodonUser
|
2021-01-28 09:10:30 +01:00
|
|
|
) -> Toot {
|
2021-02-01 11:05:34 +01:00
|
|
|
let toot: Toot = context.insertObject()
|
2021-01-27 07:50:13 +01:00
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
toot.identifier = property.identifier
|
|
|
|
toot.domain = property.domain
|
|
|
|
|
|
|
|
toot.id = property.id
|
|
|
|
toot.uri = property.uri
|
|
|
|
toot.createdAt = property.createdAt
|
|
|
|
toot.content = property.content
|
2021-01-27 07:50:13 +01:00
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
toot.visibility = property.visibility
|
|
|
|
toot.sensitive = property.sensitive
|
|
|
|
toot.spoilerText = property.spoilerText
|
2021-01-27 07:50:13 +01:00
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
if let mentions = property.mentions {
|
|
|
|
toot.mutableSetValue(forKey: #keyPath(Toot.mentions)).addObjects(from: mentions)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let emojis = property.emojis {
|
|
|
|
toot.mutableSetValue(forKey: #keyPath(Toot.mentions)).addObjects(from: emojis)
|
|
|
|
}
|
2021-01-27 07:50:13 +01:00
|
|
|
|
2021-02-02 07:10:25 +01:00
|
|
|
if let tags = property.tags {
|
|
|
|
toot.mutableSetValue(forKey: #keyPath(Toot.tags)).addObjects(from: tags)
|
|
|
|
}
|
2021-01-27 07:50:13 +01:00
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
toot.reblogsCount = property.reblogsCount
|
|
|
|
toot.favouritesCount = property.favouritesCount
|
|
|
|
toot.repliesCount = property.repliesCount
|
2021-01-27 07:50:13 +01:00
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
toot.url = property.url
|
|
|
|
toot.inReplyToID = property.inReplyToID
|
|
|
|
toot.inReplyToAccountID = property.inReplyToAccountID
|
|
|
|
toot.reblog = property.reblog
|
|
|
|
toot.language = property.language
|
|
|
|
toot.text = property.text
|
2021-01-27 07:50:13 +01:00
|
|
|
|
2021-02-02 07:10:25 +01:00
|
|
|
if let favouritedBy = property.favouritedBy {
|
|
|
|
toot.mutableSetValue(forKey: #keyPath(Toot.favouritedBy)).add(favouritedBy)
|
|
|
|
}
|
|
|
|
if let rebloggedBy = property.rebloggedBy {
|
|
|
|
toot.mutableSetValue(forKey: #keyPath(Toot.rebloggedBy)).add(rebloggedBy)
|
|
|
|
}
|
|
|
|
if let mutedBy = property.mutedBy {
|
|
|
|
toot.mutableSetValue(forKey: #keyPath(Toot.mutedBy)).add(mutedBy)
|
|
|
|
}
|
|
|
|
if let bookmarkedBy = property.bookmarkedBy {
|
|
|
|
toot.mutableSetValue(forKey: #keyPath(Toot.bookmarkedBy)).add(bookmarkedBy)
|
|
|
|
}
|
|
|
|
if let pinnedBy = property.pinnedBy {
|
|
|
|
toot.mutableSetValue(forKey: #keyPath(Toot.pinnedBy))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
toot.updatedAt = property.updatedAt
|
|
|
|
toot.deletedAt = property.deletedAt
|
|
|
|
toot.author = property.author
|
|
|
|
toot.content = property.content
|
|
|
|
toot.homeTimelineIndexes = property.homeTimelineIndexes
|
|
|
|
|
|
|
|
return toot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public extension Toot {
|
|
|
|
struct Property {
|
2021-01-27 07:50:13 +01:00
|
|
|
public init(
|
|
|
|
domain: String,
|
2021-02-01 11:05:34 +01:00
|
|
|
id: String,
|
|
|
|
uri: String,
|
2021-01-27 07:50:13 +01:00
|
|
|
createdAt: Date,
|
2021-02-01 11:05:34 +01:00
|
|
|
content: String,
|
|
|
|
visibility: String?,
|
|
|
|
sensitive: Bool,
|
|
|
|
spoilerText: String?,
|
|
|
|
mentions: [Mention]?,
|
|
|
|
emojis: [Emoji]?,
|
2021-02-02 07:10:25 +01:00
|
|
|
tags: [Tag]?,
|
|
|
|
reblogsCount: NSNumber,
|
|
|
|
favouritesCount: NSNumber,
|
|
|
|
repliesCount: NSNumber?,
|
2021-02-01 11:05:34 +01:00
|
|
|
url: String?,
|
|
|
|
inReplyToID: Toot.ID?,
|
|
|
|
inReplyToAccountID: MastodonUser.ID?,
|
|
|
|
reblog: Toot?,
|
|
|
|
language: String?,
|
|
|
|
text: String?,
|
2021-02-02 07:10:25 +01:00
|
|
|
favouritedBy: MastodonUser?,
|
|
|
|
rebloggedBy: MastodonUser?,
|
|
|
|
mutedBy: MastodonUser?,
|
|
|
|
bookmarkedBy: MastodonUser?,
|
|
|
|
pinnedBy: MastodonUser?,
|
2021-02-01 11:05:34 +01:00
|
|
|
updatedAt: Date,
|
|
|
|
deletedAt: Date?,
|
|
|
|
author: MastodonUser,
|
|
|
|
homeTimelineIndexes: Set<HomeTimelineIndex>?)
|
|
|
|
{
|
2021-01-27 07:50:13 +01:00
|
|
|
self.identifier = id + "@" + domain
|
|
|
|
self.domain = domain
|
|
|
|
self.id = id
|
2021-02-01 11:05:34 +01:00
|
|
|
self.uri = uri
|
2021-01-27 07:50:13 +01:00
|
|
|
self.createdAt = createdAt
|
2021-02-01 11:05:34 +01:00
|
|
|
self.content = content
|
|
|
|
self.visibility = visibility
|
|
|
|
self.sensitive = sensitive
|
|
|
|
self.spoilerText = spoilerText
|
|
|
|
self.mentions = mentions
|
|
|
|
self.emojis = emojis
|
2021-02-02 07:10:25 +01:00
|
|
|
self.tags = tags
|
2021-02-01 11:05:34 +01:00
|
|
|
self.reblogsCount = reblogsCount
|
|
|
|
self.favouritesCount = favouritesCount
|
|
|
|
self.repliesCount = repliesCount
|
|
|
|
self.url = url
|
|
|
|
self.inReplyToID = inReplyToID
|
|
|
|
self.inReplyToAccountID = inReplyToAccountID
|
|
|
|
self.reblog = reblog
|
|
|
|
self.language = language
|
|
|
|
self.text = text
|
2021-02-02 07:10:25 +01:00
|
|
|
self.favouritedBy = favouritedBy
|
|
|
|
self.rebloggedBy = rebloggedBy
|
|
|
|
self.mutedBy = mutedBy
|
|
|
|
self.bookmarkedBy = bookmarkedBy
|
|
|
|
self.pinnedBy = pinnedBy
|
2021-02-01 11:05:34 +01:00
|
|
|
self.updatedAt = updatedAt
|
|
|
|
self.deletedAt = deletedAt
|
|
|
|
self.author = author
|
|
|
|
self.homeTimelineIndexes = homeTimelineIndexes
|
2021-01-27 07:50:13 +01:00
|
|
|
}
|
2021-02-01 11:05:34 +01:00
|
|
|
|
|
|
|
public let identifier: ID
|
|
|
|
public let domain: String
|
|
|
|
|
|
|
|
public let id: String
|
|
|
|
public let uri: String
|
|
|
|
public let createdAt: Date
|
|
|
|
public let content: String
|
|
|
|
|
|
|
|
public let visibility: String?
|
|
|
|
public let sensitive: Bool
|
|
|
|
public let spoilerText: String?
|
|
|
|
|
|
|
|
public let mentions: [Mention]?
|
|
|
|
public let emojis: [Emoji]?
|
2021-02-02 07:10:25 +01:00
|
|
|
public let tags: [Tag]?
|
|
|
|
public let reblogsCount: NSNumber
|
|
|
|
public let favouritesCount: NSNumber
|
|
|
|
public let repliesCount: NSNumber?
|
2021-02-01 11:05:34 +01:00
|
|
|
|
|
|
|
public let url: String?
|
|
|
|
public let inReplyToID: Toot.ID?
|
|
|
|
public let inReplyToAccountID: MastodonUser.ID?
|
|
|
|
public let reblog: Toot?
|
2021-02-02 07:10:25 +01:00
|
|
|
public let language: String? // (ISO 639 Part @1 two-letter language code)
|
2021-02-01 11:05:34 +01:00
|
|
|
public let text: String?
|
|
|
|
|
2021-02-02 07:10:25 +01:00
|
|
|
public let favouritedBy: MastodonUser?
|
|
|
|
public let rebloggedBy: MastodonUser?
|
|
|
|
public let mutedBy: MastodonUser?
|
|
|
|
public let bookmarkedBy: MastodonUser?
|
|
|
|
public let pinnedBy: MastodonUser?
|
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
public let updatedAt: Date
|
|
|
|
public let deletedAt: Date?
|
|
|
|
|
|
|
|
public let author: MastodonUser
|
|
|
|
|
|
|
|
public let homeTimelineIndexes: Set<HomeTimelineIndex>?
|
2021-01-27 07:50:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 09:10:30 +01:00
|
|
|
extension Toot: Managed {
|
2021-01-27 07:50:13 +01:00
|
|
|
public static var defaultSortDescriptors: [NSSortDescriptor] {
|
2021-01-28 09:10:30 +01:00
|
|
|
return [NSSortDescriptor(keyPath: \Toot.createdAt, ascending: false)]
|
2021-01-27 07:50:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
public extension Toot {
|
|
|
|
static func predicate(idStr: String) -> NSPredicate {
|
2021-01-28 09:10:30 +01:00
|
|
|
return NSPredicate(format: "%K == %@", #keyPath(Toot.id), idStr)
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
static func predicate(idStrs: [String]) -> NSPredicate {
|
2021-01-28 09:10:30 +01:00
|
|
|
return NSPredicate(format: "%K IN %@", #keyPath(Toot.id), idStrs)
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
static func notDeleted() -> NSPredicate {
|
2021-01-28 09:10:30 +01:00
|
|
|
return NSPredicate(format: "%K == nil", #keyPath(Toot.deletedAt))
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
static func deleted() -> NSPredicate {
|
2021-01-28 09:10:30 +01:00
|
|
|
return NSPredicate(format: "%K != nil", #keyPath(Toot.deletedAt))
|
|
|
|
}
|
|
|
|
}
|