Add blue sky as a syndication source
This commit is contained in:
52
lib/adamantium/client/blue_sky.rb
Normal file
52
lib/adamantium/client/blue_sky.rb
Normal file
@@ -0,0 +1,52 @@
|
||||
require "bskyrb"
|
||||
require "digest"
|
||||
require "tempfile"
|
||||
require "open-uri"
|
||||
require "dry/monads"
|
||||
require "sanitize"
|
||||
|
||||
module Adamantium
|
||||
module Client
|
||||
class BlueSky
|
||||
include Dry::Monads[:result]
|
||||
include Deps["settings"]
|
||||
|
||||
def create_post(post:)
|
||||
unless settings.mastodon_token && settings.mastodon_server
|
||||
return Failure(:no_mastodon_credentials)
|
||||
end
|
||||
|
||||
content = if post[:name]
|
||||
"#{post[:name]} — #{settings.micropub_site_url}/post/#{post[:slug]}"
|
||||
else
|
||||
"#{sanitze_post(post[:content])} \r\n\r\n 🔗 #{settings.micropub_site_url}/post/#{post[:slug]}"
|
||||
end
|
||||
|
||||
credentials = Bskyrb::Credentials.new(settings.blue_sky_username, settings.blue_sky_password)
|
||||
session = Bskyrb::Session.new(credentials, settings.blue_sky_url)
|
||||
client = Bskyrb::RecordManager.new(session)
|
||||
|
||||
response = client.create_post content
|
||||
|
||||
if response["uri"] && response["uri"] != ""
|
||||
Success(response["uri"])
|
||||
else
|
||||
Failure(response.message)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def sanitze_post(content)
|
||||
replace_links = lambda { |env|
|
||||
return unless env[:node_name] == "a"
|
||||
node = env[:node]
|
||||
url = node[:href]
|
||||
env[:node].replace(url)
|
||||
}
|
||||
|
||||
Sanitize.fragment(content, transformers: [replace_links]).strip
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
20
lib/adamantium/syndication/blue_sky.rb
Normal file
20
lib/adamantium/syndication/blue_sky.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
require "dry/monads"
|
||||
|
||||
module Adamantium
|
||||
module Syndication
|
||||
class BlueSky
|
||||
include Dry::Monads[:result]
|
||||
include Deps[blue_sky_client: "clients.blue_sky"]
|
||||
|
||||
def call(post:)
|
||||
response = blue_sky.create_post(post: post)
|
||||
|
||||
if response.success?
|
||||
Success(response.value!)
|
||||
else
|
||||
Failure(:failed_to_syndicate)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user