2017-08-08 15:52:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::Activity
|
|
|
|
include JsonLdHelper
|
|
|
|
|
2017-12-06 05:41:57 -05:00
|
|
|
def initialize(json, account, **options)
|
2017-08-08 15:52:15 -04:00
|
|
|
@json = json
|
|
|
|
@account = account
|
|
|
|
@object = @json['object']
|
2017-10-08 11:34:34 -04:00
|
|
|
@options = options
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2017-12-06 05:41:57 -05:00
|
|
|
def factory(json, account, **options)
|
2017-08-08 15:52:15 -04:00
|
|
|
@json = json
|
2017-10-08 11:34:34 -04:00
|
|
|
klass&.new(json, account, options)
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def klass
|
|
|
|
case @json['type']
|
|
|
|
when 'Create'
|
|
|
|
ActivityPub::Activity::Create
|
|
|
|
when 'Announce'
|
|
|
|
ActivityPub::Activity::Announce
|
|
|
|
when 'Delete'
|
|
|
|
ActivityPub::Activity::Delete
|
|
|
|
when 'Follow'
|
|
|
|
ActivityPub::Activity::Follow
|
|
|
|
when 'Like'
|
|
|
|
ActivityPub::Activity::Like
|
|
|
|
when 'Block'
|
|
|
|
ActivityPub::Activity::Block
|
|
|
|
when 'Update'
|
|
|
|
ActivityPub::Activity::Update
|
|
|
|
when 'Undo'
|
|
|
|
ActivityPub::Activity::Undo
|
2017-08-10 16:33:12 -04:00
|
|
|
when 'Accept'
|
|
|
|
ActivityPub::Activity::Accept
|
|
|
|
when 'Reject'
|
|
|
|
ActivityPub::Activity::Reject
|
2018-02-28 00:54:55 -05:00
|
|
|
when 'Flag'
|
|
|
|
ActivityPub::Activity::Flag
|
2018-03-04 03:19:11 -05:00
|
|
|
when 'Add'
|
|
|
|
ActivityPub::Activity::Add
|
|
|
|
when 'Remove'
|
|
|
|
ActivityPub::Activity::Remove
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def status_from_uri(uri)
|
|
|
|
ActivityPub::TagManager.instance.uri_to_resource(uri, Status)
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_from_uri(uri)
|
|
|
|
ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
|
|
|
|
end
|
|
|
|
|
|
|
|
def object_uri
|
2017-08-21 16:57:34 -04:00
|
|
|
@object_uri ||= value_or_id(@object)
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def redis
|
|
|
|
Redis.current
|
|
|
|
end
|
|
|
|
|
|
|
|
def distribute(status)
|
2017-10-17 14:05:21 -04:00
|
|
|
crawl_links(status)
|
|
|
|
|
2018-01-19 13:11:35 -05:00
|
|
|
notify_about_reblog(status) if reblog_of_local_account?(status)
|
|
|
|
notify_about_mentions(status)
|
|
|
|
|
2018-05-03 17:02:46 -04:00
|
|
|
# Only continue if the status is supposed to have arrived in real-time.
|
|
|
|
# Note that if @options[:override_timestamps] isn't set, the status
|
|
|
|
# may have a lower snowflake id than other existing statuses, potentially
|
|
|
|
# "hiding" it from paginated API calls
|
|
|
|
return unless @options[:override_timestamps] || status.within_realtime_window?
|
2017-10-17 14:05:21 -04:00
|
|
|
|
2017-08-08 15:52:15 -04:00
|
|
|
distribute_to_followers(status)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reblog_of_local_account?(status)
|
|
|
|
status.reblog? && status.reblog.account.local?
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_about_reblog(status)
|
|
|
|
NotifyService.new.call(status.reblog.account, status)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_about_mentions(status)
|
2018-10-17 11:13:04 -04:00
|
|
|
status.active_mentions.includes(:account).each do |mention|
|
2017-08-08 15:52:15 -04:00
|
|
|
next unless mention.account.local? && audience_includes?(mention.account)
|
|
|
|
NotifyService.new.call(mention.account, mention)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def crawl_links(status)
|
|
|
|
return if status.spoiler_text?
|
2018-08-25 18:33:57 -04:00
|
|
|
|
|
|
|
# Spread out crawling randomly to avoid DDoSing the link
|
|
|
|
LinkCrawlWorker.perform_in(rand(1..59).seconds, status.id)
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def distribute_to_followers(status)
|
2017-08-12 18:44:41 -04:00
|
|
|
::DistributionWorker.perform_async(status.id)
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_arrived_first?(uri)
|
2017-09-01 15:12:59 -04:00
|
|
|
redis.exists("delete_upon_arrival:#{@account.id}:#{uri}")
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_later!(uri)
|
|
|
|
redis.setex("delete_upon_arrival:#{@account.id}:#{uri}", 6.hours.seconds, uri)
|
|
|
|
end
|
2018-05-18 05:33:56 -04:00
|
|
|
|
|
|
|
def fetch_remote_original_status
|
|
|
|
if object_uri.start_with?('http')
|
|
|
|
return if ActivityPub::TagManager.instance.local_uri?(object_uri)
|
|
|
|
ActivityPub::FetchRemoteStatusService.new.call(object_uri, id: true, on_behalf_of: @account.followers.local.first)
|
|
|
|
elsif @object['url'].present?
|
|
|
|
::FetchRemoteStatusService.new.call(@object['url'])
|
|
|
|
end
|
|
|
|
end
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|