Skip to content

Commit

Permalink
Attempt to match server-side length check on post content
Browse files Browse the repository at this point in the history
  • Loading branch information
j-f1 committed Nov 21, 2022
1 parent 12bfff8 commit ca33180
Showing 1 changed file with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,36 @@ extension ComposeContentViewModel {

// bind text
$content
.map { $0.count }
.assign(to: &$contentWeightedLength)
.sink { [weak self] _ in
DispatchQueue.main.async {
guard let attributedText = self?.contentMetaText?.textView.attributedText else {
self?.contentWeightedLength = 0
return
}
// Reference: StatusLengthValidator.countable_text
// not entirely accurate: Ruby code says “To ensure that we only
// give length concessions to entities that will be correctly
// parsed during formatting, we go through full entity extraction”
// but this code doesn’t

var length = 0
attributedText.enumerateAttributes(in: NSRange(location: 0, length: attributedText.string.length)) { attributes, range, _ in
// symbol is not public!
let meta = attributes[NSAttributedString.Key("MetaAttributeKey.meta")] as? Meta
switch meta {
case .url:
// StatusLengthValidator::URL_PLACEHOLDER_CHARS
length += 23
case .mention(_, mention: let mention, userInfo: _):
length += ("@" + mention.prefix(while: { $0 != "@" })).count
default:
length += range.length
}
}
self?.contentWeightedLength = length
}
}
.store(in: &disposeBag)

Publishers.CombineLatest(
$contentWarning,
Expand Down

0 comments on commit ca33180

Please sign in to comment.