Skip to content

Commit

Permalink
Fix media attachment order of remote posts (mastodon#28469)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire committed Dec 22, 2023
1 parent 72ff0d3 commit ea6c187
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion app/lib/activitypub/activity/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def find_existing_status
def process_status_params
@status_parser = ActivityPub::Parser::StatusParser.new(@json, followers_collection: @account.followers_url)

attachment_ids = process_attachments.take(4).map(&:id)

@params = {
uri: @status_parser.uri,
url: @status_parser.url || @status_parser.uri,
Expand All @@ -125,7 +127,8 @@ def process_status_params
visibility: @status_parser.visibility,
thread: replied_to_status,
conversation: conversation_from_uri(@object['conversation']),
media_attachment_ids: process_attachments.take(4).map(&:id),
media_attachment_ids: attachment_ids,
ordered_media_attachment_ids: attachment_ids,
poll: process_poll,
}
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ def emojis

def ordered_media_attachments
if ordered_media_attachment_ids.nil?
media_attachments
# NOTE: sort Ruby-side to avoid hitting the database when the status is
# not persisted to database yet
media_attachments.sort_by(&:id)
else
map = media_attachments.index_by(&:id)
ordered_media_attachment_ids.filter_map { |media_attachment_id| map[media_attachment_id] }
Expand Down
10 changes: 8 additions & 2 deletions spec/lib/activitypub/activity/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,21 @@ def activity_for_object(json)
mediaType: 'image/png',
url: 'http://example.com/attachment.png',
},
{
type: 'Document',
mediaType: 'image/png',
url: 'http://example.com/emoji.png',
},
],
}
end

it 'creates status' do
it 'creates status with correctly-ordered media attachments' do
status = sender.statuses.first

expect(status).to_not be_nil
expect(status.media_attachments.map(&:remote_url)).to include('http://example.com/attachment.png')
expect(status.ordered_media_attachments.map(&:remote_url)).to eq ['http://example.com/attachment.png', 'http://example.com/emoji.png']
expect(status.ordered_media_attachment_ids).to be_present
end
end

Expand Down

0 comments on commit ea6c187

Please sign in to comment.