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 it easily change remote and/or local media count limits #2621

Open
wants to merge 3 commits into
base: main
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
4 changes: 2 additions & 2 deletions app/lib/activitypub/activity/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def find_existing_status
def process_status_params
@status_parser = ActivityPub::Parser::StatusParser.new(@json, followers_collection: @account.followers_url, object: @object)

attachment_ids = process_attachments.take(4).map(&:id)
attachment_ids = process_attachments.take(Rails.configuration.x.glitch.statuses[:max_media_attachments][:remote]).map(&:id)

@params = {
uri: @status_parser.uri,
Expand Down Expand Up @@ -260,7 +260,7 @@ def process_attachments
as_array(@object['attachment']).each do |attachment|
media_attachment_parser = ActivityPub::Parser::MediaAttachmentParser.new(attachment)

next if media_attachment_parser.remote_url.blank? || media_attachments.size >= 4
next if media_attachment_parser.remote_url.blank? || media_attachments.size >= Rails.configuration.x.glitch.statuses[:max_media_attachments][:remote]

begin
media_attachment = MediaAttachment.create(
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/rest/instance_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def configuration

statuses: {
max_characters: StatusLengthValidator::MAX_CHARS,
max_media_attachments: 4,
max_media_attachments: Rails.configuration.x.glitch.statuses[:max_media_attachments][:local],
characters_reserved_per_url: StatusLengthValidator::URL_PLACEHOLDER_CHARS,
supported_mime_types: HtmlAwareFormatter::STATUS_MIME_TYPES,
},
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/rest/v1/instance_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def configuration

statuses: {
max_characters: StatusLengthValidator::MAX_CHARS,
max_media_attachments: 4,
max_media_attachments: Rails.configuration.x.glitch.statuses[:max_media_attachments][:local],
characters_reserved_per_url: StatusLengthValidator::URL_PLACEHOLDER_CHARS,
supported_mime_types: HtmlAwareFormatter::STATUS_MIME_TYPES,
},
Expand Down
2 changes: 1 addition & 1 deletion app/services/activitypub/process_status_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def update_media_attachments!
as_array(@json['attachment']).each do |attachment|
media_attachment_parser = ActivityPub::Parser::MediaAttachmentParser.new(attachment)

next if media_attachment_parser.remote_url.blank? || @next_media_attachments.size > 4
next if media_attachment_parser.remote_url.blank? || @next_media_attachments.size > Rails.configuration.x.glitch.statuses[:max_media_attachments][:remote]

begin
media_attachment = previous_media_attachments.find { |previous_media_attachment| previous_media_attachment.remote_url == media_attachment_parser.remote_url }
Expand Down
4 changes: 2 additions & 2 deletions app/services/post_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ def validate_media!
return
end

raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4 || @options[:poll].present?
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > Rails.configuration.x.glitch.statuses[:max_media_attachments][:local] || @options[:poll].present?

@media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i))
@media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(Rails.configuration.x.glitch.statuses[:max_media_attachments][:local]).map(&:to_i))

raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:audio_or_video?)
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.not_ready') if @media.any?(&:not_processed?)
Expand Down
4 changes: 2 additions & 2 deletions app/services/update_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def update_media_attachments!
def validate_media!
return [] if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)

raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4 || @options[:poll].present?
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > Rails.configuration.x.glitch.statuses[:max_media_attachments][:local] || @options[:poll].present?

media_attachments = @status.account.media_attachments.where(status_id: [nil, @status.id]).where(scheduled_status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i)).to_a
media_attachments = @status.account.media_attachments.where(status_id: [nil, @status.id]).where(scheduled_status_id: nil).where(id: @options[:media_ids].take(Rails.configuration.x.glitch.statuses[:max_media_attachments][:local]).map(&:to_i)).to_a

raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if media_attachments.size > 1 && media_attachments.find(&:audio_or_video?)
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.not_ready') if media_attachments.any?(&:not_processed?)
Expand Down
5 changes: 5 additions & 0 deletions config/glitch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
shared:
statuses:
max_media_attachments:
local: <%= ENV['GLITCH_MAX_LOCAL_MEDIA_ATTACHMENTS'] || 4 %>
remote: <%= ENV['GLITCH_MAX_REMOTE_MEDIA_ATTACHMENTS'] || 16 %>
7 changes: 7 additions & 0 deletions config/initializers/glitch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Glitch
class Application < Rails::Application
config.x.glitch = config_for(:glitch)
end
end