Tusker/Tusker/Views/StatusContentTextView.swift

60 lines
2.0 KiB
Swift

//
// StatusContentTextView.swift
// Tusker
//
// Created by Shadowfacts on 1/18/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
import WebURLFoundationExtras
class StatusContentTextView: ContentTextView {
private var statusID: String?
func setTextFrom(status: some StatusProtocol, precomputed attributedText: NSAttributedString? = nil) {
statusID = status.id
if let attributedText {
self.attributedText = attributedText
} else {
setTextFromHtml(status.content)
}
setEmojis(status.emojis, identifier: status.id)
}
override func getMention(for url: URL, text: String) -> Mention? {
let mention: Mention?
if let statusID = statusID,
let mastodonController = mastodonController,
let status = mastodonController.persistentContainer.status(for: statusID) {
mention = status.mentions.first { (mention) in
url.host == mention.url.host!.serialized && (
text.dropFirst() == mention.username // Mastodon and Pleroma include @ in the text
|| text.dropFirst() == mention.acct // Misskey includes @ and uses the whole acct
|| text == mention.username // GNU Social does not include the @ in the text, so we don't need to drop it
)
}
} else {
mention = nil
}
return mention ?? super.getMention(for: url, text: text)
}
override func getHashtag(for url: URL, text: String) -> Hashtag? {
let hashtag: Hashtag?
if let statusID = statusID,
let mastodonController = mastodonController,
let status = mastodonController.persistentContainer.status(for: statusID) {
hashtag = status.hashtags.first { (hashtag) in
URL(hashtag.url) == url
}
} else {
hashtag = nil
}
return hashtag ?? super.getHashtag(for: url, text: text)
}
}