Add Status::MEDIA_ATTACHMENTS_LIMIT configuration constant (#30433)

This commit is contained in:
Matt Jankowski 2024-05-27 05:49:44 -04:00 committed by GitHub
parent 87156f57b5
commit c61e356475
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 15 additions and 12 deletions

View File

@ -110,7 +110,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
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(Status::MEDIA_ATTACHMENTS_LIMIT).map(&:id)
@params = {
uri: @status_parser.uri,
@ -260,7 +260,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
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 >= Status::MEDIA_ATTACHMENTS_LIMIT
begin
media_attachment = MediaAttachment.create(

View File

@ -39,6 +39,8 @@ class Status < ApplicationRecord
include Status::SnapshotConcern
include Status::ThreadingConcern
MEDIA_ATTACHMENTS_LIMIT = 4
rate_limit by: :account, family: :statuses
self.discard_column = :deleted_at

View File

@ -59,7 +59,7 @@ class REST::InstanceSerializer < ActiveModel::Serializer
statuses: {
max_characters: StatusLengthValidator::MAX_CHARS,
max_media_attachments: 4,
max_media_attachments: Status::MEDIA_ATTACHMENTS_LIMIT,
characters_reserved_per_url: StatusLengthValidator::URL_PLACEHOLDER_CHARS,
},

View File

@ -64,7 +64,7 @@ class REST::V1::InstanceSerializer < ActiveModel::Serializer
statuses: {
max_characters: StatusLengthValidator::MAX_CHARS,
max_media_attachments: 4,
max_media_attachments: Status::MEDIA_ATTACHMENTS_LIMIT,
characters_reserved_per_url: StatusLengthValidator::URL_PLACEHOLDER_CHARS,
},

View File

@ -73,7 +73,7 @@ class ActivityPub::ProcessStatusUpdateService < BaseService
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 > Status::MEDIA_ATTACHMENTS_LIMIT
begin
media_attachment = previous_media_attachments.find { |previous_media_attachment| previous_media_attachment.remote_url == media_attachment_parser.remote_url }

View File

@ -130,9 +130,9 @@ class PostStatusService < BaseService
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 > Status::MEDIA_ATTACHMENTS_LIMIT || @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(Status::MEDIA_ATTACHMENTS_LIMIT).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?)

View File

@ -69,9 +69,9 @@ class UpdateStatusService < BaseService
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 > Status::MEDIA_ATTACHMENTS_LIMIT || @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(Status::MEDIA_ATTACHMENTS_LIMIT).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?)

View File

@ -45,7 +45,7 @@ describe 'Instances' do
),
statuses: include(
max_characters: StatusLengthValidator::MAX_CHARS,
max_media_attachments: 4 # TODO, move to constant somewhere
max_media_attachments: Status::MEDIA_ATTACHMENTS_LIMIT
),
polls: include(
max_options: PollValidator::MAX_OPTIONS

View File

@ -228,14 +228,15 @@ RSpec.describe PostStatusService do
expect(media.reload.status).to be_nil
end
it 'does not allow attaching more than 4 files' do
it 'does not allow attaching more files than configured limit' do
stub_const('Status::MEDIA_ATTACHMENTS_LIMIT', 1)
account = Fabricate(:account)
expect do
subject.call(
account,
text: 'test status update',
media_ids: Array.new(5) { Fabricate(:media_attachment, account: account) }.map(&:id)
media_ids: Array.new(2) { Fabricate(:media_attachment, account: account) }.map(&:id)
)
end.to raise_error(
Mastodon::ValidationError,