2017-01-25 00:49:08 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class StatusLengthValidator < ActiveModel::Validator
|
2022-02-06 22:22:28 +01:00
|
|
|
MAX_CHARS = 840
|
2021-07-10 17:58:41 +02:00
|
|
|
URL_PLACEHOLDER_CHARS = 23
|
2022-03-22 09:51:46 +01:00
|
|
|
URL_PLACEHOLDER = 'x' * 23
|
2017-01-25 00:49:08 +01:00
|
|
|
|
|
|
|
def validate(status)
|
|
|
|
return unless status.local? && !status.reblog?
|
2018-12-28 08:18:47 +01:00
|
|
|
|
2022-03-22 09:51:46 +01:00
|
|
|
status.errors.add(:text, I18n.t('statuses.over_character_limit', max: MAX_CHARS)) if too_long?(status)
|
2017-07-29 00:06:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-03-22 09:51:46 +01:00
|
|
|
def too_long?(status)
|
|
|
|
countable_length(combined_text(status)) > MAX_CHARS
|
2017-07-29 00:06:29 +02:00
|
|
|
end
|
|
|
|
|
2022-03-22 09:51:46 +01:00
|
|
|
def countable_length(str)
|
|
|
|
str.mb_chars.grapheme_length
|
2017-07-29 00:06:29 +02:00
|
|
|
end
|
|
|
|
|
2022-03-22 09:51:46 +01:00
|
|
|
def combined_text(status)
|
|
|
|
[status.spoiler_text, countable_text(status.text)].join
|
2017-07-29 00:06:29 +02:00
|
|
|
end
|
|
|
|
|
2022-03-22 09:51:46 +01:00
|
|
|
def countable_text(str)
|
|
|
|
return '' if str.blank?
|
2018-02-04 12:32:41 +01:00
|
|
|
|
2022-03-22 09:51:46 +01:00
|
|
|
# To ensure that we only give length concessions to entities that
|
|
|
|
# will be correctly parsed during formatting, we go through full
|
|
|
|
# entity extraction
|
|
|
|
|
|
|
|
entities = Extractor.remove_overlapping_entities(Extractor.extract_urls_with_indices(str, extract_url_without_protocol: false) + Extractor.extract_mentions_or_lists_with_indices(str))
|
|
|
|
|
|
|
|
rewrite_entities(str, entities) do |entity|
|
|
|
|
if entity[:url]
|
|
|
|
URL_PLACEHOLDER
|
|
|
|
elsif entity[:screen_name]
|
|
|
|
"@#{entity[:screen_name].split('@').first}"
|
|
|
|
end
|
2017-07-29 00:06:29 +02:00
|
|
|
end
|
2017-01-25 00:49:08 +01:00
|
|
|
end
|
2022-03-22 09:51:46 +01:00
|
|
|
|
|
|
|
def rewrite_entities(str, entities)
|
|
|
|
entities.sort_by! { |entity| entity[:indices].first }
|
2023-07-28 23:11:05 +02:00
|
|
|
result = +''
|
2022-03-22 09:51:46 +01:00
|
|
|
|
|
|
|
last_index = entities.reduce(0) do |index, entity|
|
|
|
|
result << str[index...entity[:indices].first]
|
|
|
|
result << yield(entity)
|
|
|
|
entity[:indices].last
|
|
|
|
end
|
|
|
|
|
2023-07-12 10:03:06 +02:00
|
|
|
result << str[last_index..]
|
2022-03-22 09:51:46 +01:00
|
|
|
result
|
|
|
|
end
|
2017-01-25 00:49:08 +01:00
|
|
|
end
|