5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-26 10:56:22 +00:00
panel/app/models/social_inbox.rb

85 lines
2.1 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
require 'distributed_press/v1/social/client'
require 'distributed_press/v1/social/hook'
2024-02-22 21:08:48 +00:00
require 'distributed_press/v1/social/inbox'
require 'distributed_press/v1/social/dereferencer'
2024-02-22 20:56:12 +00:00
require 'httparty/cache/store/redis'
# 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
@actor_id ||= generate_uri do |uri|
uri.path = '/about.jsonld'
end
end
# @return [DistributedPress::V1::Social::Client]
def client
@client ||= DistributedPress::V1::Social::Client.new(
url: site.config.dig('activity_pub', 'url'),
public_key_url: public_key_url,
private_key_pem: site.private_key_pem,
logger: Rails.logger,
cache_store: HTTParty::Cache::Store::Redis.new(redis_url: ENV['REDIS_SERVER'])
)
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
# @return [DistributedPress::V1::Social::Dereferencer]
def dereferencer
@dereferencer ||= DistributedPress::V1::Social::Dereferencer.new(client: client)
end
# @return [DistributedPress::V1::Social::Hook]
def hook
@hook ||= DistributedPress::V1::Social::Hook.new(client: client, actor: actor)
end
# @return [String]
def public_key_url
2024-02-20 20:15:43 +00:00
@public_key_url ||= generate_uri do |uri|
uri.path = '/about.jsonld'
uri.fragment = 'main-key'
2024-02-20 20:15:43 +00:00
end
end
# El hostname puede estar en varios lados...
#
# @return [String]
def hostname
@hostname ||=
site.config.dig('activity_pub', 'hostname') || site.config['hostname'] || site.hostname
end
2024-02-20 20:15:43 +00:00
# Genera una URI dentro de este sitio
#
# @return [String]
def generate_uri(&block)
2024-02-21 15:46:38 +00:00
@public_key_url ||= URI("https://#{hostname}").tap(&block).to_s
2024-02-20 20:15:43 +00:00
end
end