mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-20 06:36:21 +00:00
feat: acceder a la social inbox desde el sitio
This commit is contained in:
parent
cdf0685c67
commit
6d0ea6fa5d
2 changed files with 62 additions and 1 deletions
|
@ -1,5 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'distributed_press/v1/social/client'
|
||||
|
||||
class Site
|
||||
# Agrega soporte para Social Distributed Press en los sitios
|
||||
module SocialDistributedPress
|
||||
|
@ -10,13 +12,20 @@ class Site
|
|||
|
||||
before_save :generate_private_key_pem!, unless: :private_key_pem?
|
||||
|
||||
# @return [SocialInbox]
|
||||
def social_inbox
|
||||
@social_inbox ||= SocialInbox.new(site: self)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Genera la llave privada y la almacena
|
||||
#
|
||||
# @return [nil]
|
||||
def generate_private_key_pem!
|
||||
self.private_key_pem ||= ::DistributedPress::V1::Social::Client.new(public_key_url: nil, key_size: 2048).private_key.export
|
||||
self.private_key_pem ||= DistributedPress::V1::Social::Client.new(
|
||||
public_key_url: nil,
|
||||
key_size: 2048).private_key.export
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
52
app/models/social_inbox.rb
Normal file
52
app/models/social_inbox.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'distributed_press/v1/social/client'
|
||||
|
||||
# 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
|
||||
|
||||
# @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: :redis
|
||||
)
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
def public_key_url
|
||||
@public_key_url ||= URI("https://#{hostname}").tap do |uri|
|
||||
uri.path = '/about.jsonld'
|
||||
uri.fragment = 'main-key'
|
||||
end.to_s
|
||||
end
|
||||
|
||||
def hostname
|
||||
@hostname ||=
|
||||
begin
|
||||
host = site.config.dig('activity_pub', 'hostname')
|
||||
host ||= site.hostname
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue