1
0
mirror of https://github.com/mastodon/mastodon synced 2024-12-18 08:38:20 +09:00

Save media outside transaction (#5959)

This commit is contained in:
abcang 2017-12-11 00:33:52 +09:00 committed by Eugen Rochko
parent 2950de86c6
commit 3caec1ecc2
2 changed files with 36 additions and 7 deletions

View File

@ -20,11 +20,13 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
private
def process_status
media_attachments = process_attachments
ApplicationRecord.transaction do
@status = Status.create!(status_params)
process_tags(@status)
process_attachments(@status)
attach_media(@status, media_attachments)
end
resolve_thread(@status)
@ -105,22 +107,36 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
emoji.save
end
def process_attachments(status)
def process_attachments
return if @object['attachment'].nil?
media_attachments = []
as_array(@object['attachment']).each do |attachment|
next if unsupported_media_type?(attachment['mediaType']) || attachment['url'].blank?
href = Addressable::URI.parse(attachment['url']).normalize.to_s
media_attachment = MediaAttachment.create(status: status, account: status.account, remote_url: href, description: attachment['name'].presence)
media_attachment = MediaAttachment.create(account: @account, remote_url: href, description: attachment['name'].presence)
media_attachments << media_attachment
next if skip_download?
media_attachment.file_remote_url = href
media_attachment.save
end
media_attachments
rescue Addressable::URI::InvalidURIError => e
Rails.logger.debug e
media_attachments
end
def attach_media(status, media_attachments)
return if media_attachments.blank?
media = MediaAttachment.where(status_id: nil, id: media_attachments.take(4).map(&:id))
media.update(status_id: status.id)
end
def resolve_thread(status)

View File

@ -26,6 +26,8 @@ class OStatus::Activity::Creation < OStatus::Activity::Base
cached_reblog = reblog
status = nil
media_attachments = save_media
ApplicationRecord.transaction do
status = Status.create!(
uri: id,
@ -44,7 +46,7 @@ class OStatus::Activity::Creation < OStatus::Activity::Base
save_mentions(status)
save_hashtags(status)
save_media(status)
attach_media(status, media_attachments)
save_emojis(status)
end
@ -126,18 +128,20 @@ class OStatus::Activity::Creation < OStatus::Activity::Base
ProcessHashtagsService.new.call(parent, tags)
end
def save_media(parent)
do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media?
def save_media
do_not_download = DomainBlock.find_by(domain: @account.domain)&.reject_media?
media_attachments = []
@xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: OStatus::TagManager::XMLNS).each do |link|
next unless link['href']
media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
media = MediaAttachment.where(status: nil, remote_url: link['href']).first_or_initialize(account: @account, status: nil, remote_url: link['href'])
parsed_url = Addressable::URI.parse(link['href']).normalize
next if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty?
media.save
media_attachments << media
next if do_not_download
@ -148,6 +152,15 @@ class OStatus::Activity::Creation < OStatus::Activity::Base
next
end
end
media_attachments
end
def attach_media(parent, media_attachments)
return if media_attachments.blank?
media = MediaAttachment.where(status_id: nil, id: media_attachments.take(4).map(&:id))
media.update(status_id: parent.id)
end
def save_emojis(parent)