2017-08-08 22:52:15 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::Activity::Create < ActivityPub::Activity
|
|
|
|
def perform
|
|
|
|
return if delete_arrived_first?(object_uri) || unsupported_object_type?
|
|
|
|
|
2017-09-14 23:26:22 +03:00
|
|
|
RedisLock.acquire(lock_options) do |lock|
|
|
|
|
if lock.acquired?
|
|
|
|
@status = find_existing_status
|
|
|
|
process_status if @status.nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@status
|
|
|
|
end
|
2017-08-08 22:52:15 +03:00
|
|
|
|
2017-09-14 23:26:22 +03:00
|
|
|
private
|
2017-08-08 22:52:15 +03:00
|
|
|
|
2017-09-14 23:26:22 +03:00
|
|
|
def process_status
|
2017-08-08 22:52:15 +03:00
|
|
|
ApplicationRecord.transaction do
|
2017-09-14 23:26:22 +03:00
|
|
|
@status = Status.create!(status_params)
|
2017-08-08 22:52:15 +03:00
|
|
|
|
2017-09-14 23:26:22 +03:00
|
|
|
process_tags(@status)
|
|
|
|
process_attachments(@status)
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
2017-09-14 23:26:22 +03:00
|
|
|
resolve_thread(@status)
|
|
|
|
distribute(@status)
|
|
|
|
forward_for_reply if @status.public_visibility? || @status.unlisted_visibility?
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
2017-08-17 22:35:00 +03:00
|
|
|
def find_existing_status
|
2017-09-01 22:00:43 +03:00
|
|
|
status = status_from_uri(object_uri)
|
2017-09-02 15:01:23 +03:00
|
|
|
status ||= Status.find_by(uri: @object['atomUri']) if @object['atomUri'].present?
|
2017-08-17 22:35:00 +03:00
|
|
|
status
|
|
|
|
end
|
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
def status_params
|
|
|
|
{
|
|
|
|
uri: @object['id'],
|
2017-09-04 19:26:33 +03:00
|
|
|
url: object_url || @object['id'],
|
2017-08-08 22:52:15 +03:00
|
|
|
account: @account,
|
|
|
|
text: text_from_content || '',
|
|
|
|
language: language_from_content,
|
|
|
|
spoiler_text: @object['summary'] || '',
|
|
|
|
created_at: @object['published'] || Time.now.utc,
|
|
|
|
reply: @object['inReplyTo'].present?,
|
|
|
|
sensitive: @object['sensitive'] || false,
|
|
|
|
visibility: visibility_from_audience,
|
|
|
|
thread: replied_to_status,
|
2017-09-02 15:01:23 +03:00
|
|
|
conversation: conversation_from_uri(@object['conversation']),
|
2017-08-08 22:52:15 +03:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_tags(status)
|
|
|
|
return unless @object['tag'].is_a?(Array)
|
|
|
|
|
|
|
|
@object['tag'].each do |tag|
|
|
|
|
case tag['type']
|
|
|
|
when 'Hashtag'
|
|
|
|
process_hashtag tag, status
|
|
|
|
when 'Mention'
|
|
|
|
process_mention tag, status
|
2017-09-19 03:42:40 +03:00
|
|
|
when 'Emoji'
|
|
|
|
process_emoji tag, status
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_hashtag(tag, status)
|
2017-09-25 19:33:11 +03:00
|
|
|
return if tag['name'].blank?
|
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
hashtag = tag['name'].gsub(/\A#/, '').mb_chars.downcase
|
|
|
|
hashtag = Tag.where(name: hashtag).first_or_initialize(name: hashtag)
|
|
|
|
|
|
|
|
status.tags << hashtag
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_mention(tag, status)
|
2017-09-25 19:33:11 +03:00
|
|
|
return if tag['href'].blank?
|
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
account = account_from_uri(tag['href'])
|
2017-08-22 19:30:15 +03:00
|
|
|
account = FetchRemoteAccountService.new.call(tag['href']) if account.nil?
|
2017-08-08 22:52:15 +03:00
|
|
|
return if account.nil?
|
|
|
|
account.mentions.create(status: status)
|
|
|
|
end
|
|
|
|
|
2017-09-19 03:42:40 +03:00
|
|
|
def process_emoji(tag, _status)
|
2017-09-25 19:33:11 +03:00
|
|
|
return if tag['name'].blank? || tag['href'].blank?
|
|
|
|
|
2017-09-19 03:42:40 +03:00
|
|
|
shortcode = tag['name'].delete(':')
|
|
|
|
emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
|
|
|
|
|
|
|
|
return if !emoji.nil? || skip_download?
|
|
|
|
|
|
|
|
emoji = CustomEmoji.new(domain: @account.domain, shortcode: shortcode)
|
|
|
|
emoji.image_remote_url = tag['href']
|
|
|
|
emoji.save
|
|
|
|
end
|
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
def process_attachments(status)
|
|
|
|
return unless @object['attachment'].is_a?(Array)
|
|
|
|
|
|
|
|
@object['attachment'].each do |attachment|
|
2017-09-25 19:33:11 +03:00
|
|
|
next if unsupported_media_type?(attachment['mediaType']) || attachment['url'].blank?
|
2017-08-08 22:52:15 +03:00
|
|
|
|
|
|
|
href = Addressable::URI.parse(attachment['url']).normalize.to_s
|
|
|
|
media_attachment = MediaAttachment.create(status: status, account: status.account, remote_url: href)
|
|
|
|
|
|
|
|
next if skip_download?
|
|
|
|
|
|
|
|
media_attachment.file_remote_url = href
|
|
|
|
media_attachment.save
|
|
|
|
end
|
2017-09-25 19:33:11 +03:00
|
|
|
rescue Addressable::URI::InvalidURIError => e
|
|
|
|
Rails.logger.debug e
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def resolve_thread(status)
|
|
|
|
return unless status.reply? && status.thread.nil?
|
2017-08-26 20:55:10 +03:00
|
|
|
ThreadResolveWorker.perform_async(status.id, in_reply_to_uri)
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def conversation_from_uri(uri)
|
|
|
|
return nil if uri.nil?
|
2017-09-19 19:08:08 +03:00
|
|
|
return Conversation.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')) if OStatus::TagManager.instance.local_id?(uri)
|
2017-09-25 19:33:11 +03:00
|
|
|
Conversation.find_by(uri: uri) || Conversation.create(uri: uri)
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def visibility_from_audience
|
|
|
|
if equals_or_includes?(@object['to'], ActivityPub::TagManager::COLLECTIONS[:public])
|
|
|
|
:public
|
|
|
|
elsif equals_or_includes?(@object['cc'], ActivityPub::TagManager::COLLECTIONS[:public])
|
|
|
|
:unlisted
|
|
|
|
elsif equals_or_includes?(@object['to'], @account.followers_url)
|
|
|
|
:private
|
|
|
|
else
|
|
|
|
:direct
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def audience_includes?(account)
|
|
|
|
uri = ActivityPub::TagManager.instance.uri_for(account)
|
|
|
|
equals_or_includes?(@object['to'], uri) || equals_or_includes?(@object['cc'], uri)
|
|
|
|
end
|
|
|
|
|
|
|
|
def replied_to_status
|
2017-08-26 20:55:10 +03:00
|
|
|
return @replied_to_status if defined?(@replied_to_status)
|
|
|
|
|
|
|
|
if in_reply_to_uri.blank?
|
|
|
|
@replied_to_status = nil
|
|
|
|
else
|
|
|
|
@replied_to_status = status_from_uri(in_reply_to_uri)
|
2017-09-02 15:01:23 +03:00
|
|
|
@replied_to_status ||= status_from_uri(@object['inReplyToAtomUri']) if @object['inReplyToAtomUri'].present?
|
2017-08-26 20:55:10 +03:00
|
|
|
@replied_to_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_reply_to_uri
|
|
|
|
value_or_id(@object['inReplyTo'])
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def text_from_content
|
|
|
|
if @object['content'].present?
|
|
|
|
@object['content']
|
|
|
|
elsif language_map?
|
|
|
|
@object['contentMap'].values.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def language_from_content
|
|
|
|
return nil unless language_map?
|
|
|
|
@object['contentMap'].keys.first
|
|
|
|
end
|
|
|
|
|
2017-09-04 19:26:33 +03:00
|
|
|
def object_url
|
|
|
|
return if @object['url'].blank?
|
|
|
|
|
|
|
|
value = first_of_value(@object['url'])
|
|
|
|
|
|
|
|
return value if value.is_a?(String)
|
|
|
|
|
|
|
|
value['href']
|
|
|
|
end
|
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
def language_map?
|
|
|
|
@object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def unsupported_object_type?
|
|
|
|
@object.is_a?(String) || !%w(Article Note).include?(@object['type'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def unsupported_media_type?(mime_type)
|
|
|
|
mime_type.present? && !(MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES).include?(mime_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def skip_download?
|
|
|
|
return @skip_download if defined?(@skip_download)
|
|
|
|
@skip_download ||= DomainBlock.find_by(domain: @account.domain)&.reject_media?
|
|
|
|
end
|
2017-08-30 16:37:02 +03:00
|
|
|
|
|
|
|
def reply_to_local?
|
|
|
|
!replied_to_status.nil? && replied_to_status.account.local?
|
|
|
|
end
|
|
|
|
|
|
|
|
def forward_for_reply
|
|
|
|
return unless @json['signature'].present? && reply_to_local?
|
|
|
|
ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id)
|
|
|
|
end
|
2017-09-14 23:26:22 +03:00
|
|
|
|
|
|
|
def lock_options
|
|
|
|
{ redis: Redis.current, key: "create:#{@object['id']}" }
|
|
|
|
end
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|