2017-08-08 22:52:15 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::Activity::Create < ActivityPub::Activity
|
|
|
|
def perform
|
2020-07-22 12:43:17 +03:00
|
|
|
dereference_object!
|
|
|
|
|
2020-06-02 20:24:53 +03:00
|
|
|
case @object['type']
|
|
|
|
when 'EncryptedMessage'
|
|
|
|
create_encrypted_message
|
|
|
|
else
|
|
|
|
create_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_encrypted_message
|
2020-08-30 13:34:20 +03:00
|
|
|
return reject_payload! if invalid_origin?(object_uri) || @options[:delivered_to_account_id].blank?
|
2020-06-02 20:24:53 +03:00
|
|
|
|
|
|
|
target_account = Account.find(@options[:delivered_to_account_id])
|
|
|
|
target_device = target_account.devices.find_by(device_id: @object.dig('to', 'deviceId'))
|
|
|
|
|
|
|
|
return if target_device.nil?
|
|
|
|
|
|
|
|
target_device.encrypted_messages.create!(
|
|
|
|
from_account: @account,
|
|
|
|
from_device_id: @object.dig('attributedTo', 'deviceId'),
|
|
|
|
type: @object['messageType'],
|
|
|
|
body: @object['cipherText'],
|
|
|
|
digest: @object.dig('digest', 'digestValue'),
|
|
|
|
message_franking: message_franking.to_token
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def message_franking
|
|
|
|
MessageFranking.new(
|
|
|
|
hmac: @object.dig('digest', 'digestValue'),
|
|
|
|
original_franking: @object['messageFranking'],
|
|
|
|
source_account_id: @account.id,
|
|
|
|
target_account_id: @options[:delivered_to_account_id],
|
|
|
|
timestamp: Time.now.utc
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_status
|
2020-08-30 13:34:20 +03:00
|
|
|
return reject_payload! if unsupported_object_type? || invalid_origin?(object_uri) || tombstone_exists? || !related_to_local_activity?
|
2017-08-08 22:52:15 +03:00
|
|
|
|
2017-09-14 23:26:22 +03:00
|
|
|
RedisLock.acquire(lock_options) do |lock|
|
|
|
|
if lock.acquired?
|
2020-07-14 20:05:07 +03:00
|
|
|
return if delete_arrived_first?(object_uri) || poll_vote? # rubocop:disable Lint/NonLocalExitFromIterator
|
2019-01-16 16:42:00 +02:00
|
|
|
|
2017-09-14 23:26:22 +03:00
|
|
|
@status = find_existing_status
|
2018-10-30 16:03:55 +02:00
|
|
|
|
|
|
|
if @status.nil?
|
|
|
|
process_status
|
|
|
|
elsif @options[:delivered_to_account_id].present?
|
|
|
|
postprocess_audience_and_deliver
|
|
|
|
end
|
2018-05-16 13:29:45 +03:00
|
|
|
else
|
|
|
|
raise Mastodon::RaceConditionError
|
2017-09-14 23:26:22 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@status
|
|
|
|
end
|
2017-08-08 22:52:15 +03:00
|
|
|
|
2019-11-30 20:58:00 +02:00
|
|
|
def audience_to
|
2020-08-24 15:11:47 +03:00
|
|
|
as_array(@object['to'] || @json['to']).map { |x| value_or_id(x) }
|
2019-11-30 20:58:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def audience_cc
|
2020-08-24 15:11:47 +03:00
|
|
|
as_array(@object['cc'] || @json['cc']).map { |x| value_or_id(x) }
|
2019-11-30 20:58:00 +02:00
|
|
|
end
|
|
|
|
|
2017-09-14 23:26:22 +03:00
|
|
|
def process_status
|
2018-10-11 01:50:18 +03:00
|
|
|
@tags = []
|
|
|
|
@mentions = []
|
|
|
|
@params = {}
|
2017-12-10 17:33:52 +02:00
|
|
|
|
2018-10-11 01:50:18 +03:00
|
|
|
process_status_params
|
|
|
|
process_tags
|
2018-10-17 18:13:04 +03:00
|
|
|
process_audience
|
2017-08-08 22:52:15 +03:00
|
|
|
|
2018-10-11 01:50:18 +03:00
|
|
|
ApplicationRecord.transaction do
|
|
|
|
@status = Status.create!(@params)
|
|
|
|
attach_tags(@status)
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
2017-09-14 23:26:22 +03:00
|
|
|
resolve_thread(@status)
|
2019-02-28 16:22:21 +02:00
|
|
|
fetch_replies(@status)
|
2019-07-13 17:45:50 +03:00
|
|
|
check_for_spam
|
2017-09-14 23:26:22 +03:00
|
|
|
distribute(@status)
|
2020-08-30 13:34:20 +03:00
|
|
|
forward_for_reply
|
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
|
|
|
|
|
2018-03-08 02:22:47 +02:00
|
|
|
def process_status_params
|
2018-10-11 01:50:18 +03:00
|
|
|
@params = begin
|
|
|
|
{
|
2020-08-30 13:34:20 +03:00
|
|
|
uri: object_uri,
|
|
|
|
url: object_url || object_uri,
|
2018-10-11 01:50:18 +03:00
|
|
|
account: @account,
|
|
|
|
text: text_from_content || '',
|
|
|
|
language: detected_language,
|
2019-01-16 19:36:17 +02:00
|
|
|
spoiler_text: converted_object_type? ? '' : (text_from_summary || ''),
|
2018-10-11 01:50:18 +03:00
|
|
|
created_at: @object['published'],
|
|
|
|
override_timestamps: @options[:override_timestamps],
|
|
|
|
reply: @object['inReplyTo'].present?,
|
2020-11-04 21:45:01 +02:00
|
|
|
sensitive: @account.sensitized? || @object['sensitive'] || false,
|
2018-10-11 01:50:18 +03:00
|
|
|
visibility: visibility_from_audience,
|
|
|
|
thread: replied_to_status,
|
2019-07-28 18:47:37 +03:00
|
|
|
conversation: conversation_from_uri(@object['conversation']),
|
2018-10-11 01:50:18 +03:00
|
|
|
media_attachment_ids: process_attachments.take(4).map(&:id),
|
2019-03-28 05:44:59 +02:00
|
|
|
poll: process_poll,
|
2018-10-11 01:50:18 +03:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-17 18:13:04 +03:00
|
|
|
def process_audience
|
2020-08-24 15:11:47 +03:00
|
|
|
(audience_to + audience_cc).uniq.each do |audience|
|
2018-10-17 18:13:04 +03:00
|
|
|
next if audience == ActivityPub::TagManager::COLLECTIONS[:public]
|
|
|
|
|
|
|
|
# Unlike with tags, there is no point in resolving accounts we don't already
|
|
|
|
# know here, because silent mentions would only be used for local access
|
|
|
|
# control anyway
|
|
|
|
account = account_from_uri(audience)
|
|
|
|
|
|
|
|
next if account.nil? || @mentions.any? { |mention| mention.account_id == account.id }
|
|
|
|
|
|
|
|
@mentions << Mention.new(account: account, silent: true)
|
|
|
|
|
|
|
|
# If there is at least one silent mention, then the status can be considered
|
2018-10-25 19:12:22 +03:00
|
|
|
# as a limited-audience status, and not strictly a direct message, but only
|
|
|
|
# if we considered a direct message in the first place
|
2020-08-03 12:52:21 +03:00
|
|
|
next unless @params[:visibility] == :direct && direct_message.nil?
|
2018-10-17 18:13:04 +03:00
|
|
|
|
|
|
|
@params[:visibility] = :limited
|
|
|
|
end
|
2018-10-25 19:12:22 +03:00
|
|
|
|
|
|
|
# If the payload was delivered to a specific inbox, the inbox owner must have
|
|
|
|
# access to it, unless they already have access to it anyway
|
2018-10-26 13:59:59 +03:00
|
|
|
return if @options[:delivered_to_account_id].nil? || @mentions.any? { |mention| mention.account_id == @options[:delivered_to_account_id] }
|
2018-10-25 19:12:22 +03:00
|
|
|
|
|
|
|
@mentions << Mention.new(account_id: @options[:delivered_to_account_id], silent: true)
|
|
|
|
|
2020-08-03 12:52:21 +03:00
|
|
|
return unless @params[:visibility] == :direct && direct_message.nil?
|
2018-10-25 19:12:22 +03:00
|
|
|
|
|
|
|
@params[:visibility] = :limited
|
2018-10-17 18:13:04 +03:00
|
|
|
end
|
|
|
|
|
2018-10-30 16:03:55 +02:00
|
|
|
def postprocess_audience_and_deliver
|
|
|
|
return if @status.mentions.find_by(account_id: @options[:delivered_to_account_id])
|
|
|
|
|
|
|
|
delivered_to_account = Account.find(@options[:delivered_to_account_id])
|
|
|
|
|
|
|
|
@status.mentions.create(account: delivered_to_account, silent: true)
|
2020-08-03 12:52:21 +03:00
|
|
|
@status.update(visibility: :limited) if @status.direct_visibility? && direct_message.nil?
|
2018-10-30 16:03:55 +02:00
|
|
|
|
|
|
|
return unless delivered_to_account.following?(@account)
|
|
|
|
|
|
|
|
FeedInsertWorker.perform_async(@status.id, delivered_to_account.id, :home)
|
|
|
|
end
|
|
|
|
|
2018-10-11 01:50:18 +03:00
|
|
|
def attach_tags(status)
|
|
|
|
@tags.each do |tag|
|
|
|
|
status.tags << tag
|
2018-10-11 03:10:15 +03:00
|
|
|
TrendingTags.record_use!(tag, status.account, status.created_at) if status.public_visibility?
|
2018-10-11 01:50:18 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
@mentions.each do |mention|
|
|
|
|
mention.status = status
|
|
|
|
mention.save
|
|
|
|
end
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
2018-10-11 01:50:18 +03:00
|
|
|
def process_tags
|
2017-10-27 17:10:36 +03:00
|
|
|
return if @object['tag'].nil?
|
2017-08-08 22:52:15 +03:00
|
|
|
|
2017-10-27 17:10:36 +03:00
|
|
|
as_array(@object['tag']).each do |tag|
|
2018-05-02 13:40:24 +03:00
|
|
|
if equals_or_includes?(tag['type'], 'Hashtag')
|
2018-10-11 01:50:18 +03:00
|
|
|
process_hashtag tag
|
2018-05-02 13:40:24 +03:00
|
|
|
elsif equals_or_includes?(tag['type'], 'Mention')
|
2018-10-11 01:50:18 +03:00
|
|
|
process_mention tag
|
2018-05-02 13:40:24 +03:00
|
|
|
elsif equals_or_includes?(tag['type'], 'Emoji')
|
2018-10-11 01:50:18 +03:00
|
|
|
process_emoji tag
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-11 01:50:18 +03:00
|
|
|
def process_hashtag(tag)
|
2017-09-25 19:33:11 +03:00
|
|
|
return if tag['name'].blank?
|
|
|
|
|
2019-07-28 06:59:51 +03:00
|
|
|
Tag.find_or_create_by_names(tag['name']) do |hashtag|
|
2019-12-17 14:31:56 +02:00
|
|
|
@tags << hashtag unless @tags.include?(hashtag) || !hashtag.valid?
|
2019-07-28 06:59:51 +03:00
|
|
|
end
|
2018-03-30 16:44:54 +03:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
nil
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
2018-10-11 01:50:18 +03:00
|
|
|
def process_mention(tag)
|
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'])
|
2019-12-17 14:32:57 +02:00
|
|
|
account = ActivityPub::FetchRemoteAccountService.new.call(tag['href']) if account.nil?
|
2018-10-11 01:50:18 +03:00
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
return if account.nil?
|
2018-10-11 01:50:18 +03:00
|
|
|
|
2018-10-17 18:13:04 +03:00
|
|
|
@mentions << Mention.new(account: account, silent: false)
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
2018-10-11 01:50:18 +03:00
|
|
|
def process_emoji(tag)
|
2017-10-07 18:43:42 +03:00
|
|
|
return if skip_download?
|
|
|
|
return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
|
2017-09-25 19:33:11 +03:00
|
|
|
|
2017-09-19 03:42:40 +03:00
|
|
|
shortcode = tag['name'].delete(':')
|
2017-10-07 18:43:42 +03:00
|
|
|
image_url = tag['icon']['url']
|
|
|
|
uri = tag['id']
|
|
|
|
updated = tag['updated']
|
2017-09-19 03:42:40 +03:00
|
|
|
emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
|
|
|
|
|
2018-11-11 00:59:51 +02:00
|
|
|
return unless emoji.nil? || image_url != emoji.image_remote_url || (updated && updated >= emoji.updated_at)
|
2017-09-19 03:42:40 +03:00
|
|
|
|
2017-10-07 18:43:42 +03:00
|
|
|
emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
|
|
|
|
emoji.image_remote_url = image_url
|
2017-09-19 03:42:40 +03:00
|
|
|
emoji.save
|
2020-12-15 13:55:29 +02:00
|
|
|
rescue Seahorse::Client::NetworkingError
|
|
|
|
nil
|
2017-09-19 03:42:40 +03:00
|
|
|
end
|
|
|
|
|
2017-12-10 17:33:52 +02:00
|
|
|
def process_attachments
|
2018-03-08 02:22:47 +02:00
|
|
|
return [] if @object['attachment'].nil?
|
2017-08-08 22:52:15 +03:00
|
|
|
|
2017-12-10 17:33:52 +02:00
|
|
|
media_attachments = []
|
|
|
|
|
2017-10-27 17:10:36 +03:00
|
|
|
as_array(@object['attachment']).each do |attachment|
|
2019-09-10 16:29:12 +03:00
|
|
|
next if attachment['url'].blank? || media_attachments.size >= 4
|
2017-08-08 22:52:15 +03:00
|
|
|
|
2019-09-10 16:29:12 +03:00
|
|
|
begin
|
|
|
|
href = Addressable::URI.parse(attachment['url']).normalize.to_s
|
2020-06-29 14:56:55 +03:00
|
|
|
media_attachment = MediaAttachment.create(account: @account, remote_url: href, thumbnail_remote_url: icon_url_from_attachment(attachment), description: attachment['summary'].presence || attachment['name'].presence, focus: attachment['focalPoint'], blurhash: supported_blurhash?(attachment['blurhash']) ? attachment['blurhash'] : nil)
|
2019-09-10 16:29:12 +03:00
|
|
|
media_attachments << media_attachment
|
2017-08-08 22:52:15 +03:00
|
|
|
|
2019-09-10 16:29:12 +03:00
|
|
|
next if unsupported_media_type?(attachment['mediaType']) || skip_download?
|
2017-08-08 22:52:15 +03:00
|
|
|
|
2020-06-29 14:56:55 +03:00
|
|
|
media_attachment.download_file!
|
|
|
|
media_attachment.download_thumbnail!
|
2019-09-10 16:29:12 +03:00
|
|
|
media_attachment.save
|
|
|
|
rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError
|
|
|
|
RedownloadMediaWorker.perform_in(rand(30..600).seconds, media_attachment.id)
|
2020-12-15 13:55:29 +02:00
|
|
|
rescue Seahorse::Client::NetworkingError
|
|
|
|
nil
|
2019-09-10 16:29:12 +03:00
|
|
|
end
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
2017-12-10 17:33:52 +02:00
|
|
|
|
|
|
|
media_attachments
|
2017-09-25 19:33:11 +03:00
|
|
|
rescue Addressable::URI::InvalidURIError => e
|
2019-09-10 16:29:12 +03:00
|
|
|
Rails.logger.debug "Invalid URL in attachment: #{e}"
|
2017-12-10 17:33:52 +02:00
|
|
|
media_attachments
|
|
|
|
end
|
|
|
|
|
2020-06-29 14:56:55 +03:00
|
|
|
def icon_url_from_attachment(attachment)
|
|
|
|
url = attachment['icon'].is_a?(Hash) ? attachment['icon']['url'] : attachment['icon']
|
|
|
|
Addressable::URI.parse(url).normalize.to_s if url.present?
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2019-03-03 23:18:23 +02:00
|
|
|
def process_poll
|
|
|
|
return unless @object['type'] == 'Question' && (@object['anyOf'].is_a?(Array) || @object['oneOf'].is_a?(Array))
|
|
|
|
|
|
|
|
expires_at = begin
|
|
|
|
if @object['closed'].is_a?(String)
|
|
|
|
@object['closed']
|
2019-03-04 01:44:34 +02:00
|
|
|
elsif !@object['closed'].nil? && !@object['closed'].is_a?(FalseClass)
|
2019-03-03 23:18:23 +02:00
|
|
|
Time.now.utc
|
|
|
|
else
|
|
|
|
@object['endTime']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @object['anyOf'].is_a?(Array)
|
|
|
|
multiple = true
|
|
|
|
items = @object['anyOf']
|
|
|
|
else
|
|
|
|
multiple = false
|
|
|
|
items = @object['oneOf']
|
|
|
|
end
|
|
|
|
|
2019-09-29 23:58:01 +03:00
|
|
|
voters_count = @object['votersCount']
|
|
|
|
|
2019-03-04 02:13:42 +02:00
|
|
|
@account.polls.new(
|
2019-03-03 23:18:23 +02:00
|
|
|
multiple: multiple,
|
|
|
|
expires_at: expires_at,
|
2019-06-03 21:04:00 +03:00
|
|
|
options: items.map { |item| item['name'].presence || item['content'] }.compact,
|
2019-09-29 23:58:01 +03:00
|
|
|
cached_tallies: items.map { |item| item.dig('replies', 'totalItems') || 0 },
|
|
|
|
voters_count: voters_count
|
2019-03-03 23:18:23 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def poll_vote?
|
2019-03-28 05:44:59 +02:00
|
|
|
return false if replied_to_status.nil? || replied_to_status.preloadable_poll.nil? || !replied_to_status.local? || !replied_to_status.preloadable_poll.options.include?(@object['name'])
|
2019-03-12 23:58:59 +02:00
|
|
|
|
2019-09-29 23:58:01 +03:00
|
|
|
poll_vote! unless replied_to_status.preloadable_poll.expired?
|
2019-03-12 23:58:59 +02:00
|
|
|
|
2019-03-11 01:49:31 +02:00
|
|
|
true
|
2019-03-03 23:18:23 +02:00
|
|
|
end
|
|
|
|
|
2019-09-29 23:58:01 +03:00
|
|
|
def poll_vote!
|
|
|
|
poll = replied_to_status.preloadable_poll
|
|
|
|
already_voted = true
|
2020-06-02 20:24:53 +03:00
|
|
|
|
2019-09-29 23:58:01 +03:00
|
|
|
RedisLock.acquire(poll_lock_options) do |lock|
|
|
|
|
if lock.acquired?
|
|
|
|
already_voted = poll.votes.where(account: @account).exists?
|
2020-08-30 13:34:20 +03:00
|
|
|
poll.votes.create!(account: @account, choice: poll.options.index(@object['name']), uri: object_uri)
|
2019-09-29 23:58:01 +03:00
|
|
|
else
|
|
|
|
raise Mastodon::RaceConditionError
|
|
|
|
end
|
|
|
|
end
|
2020-06-02 20:24:53 +03:00
|
|
|
|
2019-09-29 23:58:01 +03:00
|
|
|
increment_voters_count! unless already_voted
|
|
|
|
ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, replied_to_status.id) unless replied_to_status.preloadable_poll.hide_totals?
|
|
|
|
end
|
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
def resolve_thread(status)
|
2018-12-26 20:15:53 +02:00
|
|
|
return unless status.reply? && status.thread.nil? && Request.valid_url?(in_reply_to_uri)
|
2020-06-02 20:24:53 +03:00
|
|
|
|
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
|
|
|
|
|
2019-02-28 16:22:21 +02:00
|
|
|
def fetch_replies(status)
|
|
|
|
collection = @object['replies']
|
|
|
|
return if collection.nil?
|
2020-06-02 20:24:53 +03:00
|
|
|
|
2019-02-28 16:22:21 +02:00
|
|
|
replies = ActivityPub::FetchRepliesService.new.call(status, collection, false)
|
2019-03-07 02:50:37 +02:00
|
|
|
return unless replies.nil?
|
2020-06-02 20:24:53 +03:00
|
|
|
|
2019-02-28 16:22:21 +02:00
|
|
|
uri = value_or_id(collection)
|
|
|
|
ActivityPub::FetchRepliesWorker.perform_async(status.id, uri) unless uri.nil?
|
|
|
|
end
|
|
|
|
|
2019-07-28 18:47:37 +03:00
|
|
|
def conversation_from_uri(uri)
|
|
|
|
return nil if uri.nil?
|
|
|
|
return Conversation.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')) if OStatus::TagManager.instance.local_id?(uri)
|
2020-06-02 20:24:53 +03:00
|
|
|
|
2019-07-28 18:47:37 +03:00
|
|
|
begin
|
|
|
|
Conversation.find_or_create_by!(uri: uri)
|
|
|
|
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
def visibility_from_audience
|
2020-08-24 15:11:47 +03:00
|
|
|
if audience_to.include?(ActivityPub::TagManager::COLLECTIONS[:public])
|
2017-08-08 22:52:15 +03:00
|
|
|
:public
|
2020-08-24 15:11:47 +03:00
|
|
|
elsif audience_cc.include?(ActivityPub::TagManager::COLLECTIONS[:public])
|
2017-08-08 22:52:15 +03:00
|
|
|
:unlisted
|
2020-08-24 15:11:47 +03:00
|
|
|
elsif audience_to.include?(@account.followers_url)
|
2017-08-08 22:52:15 +03:00
|
|
|
:private
|
2020-08-03 12:52:21 +03:00
|
|
|
elsif direct_message == false
|
|
|
|
:limited
|
2017-08-08 22:52:15 +03:00
|
|
|
else
|
|
|
|
:direct
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def audience_includes?(account)
|
|
|
|
uri = ActivityPub::TagManager.instance.uri_for(account)
|
2020-08-24 15:11:47 +03:00
|
|
|
audience_to.include?(uri) || audience_cc.include?(uri)
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
2020-08-03 12:52:21 +03:00
|
|
|
def direct_message
|
|
|
|
@object['directMessage']
|
|
|
|
end
|
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
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
|
2020-08-30 13:34:20 +03:00
|
|
|
return Formatter.instance.linkify([[text_from_name, text_from_summary.presence].compact.join("\n\n"), object_url || object_uri].join(' ')) if converted_object_type?
|
2017-11-30 05:06:20 +02:00
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
if @object['content'].present?
|
|
|
|
@object['content']
|
2017-11-30 05:06:20 +02:00
|
|
|
elsif content_language_map?
|
2017-08-08 22:52:15 +03:00
|
|
|
@object['contentMap'].values.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-25 14:27:34 +03:00
|
|
|
def text_from_summary
|
|
|
|
if @object['summary'].present?
|
|
|
|
@object['summary']
|
|
|
|
elsif summary_language_map?
|
|
|
|
@object['summaryMap'].values.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-30 05:06:20 +02:00
|
|
|
def text_from_name
|
|
|
|
if @object['name'].present?
|
|
|
|
@object['name']
|
|
|
|
elsif name_language_map?
|
|
|
|
@object['nameMap'].values.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def detected_language
|
|
|
|
if content_language_map?
|
|
|
|
@object['contentMap'].keys.first
|
|
|
|
elsif name_language_map?
|
|
|
|
@object['nameMap'].keys.first
|
2018-08-25 14:27:34 +03:00
|
|
|
elsif summary_language_map?
|
|
|
|
@object['summaryMap'].keys.first
|
2017-11-30 05:06:20 +02:00
|
|
|
elsif supported_object_type?
|
|
|
|
LanguageDetector.instance.detect(text_from_content, @account)
|
|
|
|
end
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
2017-09-04 19:26:33 +03:00
|
|
|
def object_url
|
|
|
|
return if @object['url'].blank?
|
2018-01-08 06:00:23 +02:00
|
|
|
|
|
|
|
url_candidate = url_to_href(@object['url'], 'text/html')
|
|
|
|
|
|
|
|
if invalid_origin?(url_candidate)
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
url_candidate
|
|
|
|
end
|
2017-09-04 19:26:33 +03:00
|
|
|
end
|
|
|
|
|
2018-08-25 14:27:34 +03:00
|
|
|
def summary_language_map?
|
|
|
|
@object['summaryMap'].is_a?(Hash) && !@object['summaryMap'].empty?
|
|
|
|
end
|
|
|
|
|
2017-11-30 05:06:20 +02:00
|
|
|
def content_language_map?
|
2017-08-08 22:52:15 +03:00
|
|
|
@object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
|
|
|
|
end
|
|
|
|
|
2017-11-30 05:06:20 +02:00
|
|
|
def name_language_map?
|
|
|
|
@object['nameMap'].is_a?(Hash) && !@object['nameMap'].empty?
|
|
|
|
end
|
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
def unsupported_media_type?(mime_type)
|
2019-06-22 03:50:36 +03:00
|
|
|
mime_type.present? && !MediaAttachment.supported_mime_types.include?(mime_type)
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
|
|
|
|
2019-04-27 04:24:09 +03:00
|
|
|
def supported_blurhash?(blurhash)
|
|
|
|
components = blurhash.blank? ? nil : Blurhash.components(blurhash)
|
|
|
|
components.present? && components.none? { |comp| comp > 5 }
|
|
|
|
end
|
|
|
|
|
2017-08-08 22:52:15 +03:00
|
|
|
def skip_download?
|
|
|
|
return @skip_download if defined?(@skip_download)
|
2020-06-02 20:24:53 +03:00
|
|
|
|
2019-06-22 01:13:10 +03:00
|
|
|
@skip_download ||= DomainBlock.reject_media?(@account.domain)
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|
2017-08-30 16:37:02 +03:00
|
|
|
|
|
|
|
def reply_to_local?
|
|
|
|
!replied_to_status.nil? && replied_to_status.account.local?
|
|
|
|
end
|
|
|
|
|
2019-02-13 19:42:47 +02:00
|
|
|
def related_to_local_activity?
|
|
|
|
fetch? || followed_by_local_accounts? || requested_through_relay? ||
|
|
|
|
responds_to_followed_account? || addresses_local_accounts?
|
|
|
|
end
|
|
|
|
|
|
|
|
def responds_to_followed_account?
|
|
|
|
!replied_to_status.nil? && (replied_to_status.account.local? || replied_to_status.account.passive_relationships.exists?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def addresses_local_accounts?
|
|
|
|
return true if @options[:delivered_to_account_id]
|
|
|
|
|
2020-08-24 15:11:47 +03:00
|
|
|
local_usernames = (audience_to + audience_cc).uniq.select { |uri| ActivityPub::TagManager.instance.local_uri?(uri) }.map { |uri| ActivityPub::TagManager.instance.uri_to_local_id(uri, :username) }
|
2019-02-13 19:42:47 +02:00
|
|
|
|
|
|
|
return false if local_usernames.empty?
|
|
|
|
|
|
|
|
Account.local.where(username: local_usernames).exists?
|
|
|
|
end
|
|
|
|
|
2020-08-30 13:34:20 +03:00
|
|
|
def tombstone_exists?
|
|
|
|
Tombstone.exists?(uri: object_uri)
|
|
|
|
end
|
|
|
|
|
2019-07-13 17:45:50 +03:00
|
|
|
def check_for_spam
|
2019-09-18 13:53:13 +03:00
|
|
|
SpamCheck.perform(@status)
|
2019-07-13 17:45:50 +03:00
|
|
|
end
|
|
|
|
|
2017-08-30 16:37:02 +03:00
|
|
|
def forward_for_reply
|
2020-08-30 13:34:20 +03:00
|
|
|
return unless @status.distributable? && @json['signature'].present? && reply_to_local?
|
2020-06-02 20:24:53 +03:00
|
|
|
|
2017-11-30 04:50:05 +02:00
|
|
|
ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id, [@account.preferred_inbox_url])
|
2017-08-30 16:37:02 +03:00
|
|
|
end
|
2017-09-14 23:26:22 +03:00
|
|
|
|
2019-09-29 23:58:01 +03:00
|
|
|
def increment_voters_count!
|
|
|
|
poll = replied_to_status.preloadable_poll
|
2020-06-02 20:24:53 +03:00
|
|
|
|
2019-09-29 23:58:01 +03:00
|
|
|
unless poll.voters_count.nil?
|
|
|
|
poll.voters_count = poll.voters_count + 1
|
|
|
|
poll.save
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
poll.reload
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
2017-09-14 23:26:22 +03:00
|
|
|
def lock_options
|
2020-08-30 13:34:20 +03:00
|
|
|
{ redis: Redis.current, key: "create:#{object_uri}" }
|
2017-09-14 23:26:22 +03:00
|
|
|
end
|
2019-09-29 23:58:01 +03:00
|
|
|
|
|
|
|
def poll_lock_options
|
|
|
|
{ redis: Redis.current, key: "vote:#{replied_to_status.poll_id}:#{@account.id}" }
|
|
|
|
end
|
2017-08-08 22:52:15 +03:00
|
|
|
end
|