Refactor syndication clients for testability
This commit is contained in:
5
config/providers/clients.rb
Normal file
5
config/providers/clients.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
Hanami.app.register_provider :clients, namespace: true do
|
||||||
|
start do
|
||||||
|
register "mastodon", Adamantium::Client::Mastodon.new
|
||||||
|
end
|
||||||
|
end
|
@@ -2,7 +2,7 @@ require "securerandom"
|
|||||||
|
|
||||||
module Adamantium
|
module Adamantium
|
||||||
class MicropubRequestParser
|
class MicropubRequestParser
|
||||||
include Deps["logger", "post_utilities.slugify", "repos.post_repo"]
|
include Deps["post_utilities.slugify", "repos.post_repo"]
|
||||||
|
|
||||||
def call(params:)
|
def call(params:)
|
||||||
return nil if params.key?(:action)
|
return nil if params.key?(:action)
|
||||||
@@ -83,8 +83,6 @@ module Adamantium
|
|||||||
new_params[:url] = params[:"bookmark-of"]
|
new_params[:url] = params[:"bookmark-of"]
|
||||||
new_params[:slug] = slug(name: new_params[:name], default_slug: params[:slug])
|
new_params[:slug] = slug(name: new_params[:name], default_slug: params[:slug])
|
||||||
|
|
||||||
logger.info(new_params)
|
|
||||||
|
|
||||||
new_params
|
new_params
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@@ -1,72 +1,23 @@
|
|||||||
require "digest"
|
|
||||||
require "dry/monads"
|
require "dry/monads"
|
||||||
require "httparty"
|
require "httparty"
|
||||||
require "tempfile"
|
|
||||||
require "open-uri"
|
|
||||||
|
|
||||||
module Adamantium
|
module Adamantium
|
||||||
module Syndication
|
module Syndication
|
||||||
class Mastodon
|
class Mastodon
|
||||||
include Dry::Monads[:result]
|
include Dry::Monads[:result]
|
||||||
include Deps["settings", "logger"]
|
include Deps[mastodon_client: "clients.mastodon"]
|
||||||
|
|
||||||
def call(post:)
|
def call(post:)
|
||||||
unless settings.mastodon_token && settings.mastodon_server
|
|
||||||
logger.info("No Mastodon credentials")
|
|
||||||
return Failure(:no_mastodon_credentials)
|
|
||||||
end
|
|
||||||
|
|
||||||
content = if post[:name]
|
|
||||||
"#{post[:name]} — #{settings.micropub_site_url}/post/#{post[:slug]}"
|
|
||||||
else
|
|
||||||
post[:content]
|
|
||||||
end
|
|
||||||
|
|
||||||
tags = post[:category].map { |tag| "##{tag}" }.join(" ")
|
|
||||||
text_with_tags = "#{content} #{tags}"
|
|
||||||
|
|
||||||
key = Digest::MD5.hexdigest text_with_tags
|
|
||||||
mastodon_token = settings.mastodon_token
|
|
||||||
mastodon_server = settings.mastodon_server.split("@").first
|
|
||||||
logger.info("Photos: #{post[:photos].inspect}")
|
|
||||||
media_ids = post[:photos]&.map do |photo|
|
media_ids = post[:photos]&.map do |photo|
|
||||||
file = Tempfile.new(SecureRandom.uuid)
|
mastodon_client.upload_media(photo: photo)
|
||||||
file.write(URI.open(photo[:value]).read)
|
|
||||||
file.rewind
|
|
||||||
response = HTTParty.post("#{mastodon_server}api/v2/media", {
|
|
||||||
headers: {
|
|
||||||
Authorization: "Bearer #{mastodon_token}"
|
|
||||||
},
|
|
||||||
multipart: true,
|
|
||||||
body: {
|
|
||||||
file: file,
|
|
||||||
description: photo[:alt]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
file.close
|
|
||||||
file.unlink
|
|
||||||
logger.info(response.code)
|
|
||||||
|
|
||||||
JSON.parse(response.body, symbolize_names: true).fetch(:id, nil)
|
|
||||||
end&.compact
|
end&.compact
|
||||||
|
|
||||||
response = HTTParty.post("#{mastodon_server}api/v1/statuses", {
|
response = mastodon_client.create_post(post: post, media_ids: media_ids)
|
||||||
headers: {
|
|
||||||
"Idempotency-Key": key,
|
|
||||||
Authorization: "Bearer #{mastodon_token}"
|
|
||||||
},
|
|
||||||
body: {
|
|
||||||
status: text_with_tags,
|
|
||||||
media_ids: media_ids
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if response.code >= 200 && response.code < 300
|
if response.success?
|
||||||
status = JSON.parse(response.body, symbolize_names: true)
|
Success(response.value!)
|
||||||
Success("#{mastodon_server}/#{status[:id]}")
|
|
||||||
else
|
else
|
||||||
logger.info("Failed to syndicate to Mastodon: #{response.message}")
|
Failure(:failed_to_syndicate)
|
||||||
Failure(response.message)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@@ -1,7 +1,11 @@
|
|||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
require "dry/monads"
|
||||||
|
|
||||||
RSpec.describe Adamantium::Syndication::Mastodon do
|
RSpec.describe Adamantium::Syndication::Mastodon do
|
||||||
subject { described_class.new }
|
include Dry::Monads[:result]
|
||||||
|
|
||||||
|
let(:client) { double("mastodon client", create_post: Success(), upload_media: Success()) }
|
||||||
|
subject { described_class.new(mastodon_client: client) }
|
||||||
|
|
||||||
describe "syndication to mastodon" do
|
describe "syndication to mastodon" do
|
||||||
let(:post) {
|
let(:post) {
|
||||||
|
Reference in New Issue
Block a user