Tusker/Tusker/Views/StatusContentTextView.swift

60 lines
2.0 KiB
Swift
Raw Normal View History

2018-08-26 18:49:22 +00:00
//
// StatusContentTextView.swift
2018-08-26 18:49:22 +00:00
// Tusker
//
// Created by Shadowfacts on 1/18/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
2018-08-26 18:49:22 +00:00
//
import UIKit
2018-09-11 14:52:21 +00:00
import Pachyderm
import WebURLFoundationExtras
2018-08-26 18:49:22 +00:00
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)
}
2018-08-28 01:27:34 +00:00
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
2018-08-26 19:19:54 +00:00
}
return hashtag ?? super.getHashtag(for: url, text: text)
2018-08-26 18:49:22 +00:00
}
2018-08-26 18:49:22 +00:00
}