2018-07-13 00:16:06 +00:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 05:58:28 +00:00
|
|
|
|
2018-07-13 00:16:06 +00:00
|
|
|
# == 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
|
2024-12-02 07:38:06 +00:00
|
|
|
validates :inbox_url, presence: true, uniqueness: true, url: true # rubocop:disable Rails/UniqueValidationWithoutIndex
|
2018-07-13 00:16:06 +00:00
|
|
|
|
2024-02-16 14:54:23 +00:00
|
|
|
enum :state, { idle: 0, pending: 1, accepted: 2, rejected: 3 }
|
2018-08-12 16:16:26 +00:00
|
|
|
|
|
|
|
scope :enabled, -> { accepted }
|
2018-07-13 00:16:06 +00:00
|
|
|
|
2023-10-24 10:06:10 +00:00
|
|
|
normalizes :inbox_url, with: ->(inbox_url) { inbox_url.strip }
|
|
|
|
|
2018-07-13 00:16:06 +00:00
|
|
|
before_destroy :ensure_disabled
|
|
|
|
|
2018-08-12 16:16:26 +00:00
|
|
|
alias enabled? accepted?
|
|
|
|
|
2024-11-12 08:35:08 +00:00
|
|
|
def to_log_human_identifier
|
|
|
|
inbox_url
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
end
|