2018-07-13 03:16:06 +03:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 07:58:28 +02:00
|
|
|
|
2018-07-13 03:16:06 +03: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 19:16:26 +03:00
|
|
|
# state :integer default("idle"), not null
|
2018-07-13 03:16:06 +03:00
|
|
|
#
|
|
|
|
|
|
|
|
class Relay < ApplicationRecord
|
|
|
|
validates :inbox_url, presence: true, uniqueness: true, url: true, if: :will_save_change_to_inbox_url?
|
|
|
|
|
2023-02-20 06:00:36 +02:00
|
|
|
enum state: { idle: 0, pending: 1, accepted: 2, rejected: 3 }
|
2018-08-12 19:16:26 +03:00
|
|
|
|
|
|
|
scope :enabled, -> { accepted }
|
2018-07-13 03:16:06 +03:00
|
|
|
|
2023-01-05 14:33:33 +02:00
|
|
|
before_validation :strip_url
|
2018-07-13 03:16:06 +03:00
|
|
|
before_destroy :ensure_disabled
|
|
|
|
|
2018-08-12 19:16:26 +03:00
|
|
|
alias enabled? accepted?
|
|
|
|
|
2018-07-13 03:16:06 +03:00
|
|
|
def enable!
|
|
|
|
activity_id = ActivityPub::TagManager.instance.generate_uri_for(nil)
|
|
|
|
payload = Oj.dump(follow_activity(activity_id))
|
|
|
|
|
2018-08-12 19:16:26 +03:00
|
|
|
update!(state: :pending, follow_activity_id: activity_id)
|
2020-04-23 23:04:18 +03:00
|
|
|
DeliveryFailureTracker.reset!(inbox_url)
|
2018-07-13 03:16:06 +03: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 19:16:26 +03:00
|
|
|
update!(state: :idle, follow_activity_id: nil)
|
2020-04-23 23:04:18 +03:00
|
|
|
DeliveryFailureTracker.reset!(inbox_url)
|
2018-07-13 03:16:06 +03: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 08:17:12 +02:00
|
|
|
@some_local_account ||= Account.representative
|
2018-07-13 03:16:06 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_disabled
|
2019-09-27 03:13:34 +03:00
|
|
|
disable! if enabled?
|
2018-07-13 03:16:06 +03:00
|
|
|
end
|
2023-01-05 14:33:33 +02:00
|
|
|
|
|
|
|
def strip_url
|
|
|
|
inbox_url&.strip!
|
|
|
|
end
|
2018-07-13 03:16:06 +03:00
|
|
|
end
|