2024-02-16 17:54:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'distributed_press/v1/social/client'
|
2024-02-26 19:12:56 +00:00
|
|
|
require 'distributed_press/v1/social/allowlist'
|
|
|
|
require 'distributed_press/v1/social/blocklist'
|
2024-02-16 19:01:17 +00:00
|
|
|
require 'distributed_press/v1/social/hook'
|
2024-02-22 21:08:48 +00:00
|
|
|
require 'distributed_press/v1/social/inbox'
|
2024-02-21 15:30:01 +00:00
|
|
|
require 'distributed_press/v1/social/dereferencer'
|
2024-02-22 20:56:12 +00:00
|
|
|
require 'httparty/cache/store/redis'
|
2024-02-16 17:54:50 +00:00
|
|
|
|
|
|
|
# Gestiona la Social Inbox de un sitio
|
|
|
|
class SocialInbox
|
|
|
|
# @return [Site]
|
|
|
|
attr_reader :site
|
|
|
|
|
|
|
|
# @param :site [Site]
|
|
|
|
def initialize(site:)
|
|
|
|
@site = site
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [String]
|
|
|
|
def actor
|
|
|
|
@actor ||=
|
|
|
|
begin
|
|
|
|
user = site.config.dig('activity_pub', 'username')
|
|
|
|
user ||= hostname.split('.', 2).first
|
|
|
|
|
|
|
|
"@#{user}@#{hostname}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-20 20:15:43 +00:00
|
|
|
def actor_id
|
2024-02-24 14:09:57 +00:00
|
|
|
@actor_id ||= SocialInbox.generate_uri(hostname) do |uri|
|
2024-02-20 20:15:43 +00:00
|
|
|
uri.path = '/about.jsonld'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-16 17:54:50 +00:00
|
|
|
# @return [DistributedPress::V1::Social::Client]
|
|
|
|
def client
|
2024-03-01 18:04:07 +00:00
|
|
|
@client ||= client_for site.config.dig('activity_pub', 'url')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Permite enviar mensajes directo a otro servidor
|
|
|
|
#
|
|
|
|
# @param url [String]
|
|
|
|
# @return [DistributedPress::V1::Social::Client]
|
|
|
|
def client_for(url)
|
2024-05-02 19:05:35 +00:00
|
|
|
raise 'Falló generar un cliente' if url.blank?
|
2024-03-01 18:04:07 +00:00
|
|
|
|
|
|
|
@client_for ||= {}
|
|
|
|
@client_for[url] ||=
|
|
|
|
DistributedPress::V1::Social::Client.new(
|
|
|
|
url: url,
|
|
|
|
public_key_url: public_key_url,
|
|
|
|
private_key_pem: site.private_key_pem,
|
|
|
|
logger: Rails.logger,
|
2024-05-02 19:05:35 +00:00
|
|
|
cache_store: HTTParty::Cache::Store::Redis.new(redis_url: ENV.fetch('REDIS_SERVER', nil))
|
2024-03-01 18:04:07 +00:00
|
|
|
)
|
2024-02-16 17:54:50 +00:00
|
|
|
end
|
|
|
|
|
2024-02-22 21:08:48 +00:00
|
|
|
# @return [DistributedPress::V1::Social::Inbox]
|
|
|
|
def inbox
|
|
|
|
@inbox ||= DistributedPress::V1::Social::Inbox.new(client: client, actor: actor)
|
|
|
|
end
|
|
|
|
|
2024-02-21 15:30:01 +00:00
|
|
|
# @return [DistributedPress::V1::Social::Dereferencer]
|
|
|
|
def dereferencer
|
|
|
|
@dereferencer ||= DistributedPress::V1::Social::Dereferencer.new(client: client)
|
|
|
|
end
|
|
|
|
|
2024-02-16 19:01:17 +00:00
|
|
|
# @return [DistributedPress::V1::Social::Hook]
|
|
|
|
def hook
|
|
|
|
@hook ||= DistributedPress::V1::Social::Hook.new(client: client, actor: actor)
|
|
|
|
end
|
|
|
|
|
2024-02-26 19:12:56 +00:00
|
|
|
# @return [DistributedPress::V1::Social::Allowlist]
|
|
|
|
def allowlist
|
|
|
|
@allowlist ||= DistributedPress::V1::Social::Allowlist.new(client: client, actor: actor)
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [DistributedPress::V1::Social::Blocklist]
|
|
|
|
def blocklist
|
|
|
|
@blocklist ||= DistributedPress::V1::Social::Blocklist.new(client: client, actor: actor)
|
|
|
|
end
|
|
|
|
|
2024-02-16 17:54:50 +00:00
|
|
|
# @return [String]
|
|
|
|
def public_key_url
|
2024-02-24 14:09:57 +00:00
|
|
|
@public_key_url ||= SocialInbox.generate_uri(hostname) do |uri|
|
2024-02-16 17:54:50 +00:00
|
|
|
uri.path = '/about.jsonld'
|
|
|
|
uri.fragment = 'main-key'
|
2024-02-20 20:15:43 +00:00
|
|
|
end
|
2024-02-16 17:54:50 +00:00
|
|
|
end
|
|
|
|
|
2024-02-22 21:13:53 +00:00
|
|
|
# El hostname puede estar en varios lados...
|
|
|
|
#
|
|
|
|
# @return [String]
|
2024-02-16 17:54:50 +00:00
|
|
|
def hostname
|
|
|
|
@hostname ||=
|
2024-02-22 21:13:53 +00:00
|
|
|
site.config.dig('activity_pub', 'hostname') || site.config['hostname'] || site.hostname
|
2024-02-16 17:54:50 +00:00
|
|
|
end
|
2024-02-20 20:15:43 +00:00
|
|
|
|
|
|
|
# Genera una URI dentro de este sitio
|
|
|
|
#
|
|
|
|
# @return [String]
|
2024-02-24 14:09:57 +00:00
|
|
|
def self.generate_uri(hostname, &block)
|
|
|
|
URI("https://#{hostname}").tap(&block).to_s
|
2024-02-20 20:15:43 +00:00
|
|
|
end
|
2024-02-16 17:54:50 +00:00
|
|
|
end
|