Skip to content

Commit

Permalink
Rebase onto current mastodon
Browse files Browse the repository at this point in the history
  • Loading branch information
boehs committed Apr 27, 2024
1 parent 65093c6 commit 98d0ef4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .env.production.sample
Expand Up @@ -75,3 +75,7 @@ S3_ALIAS_HOST=files.example.com
# -----------------------
IP_RETENTION_PERIOD=31556952
SESSION_RETENTION_PERIOD=31556952

# Instance Tweaks
# ---------------
MAX_POST_CHARS=500
2 changes: 1 addition & 1 deletion app/validators/status_length_validator.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class StatusLengthValidator < ActiveModel::Validator
MAX_CHARS = 500
MAX_CHARS = (ENV['MAX_POST_CHARS'] || 500).to_i
URL_PLACEHOLDER_CHARS = 23
URL_PLACEHOLDER = 'x' * 23

Expand Down
23 changes: 15 additions & 8 deletions spec/validators/status_length_validator_spec.rb
Expand Up @@ -22,26 +22,31 @@
expect(status).to_not have_received(:errors)
end

it 'adds an error when content warning is over 500 characters' do
status = instance_double(Status, spoiler_text: 'a' * 520, text: '', errors: activemodel_errors, local?: true, reblog?: false)
it 'adds an error when content warning is over MAX_CHARS characters' do
chars = StatusLengthValidator::MAX_CHARS + 1
status = instance_double(Status, spoiler_text: 'a' * chars, text: '', errors: activemodel_errors, local?: true, reblog?: false)
subject.validate(status)
expect(status.errors).to have_received(:add)
end

it 'adds an error when text is over 500 characters' do
status = instance_double(Status, spoiler_text: '', text: 'a' * 520, errors: activemodel_errors, local?: true, reblog?: false)
it 'adds an error when text is over MAX_CHARS characters' do
chars = StatusLengthValidator::MAX_CHARS + 1
status = instance_double(Status, spoiler_text: '', text: 'a' * chars, errors: activemodel_errors, local?: true, reblog?: false)
subject.validate(status)
expect(status.errors).to have_received(:add)
end

it 'adds an error when text and content warning are over 500 characters total' do
status = instance_double(Status, spoiler_text: 'a' * 250, text: 'b' * 251, errors: activemodel_errors, local?: true, reblog?: false)
it 'adds an error when text and content warning are over MAX_CHARS characters total' do
chars1 = 20
chars2 = StatusLengthValidator::MAX_CHARS + 1 - chars1
status = instance_double(Status, spoiler_text: 'a' * chars1, text: 'b' * chars2, errors: activemodel_errors, local?: true, reblog?: false)
subject.validate(status)
expect(status.errors).to have_received(:add)
end

it 'counts URLs as 23 characters flat' do
text = ('a' * 476) + " http://#{'b' * 30}.com/example"
chars = StatusLengthValidator::MAX_CHARS - 1 - 23
text = ('a' * chars) + " http://#{'b' * 30}.com/example"
status = instance_double(Status, spoiler_text: '', text: text, errors: activemodel_errors, local?: true, reblog?: false)

subject.validate(status)
Expand All @@ -64,7 +69,9 @@
end

it 'counts only the front part of remote usernames' do
text = ('a' * 475) + " @alice@#{'b' * 30}.com"
username = '@alice'
chars = StatusLengthValidator::MAX_CHARS - 1 - username.length
text = ('a' * chars) + " #{username}@#{'b' * 30}.com"
status = instance_double(Status, spoiler_text: '', text: text, errors: activemodel_errors, local?: true, reblog?: false)

subject.validate(status)
Expand Down

0 comments on commit 98d0ef4

Please sign in to comment.