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

161 lines
6.2 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-10-14 00:28:27 +00:00
struct StatusRecord: ContentDatabaseRecord, 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?
2021-01-31 21:59:26 +00:00
let url: String?
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 {
2020-11-13 00:13:09 +00:00
static let id = Column(CodingKeys.id)
static let uri = Column(CodingKeys.uri)
static let createdAt = Column(CodingKeys.createdAt)
static let accountId = Column(CodingKeys.accountId)
static let content = Column(CodingKeys.content)
static let visibility = Column(CodingKeys.visibility)
static let sensitive = Column(CodingKeys.sensitive)
static let spoilerText = Column(CodingKeys.spoilerText)
static let mediaAttachments = Column(CodingKeys.mediaAttachments)
static let mentions = Column(CodingKeys.mentions)
static let tags = Column(CodingKeys.tags)
static let emojis = Column(CodingKeys.emojis)
static let reblogsCount = Column(CodingKeys.reblogsCount)
static let favouritesCount = Column(CodingKeys.favouritesCount)
static let repliesCount = Column(CodingKeys.repliesCount)
static let application = Column(CodingKeys.application)
static let url = Column(CodingKeys.url)
static let inReplyToId = Column(CodingKeys.inReplyToId)
static let inReplyToAccountId = Column(CodingKeys.inReplyToAccountId)
static let reblogId = Column(CodingKeys.reblogId)
static let poll = Column(CodingKeys.poll)
static let card = Column(CodingKeys.card)
static let language = Column(CodingKeys.language)
static let text = Column(CodingKeys.text)
static let favourited = Column(CodingKeys.favourited)
static let reblogged = Column(CodingKeys.reblogged)
static let muted = Column(CodingKeys.muted)
static let bookmarked = Column(CodingKeys.bookmarked)
static let pinned = Column(CodingKeys.pinned)
2020-09-29 06:06:25 +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)
2021-02-08 01:46:51 +00:00
static let relationship = hasOne(Relationship.self,
through: Self.account,
using: AccountRecord.relationship)
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)
2021-02-08 01:46:51 +00:00
static let reblogRelationship = hasOne(
Relationship.self,
through: Self.reblog,
using: Self.relationship)
2020-10-14 00:03:01 +00:00
static let showContentToggle = hasOne(StatusShowContentToggle.self)
static let reblogShowContentToggle = hasOne(
StatusShowContentToggle.self,
2020-10-07 21:06:26 +00:00
through: Self.reblog,
2020-10-14 00:03:01 +00:00
using: Self.showContentToggle)
static let showAttachmentsToggle = hasOne(StatusShowAttachmentsToggle.self)
static let reblogShowAttachmentsToggle = hasOne(
StatusShowAttachmentsToggle.self,
through: Self.reblog,
using: Self.showAttachmentsToggle)
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]))
2021-01-31 15:49:09 +00:00
.order(StatusAncestorJoin.Columns.order)
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]))
2021-01-31 15:49:09 +00:00
.order(StatusDescendantJoin.Columns.order)
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
}
}