2017-08-09 04:52:15 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::Activity
|
|
|
|
include JsonLdHelper
|
|
|
|
|
2017-10-09 00:34:34 +09:00
|
|
|
def initialize(json, account, options = {})
|
2017-08-09 04:52:15 +09:00
|
|
|
@json = json
|
|
|
|
@account = account
|
|
|
|
@object = @json['object']
|
2017-10-09 00:34:34 +09:00
|
|
|
@options = options
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2017-10-09 00:34:34 +09:00
|
|
|
def factory(json, account, options = {})
|
2017-08-09 04:52:15 +09:00
|
|
|
@json = json
|
2017-10-09 00:34:34 +09:00
|
|
|
klass&.new(json, account, options)
|
2017-08-09 04:52:15 +09: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-11 05:33:12 +09:00
|
|
|
when 'Accept'
|
|
|
|
ActivityPub::Activity::Accept
|
|
|
|
when 'Reject'
|
|
|
|
ActivityPub::Activity::Reject
|
2017-08-09 04:52:15 +09: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-22 05:57:34 +09:00
|
|
|
@object_uri ||= value_or_id(@object)
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def redis
|
|
|
|
Redis.current
|
|
|
|
end
|
|
|
|
|
|
|
|
def distribute(status)
|
2017-10-18 03:05:21 +09:00
|
|
|
crawl_links(status)
|
|
|
|
|
|
|
|
# Only continue if the status is supposed to have
|
|
|
|
# arrived in real-time
|
|
|
|
return unless @options[:override_timestamps]
|
|
|
|
|
2017-08-09 04:52:15 +09:00
|
|
|
notify_about_reblog(status) if reblog_of_local_account?(status)
|
|
|
|
notify_about_mentions(status)
|
|
|
|
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)
|
|
|
|
status.mentions.includes(:account).each do |mention|
|
|
|
|
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?
|
|
|
|
LinkCrawlWorker.perform_async(status.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def distribute_to_followers(status)
|
2017-08-13 07:44:41 +09:00
|
|
|
::DistributionWorker.perform_async(status.id)
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_arrived_first?(uri)
|
2017-09-02 04:12:59 +09:00
|
|
|
redis.exists("delete_upon_arrival:#{@account.id}:#{uri}")
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_later!(uri)
|
|
|
|
redis.setex("delete_upon_arrival:#{@account.id}:#{uri}", 6.hours.seconds, uri)
|
|
|
|
end
|
|
|
|
end
|