Skip to content

Commit

Permalink
Fix remote media descriptions being cut off at 420 chars (mastodon#12262
Browse files Browse the repository at this point in the history
)

* Fix remote media descriptions being cut off at 420 chars

Fixes mastodon#12258

* Fix tests
  • Loading branch information
ClearlyClaire authored and Gargron committed Nov 4, 2019
1 parent 7488a9e commit 650820d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
6 changes: 4 additions & 2 deletions app/models/media_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class MediaAttachment < ApplicationRecord

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

MAX_DESCRIPTION_LENGTH = 1_500

IMAGE_FILE_EXTENSIONS = %w(.jpg .jpeg .png .gif).freeze
VIDEO_FILE_EXTENSIONS = %w(.webm .mp4 .m4v .mov).freeze
AUDIO_FILE_EXTENSIONS = %w(.ogg .oga .mp3 .wav .flac .opus .aac .m4a .3gp .wma).freeze
Expand Down Expand Up @@ -139,7 +141,7 @@ class MediaAttachment < ApplicationRecord
include Attachmentable

validates :account, presence: true
validates :description, length: { maximum: 1_500 }, if: :local?
validates :description, length: { maximum: MAX_DESCRIPTION_LENGTH }, if: :local?

scope :attached, -> { where.not(status_id: nil).or(where.not(scheduled_status_id: nil)) }
scope :unattached, -> { where(status_id: nil, scheduled_status_id: nil) }
Expand Down Expand Up @@ -243,7 +245,7 @@ def set_shortcode
end

def prepare_description
self.description = description.strip[0...420] unless description.nil?
self.description = description.strip[0...MAX_DESCRIPTION_LENGTH] unless description.nil?
end

def set_type_and_extension
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/activitypub/activity/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,32 @@
end
end


context 'with media attachments with long description' do
let(:object_json) do
{
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
type: 'Note',
content: 'Lorem ipsum',
attachment: [
{
type: 'Document',
mediaType: 'image/png',
url: 'http://example.com/attachment.png',
name: '*' * 1500,
},
],
}
end

it 'creates status' do
status = sender.statuses.first

expect(status).to_not be_nil
expect(status.media_attachments.map(&:description)).to include('*' * 1500)
end
end

context 'with media attachments with focal points' do
let(:object_json) do
{
Expand Down
4 changes: 2 additions & 2 deletions spec/models/media_attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@
end

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

expect(media.description.size).to be <= 420
expect(media.description.size).to be <= 1_500
end
end
end

0 comments on commit 650820d

Please sign in to comment.