Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make alt text limit an .env parameter #16

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .env.production.sample
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,12 @@ [email protected]
# Various ways to customize Mastodon's behavior
# ---------------

# Maximum allowed character count
# Maximum allowed character count in posts
MAX_TOOT_CHARS=500

# Maximum allowed character count in alt text
MAX_ALT_TEXT_CHARS=1500

# Maximum number of pinned posts
MAX_PINNED_TOOTS=5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import tesseractCorePath from 'tesseract.js-core/tesseract-core.wasm.js';
// eslint-disable-next-line import/extensions
import tesseractWorkerPath from 'tesseract.js/dist/worker.min.js';
import { assetHost } from 'flavours/glitch/utils/config';
import { maxAltTextChars } from 'flavours/glitch/initial_state';

const messages = defineMessages({
close: { id: 'lightbox.close', defaultMessage: 'Close' },
Expand Down Expand Up @@ -361,7 +362,7 @@ class FocalPointModal extends ImmutablePureComponent {

<div className='setting-text__toolbar'>
<button disabled={detecting || media.get('type') !== 'image' || is_changing_upload} className='link-button' onClick={this.handleTextDetection}><FormattedMessage id='upload_modal.detect_text' defaultMessage='Detect text from picture' /></button>
<CharacterCounter max={1500} text={detecting ? '' : description} />
<CharacterCounter max={maxAltTextChars} text={detecting ? '' : description} />
</div>

<Button disabled={!dirty || detecting || isUploadingThumbnail || length(description) > 1500 || is_changing_upload} text={intl.formatMessage(is_changing_upload ? messages.applying : messages.apply)} onClick={this.handleSubmit} />
Expand Down
1 change: 1 addition & 0 deletions app/javascript/flavours/glitch/initial_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export const statusPageUrl = getMeta('status_page_url');

// Glitch-soc-specific settings
export const maxChars = (initialState && initialState.max_toot_chars) || 500;
export const maxAltTextChars = (initialState && initialState.max_alt_text_chars) || 1_500;
export const favouriteModal = getMeta('favourite_modal');
export const pollLimits = (initialState && initialState.poll_limits);
export const defaultContentType = getMeta('default_content_type');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import tesseractCorePath from 'tesseract.js-core/tesseract-core.wasm.js';
// eslint-disable-next-line import/extensions
import tesseractWorkerPath from 'tesseract.js/dist/worker.min.js';
import { assetHost } from 'mastodon/utils/config';
import { maxAltTextChars } from '../../../initial_state';

const messages = defineMessages({
close: { id: 'lightbox.close', defaultMessage: 'Close' },
Expand Down Expand Up @@ -369,7 +370,7 @@ class FocalPointModal extends ImmutablePureComponent {
>
<FormattedMessage id='upload_modal.detect_text' defaultMessage='Detect text from picture' />
</button>
<CharacterCounter max={1500} text={detecting ? '' : description} />
<CharacterCounter max={maxAltTextChars} text={detecting ? '' : description} />
</div>

<Button
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/mastodon/initial_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,7 @@ export const statusPageUrl = getMeta('status_page_url');

// Glitch-soc-specific settings
export const maxChars = (initialState && initialState.max_toot_chars) || 500;
export const maxAltTextChars = (initialState && initialState.max_alt_text_chars) || 1_500;


export default initialState;
4 changes: 2 additions & 2 deletions app/models/media_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MediaAttachment < ApplicationRecord
enum type: { :image => 0, :gifv => 1, :video => 2, :unknown => 3, :audio => 4 }
enum processing: { :queued => 0, :in_progress => 1, :complete => 2, :failed => 3 }, _prefix: true

MAX_DESCRIPTION_LENGTH = 1_500
MAX_DESCRIPTION_LENGTH = (ENV['MAX_ALT_TEXT_CHARS'] || 1_500).to_i

IMAGE_LIMIT = (ENV['MAX_IMAGE_SIZE'] || 10.megabytes).to_i
VIDEO_LIMIT = (ENV['MAX_VIDEO_SIZE'] || 40.megabytes).to_i
Expand Down Expand Up @@ -199,7 +199,7 @@ class MediaAttachment < ApplicationRecord
remotable_attachment :thumbnail, IMAGE_LIMIT, suppress_errors: true, download_on_assign: false

validates :account, presence: true
validates :description, length: { maximum: MAX_DESCRIPTION_LENGTH }
validates :description, length: { maximum: MAX_DESCRIPTION_LENGTH }, if: :local?
validates :file, presence: true, if: :local?
validates :thumbnail, absence: true, if: -> { local? && !audio_or_video? }

Expand Down
6 changes: 5 additions & 1 deletion app/serializers/initial_state_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class InitialStateSerializer < ActiveModel::Serializer
attributes :meta, :compose, :accounts,
:media_attachments, :settings,
:max_toot_chars, :poll_limits,
:languages
:languages, :max_alt_text_chars

has_one :push_subscription, serializer: REST::WebPushSubscriptionSerializer
has_one :role, serializer: REST::RoleSerializer
Expand All @@ -15,6 +15,10 @@ def max_toot_chars
StatusLengthValidator::MAX_CHARS
end

def max_alt_text_chars
MediaAttachment::MAX_DESCRIPTION_LENGTH
end

def poll_limits
{
max_options: PollValidator::MAX_OPTIONS,
Expand Down
1 change: 1 addition & 0 deletions app/serializers/rest/instance_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def configuration
video_size_limit: MediaAttachment::VIDEO_LIMIT,
video_frame_rate_limit: MediaAttachment::MAX_VIDEO_FRAME_RATE,
video_matrix_limit: MediaAttachment::MAX_VIDEO_MATRIX_LIMIT,
max_alt_text_characters: MediaAttachment::MAX_DESCRIPTION_LENGTH
},

polls: {
Expand Down
7 changes: 6 additions & 1 deletion app/serializers/rest/v1/instance_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class REST::V1::InstanceSerializer < ActiveModel::Serializer
include RoutingHelper

attributes :uri, :title, :short_description, :description, :email,
:version, :urls, :stats, :thumbnail, :max_toot_chars, :poll_limits,
:version, :urls, :stats, :thumbnail, :max_toot_chars, :max_alt_text_chars, :poll_limits,
:languages, :registrations, :approval_required, :invites_enabled,
:configuration

Expand Down Expand Up @@ -40,6 +40,10 @@ def max_toot_chars
StatusLengthValidator::MAX_CHARS
end

def max_alt_text_chars
MediaAttachment::MAX_DESCRIPTION_LENGTH
end

def poll_limits
{
max_options: PollValidator::MAX_OPTIONS,
Expand Down Expand Up @@ -89,6 +93,7 @@ def configuration
video_size_limit: MediaAttachment::VIDEO_LIMIT,
video_frame_rate_limit: MediaAttachment::MAX_VIDEO_FRAME_RATE,
video_matrix_limit: MediaAttachment::MAX_VIDEO_MATRIX_LIMIT,
max_alt_text_characters: MediaAttachment::MAX_DESCRIPTION_LENGTH
},

polls: {
Expand Down