2024-02-26 15:27:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Mantiene el registro de relaciones entre sitios e instancias
|
|
|
|
class InstanceModeration < ApplicationRecord
|
|
|
|
include AASM
|
2024-03-04 17:26:41 +00:00
|
|
|
include AasmEventsConcern
|
|
|
|
|
|
|
|
IGNORED_EVENTS = []
|
|
|
|
IGNORED_STATES = []
|
2024-02-26 15:27:56 +00:00
|
|
|
|
|
|
|
belongs_to :site
|
|
|
|
belongs_to :instance, class_name: 'ActivityPub::Instance'
|
|
|
|
|
2024-02-28 19:04:37 +00:00
|
|
|
# Traer todas las instancias bloqueables, según la máquina de estados,
|
|
|
|
# todas las que no estén bloqueadas ya.
|
|
|
|
scope :may_block, -> { where.not(aasm_state: 'blocked') }
|
|
|
|
scope :may_pause, -> { where.not(aasm_state: 'paused') }
|
|
|
|
|
|
|
|
# Bloquear instancias en masa
|
|
|
|
def self.block_all!
|
|
|
|
self.may_block.update_all(aasm_state: 'blocked', updated_at: Time.now)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Pausar instancias en masa
|
|
|
|
def self.pause_all!
|
|
|
|
self.may_pause.update_all(aasm_state: 'paused', updated_at: Time.now)
|
|
|
|
end
|
|
|
|
|
2024-02-26 15:27:56 +00:00
|
|
|
aasm do
|
|
|
|
state :paused, initial: true
|
|
|
|
state :allowed
|
|
|
|
state :blocked
|
|
|
|
|
|
|
|
event :pause do
|
2024-02-26 19:50:03 +00:00
|
|
|
transitions from: %i[allowed blocked], to: :paused
|
|
|
|
|
|
|
|
before do
|
|
|
|
pause_remotely!
|
|
|
|
end
|
2024-02-26 15:27:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
event :allow do
|
2024-02-26 19:50:03 +00:00
|
|
|
transitions from: %i[paused blocked], to: :allowed
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow_remotely!
|
|
|
|
end
|
2024-02-26 15:27:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
event :block do
|
2024-02-26 19:50:03 +00:00
|
|
|
transitions from: %i[paused allowed], to: :blocked
|
|
|
|
|
|
|
|
before do
|
|
|
|
block_remotely!
|
|
|
|
end
|
2024-02-26 15:27:56 +00:00
|
|
|
end
|
|
|
|
end
|
2024-02-26 19:12:56 +00:00
|
|
|
|
|
|
|
# Elimina la instancia de todas las listas
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def pause_remotely!
|
2024-02-26 19:50:03 +00:00
|
|
|
raise AASM::InvalidTransition unless
|
|
|
|
site.social_inbox.blocklist.delete(list: [instance.list_name]).ok? &&
|
2024-02-26 19:12:56 +00:00
|
|
|
site.social_inbox.allowlist.delete(list: [instance.list_name]).ok?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Deja de permitir la instancia
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def block_remotely!
|
2024-02-26 19:50:03 +00:00
|
|
|
raise AASM::InvalidTransition unless
|
|
|
|
site.social_inbox.allowlist.delete(list: [instance.list_name]).ok? &&
|
2024-02-26 19:12:56 +00:00
|
|
|
site.social_inbox.blocklist.post(list: [instance.list_name]).ok?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Permite la instancia
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def allow_remotely!
|
2024-02-26 19:50:03 +00:00
|
|
|
raise AASM::InvalidTransition unless
|
|
|
|
site.social_inbox.blocklist.delete(list: [instance.list_name]).ok? &&
|
2024-02-26 19:12:56 +00:00
|
|
|
site.social_inbox.allowlist.post(list: [instance.list_name]).ok?
|
|
|
|
end
|
2024-02-26 15:27:56 +00:00
|
|
|
end
|