Module: LongPost::StatusLengthValidatorPatch

Includes:
PatchworkHelper
Defined in:
app/validators/long_post/status_length_validator_patch.rb

Constant Summary collapse

DEFAULT_MAX_CHARS =
500

Class Method Summary collapse

Methods included from PatchworkHelper

#patchwork_community_admin_exist?, #patchwork_server_settings_exist?, #patchwork_table_exists?

Class Method Details

.prepended(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/validators/long_post/status_length_validator_patch.rb', line 11

def self.prepended(base)
  base.class_eval do
    def validate(status)
      return unless status.local? && !status.reblog?
      max_chars = get_max_chars
      status.errors.add(:text, I18n.t('statuses.over_character_limit', max: max_chars)) if too_long?(status)
    end

    private

    def too_long?(status)
      max_chars = get_max_chars
      countable_length(combined_text(status)) > max_chars
    end

    def get_max_chars
      return DEFAULT_MAX_CHARS unless patchwork_server_settings_exist?

      begin
        long_post = NewsmastMastodon::ServerSetting.get_long_post('Long posts')

        return DEFAULT_MAX_CHARS if long_post.nil?
        return DEFAULT_MAX_CHARS unless long_post.value

        optional_value = long_post.optional_value
        return DEFAULT_MAX_CHARS if optional_value.nil? || optional_value.to_s.strip.empty?

        max_chars = optional_value.to_i
        return DEFAULT_MAX_CHARS if max_chars <= 0

        max_chars
      rescue ActiveRecord::RecordNotFound
        DEFAULT_MAX_CHARS
      rescue ActiveRecord::StatementInvalid
        DEFAULT_MAX_CHARS
      rescue NoMethodError
        DEFAULT_MAX_CHARS
      rescue StandardError
        DEFAULT_MAX_CHARS
      end
    end
  end
end