metatext/DB/Sources/DB/Content/StatusRecord.swift

159 lines
6.3 KiB
Swift
Raw Normal View History

2020-09-03 03:28:34 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import GRDB
import Mastodon
2020-09-05 02:05:15 +00:00
struct StatusRecord: Codable, Hashable {
2020-10-05 22:50:05 +00:00
let id: Status.Id
2020-09-03 03:28:34 +00:00
let uri: String
let createdAt: Date
2020-10-05 22:50:05 +00:00
let accountId: Account.Id
2020-09-03 03:28:34 +00:00
let content: HTML
let visibility: Status.Visibility
let sensitive: Bool
let spoilerText: String
let mediaAttachments: [Attachment]
let mentions: [Mention]
let tags: [Tag]
let emojis: [Emoji]
let reblogsCount: Int
let favouritesCount: Int
let repliesCount: Int
let application: Application?
let url: URL?
2020-10-05 22:50:05 +00:00
let inReplyToId: Status.Id?
let inReplyToAccountId: Account.Id?
let reblogId: Status.Id?
2020-09-03 03:28:34 +00:00
let poll: Poll?
let card: Card?
let language: String?
let text: String?
let favourited: Bool
let reblogged: Bool
let muted: Bool
let bookmarked: Bool
let pinned: Bool?
}
2020-09-29 06:06:25 +00:00
extension StatusRecord {
enum Columns {
static let id = Column(StatusRecord.CodingKeys.id)
static let uri = Column(StatusRecord.CodingKeys.uri)
static let createdAt = Column(StatusRecord.CodingKeys.createdAt)
static let accountId = Column(StatusRecord.CodingKeys.accountId)
static let content = Column(StatusRecord.CodingKeys.content)
static let visibility = Column(StatusRecord.CodingKeys.visibility)
static let sensitive = Column(StatusRecord.CodingKeys.sensitive)
static let spoilerText = Column(StatusRecord.CodingKeys.spoilerText)
static let mediaAttachments = Column(StatusRecord.CodingKeys.mediaAttachments)
static let mentions = Column(StatusRecord.CodingKeys.mentions)
static let tags = Column(StatusRecord.CodingKeys.tags)
static let emojis = Column(StatusRecord.CodingKeys.emojis)
static let reblogsCount = Column(StatusRecord.CodingKeys.reblogsCount)
static let favouritesCount = Column(StatusRecord.CodingKeys.favouritesCount)
static let repliesCount = Column(StatusRecord.CodingKeys.repliesCount)
static let application = Column(StatusRecord.CodingKeys.application)
static let url = Column(StatusRecord.CodingKeys.url)
static let inReplyToId = Column(StatusRecord.CodingKeys.inReplyToId)
static let inReplyToAccountId = Column(StatusRecord.CodingKeys.inReplyToAccountId)
static let reblogId = Column(StatusRecord.CodingKeys.reblogId)
static let poll = Column(StatusRecord.CodingKeys.poll)
static let card = Column(StatusRecord.CodingKeys.card)
static let language = Column(StatusRecord.CodingKeys.language)
static let text = Column(StatusRecord.CodingKeys.text)
static let favourited = Column(StatusRecord.CodingKeys.favourited)
static let reblogged = Column(StatusRecord.CodingKeys.reblogged)
static let muted = Column(StatusRecord.CodingKeys.muted)
static let bookmarked = Column(StatusRecord.CodingKeys.bookmarked)
static let pinned = Column(StatusRecord.CodingKeys.pinned)
}
}
2020-09-05 02:05:15 +00:00
extension StatusRecord: FetchableRecord, PersistableRecord {
2020-09-03 03:28:34 +00:00
static func databaseJSONDecoder(for column: String) -> JSONDecoder {
MastodonDecoder()
2020-09-03 03:28:34 +00:00
}
static func databaseJSONEncoder(for column: String) -> JSONEncoder {
MastodonEncoder()
2020-09-03 03:28:34 +00:00
}
}
2020-09-05 02:05:15 +00:00
extension StatusRecord {
2020-09-29 23:44:31 +00:00
static let account = belongsTo(AccountRecord.self)
2020-09-05 02:05:15 +00:00
static let accountMoved = hasOne(AccountRecord.self,
through: Self.account,
2020-09-29 23:44:31 +00:00
using: AccountRecord.moved)
2020-09-05 02:05:15 +00:00
static let reblogAccount = hasOne(AccountRecord.self,
through: Self.reblog,
2020-09-29 23:44:31 +00:00
using: Self.account)
2020-09-05 02:05:15 +00:00
static let reblogAccountMoved = hasOne(AccountRecord.self,
through: Self.reblogAccount,
2020-09-29 23:44:31 +00:00
using: AccountRecord.moved)
static let reblog = belongsTo(StatusRecord.self)
2020-10-07 21:06:26 +00:00
static let showMoreToggle = hasOne(StatusShowMoreToggle.self)
static let reblogShowMoreToggle = hasOne(
StatusShowMoreToggle.self,
through: Self.reblog,
using: Self.showMoreToggle)
2020-09-29 06:06:25 +00:00
static let ancestorJoins = hasMany(
2020-10-03 09:19:05 +00:00
StatusAncestorJoin.self,
using: ForeignKey([StatusAncestorJoin.Columns.parentId]))
.order(StatusAncestorJoin.Columns.index)
2020-09-29 06:06:25 +00:00
static let descendantJoins = hasMany(
2020-10-03 09:19:05 +00:00
StatusDescendantJoin.self,
using: ForeignKey([StatusDescendantJoin.Columns.parentId]))
.order(StatusDescendantJoin.Columns.index)
2020-09-05 02:05:15 +00:00
static let ancestors = hasMany(StatusRecord.self,
2020-09-03 03:28:34 +00:00
through: ancestorJoins,
2020-10-03 09:19:05 +00:00
using: StatusAncestorJoin.status)
2020-09-05 02:05:15 +00:00
static let descendants = hasMany(StatusRecord.self,
2020-09-03 03:28:34 +00:00
through: descendantJoins,
2020-10-03 09:19:05 +00:00
using: StatusDescendantJoin.status)
2020-09-03 03:28:34 +00:00
2020-09-29 23:56:09 +00:00
var ancestors: QueryInterfaceRequest<StatusInfo> {
StatusInfo.request(request(for: Self.ancestors))
2020-09-03 03:28:34 +00:00
}
2020-09-29 23:56:09 +00:00
var descendants: QueryInterfaceRequest<StatusInfo> {
StatusInfo.request(request(for: Self.descendants))
2020-09-03 03:28:34 +00:00
}
var filterableContent: [String] {
[content.attributed.string, spoilerText] + (poll?.options.map(\.title) ?? [])
}
2020-09-03 03:28:34 +00:00
init(status: Status) {
id = status.id
uri = status.uri
createdAt = status.createdAt
accountId = status.account.id
content = status.content
visibility = status.visibility
sensitive = status.sensitive
spoilerText = status.spoilerText
mediaAttachments = status.mediaAttachments
mentions = status.mentions
tags = status.tags
emojis = status.emojis
reblogsCount = status.reblogsCount
favouritesCount = status.favouritesCount
repliesCount = status.repliesCount
application = status.application
url = status.url
inReplyToId = status.inReplyToId
inReplyToAccountId = status.inReplyToAccountId
reblogId = status.reblog?.id
poll = status.poll
card = status.card
language = status.language
text = status.text
favourited = status.favourited
reblogged = status.reblogged
muted = status.muted
bookmarked = status.bookmarked
pinned = status.pinned
}
}