mastodon-ios/Mastodon/Extension/CoreDataStack/Status.swift

99 lines
2.5 KiB
Swift
Raw Normal View History

//
2021-04-01 08:39:15 +02:00
// Status.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021/2/4.
//
import CoreDataStack
2021-04-30 08:55:02 +02:00
import Foundation
import MastodonSDK
2021-04-01 08:39:15 +02:00
extension Status.Property {
init(entity: Mastodon.Entity.Status, domain: String, networkDate: Date) {
self.init(
domain: domain,
id: entity.id,
uri: entity.uri,
createdAt: entity.createdAt,
content: entity.content!,
visibility: entity.visibility?.rawValue,
sensitive: entity.sensitive ?? false,
spoilerText: entity.spoilerText,
emojisData: entity.emojis.flatMap { Status.encode(emojis: $0) },
reblogsCount: NSNumber(value: entity.reblogsCount),
favouritesCount: NSNumber(value: entity.favouritesCount),
2021-02-04 07:57:16 +01:00
repliesCount: entity.repliesCount.flatMap { NSNumber(value: $0) },
url: entity.url ?? entity.uri,
inReplyToID: entity.inReplyToID,
inReplyToAccountID: entity.inReplyToAccountID,
language: entity.language,
text: entity.text,
networkDate: networkDate
)
}
}
extension Status {
enum SensitiveType {
case none
case all
case media(isSensitive: Bool)
}
var sensitiveType: SensitiveType {
let spoilerText = self.spoilerText ?? ""
// cast .all sensitive when has spoiter text
if !spoilerText.isEmpty {
return .all
}
if let firstAttachment = mediaAttachments?.first {
// cast .media when has non audio media
if firstAttachment.type != .audio {
return .media(isSensitive: sensitive)
} else {
return .none
}
}
// not sensitive
return .none
}
}
2021-04-29 04:50:10 +02:00
extension Status {
var authorForUserProvider: MastodonUser {
let author = (reblog ?? self).author
return author
}
}
2021-04-29 05:03:21 +02:00
extension Status {
var statusURL: URL {
2021-04-29 09:51:52 +02:00
if let urlString = self.url,
2021-04-30 08:55:02 +02:00
let url = URL(string: urlString)
{
2021-04-29 09:51:52 +02:00
return url
} else {
return URL(string: "https://\(self.domain)/web/statuses/\(self.id)")!
}
2021-04-29 05:03:21 +02:00
}
var activityItems: [Any] {
var items: [Any] = []
2021-04-30 08:55:02 +02:00
items.append(self.statusURL)
2021-04-29 05:03:21 +02:00
return items
}
}
extension Status: EmojiContainer { }
extension Status {
var visibilityEnum: Mastodon.Entity.Status.Visibility? {
return visibility.flatMap { Mastodon.Entity.Status.Visibility(rawValue: $0) }
}
}