2018-07-13 00:16:06 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: relays
|
|
|
|
#
|
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# inbox_url :string default(""), not null
|
|
|
|
# follow_activity_id :string
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2018-08-12 16:16:26 +00:00
|
|
|
# state :integer default("idle"), not null
|
2018-07-13 00:16:06 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
class Relay < ApplicationRecord
|
|
|
|
validates :inbox_url, presence: true, uniqueness: true, url: true, if: :will_save_change_to_inbox_url?
|
|
|
|
|
2018-08-12 16:16:26 +00:00
|
|
|
enum state: [:idle, :pending, :accepted, :rejected]
|
|
|
|
|
|
|
|
scope :enabled, -> { accepted }
|
2018-07-13 00:16:06 +00:00
|
|
|
|
2023-01-05 12:33:33 +00:00
|
|
|
before_validation :strip_url
|
2018-07-13 00:16:06 +00:00
|
|
|
before_destroy :ensure_disabled
|
|
|
|
|
2018-08-12 16:16:26 +00:00
|
|
|
alias enabled? accepted?
|
|
|
|
|
2018-07-13 00:16:06 +00:00
|
|
|
def enable!
|
|
|
|
activity_id = ActivityPub::TagManager.instance.generate_uri_for(nil)
|
|
|
|
payload = Oj.dump(follow_activity(activity_id))
|
|
|
|
|
2018-08-12 16:16:26 +00:00
|
|
|
update!(state: :pending, follow_activity_id: activity_id)
|
2020-04-23 20:04:18 +00:00
|
|
|
DeliveryFailureTracker.reset!(inbox_url)
|
2018-07-13 00:16:06 +00:00
|
|
|
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable!
|
|
|
|
activity_id = ActivityPub::TagManager.instance.generate_uri_for(nil)
|
|
|
|
payload = Oj.dump(unfollow_activity(activity_id))
|
|
|
|
|
2018-08-12 16:16:26 +00:00
|
|
|
update!(state: :idle, follow_activity_id: nil)
|
2020-04-23 20:04:18 +00:00
|
|
|
DeliveryFailureTracker.reset!(inbox_url)
|
2018-07-13 00:16:06 +00:00
|
|
|
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def follow_activity(activity_id)
|
|
|
|
{
|
|
|
|
'@context': ActivityPub::TagManager::CONTEXT,
|
|
|
|
id: activity_id,
|
|
|
|
type: 'Follow',
|
|
|
|
actor: ActivityPub::TagManager.instance.uri_for(some_local_account),
|
|
|
|
object: ActivityPub::TagManager::COLLECTIONS[:public],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def unfollow_activity(activity_id)
|
|
|
|
{
|
|
|
|
'@context': ActivityPub::TagManager::CONTEXT,
|
|
|
|
id: activity_id,
|
|
|
|
type: 'Undo',
|
|
|
|
actor: ActivityPub::TagManager.instance.uri_for(some_local_account),
|
|
|
|
object: {
|
|
|
|
id: follow_activity_id,
|
|
|
|
type: 'Follow',
|
|
|
|
actor: ActivityPub::TagManager.instance.uri_for(some_local_account),
|
|
|
|
object: ActivityPub::TagManager::COLLECTIONS[:public],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def some_local_account
|
2019-01-05 06:17:12 +00:00
|
|
|
@some_local_account ||= Account.representative
|
2018-07-13 00:16:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_disabled
|
2019-09-27 00:13:34 +00:00
|
|
|
disable! if enabled?
|
2018-07-13 00:16:06 +00:00
|
|
|
end
|
2023-01-05 12:33:33 +00:00
|
|
|
|
|
|
|
def strip_url
|
|
|
|
inbox_url&.strip!
|
|
|
|
end
|
2018-07-13 00:16:06 +00:00
|
|
|
end
|