Tusker/Tusker/Views/StatusContentTextView.swift

56 lines
1.8 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
2018-08-26 18:49:22 +00:00
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)
2019-02-10 02:12:31 +00:00
setEmojis(status.emojis)
}
}
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.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
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
}