Tusker/Tusker/Views/StatusContentTextView.swift

56 lines
1.8 KiB
Swift

//
// StatusContentTextView.swift
// Tusker
//
// Created by Shadowfacts on 1/18/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
class StatusContentTextView: ContentTextView {
var statusID: String? {
didSet {
guard let statusID = statusID else { return }
guard let mastodonController = mastodonController,
let status = mastodonController.persistentContainer.status(for: statusID) else {
fatalError("Can't set StatusContentTextView text without cached status for \(statusID)")
}
setTextFromHtml(status.content)
setEmojis(status.emojis)
}
}
override func getMention(for url: URL, text: String) -> Mention? {
let mention: Mention?
if let statusID = statusID,
let mastodonController = mastodonController,
let status = mastodonController.cache.status(for: statusID) {
mention = status.mentions.first { (mention) in
// Mastodon and Pleroma include the @ in the <a> text, GNU Social does not
(text.dropFirst() == mention.username || text == mention.username) && url.host == mention.url.host
}
} 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.cache.status(for: statusID) {
hashtag = status.hashtags.first { (hashtag) in
hashtag.url == url
}
} else {
hashtag = nil
}
return hashtag ?? super.getHashtag(for: url, text: text)
}
}