Skip to content

Commit 650820d

Browse files
ClearlyClaireGargron
authored andcommitted
Fix remote media descriptions being cut off at 420 chars (mastodon#12262)
* Fix remote media descriptions being cut off at 420 chars Fixes mastodon#12258 * Fix tests
1 parent 7488a9e commit 650820d

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

app/models/media_attachment.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class MediaAttachment < ApplicationRecord
2626

2727
enum type: [:image, :gifv, :video, :unknown, :audio]
2828

29+
MAX_DESCRIPTION_LENGTH = 1_500
30+
2931
IMAGE_FILE_EXTENSIONS = %w(.jpg .jpeg .png .gif).freeze
3032
VIDEO_FILE_EXTENSIONS = %w(.webm .mp4 .m4v .mov).freeze
3133
AUDIO_FILE_EXTENSIONS = %w(.ogg .oga .mp3 .wav .flac .opus .aac .m4a .3gp .wma).freeze
@@ -139,7 +141,7 @@ class MediaAttachment < ApplicationRecord
139141
include Attachmentable
140142

141143
validates :account, presence: true
142-
validates :description, length: { maximum: 1_500 }, if: :local?
144+
validates :description, length: { maximum: MAX_DESCRIPTION_LENGTH }, if: :local?
143145

144146
scope :attached, -> { where.not(status_id: nil).or(where.not(scheduled_status_id: nil)) }
145147
scope :unattached, -> { where(status_id: nil, scheduled_status_id: nil) }
@@ -243,7 +245,7 @@ def set_shortcode
243245
end
244246

245247
def prepare_description
246-
self.description = description.strip[0...420] unless description.nil?
248+
self.description = description.strip[0...MAX_DESCRIPTION_LENGTH] unless description.nil?
247249
end
248250

249251
def set_type_and_extension

spec/lib/activitypub/activity/create_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,32 @@
261261
end
262262
end
263263

264+
265+
context 'with media attachments with long description' do
266+
let(:object_json) do
267+
{
268+
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
269+
type: 'Note',
270+
content: 'Lorem ipsum',
271+
attachment: [
272+
{
273+
type: 'Document',
274+
mediaType: 'image/png',
275+
url: 'http://example.com/attachment.png',
276+
name: '*' * 1500,
277+
},
278+
],
279+
}
280+
end
281+
282+
it 'creates status' do
283+
status = sender.statuses.first
284+
285+
expect(status).to_not be_nil
286+
expect(status.media_attachments.map(&:description)).to include('*' * 1500)
287+
end
288+
end
289+
264290
context 'with media attachments with focal points' do
265291
let(:object_json) do
266292
{

spec/models/media_attachment_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@
136136
end
137137

138138
describe 'descriptions for remote attachments' do
139-
it 'are cut off at 140 characters' do
139+
it 'are cut off at 1500 characters' do
140140
media = Fabricate(:media_attachment, description: 'foo' * 1000, remote_url: 'http://example.com/blah.jpg')
141141

142-
expect(media.description.size).to be <= 420
142+
expect(media.description.size).to be <= 1_500
143143
end
144144
end
145145
end

0 commit comments

Comments
 (0)